… | |
… | |
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 | |
67 | our $idle; # idle handler |
68 | our $idle; # idle handler |
68 | our $main; # main coroutine |
69 | our $main; # main coroutine |
69 | our $current; # current coroutine |
70 | our $current; # current coroutine |
70 | |
71 | |
71 | our $VERSION = 4.743; |
72 | our $VERSION = 4.8; |
72 | |
73 | |
73 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); |
74 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); |
74 | our %EXPORT_TAGS = ( |
75 | our %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 | |
83 | This variable stores the coroutine object that represents the main |
84 | This variable stores the coroutine object that represents the main |
84 | program. While you cna C<ready> it and do most other things you can do to |
85 | program. While you cna C<ready> it and do most other things you can do to |
85 | coroutines, it is mainly useful to compare again C<$Coro::current>, to see |
86 | coroutines, it is mainly useful to compare again C<$Coro::current>, to see |
86 | wether you are running in the main program or not. |
87 | whether 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 | |
… | |
… | |
309 | This makes C<schedule> I<the> generic method to use to block the current |
310 | This makes C<schedule> I<the> generic method to use to block the current |
310 | coroutine and wait for events: first you remember the current coroutine in |
311 | coroutine and wait for events: first you remember the current coroutine in |
311 | a variable, then arrange for some callback of yours to call C<< ->ready |
312 | a variable, then arrange for some callback of yours to call C<< ->ready |
312 | >> 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 |
313 | yourself to sleep. Note that a lot of things can wake your coroutine up, |
314 | yourself to sleep. Note that a lot of things can wake your coroutine up, |
314 | so you need to check wether the event indeed happened, e.g. by storing the |
315 | so you need to check whether the event indeed happened, e.g. by storing the |
315 | status in a variable. |
316 | status in a variable. |
316 | |
317 | |
317 | The canonical way to wait on external events is this: |
318 | The canonical way to wait on external events is this: |
318 | |
319 | |
319 | { |
320 | { |
… | |
… | |
358 | Kills/terminates/cancels all coroutines except the currently running |
359 | Kills/terminates/cancels all coroutines except the currently running |
359 | one. This is useful after a fork, either in the child or the parent, as |
360 | one. This is useful after a fork, either in the child or the parent, as |
360 | usually only one of them should inherit the running coroutines. |
361 | usually only one of them should inherit the running coroutines. |
361 | |
362 | |
362 | Note that while this will try to free some of the main programs resources, |
363 | Note that while this will try to free some of the main programs resources, |
363 | you cnanot free all of them, so if a coroutine that is not the main |
364 | you cannot free all of them, so if a coroutine that is not the main |
364 | program calls this function, there will be some one-time resource leak. |
365 | program calls this function, there will be some one-time resource leak. |
365 | |
366 | |
366 | =cut |
367 | =cut |
367 | |
368 | |
368 | sub terminate { |
369 | sub terminate { |
… | |
… | |
417 | once all the coroutines of higher priority and all coroutines of the same |
418 | once all the coroutines of higher priority and all coroutines of the same |
418 | priority that were put into the ready queue earlier have been resumed. |
419 | priority that were put into the ready queue earlier have been resumed. |
419 | |
420 | |
420 | =item $is_ready = $coroutine->is_ready |
421 | =item $is_ready = $coroutine->is_ready |
421 | |
422 | |
422 | Return wether the coroutine is currently the ready queue or not, |
423 | Return whether the coroutine is currently the ready queue or not, |
423 | |
424 | |
424 | =item $coroutine->cancel (arg...) |
425 | =item $coroutine->cancel (arg...) |
425 | |
426 | |
426 | Terminates the given coroutine and makes it return the given arguments as |
427 | Terminates the given coroutine and makes it return the given arguments as |
427 | status (default: the empty list). Never returns if the coroutine is the |
428 | status (default: the empty list). Never returns if the coroutine is the |