ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/Coro.pm
(Generate patch)

Comparing cvsroot/Coro/Coro.pm (file contents):
Revision 1.181 by root, Fri May 9 22:04:37 2008 UTC vs.
Revision 1.196 by root, Sat Aug 30 03:07:46 2008 UTC

35parallel access are rarely an issue, making coroutine programming much 35parallel access are rarely an issue, making coroutine programming much
36safer and easier than threads programming. 36safer and easier than threads programming.
37 37
38Unlike a normal perl program, however, coroutines allow you to have 38Unlike a normal perl program, however, coroutines allow you to have
39multiple running interpreters that share data, which is especially useful 39multiple running interpreters that share data, which is especially useful
40to code pseudo-parallel processes, such as multiple HTTP-GET requests 40to code pseudo-parallel processes and for event-based programming, such as
41running concurrently. 41multiple HTTP-GET requests running concurrently. See L<Coro::AnyEvent> to
42learn more.
42 43
43Coroutines are also useful because Perl has no support for threads (the so 44Coroutines are also useful because Perl has no support for threads (the so
44called "threads" that perl offers are nothing more than the (bad) process 45called "threads" that perl offers are nothing more than the (bad) process
45emulation coming from the Windows platform: On standard operating systems 46emulation coming from the Windows platform: On standard operating systems
46they serve no purpose whatsoever, except by making your programs slow and 47they serve no purpose whatsoever, except by making your programs slow and
65 66
66our $idle; # idle handler 67our $idle; # idle handler
67our $main; # main coroutine 68our $main; # main coroutine
68our $current; # current coroutine 69our $current; # current coroutine
69 70
70our $VERSION = 4.6; 71our $VERSION = 4.745;
71 72
72our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); 73our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub);
73our %EXPORT_TAGS = ( 74our %EXPORT_TAGS = (
74 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 75 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
75); 76);
80=item $Coro::main 81=item $Coro::main
81 82
82This variable stores the coroutine object that represents the main 83This variable stores the coroutine object that represents the main
83program. While you cna C<ready> it and do most other things you can do to 84program. While you cna C<ready> it and do most other things you can do to
84coroutines, it is mainly useful to compare again C<$Coro::current>, to see 85coroutines, it is mainly useful to compare again C<$Coro::current>, to see
85wether you are running in the main program or not. 86whether you are running in the main program or not.
86 87
87=cut 88=cut
88 89
89$main = new Coro; 90$main = new Coro;
90 91
265 _pool_2 $cb; 266 _pool_2 $cb;
266 &schedule; 267 &schedule;
267 } 268 }
268 }; 269 };
269 270
271 if ($@) {
270 last if $@ eq "\3async_pool terminate\2\n"; 272 last if $@ eq "\3async_pool terminate\2\n";
271 warn $@ if $@; 273 warn $@;
274 }
272 } 275 }
273} 276}
274 277
275sub async_pool(&@) { 278sub async_pool(&@) {
276 # this is also inlined into the unlock_scheduler 279 # this is also inlined into the unlock_scheduler
306This makes C<schedule> I<the> generic method to use to block the current 309This makes C<schedule> I<the> generic method to use to block the current
307coroutine and wait for events: first you remember the current coroutine in 310coroutine and wait for events: first you remember the current coroutine in
308a variable, then arrange for some callback of yours to call C<< ->ready 311a variable, then arrange for some callback of yours to call C<< ->ready
309>> on that once some event happens, and last you call C<schedule> to put 312>> on that once some event happens, and last you call C<schedule> to put
310yourself to sleep. Note that a lot of things can wake your coroutine up, 313yourself to sleep. Note that a lot of things can wake your coroutine up,
311so you need to check wether the event indeed happened, e.g. by storing the 314so you need to check whether the event indeed happened, e.g. by storing the
312status in a variable. 315status in a variable.
313 316
314The canonical way to wait on external events is this: 317The canonical way to wait on external events is this:
315 318
316 { 319 {
355Kills/terminates/cancels all coroutines except the currently running 358Kills/terminates/cancels all coroutines except the currently running
356one. This is useful after a fork, either in the child or the parent, as 359one. This is useful after a fork, either in the child or the parent, as
357usually only one of them should inherit the running coroutines. 360usually only one of them should inherit the running coroutines.
358 361
359Note that while this will try to free some of the main programs resources, 362Note that while this will try to free some of the main programs resources,
360you cnanot free all of them, so if a coroutine that is not the main 363you cannot free all of them, so if a coroutine that is not the main
361program calls this function, there will be some one-time resource leak. 364program calls this function, there will be some one-time resource leak.
362 365
363=cut 366=cut
364 367
365sub terminate { 368sub terminate {
414once all the coroutines of higher priority and all coroutines of the same 417once all the coroutines of higher priority and all coroutines of the same
415priority that were put into the ready queue earlier have been resumed. 418priority that were put into the ready queue earlier have been resumed.
416 419
417=item $is_ready = $coroutine->is_ready 420=item $is_ready = $coroutine->is_ready
418 421
419Return wether the coroutine is currently the ready queue or not, 422Return whether the coroutine is currently the ready queue or not,
420 423
421=item $coroutine->cancel (arg...) 424=item $coroutine->cancel (arg...)
422 425
423Terminates the given coroutine and makes it return the given arguments as 426Terminates the given coroutine and makes it return the given arguments as
424status (default: the empty list). Never returns if the coroutine is the 427status (default: the empty list). Never returns if the coroutine is the
610creating event callbacks that want to block. 613creating event callbacks that want to block.
611 614
612If your handler does not plan to block (e.g. simply sends a message to 615If your handler does not plan to block (e.g. simply sends a message to
613another coroutine, or puts some other coroutine into the ready queue), 616another coroutine, or puts some other coroutine into the ready queue),
614there is no reason to use C<unblock_sub>. 617there is no reason to use C<unblock_sub>.
618
619Note that you also need to use C<unblock_sub> for any other callbacks that
620are indirectly executed by any C-based event loop. For example, when you
621use a module that uses L<AnyEvent> (and you use L<Coro::AnyEvent>) and it
622provides callbacks that are the result of some event callback, then you
623must not block either, or use C<unblock_sub>.
615 624
616=cut 625=cut
617 626
618our @unblock_queue; 627our @unblock_queue;
619 628

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines