lkml.org 
[lkml]   [2001]   [Jun]   [16]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: threading question
Date
From
> Can you provide any info and/or examples of co-routines? I'm curious to
> see a good example of co-routines' "betterness."

With co-routines you don't need

8K of kernel stack
Scheduler overhead
Fancy locking

You don't get the automatic thread switching stuff though.

So you might get code that reads like this (note that aio_ stuff works rather
well combined with co-routines as it fixes a lack of asynchronicity in the
unix disk I/O world)


select(....)
if(FD_ISSET(copier_fd))
run_coroutine(&copier_state);
...


and the copier might be something like

while(1)
{
// Yes 1 at a time is dumb but this is an example..
// Yes Im ignoring EOF for this
if(read(copier_fd, buf[bufptr], 1)==-1)
{
if(errno==-EWOULDBLOCK)
{
coroutine_return();
continue;
}
}
if(bufptr==255 || buf[bufptr]=='\n')
{
run_coroutine(run_command, buf);
bufptr=0;
}
else
bufptr++;
}

it lets you express a state machine as a set of multiple such small state
machines instead. run_coroutine() will continue a routine where it last
coroutine_return()'d from. Thus in the above case we are expressing read
bytes until you see a new line cleanly - not mangled in with keeping state
in global structures but by using natural C local variables and code flow

Alan



-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 12:55    [from the cache]
©2003-2011 Jasper Spaans. Advertise on this site