ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/README
Revision: 1.31
Committed: Fri Dec 7 23:23:15 2012 UTC (11 years, 5 months ago) by root
Branch: MAIN
Changes since 1.30: +5 -4 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 NAME
2 root 1.19 Coro - the only real threads in perl
3 root 1.1
4     SYNOPSIS
5 root 1.14 use Coro;
6 root 1.24
7     async {
8 root 1.14 # 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 root 1.21 cede; # yield to coro
15 root 1.14 print "3\n";
16     cede; # and again
17 root 1.24
18     # use locking
19 root 1.14 my $lock = new Coro::Semaphore;
20     my $locked;
21 root 1.24
22     $lock->down;
23 root 1.14 $locked = 1;
24     $lock->up;
25 root 1.1
26     DESCRIPTION
27 root 1.19 For a tutorial-style introduction, please read the Coro::Intro manpage.
28     This manpage mainly contains reference information.
29 root 1.1
30 root 1.19 This module collection manages continuations in general, most often in
31 root 1.21 the form of cooperative threads (also called coros, or simply "coro" in
32     the documentation). They are similar to kernel threads but don't (in
33 root 1.19 general) run in parallel at the same time even on SMP machines. The
34     specific flavor of thread offered by this module also guarantees you
35     that it will not switch between threads unless necessary, at
36     easily-identified points in your program, so locking and parallel access
37     are rarely an issue, making thread programming much safer and easier
38     than using other thread models.
39    
40     Unlike the so-called "Perl threads" (which are not actually real threads
41 root 1.26 but only the windows process emulation (see section of same name for
42 root 1.29 more details) ported to UNIX, and as such act as processes), Coro
43 root 1.26 provides a full shared address space, which makes communication between
44 root 1.29 threads very easy. And coro threads are fast, too: disabling the Windows
45     process emulation code in your perl and using Coro can easily result in
46     a two to four times speed increase for your programs. A parallel matrix
47     multiplication benchmark (very communication-intensive) runs over 300
48     times faster on a single core than perls pseudo-threads on a quad core
49     using all four cores.
50 root 1.19
51     Coro achieves that by supporting multiple running interpreters that
52     share data, which is especially useful to code pseudo-parallel processes
53     and for event-based programming, such as multiple HTTP-GET requests
54     running concurrently. See Coro::AnyEvent to learn more on how to
55     integrate Coro into an event-based environment.
56    
57     In this module, a thread is defined as "callchain + lexical variables +
58 root 1.21 some package variables + C stack), that is, a thread has its own
59     callchain, its own set of lexicals and its own set of perls most
60     important global variables (see Coro::State for more configuration and
61     background info).
62 root 1.19
63     See also the "SEE ALSO" section at the end of this document - the Coro
64     module family is quite large.
65    
66 root 1.29 CORO THREAD LIFE CYCLE
67     During the long and exciting (or not) life of a coro thread, it goes
68     through a number of states:
69    
70     1. Creation
71     The first thing in the life of a coro thread is it's creation -
72     obviously. The typical way to create a thread is to call the "async
73     BLOCK" function:
74    
75     async {
76     # thread code goes here
77     };
78    
79     You can also pass arguments, which are put in @_:
80    
81     async {
82     print $_[1]; # prints 2
83     } 1, 2, 3;
84    
85     This creates a new coro thread and puts it into the ready queue,
86     meaning it will run as soon as the CPU is free for it.
87    
88 root 1.30 "async" will return a Coro object - you can store this for future
89     reference or ignore it - a thread that is running, ready to run or
90     waiting for some event is alive on it's own.
91 root 1.29
92     Another way to create a thread is to call the "new" constructor with
93     a code-reference:
94    
95     new Coro sub {
96     # thread code goes here
97     }, @optional_arguments;
98    
99     This is quite similar to calling "async", but the important
100     difference is that the new thread is not put into the ready queue,
101     so the thread will not run until somebody puts it there. "async" is,
102     therefore, identical to this sequence:
103    
104     my $coro = new Coro sub {
105     # thread code goes here
106     };
107     $coro->ready;
108     return $coro;
109    
110     2. Startup
111     When a new coro thread is created, only a copy of the code reference
112     and the arguments are stored, no extra memory for stacks and so on
113     is allocated, keeping the coro thread in a low-memory state.
114    
115     Only when it actually starts executing will all the resources be
116     finally allocated.
117    
118     The optional arguments specified at coro creation are available in
119     @_, similar to function calls.
120    
121     3. Running / Blocking
122     A lot can happen after the coro thread has started running. Quite
123     usually, it will not run to the end in one go (because you could use
124     a function instead), but it will give up the CPU regularly because
125     it waits for external events.
126    
127 root 1.30 As long as a coro thread runs, its Coro object is available in the
128 root 1.29 global variable $Coro::current.
129    
130     The low-level way to give up the CPU is to call the scheduler, which
131     selects a new coro thread to run:
132    
133     Coro::schedule;
134    
135     Since running threads are not in the ready queue, calling the
136     scheduler without doing anything else will block the coro thread
137     forever - you need to arrange either for the coro to put woken up
138     (readied) by some other event or some other thread, or you can put
139     it into the ready queue before scheduling:
140    
141     # this is exactly what Coro::cede does
142     $Coro::current->ready;
143     Coro::schedule;
144    
145     All the higher-level synchronisation methods (Coro::Semaphore,
146     Coro::rouse_*...) are actually implemented via "->ready" and
147     "Coro::schedule".
148    
149     While the coro thread is running it also might get assigned a
150     C-level thread, or the C-level thread might be unassigned from it,
151     as the Coro runtime wishes. A C-level thread needs to be assigned
152     when your perl thread calls into some C-level function and that
153     function in turn calls perl and perl then wants to switch
154     coroutines. This happens most often when you run an event loop and
155     block in the callback, or when perl itself calls some function such
156     as "AUTOLOAD" or methods via the "tie" mechanism.
157    
158     4. Termination
159     Many threads actually terminate after some time. There are a number
160     of ways to terminate a coro thread, the simplest is returning from
161     the top-level code reference:
162    
163     async {
164     # after returning from here, the coro thread is terminated
165     };
166    
167     async {
168     return if 0.5 < rand; # terminate a little earlier, maybe
169     print "got a chance to print this\n";
170     # or here
171     };
172    
173     Any values returned from the coroutine can be recovered using
174     "->join":
175    
176     my $coro = async {
177     "hello, world\n" # return a string
178     };
179    
180     my $hello_world = $coro->join;
181    
182     print $hello_world;
183    
184     Another way to terminate is to call "Coro::terminate", which at any
185     subroutine call nesting level:
186    
187     async {
188     Coro::terminate "return value 1", "return value 2";
189     };
190    
191 root 1.30 And yet another way is to "->cancel" (or "->safe_cancel") the coro
192     thread from another thread:
193 root 1.29
194     my $coro = async {
195     exit 1;
196     };
197    
198 root 1.30 $coro->cancel; # also accepts values for ->join to retrieve
199 root 1.29
200     Cancellation *can* be dangerous - it's a bit like calling "exit"
201     without actually exiting, and might leave C libraries and XS modules
202     in a weird state. Unlike other thread implementations, however, Coro
203     is exceptionally safe with regards to cancellation, as perl will
204 root 1.30 always be in a consistent state, and for those cases where you want
205     to do truly marvellous things with your coro while it is being
206     cancelled - that is, make sure all cleanup code is executed from the
207     thread being cancelled - there is even a "->safe_cancel" method.
208 root 1.29
209     So, cancelling a thread that runs in an XS event loop might not be
210     the best idea, but any other combination that deals with perl only
211     (cancelling when a thread is in a "tie" method or an "AUTOLOAD" for
212     example) is safe.
213    
214 root 1.30 Lastly, a coro thread object that isn't referenced is "->cancel"'ed
215     automatically - just like other objects in Perl. This is not such a
216     common case, however - a running thread is referencedy b
217     $Coro::current, a thread ready to run is referenced by the ready
218     queue, a thread waiting on a lock or semaphore is referenced by
219     being in some wait list and so on. But a thread that isn't in any of
220     those queues gets cancelled:
221    
222     async {
223     schedule; # cede to other coros, don't go into the ready queue
224     };
225    
226     cede;
227     # now the async above is destroyed, as it is not referenced by anything.
228    
229 root 1.29 5. Cleanup
230     Threads will allocate various resources. Most but not all will be
231     returned when a thread terminates, during clean-up.
232    
233     Cleanup is quite similar to throwing an uncaught exception: perl
234     will work it's way up through all subroutine calls and blocks. On
235     it's way, it will release all "my" variables, undo all "local"'s and
236     free any other resources truly local to the thread.
237    
238     So, a common way to free resources is to keep them referenced only
239     by my variables:
240    
241     async {
242     my $big_cache = new Cache ...;
243     };
244    
245     If there are no other references, then the $big_cache object will be
246     freed when the thread terminates, regardless of how it does so.
247    
248     What it does "NOT" do is unlock any Coro::Semaphores or similar
249     resources, but that's where the "guard" methods come in handy:
250    
251     my $sem = new Coro::Semaphore;
252    
253     async {
254     my $lock_guard = $sem->guard;
255     # if we reutrn, or die or get cancelled, here,
256     # then the semaphore will be "up"ed.
257     };
258    
259     The "Guard::guard" function comes in handy for any custom cleanup
260 root 1.30 you might want to do (but you cannot switch to other coroutines form
261     those code blocks):
262 root 1.29
263     async {
264     my $window = new Gtk2::Window "toplevel";
265     # The window will not be cleaned up automatically, even when $window
266     # gets freed, so use a guard to ensure it's destruction
267     # in case of an error:
268     my $window_guard = Guard::guard { $window->destroy };
269    
270     # we are safe here
271     };
272    
273     Last not least, "local" can often be handy, too, e.g. when
274     temporarily replacing the coro thread description:
275    
276     sub myfunction {
277     local $Coro::current->{desc} = "inside myfunction(@_)";
278    
279     # if we return or die here, the description will be restored
280     }
281    
282     6. Viva La Zombie Muerte
283 root 1.30 Even after a thread has terminated and cleaned up its resources, the
284     Coro object still is there and stores the return values of the
285     thread.
286 root 1.29
287 root 1.30 The means the Coro object gets freed automatically when the thread
288 root 1.29 has terminated and cleaned up and there arenot other references.
289    
290 root 1.30 If there are, the Coro object will stay around, and you can call
291 root 1.29 "->join" as many times as you wish to retrieve the result values:
292    
293     async {
294     print "hi\n";
295     1
296     };
297    
298     # run the async above, and free everything before returning
299     # from Coro::cede:
300     Coro::cede;
301    
302     {
303     my $coro = async {
304     print "hi\n";
305     1
306     };
307    
308     # run the async above, and clean up, but do not free the coro
309     # object:
310     Coro::cede;
311    
312     # optionally retrieve the result values
313     my @results = $coro->join;
314    
315     # now $coro goes out of scope, and presumably gets freed
316     };
317    
318 root 1.19 GLOBAL VARIABLES
319 root 1.14 $Coro::main
320 root 1.21 This variable stores the Coro object that represents the main
321 root 1.31 program. While you can "ready" it and do most other things you can
322 root 1.21 do to coro, it is mainly useful to compare again $Coro::current, to
323     see whether you are running in the main program or not.
324 root 1.14
325     $Coro::current
326 root 1.21 The Coro object representing the current coro (the last coro that
327     the Coro scheduler switched to). The initial value is $Coro::main
328     (of course).
329 root 1.14
330     This variable is strictly *read-only*. You can take copies of the
331 root 1.21 value stored in it and use it as any other Coro object, but you must
332     not otherwise modify the variable itself.
333 root 1.14
334     $Coro::idle
335     This variable is mainly useful to integrate Coro into event loops.
336 root 1.19 It is usually better to rely on Coro::AnyEvent or Coro::EV, as this
337     is pretty low-level functionality.
338    
339 root 1.26 This variable stores a Coro object that is put into the ready queue
340     when there are no other ready threads (without invoking any ready
341     hooks).
342 root 1.19
343 root 1.26 The default implementation dies with "FATAL: deadlock detected.",
344     followed by a thread listing, because the program has no other way
345 root 1.21 to continue.
346    
347 root 1.19 This hook is overwritten by modules such as "Coro::EV" and
348 root 1.29 "Coro::AnyEvent" to wait on an external event that hopefully wakes
349     up a coro so the scheduler can run it.
350 root 1.1
351 root 1.26 See Coro::EV or Coro::AnyEvent for examples of using this technique.
352 root 1.1
353 root 1.21 SIMPLE CORO CREATION
354 root 1.14 async { ... } [@args...]
355 root 1.21 Create a new coro and return its Coro object (usually unused). The
356     coro will be put into the ready queue, so it will start running
357     automatically on the next scheduler run.
358 root 1.1
359 root 1.14 The first argument is a codeblock/closure that should be executed in
360 root 1.21 the coro. When it returns argument returns the coro is automatically
361     terminated.
362 root 1.1
363 root 1.14 The remaining arguments are passed as arguments to the closure.
364    
365 root 1.21 See the "Coro::State::new" constructor for info about the coro
366     environment in which coro are executed.
367 root 1.10
368 root 1.21 Calling "exit" in a coro will do the same as calling exit outside
369     the coro. Likewise, when the coro dies, the program will exit, just
370     as it would in the main program.
371 root 1.3
372 root 1.14 If you do not want that, you can provide a default "die" handler, or
373     simply avoid dieing (by use of "eval").
374    
375 root 1.21 Example: Create a new coro that just prints its arguments.
376 root 1.14
377 root 1.1 async {
378     print "@_\n";
379     } 1,2,3,4;
380    
381 root 1.6 async_pool { ... } [@args...]
382 root 1.21 Similar to "async", but uses a coro pool, so you should not call
383     terminate or join on it (although you are allowed to), and you get a
384     coro that might have executed other code already (which can be good
385     or bad :).
386 root 1.14
387 root 1.18 On the plus side, this function is about twice as fast as creating
388 root 1.21 (and destroying) a completely new coro, so if you need a lot of
389     generic coros in quick successsion, use "async_pool", not "async".
390 root 1.6
391 root 1.14 The code block is executed in an "eval" context and a warning will
392 root 1.6 be issued in case of an exception instead of terminating the
393 root 1.21 program, as "async" does. As the coro is being reused, stuff like
394     "on_destroy" will not work in the expected way, unless you call
395 root 1.14 terminate or cancel, which somehow defeats the purpose of pooling
396     (but is fine in the exceptional case).
397 root 1.6
398 root 1.14 The priority will be reset to 0 after each run, tracing will be
399 root 1.10 disabled, the description will be reset and the default output
400 root 1.14 filehandle gets restored, so you can change all these. Otherwise the
401 root 1.21 coro will be re-used "as-is": most notably if you change other
402     per-coro global stuff such as $/ you *must needs* revert that
403 root 1.16 change, which is most simply done by using local as in: "local $/".
404    
405 root 1.21 The idle pool size is limited to 8 idle coros (this can be adjusted
406     by changing $Coro::POOL_SIZE), but there can be as many non-idle
407     coros as required.
408 root 1.6
409 root 1.21 If you are concerned about pooled coros growing a lot because a
410 root 1.6 single "async_pool" used a lot of stackspace you can e.g.
411     "async_pool { terminate }" once per second or so to slowly replenish
412 root 1.9 the pool. In addition to that, when the stacks used by a handler
413 root 1.19 grows larger than 32kb (adjustable via $Coro::POOL_RSS) it will also
414 root 1.14 be destroyed.
415    
416 root 1.19 STATIC METHODS
417     Static methods are actually functions that implicitly operate on the
418 root 1.21 current coro.
419 root 1.6
420 root 1.1 schedule
421 root 1.21 Calls the scheduler. The scheduler will find the next coro that is
422     to be run from the ready queue and switches to it. The next coro to
423     be run is simply the one with the highest priority that is longest
424 root 1.26 in its ready queue. If there is no coro ready, it will call the
425 root 1.21 $Coro::idle hook.
426    
427     Please note that the current coro will *not* be put into the ready
428     queue, so calling this function usually means you will never be
429     called again unless something else (e.g. an event handler) calls
430 root 1.14 "->ready", thus waking you up.
431    
432     This makes "schedule" *the* generic method to use to block the
433 root 1.21 current coro and wait for events: first you remember the current
434     coro in a variable, then arrange for some callback of yours to call
435     "->ready" on that once some event happens, and last you call
436     "schedule" to put yourself to sleep. Note that a lot of things can
437     wake your coro up, so you need to check whether the event indeed
438     happened, e.g. by storing the status in a variable.
439 root 1.4
440 root 1.18 See HOW TO WAIT FOR A CALLBACK, below, for some ways to wait for
441     callbacks.
442 root 1.1
443     cede
444 root 1.21 "Cede" to other coros. This function puts the current coro into the
445     ready queue and calls "schedule", which has the effect of giving up
446     the current "timeslice" to other coros of the same or higher
447     priority. Once your coro gets its turn again it will automatically
448     be resumed.
449 root 1.14
450     This function is often called "yield" in other languages.
451 root 1.1
452 root 1.6 Coro::cede_notself
453 root 1.14 Works like cede, but is not exported by default and will cede to
454 root 1.21 *any* coro, regardless of priority. This is useful sometimes to
455 root 1.14 ensure progress is made.
456 root 1.6
457 root 1.1 terminate [arg...]
458 root 1.21 Terminates the current coro with the given status values (see
459 root 1.30 cancel). The values will not be copied, but referenced directly.
460 root 1.1
461 root 1.21 Coro::on_enter BLOCK, Coro::on_leave BLOCK
462     These function install enter and leave winders in the current scope.
463     The enter block will be executed when on_enter is called and
464     whenever the current coro is re-entered by the scheduler, while the
465     leave block is executed whenever the current coro is blocked by the
466     scheduler, and also when the containing scope is exited (by whatever
467     means, be it exit, die, last etc.).
468    
469     *Neither invoking the scheduler, nor exceptions, are allowed within
470     those BLOCKs*. That means: do not even think about calling "die"
471     without an eval, and do not even think of entering the scheduler in
472     any way.
473    
474     Since both BLOCKs are tied to the current scope, they will
475     automatically be removed when the current scope exits.
476    
477     These functions implement the same concept as "dynamic-wind" in
478     scheme does, and are useful when you want to localise some resource
479     to a specific coro.
480    
481 root 1.24 They slow down thread switching considerably for coros that use them
482     (about 40% for a BLOCK with a single assignment, so thread switching
483     is still reasonably fast if the handlers are fast).
484 root 1.21
485     These functions are best understood by an example: The following
486     function will change the current timezone to
487     "Antarctica/South_Pole", which requires a call to "tzset", but by
488     using "on_enter" and "on_leave", which remember/change the current
489     timezone and restore the previous value, respectively, the timezone
490 root 1.23 is only changed for the coro that installed those handlers.
491 root 1.21
492     use POSIX qw(tzset);
493    
494     async {
495     my $old_tz; # store outside TZ value here
496    
497     Coro::on_enter {
498     $old_tz = $ENV{TZ}; # remember the old value
499    
500     $ENV{TZ} = "Antarctica/South_Pole";
501     tzset; # enable new value
502     };
503    
504     Coro::on_leave {
505     $ENV{TZ} = $old_tz;
506     tzset; # restore old value
507     };
508    
509     # at this place, the timezone is Antarctica/South_Pole,
510     # without disturbing the TZ of any other coro.
511     };
512    
513     This can be used to localise about any resource (locale, uid,
514     current working directory etc.) to a block, despite the existance of
515     other coros.
516    
517 root 1.24 Another interesting example implements time-sliced multitasking
518     using interval timers (this could obviously be optimised, but does
519     the job):
520    
521     # "timeslice" the given block
522     sub timeslice(&) {
523     use Time::HiRes ();
524    
525     Coro::on_enter {
526     # on entering the thread, we set an VTALRM handler to cede
527     $SIG{VTALRM} = sub { cede };
528     # and then start the interval timer
529     Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01;
530     };
531     Coro::on_leave {
532     # on leaving the thread, we stop the interval timer again
533     Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0;
534     };
535    
536     &{+shift};
537     }
538    
539     # use like this:
540     timeslice {
541     # The following is an endless loop that would normally
542     # monopolise the process. Since it runs in a timesliced
543     # environment, it will regularly cede to other threads.
544     while () { }
545     };
546    
547 root 1.10 killall
548 root 1.21 Kills/terminates/cancels all coros except the currently running one.
549    
550     Note that while this will try to free some of the main interpreter
551     resources if the calling coro isn't the main coro, but one cannot
552     free all of them, so if a coro that is not the main coro calls this
553     function, there will be some one-time resource leak.
554    
555     CORO OBJECT METHODS
556     These are the methods you can call on coro objects (or to create them).
557 root 1.1
558     new Coro \&sub [, @args...]
559 root 1.21 Create a new coro and return it. When the sub returns, the coro
560     automatically terminates as if "terminate" with the returned values
561     were called. To make the coro run you must first put it into the
562     ready queue by calling the ready method.
563 root 1.4
564 root 1.10 See "async" and "Coro::State::new" for additional info about the
565 root 1.21 coro environment.
566 root 1.4
567 root 1.21 $success = $coro->ready
568     Put the given coro into the end of its ready queue (there is one
569     queue for each priority) and return true. If the coro is already in
570     the ready queue, do nothing and return false.
571    
572     This ensures that the scheduler will resume this coro automatically
573     once all the coro of higher priority and all coro of the same
574     priority that were put into the ready queue earlier have been
575     resumed.
576    
577 root 1.22 $coro->suspend
578     Suspends the specified coro. A suspended coro works just like any
579     other coro, except that the scheduler will not select a suspended
580     coro for execution.
581    
582     Suspending a coro can be useful when you want to keep the coro from
583     running, but you don't want to destroy it, or when you want to
584     temporarily freeze a coro (e.g. for debugging) to resume it later.
585    
586     A scenario for the former would be to suspend all (other) coros
587     after a fork and keep them alive, so their destructors aren't
588     called, but new coros can be created.
589    
590     $coro->resume
591     If the specified coro was suspended, it will be resumed. Note that
592     when the coro was in the ready queue when it was suspended, it might
593     have been unreadied by the scheduler, so an activation might have
594     been lost.
595    
596     To avoid this, it is best to put a suspended coro into the ready
597     queue unconditionally, as every synchronisation mechanism must
598     protect itself against spurious wakeups, and the one in the Coro
599     family certainly do that.
600    
601 root 1.30 $state->is_new
602     Returns true iff this Coro object is "new", i.e. has never been run
603     yet. Those states basically consist of only the code reference to
604     call and the arguments, but consumes very little other resources.
605     New states will automatically get assigned a perl interpreter when
606     they are transfered to.
607    
608     $state->is_zombie
609     Returns true iff the Coro object has been cancelled, i.e. it's
610     resources freed because they were "cancel"'ed, "terminate"'d,
611     "safe_cancel"'ed or simply went out of scope.
612    
613     The name "zombie" stems from UNIX culture, where a process that has
614     exited and only stores and exit status and no other resources is
615     called a "zombie".
616    
617 root 1.21 $is_ready = $coro->is_ready
618     Returns true iff the Coro object is in the ready queue. Unless the
619     Coro object gets destroyed, it will eventually be scheduled by the
620     scheduler.
621    
622     $is_running = $coro->is_running
623     Returns true iff the Coro object is currently running. Only one Coro
624     object can ever be in the running state (but it currently is
625     possible to have multiple running Coro::States).
626    
627     $is_suspended = $coro->is_suspended
628     Returns true iff this Coro object has been suspended. Suspended
629     Coros will not ever be scheduled.
630    
631     $coro->cancel (arg...)
632 root 1.30 Terminates the given Coro thread and makes it return the given
633     arguments as status (default: an empty list). Never returns if the
634     Coro is the current Coro.
635    
636     This is a rather brutal way to free a coro, with some limitations -
637     if the thread is inside a C callback that doesn't expect to be
638     canceled, bad things can happen, or if the cancelled thread insists
639     on running complicated cleanup handlers that rely on its thread
640     context, things will not work.
641    
642     Any cleanup code being run (e.g. from "guard" blocks) will be run
643     without a thread context, and is not allowed to switch to other
644     threads. On the plus side, "->cancel" will always clean up the
645     thread, no matter what. If your cleanup code is complex or you want
646     to avoid cancelling a C-thread that doesn't know how to clean up
647     itself, it can be better to "->throw" an exception, or use
648     "->safe_cancel".
649    
650     The arguments to "->cancel" are not copied, but instead will be
651     referenced directly (e.g. if you pass $var and after the call change
652     that variable, then you might change the return values passed to
653     e.g. "join", so don't do that).
654    
655     The resources of the Coro are usually freed (or destructed) before
656     this call returns, but this can be delayed for an indefinite amount
657     of time, as in some cases the manager thread has to run first to
658     actually destruct the Coro object.
659    
660     $coro->safe_cancel ($arg...)
661     Works mostly like "->cancel", but is inherently "safer", and
662     consequently, can fail with an exception in cases the thread is not
663     in a cancellable state.
664    
665     This method works a bit like throwing an exception that cannot be
666     caught - specifically, it will clean up the thread from within
667     itself, so all cleanup handlers (e.g. "guard" blocks) are run with
668     full thread context and can block if they wish. The downside is that
669     there is no guarantee that the thread can be cancelled when you call
670     this method, and therefore, it might fail. It is also considerably
671     slower than "cancel" or "terminate".
672    
673     A thread is in a safe-cancellable state if it either hasn't been run
674     yet, or it has no C context attached and is inside an SLF function.
675    
676     The latter two basically mean that the thread isn't currently inside
677     a perl callback called from some C function (usually via some XS
678     modules) and isn't currently executing inside some C function itself
679     (via Coro's XS API).
680    
681     This call returns true when it could cancel the thread, or croaks
682     with an error otherwise (i.e. it either returns true or doesn't
683     return at all).
684    
685     Why the weird interface? Well, there are two common models on how
686     and when to cancel things. In the first, you have the expectation
687     that your coro thread can be cancelled when you want to cancel it -
688     if the thread isn't cancellable, this would be a bug somewhere, so
689     "->safe_cancel" croaks to notify of the bug.
690    
691     In the second model you sometimes want to ask nicely to cancel a
692     thread, but if it's not a good time, well, then don't cancel. This
693     can be done relatively easy like this:
694    
695     if (! eval { $coro->safe_cancel }) {
696     warn "unable to cancel thread: $@";
697     }
698    
699     However, what you never should do is first try to cancel "safely"
700     and if that fails, cancel the "hard" way with "->cancel". That makes
701     no sense: either you rely on being able to execute cleanup code in
702     your thread context, or you don't. If you do, then "->safe_cancel"
703     is the only way, and if you don't, then "->cancel" is always faster
704     and more direct.
705 root 1.21
706     $coro->schedule_to
707     Puts the current coro to sleep (like "Coro::schedule"), but instead
708     of continuing with the next coro from the ready queue, always switch
709     to the given coro object (regardless of priority etc.). The
710     readyness state of that coro isn't changed.
711 root 1.18
712     This is an advanced method for special cases - I'd love to hear
713     about any uses for this one.
714    
715 root 1.21 $coro->cede_to
716     Like "schedule_to", but puts the current coro into the ready queue.
717     This has the effect of temporarily switching to the given coro, and
718     continuing some time later.
719 root 1.18
720     This is an advanced method for special cases - I'd love to hear
721     about any uses for this one.
722    
723 root 1.21 $coro->throw ([$scalar])
724 root 1.17 If $throw is specified and defined, it will be thrown as an
725 root 1.21 exception inside the coro at the next convenient point in time.
726 root 1.17 Otherwise clears the exception object.
727    
728 root 1.18 Coro will check for the exception each time a schedule-like-function
729     returns, i.e. after each "schedule", "cede",
730     "Coro::Semaphore->down", "Coro::Handle->readable" and so on. Most of
731 root 1.30 those functions (all that are part of Coro itself) detect this case
732     and return early in case an exception is pending.
733 root 1.18
734 root 1.17 The exception object will be thrown "as is" with the specified
735     scalar in $@, i.e. if it is a string, no line number or newline will
736     be appended (unlike with "die").
737    
738 root 1.30 This can be used as a softer means than either "cancel" or
739     "safe_cancel "to ask a coro to end itself, although there is no
740     guarantee that the exception will lead to termination, and if the
741     exception isn't caught it might well end the whole program.
742 root 1.17
743     You might also think of "throw" as being the moral equivalent of
744 root 1.21 "kill"ing a coro with a signal (in this case, a scalar).
745 root 1.17
746 root 1.21 $coro->join
747     Wait until the coro terminates and return any values given to the
748     "terminate" or "cancel" functions. "join" can be called concurrently
749 root 1.30 from multiple threads, and all will be resumed and given the status
750 root 1.21 return once the $coro terminates.
751    
752     $coro->on_destroy (\&cb)
753 root 1.28 Registers a callback that is called when this coro thread gets
754 root 1.30 destroyed, that is, after it's resources have been freed but before
755     it is joined. The callback gets passed the terminate/cancel
756     arguments, if any, and *must not* die, under any circumstances.
757 root 1.28
758 root 1.30 There can be any number of "on_destroy" callbacks per coro, and
759     there is no way currently to remove a callback once added.
760 root 1.21
761     $oldprio = $coro->prio ($newprio)
762 root 1.28 Sets (or gets, if the argument is missing) the priority of the coro
763     thread. Higher priority coro get run before lower priority coros.
764     Priorities are small signed integers (currently -4 .. +3), that you
765     can refer to using PRIO_xxx constants (use the import tag :prio to
766     get then):
767 root 1.1
768     PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN
769     3 > 1 > 0 > -1 > -3 > -4
770    
771     # set priority to HIGH
772 root 1.21 current->prio (PRIO_HIGH);
773 root 1.1
774 root 1.28 The idle coro thread ($Coro::idle) always has a lower priority than
775     any existing coro.
776 root 1.1
777 root 1.21 Changing the priority of the current coro will take effect
778 root 1.28 immediately, but changing the priority of a coro in the ready queue
779 root 1.21 (but not running) will only take effect after the next schedule (of
780     that coro). This is a bug that will be fixed in some future version.
781 root 1.1
782 root 1.21 $newprio = $coro->nice ($change)
783 root 1.1 Similar to "prio", but subtract the given value from the priority
784 root 1.28 (i.e. higher values mean lower priority, just as in UNIX's nice
785     command).
786 root 1.1
787 root 1.21 $olddesc = $coro->desc ($newdesc)
788 root 1.1 Sets (or gets in case the argument is missing) the description for
789 root 1.28 this coro thread. This is just a free-form string you can associate
790     with a coro.
791 root 1.4
792 root 1.21 This method simply sets the "$coro->{desc}" member to the given
793 root 1.28 string. You can modify this member directly if you wish, and in
794     fact, this is often preferred to indicate major processing states
795 root 1.31 that can then be seen for example in a Coro::Debug session:
796 root 1.28
797     sub my_long_function {
798     local $Coro::current->{desc} = "now in my_long_function";
799     ...
800     $Coro::current->{desc} = "my_long_function: phase 1";
801     ...
802     $Coro::current->{desc} = "my_long_function: phase 2";
803     ...
804     }
805 root 1.10
806 root 1.19 GLOBAL FUNCTIONS
807 root 1.5 Coro::nready
808 root 1.21 Returns the number of coro that are currently in the ready state,
809     i.e. that can be switched to by calling "schedule" directory or
810     indirectly. The value 0 means that the only runnable coro is the
811     currently running one, so "cede" would have no effect, and
812 root 1.14 "schedule" would cause a deadlock unless there is an idle handler
813 root 1.21 that wakes up some coro.
814 root 1.5
815 root 1.6 my $guard = Coro::guard { ... }
816 root 1.21 This function still exists, but is deprecated. Please use the
817     "Guard::guard" function instead.
818 root 1.6
819 root 1.4 unblock_sub { ... }
820     This utility function takes a BLOCK or code reference and "unblocks"
821 root 1.14 it, returning a new coderef. Unblocking means that calling the new
822     coderef will return immediately without blocking, returning nothing,
823     while the original code ref will be called (with parameters) from
824 root 1.21 within another coro.
825 root 1.4
826 root 1.8 The reason this function exists is that many event libraries (such
827 root 1.21 as the venerable Event module) are not thread-safe (a weaker form of
828     reentrancy). This means you must not block within event callbacks,
829     otherwise you might suffer from crashes or worse. The only event
830     library currently known that is safe to use without "unblock_sub" is
831 root 1.28 EV (but you might still run into deadlocks if all event loops are
832     blocked).
833 root 1.4
834 root 1.27 Coro will try to catch you when you block in the event loop
835     ("FATAL:$Coro::IDLE blocked itself"), but this is just best effort
836     and only works when you do not run your own event loop.
837    
838 root 1.4 This function allows your callbacks to block by executing them in
839 root 1.21 another coro where it is safe to block. One example where blocking
840     is handy is when you use the Coro::AIO functions to save results to
841     disk, for example.
842 root 1.4
843     In short: simply use "unblock_sub { ... }" instead of "sub { ... }"
844     when creating event callbacks that want to block.
845 root 1.1
846 root 1.14 If your handler does not plan to block (e.g. simply sends a message
847 root 1.21 to another coro, or puts some other coro into the ready queue),
848     there is no reason to use "unblock_sub".
849 root 1.14
850     Note that you also need to use "unblock_sub" for any other callbacks
851     that are indirectly executed by any C-based event loop. For example,
852     when you use a module that uses AnyEvent (and you use
853     Coro::AnyEvent) and it provides callbacks that are the result of
854     some event callback, then you must not block either, or use
855     "unblock_sub".
856    
857 root 1.26 $cb = rouse_cb
858 root 1.18 Create and return a "rouse callback". That's a code reference that,
859 root 1.19 when called, will remember a copy of its arguments and notify the
860 root 1.21 owner coro of the callback.
861 root 1.18
862     See the next function.
863    
864 root 1.26 @args = rouse_wait [$cb]
865 root 1.19 Wait for the specified rouse callback (or the last one that was
866 root 1.21 created in this coro).
867 root 1.18
868 root 1.19 As soon as the callback is invoked (or when the callback was invoked
869     before "rouse_wait"), it will return the arguments originally passed
870 root 1.25 to the rouse callback. In scalar context, that means you get the
871     *last* argument, just as if "rouse_wait" had a "return ($a1, $a2,
872     $a3...)" statement at the end.
873 root 1.18
874     See the section HOW TO WAIT FOR A CALLBACK for an actual usage
875     example.
876    
877     HOW TO WAIT FOR A CALLBACK
878 root 1.21 It is very common for a coro to wait for some callback to be called.
879     This occurs naturally when you use coro in an otherwise event-based
880     program, or when you use event-based libraries.
881 root 1.18
882     These typically register a callback for some event, and call that
883 root 1.21 callback when the event occured. In a coro, however, you typically want
884     to just wait for the event, simplyifying things.
885 root 1.18
886     For example "AnyEvent->child" registers a callback to be called when a
887     specific child has exited:
888    
889     my $child_watcher = AnyEvent->child (pid => $pid, cb => sub { ... });
890    
891 root 1.21 But from within a coro, you often just want to write this:
892 root 1.18
893     my $status = wait_for_child $pid;
894    
895     Coro offers two functions specifically designed to make this easy,
896     "Coro::rouse_cb" and "Coro::rouse_wait".
897    
898     The first function, "rouse_cb", generates and returns a callback that,
899 root 1.21 when invoked, will save its arguments and notify the coro that created
900     the callback.
901 root 1.18
902     The second function, "rouse_wait", waits for the callback to be called
903     (by calling "schedule" to go to sleep) and returns the arguments
904     originally passed to the callback.
905    
906     Using these functions, it becomes easy to write the "wait_for_child"
907     function mentioned above:
908    
909     sub wait_for_child($) {
910     my ($pid) = @_;
911    
912     my $watcher = AnyEvent->child (pid => $pid, cb => Coro::rouse_cb);
913    
914     my ($rpid, $rstatus) = Coro::rouse_wait;
915     $rstatus
916     }
917    
918     In the case where "rouse_cb" and "rouse_wait" are not flexible enough,
919 root 1.31 you can roll your own, using "schedule" and "ready":
920 root 1.18
921     sub wait_for_child($) {
922     my ($pid) = @_;
923    
924 root 1.21 # store the current coro in $current,
925 root 1.18 # and provide result variables for the closure passed to ->child
926     my $current = $Coro::current;
927     my ($done, $rstatus);
928    
929     # pass a closure to ->child
930     my $watcher = AnyEvent->child (pid => $pid, cb => sub {
931     $rstatus = $_[1]; # remember rstatus
932 root 1.31 $done = 1; # mark $rstatus as valid
933     $current->ready; # wake up the waiting thread
934 root 1.18 });
935    
936     # wait until the closure has been called
937     schedule while !$done;
938    
939     $rstatus
940     }
941    
942 root 1.1 BUGS/LIMITATIONS
943 root 1.18 fork with pthread backend
944     When Coro is compiled using the pthread backend (which isn't
945     recommended but required on many BSDs as their libcs are completely
946 root 1.21 broken), then coro will not survive a fork. There is no known
947 root 1.18 workaround except to fix your libc and use a saner backend.
948    
949     perl process emulation ("threads")
950     This module is not perl-pseudo-thread-safe. You should only ever use
951 root 1.19 this module from the first thread (this requirement might be removed
952 root 1.18 in the future to allow per-thread schedulers, but Coro::State does
953     not yet allow this). I recommend disabling thread support and using
954     processes, as having the windows process emulation enabled under
955     unix roughly halves perl performance, even when not used.
956    
957 root 1.30 Attempts to use threads created in another emulated process will
958     crash ("cleanly", with a null pointer exception).
959    
960 root 1.21 coro switching is not signal safe
961     You must not switch to another coro from within a signal handler
962     (only relevant with %SIG - most event libraries provide safe
963 root 1.27 signals), *unless* you are sure you are not interrupting a Coro
964     function.
965 root 1.18
966     That means you *MUST NOT* call any function that might "block" the
967 root 1.21 current coro - "cede", "schedule" "Coro::Semaphore->down" or
968 root 1.18 anything that calls those. Everything else, including calling
969     "ready", works.
970 root 1.1
971 root 1.26 WINDOWS PROCESS EMULATION
972     A great many people seem to be confused about ithreads (for example,
973     Chip Salzenberg called me unintelligent, incapable, stupid and gullible,
974     while in the same mail making rather confused statements about perl
975     ithreads (for example, that memory or files would be shared), showing
976     his lack of understanding of this area - if it is hard to understand for
977     Chip, it is probably not obvious to everybody).
978    
979     What follows is an ultra-condensed version of my talk about threads in
980 root 1.28 scripting languages given on the perl workshop 2009:
981 root 1.26
982     The so-called "ithreads" were originally implemented for two reasons:
983     first, to (badly) emulate unix processes on native win32 perls, and
984     secondly, to replace the older, real thread model ("5.005-threads").
985    
986     It does that by using threads instead of OS processes. The difference
987     between processes and threads is that threads share memory (and other
988     state, such as files) between threads within a single process, while
989     processes do not share anything (at least not semantically). That means
990     that modifications done by one thread are seen by others, while
991     modifications by one process are not seen by other processes.
992    
993     The "ithreads" work exactly like that: when creating a new ithreads
994     process, all state is copied (memory is copied physically, files and
995     code is copied logically). Afterwards, it isolates all modifications. On
996     UNIX, the same behaviour can be achieved by using operating system
997     processes, except that UNIX typically uses hardware built into the
998     system to do this efficiently, while the windows process emulation
999     emulates this hardware in software (rather efficiently, but of course it
1000     is still much slower than dedicated hardware).
1001    
1002     As mentioned before, loading code, modifying code, modifying data
1003     structures and so on is only visible in the ithreads process doing the
1004     modification, not in other ithread processes within the same OS process.
1005    
1006     This is why "ithreads" do not implement threads for perl at all, only
1007     processes. What makes it so bad is that on non-windows platforms, you
1008     can actually take advantage of custom hardware for this purpose (as
1009     evidenced by the forks module, which gives you the (i-) threads API,
1010     just much faster).
1011    
1012     Sharing data is in the i-threads model is done by transfering data
1013     structures between threads using copying semantics, which is very slow -
1014     shared data simply does not exist. Benchmarks using i-threads which are
1015     communication-intensive show extremely bad behaviour with i-threads (in
1016     fact, so bad that Coro, which cannot take direct advantage of multiple
1017     CPUs, is often orders of magnitude faster because it shares data using
1018     real threads, refer to my talk for details).
1019    
1020     As summary, i-threads *use* threads to implement processes, while the
1021     compatible forks module *uses* processes to emulate, uhm, processes.
1022     I-threads slow down every perl program when enabled, and outside of
1023     windows, serve no (or little) practical purpose, but disadvantages every
1024     single-threaded Perl program.
1025    
1026     This is the reason that I try to avoid the name "ithreads", as it is
1027     misleading as it implies that it implements some kind of thread model
1028     for perl, and prefer the name "windows process emulation", which
1029     describes the actual use and behaviour of it much better.
1030    
1031 root 1.1 SEE ALSO
1032 root 1.14 Event-Loop integration: Coro::AnyEvent, Coro::EV, Coro::Event.
1033 root 1.12
1034     Debugging: Coro::Debug.
1035    
1036     Support/Utility: Coro::Specific, Coro::Util.
1037 root 1.2
1038 root 1.19 Locking and IPC: Coro::Signal, Coro::Channel, Coro::Semaphore,
1039 root 1.2 Coro::SemaphoreSet, Coro::RWLock.
1040    
1041 root 1.19 I/O and Timers: Coro::Timer, Coro::Handle, Coro::Socket, Coro::AIO.
1042 root 1.14
1043 root 1.19 Compatibility with other modules: Coro::LWP (but see also AnyEvent::HTTP
1044     for a better-working alternative), Coro::BDB, Coro::Storable,
1045     Coro::Select.
1046 root 1.12
1047 root 1.14 XS API: Coro::MakeMaker.
1048 root 1.2
1049 root 1.19 Low level Configuration, Thread Environment, Continuations: Coro::State.
1050 root 1.1
1051     AUTHOR
1052     Marc Lehmann <schmorp@schmorp.de>
1053     http://home.schmorp.de/
1054