… | |
… | |
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.4'; |
55 | our $VERSION = '3.56'; |
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 | ); |
… | |
… | |
185 | |
185 | |
186 | Create a new asynchronous coroutine and return it's coroutine object |
186 | Create a new asynchronous coroutine and return it's coroutine object |
187 | (usually unused). When the sub returns the new coroutine is automatically |
187 | (usually unused). When the sub returns the new coroutine is automatically |
188 | terminated. |
188 | terminated. |
189 | |
189 | |
190 | Calling C<exit> in a coroutine will not work correctly, so do not do that. |
190 | Calling C<exit> in a coroutine will try to do the same as calling exit |
|
|
191 | outside the coroutine, but this is experimental. It is best not to rely on |
|
|
192 | exit doing any cleanups or even not crashing. |
191 | |
193 | |
192 | When the coroutine dies, the program will exit, just as in the main |
194 | When the coroutine dies, the program will exit, just as in the main |
193 | program. |
195 | program. |
194 | |
196 | |
195 | # create a new coroutine that just prints its arguments |
197 | # create a new coroutine that just prints its arguments |
… | |
… | |
242 | warn $@ if $@; |
244 | warn $@ if $@; |
243 | |
245 | |
244 | last if @pool >= $POOL_SIZE; |
246 | last if @pool >= $POOL_SIZE; |
245 | push @pool, $current; |
247 | push @pool, $current; |
246 | |
248 | |
|
|
249 | $current->save (Coro::State::SAVE_DEF); |
247 | $current->prio (0); |
250 | $current->prio (0); |
248 | schedule; |
251 | schedule; |
249 | } |
252 | } |
250 | } |
253 | } |
251 | |
254 | |
… | |
… | |
325 | Create a new coroutine and return it. When the sub returns the coroutine |
328 | Create a new coroutine and return it. When the sub returns the coroutine |
326 | automatically terminates as if C<terminate> with the returned values were |
329 | automatically terminates as if C<terminate> with the returned values were |
327 | called. To make the coroutine run you must first put it into the ready queue |
330 | called. To make the coroutine run you must first put it into the ready queue |
328 | by calling the ready method. |
331 | by calling the ready method. |
329 | |
332 | |
330 | Calling C<exit> in a coroutine will not work correctly, so do not do that. |
333 | See C<async> for additional discussion. |
331 | |
334 | |
332 | =cut |
335 | =cut |
333 | |
336 | |
334 | sub _run_coro { |
337 | sub _run_coro { |
335 | terminate &{+shift}; |
338 | terminate &{+shift}; |
… | |
… | |
465 | and C<schedule> would cause a deadlock unless there is an idle handler |
468 | and C<schedule> would cause a deadlock unless there is an idle handler |
466 | that wakes up some coroutines. |
469 | that wakes up some coroutines. |
467 | |
470 | |
468 | =item my $guard = Coro::guard { ... } |
471 | =item my $guard = Coro::guard { ... } |
469 | |
472 | |
470 | This creates and returns a guard object. Nothing happens until the objetc |
473 | This creates and returns a guard object. Nothing happens until the object |
471 | gets destroyed, in which case the codeblock given as argument will be |
474 | gets destroyed, in which case the codeblock given as argument will be |
472 | executed. This is useful to free locks or other resources in case of a |
475 | executed. This is useful to free locks or other resources in case of a |
473 | runtime error or when the coroutine gets canceled, as in both cases the |
476 | runtime error or when the coroutine gets canceled, as in both cases the |
474 | guard block will be executed. The guard object supports only one method, |
477 | guard block will be executed. The guard object supports only one method, |
475 | C<< ->cancel >>, which will keep the codeblock from being executed. |
478 | C<< ->cancel >>, which will keep the codeblock from being executed. |