… | |
… | |
67 | |
67 | |
68 | our $idle; # idle handler |
68 | our $idle; # idle handler |
69 | our $main; # main coroutine |
69 | our $main; # main coroutine |
70 | our $current; # current coroutine |
70 | our $current; # current coroutine |
71 | |
71 | |
72 | our $VERSION = 5.0; |
72 | our $VERSION = "5.0"; |
73 | |
73 | |
74 | 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); |
75 | our %EXPORT_TAGS = ( |
75 | our %EXPORT_TAGS = ( |
76 | 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)], |
77 | ); |
77 | ); |
… | |
… | |
135 | $idle = sub { |
135 | $idle = sub { |
136 | require Carp; |
136 | require Carp; |
137 | Carp::croak ("FATAL: deadlock detected"); |
137 | Carp::croak ("FATAL: deadlock detected"); |
138 | }; |
138 | }; |
139 | |
139 | |
140 | sub _cancel { |
|
|
141 | my ($self) = @_; |
|
|
142 | |
|
|
143 | # free coroutine data and mark as destructed |
|
|
144 | $self->_destroy |
|
|
145 | or return; |
|
|
146 | |
|
|
147 | # call all destruction callbacks |
|
|
148 | $_->(@{$self->{_status}}) |
|
|
149 | for @{ delete $self->{_on_destroy} || [] }; |
|
|
150 | } |
|
|
151 | |
|
|
152 | # this coroutine is necessary because a coroutine |
140 | # this coroutine is necessary because a coroutine |
153 | # cannot destroy itself. |
141 | # cannot destroy itself. |
154 | my @destroy; |
142 | our @destroy; |
155 | my $manager; |
143 | our $manager; |
156 | |
144 | |
157 | $manager = new Coro sub { |
145 | $manager = new Coro sub { |
158 | while () { |
146 | while () { |
159 | (shift @destroy)->_cancel |
147 | Coro::_cancel shift @destroy |
160 | while @destroy; |
148 | while @destroy; |
161 | |
149 | |
162 | &schedule; |
150 | &schedule; |
163 | } |
151 | } |
164 | }; |
152 | }; |
… | |
… | |
212 | Similar to C<async>, but uses a coroutine pool, so you should not call |
200 | Similar to C<async>, but uses a coroutine pool, so you should not call |
213 | terminate or join on it (although you are allowed to), and you get a |
201 | terminate or join on it (although you are allowed to), and you get a |
214 | coroutine that might have executed other code already (which can be good |
202 | coroutine that might have executed other code already (which can be good |
215 | or bad :). |
203 | or bad :). |
216 | |
204 | |
217 | On the plus side, this function is faster than creating (and destroying) |
205 | On the plus side, this function is about twice as fast as creating (and |
218 | a completly new coroutine, so if you need a lot of generic coroutines in |
206 | destroying) a completely new coroutine, so if you need a lot of generic |
219 | quick successsion, use C<async_pool>, not C<async>. |
207 | coroutines in quick successsion, use C<async_pool>, not C<async>. |
220 | |
208 | |
221 | The code block is executed in an C<eval> context and a warning will be |
209 | The code block is executed in an C<eval> context and a warning will be |
222 | issued in case of an exception instead of terminating the program, as |
210 | issued in case of an exception instead of terminating the program, as |
223 | C<async> does. As the coroutine is being reused, stuff like C<on_destroy> |
211 | C<async> does. As the coroutine is being reused, stuff like C<on_destroy> |
224 | will not work in the expected way, unless you call terminate or cancel, |
212 | will not work in the expected way, unless you call terminate or cancel, |
… | |
… | |
247 | our $POOL_SIZE = 8; |
235 | our $POOL_SIZE = 8; |
248 | our $POOL_RSS = 16 * 1024; |
236 | our $POOL_RSS = 16 * 1024; |
249 | our @async_pool; |
237 | our @async_pool; |
250 | |
238 | |
251 | sub pool_handler { |
239 | sub pool_handler { |
252 | my $cb; |
|
|
253 | |
|
|
254 | while () { |
240 | while () { |
255 | eval { |
241 | eval { |
256 | while () { |
242 | &{&_pool_handler} while 1; |
257 | _pool_1 $cb; |
|
|
258 | &$cb; |
|
|
259 | _pool_2 $cb; |
|
|
260 | &schedule; |
|
|
261 | } |
|
|
262 | }; |
243 | }; |
263 | |
244 | |
264 | if ($@) { |
|
|
265 | last if $@ eq "\3async_pool terminate\2\n"; |
|
|
266 | warn $@; |
245 | warn $@ if $@; |
267 | } |
|
|
268 | } |
246 | } |
269 | } |
|
|
270 | |
|
|
271 | sub async_pool(&@) { |
|
|
272 | # this is also inlined into the unblock_scheduler |
|
|
273 | my $coro = (pop @async_pool) || new Coro \&pool_handler; |
|
|
274 | |
|
|
275 | $coro->{_invoke} = [@_]; |
|
|
276 | $coro->ready; |
|
|
277 | |
|
|
278 | $coro |
|
|
279 | } |
247 | } |
280 | |
248 | |
281 | =back |
249 | =back |
282 | |
250 | |
283 | =head2 STATIC METHODS |
251 | =head2 STATIC METHODS |
… | |
… | |
339 | you cannot free all of them, so if a coroutine that is not the main |
307 | you cannot free all of them, so if a coroutine that is not the main |
340 | program calls this function, there will be some one-time resource leak. |
308 | program calls this function, there will be some one-time resource leak. |
341 | |
309 | |
342 | =cut |
310 | =cut |
343 | |
311 | |
344 | sub terminate { |
|
|
345 | $current->cancel (@_); |
|
|
346 | } |
|
|
347 | |
|
|
348 | sub killall { |
312 | sub killall { |
349 | for (Coro::State::list) { |
313 | for (Coro::State::list) { |
350 | $_->cancel |
314 | $_->cancel |
351 | if $_ != $current && UNIVERSAL::isa $_, "Coro"; |
315 | if $_ != $current && UNIVERSAL::isa $_, "Coro"; |
352 | } |
316 | } |
… | |
… | |
371 | See C<async> and C<Coro::State::new> for additional info about the |
335 | See C<async> and C<Coro::State::new> for additional info about the |
372 | coroutine environment. |
336 | coroutine environment. |
373 | |
337 | |
374 | =cut |
338 | =cut |
375 | |
339 | |
376 | sub _run_coro { |
340 | sub _terminate { |
377 | terminate &{+shift}; |
341 | terminate &{+shift}; |
378 | } |
|
|
379 | |
|
|
380 | sub new { |
|
|
381 | my $class = shift; |
|
|
382 | |
|
|
383 | $class->SUPER::new (\&_run_coro, @_) |
|
|
384 | } |
342 | } |
385 | |
343 | |
386 | =item $success = $coroutine->ready |
344 | =item $success = $coroutine->ready |
387 | |
345 | |
388 | Put the given coroutine into the end of its ready queue (there is one |
346 | Put the given coroutine into the end of its ready queue (there is one |
… | |
… | |
405 | |
363 | |
406 | =cut |
364 | =cut |
407 | |
365 | |
408 | sub cancel { |
366 | sub cancel { |
409 | my $self = shift; |
367 | my $self = shift; |
410 | $self->{_status} = [@_]; |
|
|
411 | |
368 | |
412 | if ($current == $self) { |
369 | if ($current == $self) { |
413 | push @destroy, $self; |
370 | terminate @_; |
414 | $manager->ready; |
|
|
415 | &schedule while 1; |
|
|
416 | } else { |
371 | } else { |
|
|
372 | $self->{_status} = [@_]; |
417 | $self->_cancel; |
373 | $self->_cancel; |
418 | } |
374 | } |
419 | } |
375 | } |
|
|
376 | |
|
|
377 | =item $coroutine->schedule_to |
|
|
378 | |
|
|
379 | Puts the current coroutine to sleep (like C<Coro::schedule>), but instead |
|
|
380 | of continuing with the next coro from the ready queue, always switch to |
|
|
381 | the given coroutine object (regardless of priority etc.). The readyness |
|
|
382 | state of that coroutine isn't changed. |
|
|
383 | |
|
|
384 | This is an advanced method for special cases - I'd love to hear about any |
|
|
385 | uses for this one. |
|
|
386 | |
|
|
387 | =item $coroutine->cede_to |
|
|
388 | |
|
|
389 | Like C<schedule_to>, but puts the current coroutine into the ready |
|
|
390 | queue. This has the effect of temporarily switching to the given |
|
|
391 | coroutine, and continuing some time later. |
|
|
392 | |
|
|
393 | This is an advanced method for special cases - I'd love to hear about any |
|
|
394 | uses for this one. |
420 | |
395 | |
421 | =item $coroutine->throw ([$scalar]) |
396 | =item $coroutine->throw ([$scalar]) |
422 | |
397 | |
423 | If C<$throw> is specified and defined, it will be thrown as an exception |
398 | If C<$throw> is specified and defined, it will be thrown as an exception |
424 | inside the coroutine at the next convenient point in time. Otherwise |
399 | inside the coroutine at the next convenient point in time. Otherwise |
… | |
… | |
615 | # return immediately and can be reused) and because we cannot cede |
590 | # return immediately and can be reused) and because we cannot cede |
616 | # inside an event callback. |
591 | # inside an event callback. |
617 | our $unblock_scheduler = new Coro sub { |
592 | our $unblock_scheduler = new Coro sub { |
618 | while () { |
593 | while () { |
619 | while (my $cb = pop @unblock_queue) { |
594 | while (my $cb = pop @unblock_queue) { |
620 | # this is an inlined copy of async_pool |
595 | &async_pool (@$cb); |
621 | my $coro = (pop @async_pool) || new Coro \&pool_handler; |
|
|
622 | |
596 | |
623 | $coro->{_invoke} = $cb; |
|
|
624 | $coro->ready; |
|
|
625 | cede; # for short-lived callbacks, this reduces pressure on the coro pool |
597 | # for short-lived callbacks, this reduces pressure on the coro pool |
|
|
598 | # as the chance is very high that the async_poll coro will be back |
|
|
599 | # in the idle state when cede returns |
|
|
600 | cede; |
626 | } |
601 | } |
627 | schedule; # sleep well |
602 | schedule; # sleep well |
628 | } |
603 | } |
629 | }; |
604 | }; |
630 | $unblock_scheduler->{desc} = "[unblock_sub scheduler]"; |
605 | $unblock_scheduler->{desc} = "[unblock_sub scheduler]"; |