… | |
… | |
399 | our @destroy; |
399 | our @destroy; |
400 | our $manager; |
400 | our $manager; |
401 | |
401 | |
402 | $manager = new Coro sub { |
402 | $manager = new Coro sub { |
403 | while () { |
403 | while () { |
404 | Coro::State::cancel shift @destroy |
404 | _destroy shift @destroy |
405 | while @destroy; |
405 | while @destroy; |
406 | |
406 | |
407 | &schedule; |
407 | &schedule; |
408 | } |
408 | } |
409 | }; |
409 | }; |
… | |
… | |
543 | coro, regardless of priority. This is useful sometimes to ensure |
543 | coro, regardless of priority. This is useful sometimes to ensure |
544 | progress is made. |
544 | progress is made. |
545 | |
545 | |
546 | =item terminate [arg...] |
546 | =item terminate [arg...] |
547 | |
547 | |
548 | Terminates the current coro with the given status values (see L<cancel>). |
548 | Terminates the current coro with the given status values (see |
|
|
549 | L<cancel>). The values will not be copied, but referenced directly. |
549 | |
550 | |
550 | =item Coro::on_enter BLOCK, Coro::on_leave BLOCK |
551 | =item Coro::on_enter BLOCK, Coro::on_leave BLOCK |
551 | |
552 | |
552 | These function install enter and leave winders in the current scope. The |
553 | These function install enter and leave winders in the current scope. The |
553 | enter block will be executed when on_enter is called and whenever the |
554 | enter block will be executed when on_enter is called and whenever the |
… | |
… | |
727 | Returns true iff this Coro object has been suspended. Suspended Coros will |
728 | Returns true iff this Coro object has been suspended. Suspended Coros will |
728 | not ever be scheduled. |
729 | not ever be scheduled. |
729 | |
730 | |
730 | =item $coro->cancel (arg...) |
731 | =item $coro->cancel (arg...) |
731 | |
732 | |
732 | Terminates the given Coro and makes it return the given arguments as |
733 | Terminates the given Coro object and makes it return the given arguments as |
733 | status (default: the empty list). Never returns if the Coro is the |
734 | status (default: an empty list). Never returns if the Coro is the |
734 | current Coro. |
735 | current Coro. |
735 | |
736 | |
736 | =cut |
737 | The arguments are not copied, but instead will be referenced directly |
|
|
738 | (e.g. if you pass C<$var> and after the call change that variable, then |
|
|
739 | you might change the return values passed to e.g. C<join>, so don't do |
|
|
740 | that). |
737 | |
741 | |
738 | sub cancel { |
742 | The resources of the Coro are usually freed (or destructed) before this |
739 | my $self = shift; |
743 | call returns, but this can be delayed for an indefinite amount of time, as |
740 | |
744 | in some cases the manager thread has to run first to actually destruct the |
741 | if ($current == $self) { |
745 | Coro object. |
742 | terminate @_; |
|
|
743 | } else { |
|
|
744 | $self->{_status} = [@_]; |
|
|
745 | Coro::State::cancel $self; |
|
|
746 | } |
|
|
747 | } |
|
|
748 | |
746 | |
749 | =item $coro->schedule_to |
747 | =item $coro->schedule_to |
750 | |
748 | |
751 | Puts the current coro to sleep (like C<Coro::schedule>), but instead |
749 | Puts the current coro to sleep (like C<Coro::schedule>), but instead |
752 | of continuing with the next coro from the ready queue, always switch to |
750 | of continuing with the next coro from the ready queue, always switch to |
… | |
… | |
790 | |
788 | |
791 | =item $coro->join |
789 | =item $coro->join |
792 | |
790 | |
793 | Wait until the coro terminates and return any values given to the |
791 | Wait until the coro terminates and return any values given to the |
794 | C<terminate> or C<cancel> functions. C<join> can be called concurrently |
792 | C<terminate> or C<cancel> functions. C<join> can be called concurrently |
795 | from multiple coro, and all will be resumed and given the status |
793 | from multiple threads, and all will be resumed and given the status |
796 | return once the C<$coro> terminates. |
794 | return once the C<$coro> terminates. |
797 | |
795 | |
798 | =cut |
796 | =cut |
799 | |
797 | |
800 | sub join { |
798 | sub join { |