… | |
… | |
50 | |
50 | |
51 | our $idle; # idle handler |
51 | our $idle; # idle handler |
52 | our $main; # main coroutine |
52 | our $main; # main coroutine |
53 | our $current; # current coroutine |
53 | our $current; # current coroutine |
54 | |
54 | |
55 | our $VERSION = '3.7'; |
55 | our $VERSION = '3.8'; |
56 | |
56 | |
57 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); |
57 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); |
58 | our %EXPORT_TAGS = ( |
58 | our %EXPORT_TAGS = ( |
59 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
59 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
60 | ); |
60 | ); |
… | |
… | |
241 | my $cb; |
241 | my $cb; |
242 | |
242 | |
243 | while () { |
243 | while () { |
244 | eval { |
244 | eval { |
245 | while () { |
245 | while () { |
246 | $cb = &_pool_1 |
246 | _pool_1 $cb; |
247 | or return; |
|
|
248 | |
|
|
249 | &$cb; |
247 | &$cb; |
250 | |
248 | _pool_2 $cb; |
251 | return if &_pool_2; |
|
|
252 | |
|
|
253 | undef $cb; |
|
|
254 | schedule; |
249 | &schedule; |
255 | } |
250 | } |
256 | }; |
251 | }; |
257 | |
252 | |
|
|
253 | last if $@ eq "\3terminate\2\n"; |
258 | warn $@ if $@; |
254 | warn $@ if $@; |
259 | } |
255 | } |
260 | } |
256 | } |
261 | |
257 | |
262 | sub async_pool(&@) { |
258 | sub async_pool(&@) { |
263 | # this is also inlined into the unlock_scheduler |
259 | # this is also inlined into the unlock_scheduler |
264 | my $coro = (pop @async_pool) || new Coro \&pool_handler;; |
260 | my $coro = (pop @async_pool) || new Coro \&pool_handler; |
265 | |
261 | |
266 | $coro->{_invoke} = [@_]; |
262 | $coro->{_invoke} = [@_]; |
267 | $coro->ready; |
263 | $coro->ready; |
268 | |
264 | |
269 | $coro |
265 | $coro |
… | |
… | |
312 | |
308 | |
313 | =item terminate [arg...] |
309 | =item terminate [arg...] |
314 | |
310 | |
315 | Terminates the current coroutine with the given status values (see L<cancel>). |
311 | Terminates the current coroutine with the given status values (see L<cancel>). |
316 | |
312 | |
|
|
313 | =item killall |
|
|
314 | |
|
|
315 | Kills/terminates/cancels all coroutines except the currently running |
|
|
316 | one. This is useful after a fork, either in the child or the parent, as |
|
|
317 | usually only one of them should inherit the running coroutines. |
|
|
318 | |
317 | =cut |
319 | =cut |
318 | |
320 | |
319 | sub terminate { |
321 | sub terminate { |
320 | $current->cancel (@_); |
322 | $current->cancel (@_); |
|
|
323 | } |
|
|
324 | |
|
|
325 | sub killall { |
|
|
326 | for (Coro::State::list) { |
|
|
327 | $_->cancel |
|
|
328 | if $_ != $current && UNIVERSAL::isa $_, "Coro"; |
|
|
329 | } |
321 | } |
330 | } |
322 | |
331 | |
323 | =back |
332 | =back |
324 | |
333 | |
325 | # dynamic methods |
334 | # dynamic methods |