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

Comparing Coro/Coro.pm (file contents):
Revision 1.182 by root, Fri May 9 22:29:05 2008 UTC vs.
Revision 1.202 by root, Tue Sep 30 17:12:34 2008 UTC

16 cede; # yield to coroutine 16 cede; # yield to coroutine
17 print "3\n"; 17 print "3\n";
18 cede; # and again 18 cede; # and again
19 19
20 # use locking 20 # use locking
21 use Coro::Semaphore;
21 my $lock = new Coro::Semaphore; 22 my $lock = new Coro::Semaphore;
22 my $locked; 23 my $locked;
23 24
24 $lock->down; 25 $lock->down;
25 $locked = 1; 26 $locked = 1;
66 67
67our $idle; # idle handler 68our $idle; # idle handler
68our $main; # main coroutine 69our $main; # main coroutine
69our $current; # current coroutine 70our $current; # current coroutine
70 71
71our $VERSION = 4.6; 72our $VERSION = 4.8;
72 73
73our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); 74our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub);
74our %EXPORT_TAGS = ( 75our %EXPORT_TAGS = (
75 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 76 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
76); 77);
81=item $Coro::main 82=item $Coro::main
82 83
83This variable stores the coroutine object that represents the main 84This variable stores the coroutine object that represents the main
84program. While you cna C<ready> it and do most other things you can do to 85program. While you cna C<ready> it and do most other things you can do to
85coroutines, it is mainly useful to compare again C<$Coro::current>, to see 86coroutines, it is mainly useful to compare again C<$Coro::current>, to see
86wether you are running in the main program or not. 87whether you are running in the main program or not.
87 88
88=cut 89=cut
89 90
90$main = new Coro; 91$main = new Coro;
91 92
266 _pool_2 $cb; 267 _pool_2 $cb;
267 &schedule; 268 &schedule;
268 } 269 }
269 }; 270 };
270 271
272 if ($@) {
271 last if $@ eq "\3async_pool terminate\2\n"; 273 last if $@ eq "\3async_pool terminate\2\n";
272 warn $@ if $@; 274 warn $@;
275 }
273 } 276 }
274} 277}
275 278
276sub async_pool(&@) { 279sub async_pool(&@) {
277 # this is also inlined into the unlock_scheduler 280 # this is also inlined into the unlock_scheduler
307This makes C<schedule> I<the> generic method to use to block the current 310This makes C<schedule> I<the> generic method to use to block the current
308coroutine and wait for events: first you remember the current coroutine in 311coroutine and wait for events: first you remember the current coroutine in
309a variable, then arrange for some callback of yours to call C<< ->ready 312a variable, then arrange for some callback of yours to call C<< ->ready
310>> on that once some event happens, and last you call C<schedule> to put 313>> on that once some event happens, and last you call C<schedule> to put
311yourself to sleep. Note that a lot of things can wake your coroutine up, 314yourself to sleep. Note that a lot of things can wake your coroutine up,
312so you need to check wether the event indeed happened, e.g. by storing the 315so you need to check whether the event indeed happened, e.g. by storing the
313status in a variable. 316status in a variable.
314 317
315The canonical way to wait on external events is this: 318The canonical way to wait on external events is this:
316 319
317 { 320 {
356Kills/terminates/cancels all coroutines except the currently running 359Kills/terminates/cancels all coroutines except the currently running
357one. This is useful after a fork, either in the child or the parent, as 360one. This is useful after a fork, either in the child or the parent, as
358usually only one of them should inherit the running coroutines. 361usually only one of them should inherit the running coroutines.
359 362
360Note that while this will try to free some of the main programs resources, 363Note that while this will try to free some of the main programs resources,
361you cnanot free all of them, so if a coroutine that is not the main 364you cannot free all of them, so if a coroutine that is not the main
362program calls this function, there will be some one-time resource leak. 365program calls this function, there will be some one-time resource leak.
363 366
364=cut 367=cut
365 368
366sub terminate { 369sub terminate {
415once all the coroutines of higher priority and all coroutines of the same 418once all the coroutines of higher priority and all coroutines of the same
416priority that were put into the ready queue earlier have been resumed. 419priority that were put into the ready queue earlier have been resumed.
417 420
418=item $is_ready = $coroutine->is_ready 421=item $is_ready = $coroutine->is_ready
419 422
420Return wether the coroutine is currently the ready queue or not, 423Return whether the coroutine is currently the ready queue or not,
421 424
422=item $coroutine->cancel (arg...) 425=item $coroutine->cancel (arg...)
423 426
424Terminates the given coroutine and makes it return the given arguments as 427Terminates the given coroutine and makes it return the given arguments as
425status (default: the empty list). Never returns if the coroutine is the 428status (default: the empty list). Never returns if the coroutine is the
611creating event callbacks that want to block. 614creating event callbacks that want to block.
612 615
613If your handler does not plan to block (e.g. simply sends a message to 616If your handler does not plan to block (e.g. simply sends a message to
614another coroutine, or puts some other coroutine into the ready queue), 617another coroutine, or puts some other coroutine into the ready queue),
615there is no reason to use C<unblock_sub>. 618there is no reason to use C<unblock_sub>.
619
620Note that you also need to use C<unblock_sub> for any other callbacks that
621are indirectly executed by any C-based event loop. For example, when you
622use a module that uses L<AnyEvent> (and you use L<Coro::AnyEvent>) and it
623provides callbacks that are the result of some event callback, then you
624must not block either, or use C<unblock_sub>.
616 625
617=cut 626=cut
618 627
619our @unblock_queue; 628our @unblock_queue;
620 629

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines