… | |
… | |
196 | |
196 | |
197 | async { |
197 | async { |
198 | Coro::terminate "return value 1", "return value 2"; |
198 | Coro::terminate "return value 1", "return value 2"; |
199 | }; |
199 | }; |
200 | |
200 | |
201 | And yet another way is to C<< ->cancel >> the coro thread from another |
201 | And yet another way is to C<< ->cancel >> (or C<< ->safe_cancel >>) the |
202 | thread: |
202 | coro thread from another thread: |
203 | |
203 | |
204 | my $coro = async { |
204 | my $coro = async { |
205 | exit 1; |
205 | exit 1; |
206 | }; |
206 | }; |
207 | |
207 | |
208 | $coro->cancel; # an also accept values for ->join to retrieve |
208 | $coro->cancel; # an also accept values for ->join to retrieve |
209 | |
209 | |
210 | Cancellation I<can> be dangerous - it's a bit like calling C<exit> without |
210 | Cancellation I<can> be dangerous - it's a bit like calling C<exit> |
211 | actually exiting, and might leave C libraries and XS modules in a weird |
211 | without actually exiting, and might leave C libraries and XS modules in |
212 | state. Unlike other thread implementations, however, Coro is exceptionally |
212 | a weird state. Unlike other thread implementations, however, Coro is |
213 | safe with regards to cancellation, as perl will always be in a consistent |
213 | exceptionally safe with regards to cancellation, as perl will always be |
214 | state. |
214 | in a consistent state, and for those cases where you want to do truly |
|
|
215 | marvellous things with your coro while it is being cancelled, there is |
|
|
216 | even a C<< ->safe_cancel >> method. |
215 | |
217 | |
216 | So, cancelling a thread that runs in an XS event loop might not be the |
218 | So, cancelling a thread that runs in an XS event loop might not be the |
217 | best idea, but any other combination that deals with perl only (cancelling |
219 | best idea, but any other combination that deals with perl only (cancelling |
218 | when a thread is in a C<tie> method or an C<AUTOLOAD> for example) is |
220 | when a thread is in a C<tie> method or an C<AUTOLOAD> for example) is |
219 | safe. |
221 | safe. |
… | |
… | |
728 | Returns true iff this Coro object has been suspended. Suspended Coros will |
730 | Returns true iff this Coro object has been suspended. Suspended Coros will |
729 | not ever be scheduled. |
731 | not ever be scheduled. |
730 | |
732 | |
731 | =item $coro->cancel (arg...) |
733 | =item $coro->cancel (arg...) |
732 | |
734 | |
733 | Terminates the given Coro object and makes it return the given arguments as |
735 | Terminates the given Coro thread and makes it return the given arguments as |
734 | status (default: an empty list). Never returns if the Coro is the |
736 | status (default: an empty list). Never returns if the Coro is the |
735 | current Coro. |
737 | current Coro. |
|
|
738 | |
|
|
739 | This is a rather brutal way to free a coro, with some limitations - if |
|
|
740 | the thread is inside a C callback that doesn't expect to be canceled, |
|
|
741 | bad things can happen, or if the cancelled thread insists on running |
|
|
742 | complicated cleanup handlers that rely on it'S thread context, things will |
|
|
743 | not work. |
|
|
744 | |
|
|
745 | Sometimes it is safer to C<< ->throw >> an exception, or use C<< |
|
|
746 | ->safe_cancel >>. |
736 | |
747 | |
737 | The arguments are not copied, but instead will be referenced directly |
748 | 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 |
749 | (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 |
750 | you might change the return values passed to e.g. C<join>, so don't do |
740 | that). |
751 | that). |
741 | |
752 | |
742 | The resources of the Coro are usually freed (or destructed) before this |
753 | The resources of the Coro are usually freed (or destructed) before this |
743 | call returns, but this can be delayed for an indefinite amount of time, as |
754 | call returns, but this can be delayed for an indefinite amount of time, as |
744 | in some cases the manager thread has to run first to actually destruct the |
755 | in some cases the manager thread has to run first to actually destruct the |
745 | Coro object. |
756 | Coro object. |
|
|
757 | |
|
|
758 | =item $coro->safe_cancel ($arg...) |
|
|
759 | |
|
|
760 | Works mostly like C<< ->cancel >>, but is inherently "safer", and |
|
|
761 | consequently, can fail with an exception in cases the thread is not in a |
|
|
762 | cancellable state. |
|
|
763 | |
|
|
764 | This method works a bit like throwing an exception that cannot be caught |
|
|
765 | - specifically, it will clean up the thread from within itself, so all |
|
|
766 | cleanup handlers (e.g. C<guard> blocks) are run with full thread context |
|
|
767 | and can block if they wish. |
|
|
768 | |
|
|
769 | A thread is safe-cancellable if it either hasn't been run yet, or |
|
|
770 | it has no C context attached and is inside an SLF function. |
|
|
771 | |
|
|
772 | The latter two basically mean that the thread isn't currently inside a |
|
|
773 | perl callback called from some C function (usually XS modules) and isn't |
|
|
774 | currently inside some C function itself. |
|
|
775 | |
|
|
776 | This call always returns true when it could cancel the thread, or croaks |
|
|
777 | with an error otherwise, so you can write things like this: |
|
|
778 | |
|
|
779 | if (! eval { $coro->safe_cancel }) { |
|
|
780 | warn "unable to cancel thread: $@"; |
|
|
781 | } |
746 | |
782 | |
747 | =item $coro->schedule_to |
783 | =item $coro->schedule_to |
748 | |
784 | |
749 | Puts the current coro to sleep (like C<Coro::schedule>), but instead |
785 | Puts the current coro to sleep (like C<Coro::schedule>), but instead |
750 | of continuing with the next coro from the ready queue, always switch to |
786 | of continuing with the next coro from the ready queue, always switch to |