… | |
… | |
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.72'; |
72 | our $VERSION = 4.746; |
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 | |
… | |
… | |
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 | |
276 | sub async_pool(&@) { |
279 | sub async_pool(&@) { |
277 | # this is also inlined into the unlock_scheduler |
280 | # this is also inlined into the unlock_scheduler |
… | |
… | |
307 | 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 |
308 | 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 |
309 | 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 |
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 |
311 | 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, |
312 | 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 |
313 | status in a variable. |
316 | status in a variable. |
314 | |
317 | |
315 | The canonical way to wait on external events is this: |
318 | The canonical way to wait on external events is this: |
316 | |
319 | |
317 | { |
320 | { |
… | |
… | |
356 | Kills/terminates/cancels all coroutines except the currently running |
359 | Kills/terminates/cancels all coroutines except the currently running |
357 | 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 |
358 | usually only one of them should inherit the running coroutines. |
361 | usually only one of them should inherit the running coroutines. |
359 | |
362 | |
360 | 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, |
361 | 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 |
362 | 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. |
363 | |
366 | |
364 | =cut |
367 | =cut |
365 | |
368 | |
366 | sub terminate { |
369 | sub terminate { |
… | |
… | |
415 | 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 |
416 | 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. |
417 | |
420 | |
418 | =item $is_ready = $coroutine->is_ready |
421 | =item $is_ready = $coroutine->is_ready |
419 | |
422 | |
420 | Return wether the coroutine is currently the ready queue or not, |
423 | Return whether the coroutine is currently the ready queue or not, |
421 | |
424 | |
422 | =item $coroutine->cancel (arg...) |
425 | =item $coroutine->cancel (arg...) |
423 | |
426 | |
424 | 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 |
425 | 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 |