ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/README
Revision: 1.14
Committed: Sat May 10 22:32:40 2008 UTC (16 years ago) by root
Branch: MAIN
CVS Tags: rel-4_741, rel-4_743, rel-4_742, rel-4_744, rel-4_74, rel-4_71, rel-4_72, rel-4_73, rel-4_745, rel-4_7
Changes since 1.13: +192 -105 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 NAME
2     Coro - coroutine process abstraction
3    
4     SYNOPSIS
5 root 1.14 use Coro;
6    
7     async {
8     # some asynchronous thread of execution
9     print "2\n";
10     cede; # yield back to main
11     print "4\n";
12     };
13     print "1\n";
14     cede; # yield to coroutine
15     print "3\n";
16     cede; # and again
17    
18     # use locking
19     my $lock = new Coro::Semaphore;
20     my $locked;
21    
22     $lock->down;
23     $locked = 1;
24     $lock->up;
25 root 1.1
26     DESCRIPTION
27     This module collection manages coroutines. Coroutines are similar to
28 root 1.14 threads but don't (in general) run in parallel at the same time even on
29     SMP machines. The specific flavor of coroutine used in this module also
30     guarantees you that it will not switch between coroutines unless
31     necessary, at easily-identified points in your program, so locking and
32     parallel access are rarely an issue, making coroutine programming much
33     safer and easier than threads programming.
34    
35     Unlike a normal perl program, however, coroutines allow you to have
36     multiple running interpreters that share data, which is especially
37     useful to code pseudo-parallel processes and for event-based
38     programming, such as multiple HTTP-GET requests running concurrently.
39     See Coro::AnyEvent to learn more.
40    
41     Coroutines are also useful because Perl has no support for threads (the
42     so called "threads" that perl offers are nothing more than the (bad)
43     process emulation coming from the Windows platform: On standard
44     operating systems they serve no purpose whatsoever, except by making
45     your programs slow and making them use a lot of memory. Best disable
46     them when building perl, or aks your software vendor/distributor to do
47     it for you).
48 root 1.1
49     In this module, coroutines are defined as "callchain + lexical variables
50 root 1.5 + @_ + $_ + $@ + $/ + C stack), that is, a coroutine has its own
51     callchain, its own set of lexicals and its own set of perls most
52 root 1.12 important global variables (see Coro::State for more configuration).
53 root 1.1
54 root 1.14 $Coro::main
55     This variable stores the coroutine object that represents the main
56     program. While you cna "ready" it and do most other things you can
57     do to coroutines, it is mainly useful to compare again
58     $Coro::current, to see wether you are running in the main program or
59     not.
60    
61     $Coro::current
62     The coroutine object representing the current coroutine (the last
63     coroutine that the Coro scheduler switched to). The initial value is
64     $main (of course).
65    
66     This variable is strictly *read-only*. You can take copies of the
67     value stored in it and use it as any other coroutine object, but you
68     must not otherwise modify the variable itself.
69    
70     $Coro::idle
71     This variable is mainly useful to integrate Coro into event loops.
72     It is usually better to rely on Coro::AnyEvent or L"Coro::EV", as
73     this is pretty low-level functionality.
74    
75     This variable stores a callback that is called whenever the
76     scheduler finds no ready coroutines to run. The default
77     implementation prints "FATAL: deadlock detected" and exits, because
78     the program has no other way to continue.
79    
80     This hook is overwritten by modules such as "Coro::Timer" and
81     "Coro::AnyEvent" to wait on an external event that hopefully wake up
82     a coroutine so the scheduler can run it.
83 root 1.1
84 root 1.14 Note that the callback *must not*, under any circumstances, block
85     the current coroutine. Normally, this is achieved by having an "idle
86     coroutine" that calls the event loop and then blocks again, and then
87     readying that coroutine in the idle handler.
88 root 1.4
89 root 1.14 See Coro::Event or Coro::AnyEvent for examples of using this
90     technique.
91 root 1.4
92     Please note that if your callback recursively invokes perl (e.g. for
93 root 1.12 event handlers), then it must be prepared to be called recursively
94     itself.
95 root 1.1
96 root 1.14 SIMPLE COROUTINE CREATION
97     async { ... } [@args...]
98     Create a new coroutine and return it's coroutine object (usually
99     unused). The coroutine will be put into the ready queue, so it will
100     start running automatically on the next scheduler run.
101 root 1.1
102 root 1.14 The first argument is a codeblock/closure that should be executed in
103     the coroutine. When it returns argument returns the coroutine is
104 root 1.1 automatically terminated.
105    
106 root 1.14 The remaining arguments are passed as arguments to the closure.
107    
108 root 1.10 See the "Coro::State::new" constructor for info about the coroutine
109 root 1.14 environment in which coroutines are executed.
110 root 1.10
111 root 1.7 Calling "exit" in a coroutine will do the same as calling exit
112     outside the coroutine. Likewise, when the coroutine dies, the
113     program will exit, just as it would in the main program.
114 root 1.3
115 root 1.14 If you do not want that, you can provide a default "die" handler, or
116     simply avoid dieing (by use of "eval").
117    
118     Example: Create a new coroutine that just prints its arguments.
119    
120 root 1.1 async {
121     print "@_\n";
122     } 1,2,3,4;
123    
124 root 1.6 async_pool { ... } [@args...]
125     Similar to "async", but uses a coroutine pool, so you should not
126 root 1.14 call terminate or join on it (although you are allowed to), and you
127     get a coroutine that might have executed other code already (which
128     can be good or bad :).
129    
130     On the plus side, this function is faster than creating (and
131     destroying) a completely new coroutine, so if you need a lot of
132     generic coroutines in quick successsion, use "async_pool", not
133     "async".
134 root 1.6
135 root 1.14 The code block is executed in an "eval" context and a warning will
136 root 1.6 be issued in case of an exception instead of terminating the
137     program, as "async" does. As the coroutine is being reused, stuff
138     like "on_destroy" will not work in the expected way, unless you call
139 root 1.14 terminate or cancel, which somehow defeats the purpose of pooling
140     (but is fine in the exceptional case).
141 root 1.6
142 root 1.14 The priority will be reset to 0 after each run, tracing will be
143 root 1.10 disabled, the description will be reset and the default output
144 root 1.14 filehandle gets restored, so you can change all these. Otherwise the
145     coroutine will be re-used "as-is": most notably if you change other
146     per-coroutine global stuff such as $/ you *must needs* to revert
147     that change, which is most simply done by using local as in: " local
148     $/ ".
149 root 1.6
150     The pool size is limited to 8 idle coroutines (this can be adjusted
151     by changing $Coro::POOL_SIZE), and there can be as many non-idle
152     coros as required.
153    
154     If you are concerned about pooled coroutines growing a lot because a
155     single "async_pool" used a lot of stackspace you can e.g.
156     "async_pool { terminate }" once per second or so to slowly replenish
157 root 1.9 the pool. In addition to that, when the stacks used by a handler
158 root 1.14 grows larger than 16kb (adjustable via $Coro::POOL_RSS) it will also
159     be destroyed.
160    
161     STATIC METHODS
162     Static methods are actually functions that operate on the current
163     coroutine.
164 root 1.6
165 root 1.1 schedule
166 root 1.14 Calls the scheduler. The scheduler will find the next coroutine that
167     is to be run from the ready queue and switches to it. The next
168     coroutine to be run is simply the one with the highest priority that
169     is longest in its ready queue. If there is no coroutine ready, it
170     will clal the $Coro::idle hook.
171    
172     Please note that the current coroutine will *not* be put into the
173     ready queue, so calling this function usually means you will never
174     be called again unless something else (e.g. an event handler) calls
175     "->ready", thus waking you up.
176    
177     This makes "schedule" *the* generic method to use to block the
178     current coroutine and wait for events: first you remember the
179     current coroutine in a variable, then arrange for some callback of
180     yours to call "->ready" on that once some event happens, and last
181     you call "schedule" to put yourself to sleep. Note that a lot of
182     things can wake your coroutine up, so you need to check wether the
183     event indeed happened, e.g. by storing the status in a variable.
184 root 1.4
185     The canonical way to wait on external events is this:
186    
187     {
188     # remember current coroutine
189     my $current = $Coro::current;
190    
191     # register a hypothetical event handler
192     on_event_invoke sub {
193     # wake up sleeping coroutine
194     $current->ready;
195     undef $current;
196     };
197    
198 root 1.8 # call schedule until event occurred.
199 root 1.4 # in case we are woken up for other reasons
200     # (current still defined), loop.
201     Coro::schedule while $current;
202     }
203 root 1.1
204     cede
205 root 1.4 "Cede" to other coroutines. This function puts the current coroutine
206 root 1.1 into the ready queue and calls "schedule", which has the effect of
207     giving up the current "timeslice" to other coroutines of the same or
208 root 1.14 higher priority. Once your coroutine gets its turn again it will
209     automatically be resumed.
210    
211     This function is often called "yield" in other languages.
212 root 1.1
213 root 1.6 Coro::cede_notself
214 root 1.14 Works like cede, but is not exported by default and will cede to
215     *any* coroutine, regardless of priority. This is useful sometimes to
216     ensure progress is made.
217 root 1.6
218 root 1.1 terminate [arg...]
219 root 1.4 Terminates the current coroutine with the given status values (see
220 root 1.1 cancel).
221    
222 root 1.10 killall
223     Kills/terminates/cancels all coroutines except the currently running
224     one. This is useful after a fork, either in the child or the parent,
225     as usually only one of them should inherit the running coroutines.
226    
227 root 1.14 Note that while this will try to free some of the main programs
228     resources, you cnanot free all of them, so if a coroutine that is
229     not the main program calls this function, there will be some
230     one-time resource leak.
231 root 1.1
232 root 1.4 COROUTINE METHODS
233 root 1.14 These are the methods you can call on coroutine objects (or to create
234     them).
235 root 1.1
236     new Coro \&sub [, @args...]
237 root 1.14 Create a new coroutine and return it. When the sub returns, the
238 root 1.4 coroutine automatically terminates as if "terminate" with the
239     returned values were called. To make the coroutine run you must
240     first put it into the ready queue by calling the ready method.
241    
242 root 1.10 See "async" and "Coro::State::new" for additional info about the
243     coroutine environment.
244 root 1.4
245     $success = $coroutine->ready
246 root 1.14 Put the given coroutine into the end of its ready queue (there is
247     one queue for each priority) and return true. If the coroutine is
248     already in the ready queue, do nothing and return false.
249    
250     This ensures that the scheduler will resume this coroutine
251     automatically once all the coroutines of higher priority and all
252     coroutines of the same priority that were put into the ready queue
253     earlier have been resumed.
254 root 1.4
255     $is_ready = $coroutine->is_ready
256     Return wether the coroutine is currently the ready queue or not,
257    
258     $coroutine->cancel (arg...)
259     Terminates the given coroutine and makes it return the given
260 root 1.6 arguments as status (default: the empty list). Never returns if the
261     coroutine is the current coroutine.
262 root 1.1
263 root 1.4 $coroutine->join
264 root 1.1 Wait until the coroutine terminates and return any values given to
265 root 1.10 the "terminate" or "cancel" functions. "join" can be called
266 root 1.14 concurrently from multiple coroutines, and all will be resumed and
267     given the status return once the $coroutine terminates.
268 root 1.1
269 root 1.6 $coroutine->on_destroy (\&cb)
270     Registers a callback that is called when this coroutine gets
271     destroyed, but before it is joined. The callback gets passed the
272 root 1.14 terminate arguments, if any, and *must not* die, under any
273     circumstances.
274 root 1.6
275 root 1.4 $oldprio = $coroutine->prio ($newprio)
276 root 1.1 Sets (or gets, if the argument is missing) the priority of the
277 root 1.4 coroutine. Higher priority coroutines get run before lower priority
278     coroutines. Priorities are small signed integers (currently -4 ..
279 root 1.1 +3), that you can refer to using PRIO_xxx constants (use the import
280     tag :prio to get then):
281    
282     PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN
283     3 > 1 > 0 > -1 > -3 > -4
284    
285     # set priority to HIGH
286     current->prio(PRIO_HIGH);
287    
288     The idle coroutine ($Coro::idle) always has a lower priority than
289     any existing coroutine.
290    
291 root 1.4 Changing the priority of the current coroutine will take effect
292     immediately, but changing the priority of coroutines in the ready
293 root 1.1 queue (but not running) will only take effect after the next
294 root 1.4 schedule (of that coroutine). This is a bug that will be fixed in
295     some future version.
296 root 1.1
297 root 1.4 $newprio = $coroutine->nice ($change)
298 root 1.1 Similar to "prio", but subtract the given value from the priority
299     (i.e. higher values mean lower priority, just as in unix).
300    
301 root 1.4 $olddesc = $coroutine->desc ($newdesc)
302 root 1.1 Sets (or gets in case the argument is missing) the description for
303 root 1.4 this coroutine. This is just a free-form string you can associate
304     with a coroutine.
305    
306 root 1.10 This method simply sets the "$coroutine->{desc}" member to the given
307     string. You can modify this member directly if you wish.
308    
309 root 1.11 $coroutine->throw ([$scalar])
310     If $throw is specified and defined, it will be thrown as an
311     exception inside the coroutine at the next convinient point in time
312     (usually after it gains control at the next schedule/transfer/cede).
313     Otherwise clears the exception object.
314    
315     The exception object will be thrown "as is" with the specified
316     scalar in $@, i.e. if it is a string, no line number or newline will
317     be appended (unlike with "die").
318    
319     This can be used as a softer means than "cancel" to ask a coroutine
320     to end itself, although there is no guarentee that the exception
321     will lead to termination, and if the exception isn't caught it might
322     well end the whole program.
323    
324 root 1.5 GLOBAL FUNCTIONS
325     Coro::nready
326     Returns the number of coroutines that are currently in the ready
327 root 1.14 state, i.e. that can be switched to by calling "schedule" directory
328     or indirectly. The value 0 means that the only runnable coroutine is
329     the currently running one, so "cede" would have no effect, and
330     "schedule" would cause a deadlock unless there is an idle handler
331     that wakes up some coroutines.
332 root 1.5
333 root 1.6 my $guard = Coro::guard { ... }
334     This creates and returns a guard object. Nothing happens until the
335 root 1.7 object gets destroyed, in which case the codeblock given as argument
336 root 1.6 will be executed. This is useful to free locks or other resources in
337     case of a runtime error or when the coroutine gets canceled, as in
338     both cases the guard block will be executed. The guard object
339     supports only one method, "->cancel", which will keep the codeblock
340     from being executed.
341    
342     Example: set some flag and clear it again when the coroutine gets
343     canceled or the function returns:
344    
345     sub do_something {
346     my $guard = Coro::guard { $busy = 0 };
347     $busy = 1;
348    
349     # do something that requires $busy to be true
350     }
351    
352 root 1.4 unblock_sub { ... }
353     This utility function takes a BLOCK or code reference and "unblocks"
354 root 1.14 it, returning a new coderef. Unblocking means that calling the new
355     coderef will return immediately without blocking, returning nothing,
356     while the original code ref will be called (with parameters) from
357     within another coroutine.
358 root 1.4
359 root 1.8 The reason this function exists is that many event libraries (such
360 root 1.4 as the venerable Event module) are not coroutine-safe (a weaker form
361     of thread-safety). This means you must not block within event
362 root 1.14 callbacks, otherwise you might suffer from crashes or worse. The
363     only event library currently known that is safe to use without
364     "unblock_sub" is EV.
365 root 1.4
366     This function allows your callbacks to block by executing them in
367     another coroutine where it is safe to block. One example where
368     blocking is handy is when you use the Coro::AIO functions to save
369 root 1.14 results to disk, for example.
370 root 1.4
371     In short: simply use "unblock_sub { ... }" instead of "sub { ... }"
372     when creating event callbacks that want to block.
373 root 1.1
374 root 1.14 If your handler does not plan to block (e.g. simply sends a message
375     to another coroutine, or puts some other coroutine into the ready
376     queue), there is no reason to use "unblock_sub".
377    
378     Note that you also need to use "unblock_sub" for any other callbacks
379     that are indirectly executed by any C-based event loop. For example,
380     when you use a module that uses AnyEvent (and you use
381     Coro::AnyEvent) and it provides callbacks that are the result of
382     some event callback, then you must not block either, or use
383     "unblock_sub".
384    
385 root 1.1 BUGS/LIMITATIONS
386 root 1.14 This module is not perl-pseudo-thread-safe. You should only ever use
387     this module from the same thread (this requirement might be removed in
388     the future to allow per-thread schedulers, but Coro::State does not yet
389     allow this). I recommend disabling thread support and using processes,
390     as this is much faster and uses less memory.
391 root 1.1
392     SEE ALSO
393 root 1.14 Event-Loop integration: Coro::AnyEvent, Coro::EV, Coro::Event.
394 root 1.12
395     Debugging: Coro::Debug.
396    
397     Support/Utility: Coro::Specific, Coro::Util.
398 root 1.2
399     Locking/IPC: Coro::Signal, Coro::Channel, Coro::Semaphore,
400     Coro::SemaphoreSet, Coro::RWLock.
401    
402 root 1.14 IO/Timers: Coro::Timer, Coro::Handle, Coro::Socket, Coro::AIO.
403    
404     Compatibility: Coro::LWP, Coro::BDB, Coro::Storable, Coro::Select.
405 root 1.12
406 root 1.14 XS API: Coro::MakeMaker.
407 root 1.2
408 root 1.14 Low level Configuration, Coroutine Environment: Coro::State.
409 root 1.1
410     AUTHOR
411     Marc Lehmann <schmorp@schmorp.de>
412     http://home.schmorp.de/
413