… | |
… | |
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 |
… | |
… | |
305 | >> on that once some event happens, and last you call C<schedule> to put |
273 | >> on that once some event happens, and last you call C<schedule> to put |
306 | yourself to sleep. Note that a lot of things can wake your coroutine up, |
274 | yourself to sleep. Note that a lot of things can wake your coroutine up, |
307 | so you need to check whether the event indeed happened, e.g. by storing the |
275 | so you need to check whether the event indeed happened, e.g. by storing the |
308 | status in a variable. |
276 | status in a variable. |
309 | |
277 | |
310 | The canonical way to wait on external events is this: |
278 | See B<HOW TO WAIT FOR A CALLBACK>, below, for some ways to wait for callbacks. |
311 | |
|
|
312 | { |
|
|
313 | # remember current coroutine |
|
|
314 | my $current = $Coro::current; |
|
|
315 | |
|
|
316 | # register a hypothetical event handler |
|
|
317 | on_event_invoke sub { |
|
|
318 | # wake up sleeping coroutine |
|
|
319 | $current->ready; |
|
|
320 | undef $current; |
|
|
321 | }; |
|
|
322 | |
|
|
323 | # call schedule until event occurred. |
|
|
324 | # in case we are woken up for other reasons |
|
|
325 | # (current still defined), loop. |
|
|
326 | Coro::schedule while $current; |
|
|
327 | } |
|
|
328 | |
279 | |
329 | =item cede |
280 | =item cede |
330 | |
281 | |
331 | "Cede" to other coroutines. This function puts the current coroutine into |
282 | "Cede" to other coroutines. This function puts the current coroutine into |
332 | the ready queue and calls C<schedule>, which has the effect of giving |
283 | the ready queue and calls C<schedule>, which has the effect of giving |
… | |
… | |
356 | 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 |
357 | 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. |
358 | |
309 | |
359 | =cut |
310 | =cut |
360 | |
311 | |
361 | sub terminate { |
|
|
362 | $current->cancel (@_); |
|
|
363 | } |
|
|
364 | |
|
|
365 | sub killall { |
312 | sub killall { |
366 | for (Coro::State::list) { |
313 | for (Coro::State::list) { |
367 | $_->cancel |
314 | $_->cancel |
368 | if $_ != $current && UNIVERSAL::isa $_, "Coro"; |
315 | if $_ != $current && UNIVERSAL::isa $_, "Coro"; |
369 | } |
316 | } |
… | |
… | |
388 | 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 |
389 | coroutine environment. |
336 | coroutine environment. |
390 | |
337 | |
391 | =cut |
338 | =cut |
392 | |
339 | |
393 | sub _run_coro { |
340 | sub _terminate { |
394 | terminate &{+shift}; |
341 | terminate &{+shift}; |
395 | } |
|
|
396 | |
|
|
397 | sub new { |
|
|
398 | my $class = shift; |
|
|
399 | |
|
|
400 | $class->SUPER::new (\&_run_coro, @_) |
|
|
401 | } |
342 | } |
402 | |
343 | |
403 | =item $success = $coroutine->ready |
344 | =item $success = $coroutine->ready |
404 | |
345 | |
405 | 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 |
… | |
… | |
422 | |
363 | |
423 | =cut |
364 | =cut |
424 | |
365 | |
425 | sub cancel { |
366 | sub cancel { |
426 | my $self = shift; |
367 | my $self = shift; |
427 | $self->{_status} = [@_]; |
|
|
428 | |
368 | |
429 | if ($current == $self) { |
369 | if ($current == $self) { |
430 | push @destroy, $self; |
370 | terminate @_; |
431 | $manager->ready; |
|
|
432 | &schedule while 1; |
|
|
433 | } else { |
371 | } else { |
|
|
372 | $self->{_status} = [@_]; |
434 | $self->_cancel; |
373 | $self->_cancel; |
435 | } |
374 | } |
436 | } |
375 | } |
437 | |
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. |
|
|
395 | |
438 | =item $coroutine->throw ([$scalar]) |
396 | =item $coroutine->throw ([$scalar]) |
439 | |
397 | |
440 | 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 |
441 | inside the coroutine at the next convenient point in time (usually after |
399 | inside the coroutine at the next convenient point in time. Otherwise |
442 | it gains control at the next schedule/transfer/cede). Otherwise clears the |
|
|
443 | exception object. |
400 | clears the exception object. |
|
|
401 | |
|
|
402 | Coro will check for the exception each time a schedule-like-function |
|
|
403 | returns, i.e. after each C<schedule>, C<cede>, C<< Coro::Semaphore->down |
|
|
404 | >>, C<< Coro::Handle->readable >> and so on. Most of these functions |
|
|
405 | detect this case and return early in case an exception is pending. |
444 | |
406 | |
445 | The exception object will be thrown "as is" with the specified scalar in |
407 | The exception object will be thrown "as is" with the specified scalar in |
446 | C<$@>, i.e. if it is a string, no line number or newline will be appended |
408 | C<$@>, i.e. if it is a string, no line number or newline will be appended |
447 | (unlike with C<die>). |
409 | (unlike with C<die>). |
448 | |
410 | |
… | |
… | |
628 | # return immediately and can be reused) and because we cannot cede |
590 | # return immediately and can be reused) and because we cannot cede |
629 | # inside an event callback. |
591 | # inside an event callback. |
630 | our $unblock_scheduler = new Coro sub { |
592 | our $unblock_scheduler = new Coro sub { |
631 | while () { |
593 | while () { |
632 | while (my $cb = pop @unblock_queue) { |
594 | while (my $cb = pop @unblock_queue) { |
633 | # this is an inlined copy of async_pool |
595 | &async_pool (@$cb); |
634 | my $coro = (pop @async_pool) || new Coro \&pool_handler; |
|
|
635 | |
596 | |
636 | $coro->{_invoke} = $cb; |
|
|
637 | $coro->ready; |
|
|
638 | 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; |
639 | } |
601 | } |
640 | schedule; # sleep well |
602 | schedule; # sleep well |
641 | } |
603 | } |
642 | }; |
604 | }; |
643 | $unblock_scheduler->{desc} = "[unblock_sub scheduler]"; |
605 | $unblock_scheduler->{desc} = "[unblock_sub scheduler]"; |
… | |
… | |
649 | unshift @unblock_queue, [$cb, @_]; |
611 | unshift @unblock_queue, [$cb, @_]; |
650 | $unblock_scheduler->ready; |
612 | $unblock_scheduler->ready; |
651 | } |
613 | } |
652 | } |
614 | } |
653 | |
615 | |
|
|
616 | =item $cb = Coro::rouse_cb |
|
|
617 | |
|
|
618 | Create and return a "rouse callback". That's a code reference that, when |
|
|
619 | called, will save its arguments and notify the owner coroutine of the |
|
|
620 | callback. |
|
|
621 | |
|
|
622 | See the next function. |
|
|
623 | |
|
|
624 | =item @args = Coro::rouse_wait [$cb] |
|
|
625 | |
|
|
626 | Wait for the specified rouse callback (or the last one tht was created in |
|
|
627 | this coroutine). |
|
|
628 | |
|
|
629 | As soon as the callback is invoked (or when the calback was invoked before |
|
|
630 | C<rouse_wait>), it will return a copy of the arguments originally passed |
|
|
631 | to the rouse callback. |
|
|
632 | |
|
|
633 | See the section B<HOW TO WAIT FOR A CALLBACK> for an actual usage example. |
|
|
634 | |
654 | =back |
635 | =back |
655 | |
636 | |
656 | =cut |
637 | =cut |
657 | |
638 | |
658 | 1; |
639 | 1; |
|
|
640 | |
|
|
641 | =head1 HOW TO WAIT FOR A CALLBACK |
|
|
642 | |
|
|
643 | It is very common for a coroutine to wait for some callback to be |
|
|
644 | called. This occurs naturally when you use coroutines in an otherwise |
|
|
645 | event-based program, or when you use event-based libraries. |
|
|
646 | |
|
|
647 | These typically register a callback for some event, and call that callback |
|
|
648 | when the event occured. In a coroutine, however, you typically want to |
|
|
649 | just wait for the event, simplyifying things. |
|
|
650 | |
|
|
651 | For example C<< AnyEvent->child >> registers a callback to be called when |
|
|
652 | a specific child has exited: |
|
|
653 | |
|
|
654 | my $child_watcher = AnyEvent->child (pid => $pid, cb => sub { ... }); |
|
|
655 | |
|
|
656 | But from withina coroutine, you often just want to write this: |
|
|
657 | |
|
|
658 | my $status = wait_for_child $pid; |
|
|
659 | |
|
|
660 | Coro offers two functions specifically designed to make this easy, |
|
|
661 | C<Coro::rouse_cb> and C<Coro::rouse_wait>. |
|
|
662 | |
|
|
663 | The first function, C<rouse_cb>, generates and returns a callback that, |
|
|
664 | when invoked, will save it's arguments and notify the coroutine that |
|
|
665 | created the callback. |
|
|
666 | |
|
|
667 | The second function, C<rouse_wait>, waits for the callback to be called |
|
|
668 | (by calling C<schedule> to go to sleep) and returns the arguments |
|
|
669 | originally passed to the callback. |
|
|
670 | |
|
|
671 | Using these functions, it becomes easy to write the C<wait_for_child> |
|
|
672 | function mentioned above: |
|
|
673 | |
|
|
674 | sub wait_for_child($) { |
|
|
675 | my ($pid) = @_; |
|
|
676 | |
|
|
677 | my $watcher = AnyEvent->child (pid => $pid, cb => Coro::rouse_cb); |
|
|
678 | |
|
|
679 | my ($rpid, $rstatus) = Coro::rouse_wait; |
|
|
680 | $rstatus |
|
|
681 | } |
|
|
682 | |
|
|
683 | In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough, |
|
|
684 | you can roll your own, using C<schedule>: |
|
|
685 | |
|
|
686 | sub wait_for_child($) { |
|
|
687 | my ($pid) = @_; |
|
|
688 | |
|
|
689 | # store the current coroutine in $current, |
|
|
690 | # and provide result variables for the closure passed to ->child |
|
|
691 | my $current = $Coro::current; |
|
|
692 | my ($done, $rstatus); |
|
|
693 | |
|
|
694 | # pass a closure to ->child |
|
|
695 | my $watcher = AnyEvent->child (pid => $pid, cb => sub { |
|
|
696 | $rstatus = $_[1]; # remember rstatus |
|
|
697 | $done = 1; # mark $rstatus as valud |
|
|
698 | }); |
|
|
699 | |
|
|
700 | # wait until the closure has been called |
|
|
701 | schedule while !$done; |
|
|
702 | |
|
|
703 | $rstatus |
|
|
704 | } |
|
|
705 | |
659 | |
706 | |
660 | =head1 BUGS/LIMITATIONS |
707 | =head1 BUGS/LIMITATIONS |
661 | |
708 | |
662 | =over 4 |
709 | =over 4 |
663 | |
710 | |
… | |
… | |
680 | =item coroutine switching not signal safe |
727 | =item coroutine switching not signal safe |
681 | |
728 | |
682 | You must not switch to another coroutine from within a signal handler |
729 | You must not switch to another coroutine from within a signal handler |
683 | (only relevant with %SIG - most event libraries provide safe signals). |
730 | (only relevant with %SIG - most event libraries provide safe signals). |
684 | |
731 | |
685 | That means you I<MUST NOT> call any fucntion that might "block" the |
732 | That means you I<MUST NOT> call any function that might "block" the |
686 | current coroutine - C<cede>, C<schedule> C<< Coro::Semaphore->down >> or |
733 | current coroutine - C<cede>, C<schedule> C<< Coro::Semaphore->down >> or |
687 | anything that calls those. Everything else, including calling C<ready>, |
734 | anything that calls those. Everything else, including calling C<ready>, |
688 | works. |
735 | works. |
689 | |
736 | |
690 | =back |
737 | =back |