ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/README
Revision: 1.15
Committed: Sun Sep 21 01:23:26 2008 UTC (15 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-4_748, rel-4_8, rel-4_747, rel-4_479, rel-4_746
Changes since 1.14: +6 -5 lines
Log Message:
4.746

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