… | |
… | |
66 | |
66 | |
67 | our $idle; # idle handler |
67 | our $idle; # idle handler |
68 | our $main; # main coroutine |
68 | our $main; # main coroutine |
69 | our $current; # current coroutine |
69 | our $current; # current coroutine |
70 | |
70 | |
71 | our $VERSION = 4.742; |
71 | our $VERSION = 4.745; |
72 | |
72 | |
73 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); |
73 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); |
74 | our %EXPORT_TAGS = ( |
74 | our %EXPORT_TAGS = ( |
75 | 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)], |
76 | ); |
76 | ); |
… | |
… | |
81 | =item $Coro::main |
81 | =item $Coro::main |
82 | |
82 | |
83 | This variable stores the coroutine object that represents the main |
83 | 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 |
84 | 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 |
85 | coroutines, it is mainly useful to compare again C<$Coro::current>, to see |
86 | wether you are running in the main program or not. |
86 | whether you are running in the main program or not. |
87 | |
87 | |
88 | =cut |
88 | =cut |
89 | |
89 | |
90 | $main = new Coro; |
90 | $main = new Coro; |
91 | |
91 | |
… | |
… | |
266 | _pool_2 $cb; |
266 | _pool_2 $cb; |
267 | &schedule; |
267 | &schedule; |
268 | } |
268 | } |
269 | }; |
269 | }; |
270 | |
270 | |
|
|
271 | if ($@) { |
271 | last if $@ eq "\3async_pool terminate\2\n"; |
272 | last if $@ eq "\3async_pool terminate\2\n"; |
272 | warn $@ if $@; |
273 | warn $@; |
|
|
274 | } |
273 | } |
275 | } |
274 | } |
276 | } |
275 | |
277 | |
276 | sub async_pool(&@) { |
278 | sub async_pool(&@) { |
277 | # this is also inlined into the unlock_scheduler |
279 | # this is also inlined into the unlock_scheduler |
… | |
… | |
307 | This makes C<schedule> I<the> generic method to use to block the current |
309 | 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 |
310 | 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 |
311 | 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 |
312 | >> 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, |
313 | 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 |
314 | so you need to check whether the event indeed happened, e.g. by storing the |
313 | status in a variable. |
315 | status in a variable. |
314 | |
316 | |
315 | The canonical way to wait on external events is this: |
317 | The canonical way to wait on external events is this: |
316 | |
318 | |
317 | { |
319 | { |
… | |
… | |
356 | Kills/terminates/cancels all coroutines except the currently running |
358 | 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 |
359 | 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. |
360 | usually only one of them should inherit the running coroutines. |
359 | |
361 | |
360 | Note that while this will try to free some of the main programs resources, |
362 | 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 |
363 | 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. |
364 | program calls this function, there will be some one-time resource leak. |
363 | |
365 | |
364 | =cut |
366 | =cut |
365 | |
367 | |
366 | sub terminate { |
368 | sub terminate { |
… | |
… | |
415 | once all the coroutines of higher priority and all coroutines of the same |
417 | 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. |
418 | priority that were put into the ready queue earlier have been resumed. |
417 | |
419 | |
418 | =item $is_ready = $coroutine->is_ready |
420 | =item $is_ready = $coroutine->is_ready |
419 | |
421 | |
420 | Return wether the coroutine is currently the ready queue or not, |
422 | Return whether the coroutine is currently the ready queue or not, |
421 | |
423 | |
422 | =item $coroutine->cancel (arg...) |
424 | =item $coroutine->cancel (arg...) |
423 | |
425 | |
424 | Terminates the given coroutine and makes it return the given arguments as |
426 | 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 |
427 | status (default: the empty list). Never returns if the coroutine is the |