… | |
… | |
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; # also accepts values for ->join to retrieve |
209 | |
209 | |
210 | Cancellation I<can> be dangerous - it's a bit like calling C<exit> |
210 | Cancellation I<can> be dangerous - it's a bit like calling C<exit> without |
211 | without actually exiting, and might leave C libraries and XS modules in |
211 | actually exiting, and might leave C libraries and XS modules in a weird |
212 | a weird state. Unlike other thread implementations, however, Coro is |
212 | state. Unlike other thread implementations, however, Coro is exceptionally |
213 | exceptionally safe with regards to cancellation, as perl will always be |
213 | safe with regards to cancellation, as perl will always be in a consistent |
214 | in a consistent state, and for those cases where you want to do truly |
214 | state, and for those cases where you want to do truly marvellous things |
215 | marvellous things with your coro while it is being cancelled, there is |
215 | with your coro while it is being cancelled - that is, make sure all |
|
|
216 | cleanup code is executed from the thread being cancelled - there is even a |
216 | even a C<< ->safe_cancel >> method. |
217 | C<< ->safe_cancel >> method. |
217 | |
218 | |
218 | So, cancelling a thread that runs in an XS event loop might not be the |
219 | So, cancelling a thread that runs in an XS event loop might not be the |
219 | best idea, but any other combination that deals with perl only (cancelling |
220 | best idea, but any other combination that deals with perl only (cancelling |
220 | when a thread is in a C<tie> method or an C<AUTOLOAD> for example) is |
221 | when a thread is in a C<tie> method or an C<AUTOLOAD> for example) is |
221 | safe. |
222 | safe. |
… | |
… | |
740 | the thread is inside a C callback that doesn't expect to be canceled, |
741 | 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 | 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 | complicated cleanup handlers that rely on it'S thread context, things will |
743 | not work. |
744 | not work. |
744 | |
745 | |
745 | Sometimes it is safer to C<< ->throw >> an exception, or use C<< |
746 | Any cleanup code being run (e.g. from C<guard> blocks) will be run without |
746 | ->safe_cancel >>. |
747 | a thread context, and is not allowed to switch to other threads. On the |
|
|
748 | plus side, C<< ->cancel >> will always clean up the thread, no matter |
|
|
749 | what. If your cleanup code is complex or you want to avoid cancelling a |
|
|
750 | C-thread that doesn't know how to clean up itself, it can be better to C<< |
|
|
751 | ->throw >> an exception, or use C<< ->safe_cancel >>. |
747 | |
752 | |
748 | The arguments are not copied, but instead will be referenced directly |
753 | The arguments to C<< ->cancel >> are not copied, but instead will |
749 | (e.g. if you pass C<$var> and after the call change that variable, then |
754 | be referenced directly (e.g. if you pass C<$var> and after the call |
750 | you might change the return values passed to e.g. C<join>, so don't do |
755 | change that variable, then you might change the return values passed to |
751 | that). |
756 | e.g. C<join>, so don't do that). |
752 | |
757 | |
753 | The resources of the Coro are usually freed (or destructed) before this |
758 | The resources of the Coro are usually freed (or destructed) before this |
754 | call returns, but this can be delayed for an indefinite amount of time, as |
759 | call returns, but this can be delayed for an indefinite amount of time, as |
755 | in some cases the manager thread has to run first to actually destruct the |
760 | in some cases the manager thread has to run first to actually destruct the |
756 | Coro object. |
761 | Coro object. |
… | |
… | |
760 | Works mostly like C<< ->cancel >>, but is inherently "safer", and |
765 | 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 |
766 | consequently, can fail with an exception in cases the thread is not in a |
762 | cancellable state. |
767 | cancellable state. |
763 | |
768 | |
764 | This method works a bit like throwing an exception that cannot be caught |
769 | 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 |
770 | - specifically, it will clean up the thread from within itself, so |
766 | cleanup handlers (e.g. C<guard> blocks) are run with full thread context |
771 | all cleanup handlers (e.g. C<guard> blocks) are run with full thread |
767 | and can block if they wish. |
772 | context and can block if they wish. The downside is that there is no |
|
|
773 | guarantee that the thread can be cancelled when you call this method, and |
|
|
774 | therefore, it might fail. It is also considerably slower than C<cancel> or |
|
|
775 | C<terminate>. |
768 | |
776 | |
769 | A thread is safe-cancellable if it either hasn't been run yet, or |
777 | A thread is in a safe-cancellable state if it either hasn't been run yet, |
770 | it has no C context attached and is inside an SLF function. |
778 | or it has no C context attached and is inside an SLF function. |
771 | |
779 | |
772 | The latter two basically mean that the thread isn't currently inside a |
780 | 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 |
781 | perl callback called from some C function (usually via some XS modules) |
774 | currently inside some C function itself. |
782 | and isn't currently executing inside some C function itself (via Coro's XS |
|
|
783 | API). |
775 | |
784 | |
776 | This call always returns true when it could cancel the thread, or croaks |
785 | This call returns true when it could cancel the thread, or croaks with an |
777 | with an error otherwise, so you can write things like this: |
786 | error otherwise (i.e. it either returns true or doesn't return at all). |
|
|
787 | |
|
|
788 | Why the weird interface? Well, there are two common models on how and |
|
|
789 | when to cancel things. In the first, you have the expectation that your |
|
|
790 | coro thread can be cancelled when you want to cancel it - if the thread |
|
|
791 | isn't cancellable, this would be a bug somewhere, so C<< ->safe_cancel >> |
|
|
792 | croaks to notify of the bug. |
|
|
793 | |
|
|
794 | In the second model you sometimes want to ask nicely to cancel a thread, |
|
|
795 | but if it's not a good time, well, then don't cancel. This can be done |
|
|
796 | relatively easy like this: |
778 | |
797 | |
779 | if (! eval { $coro->safe_cancel }) { |
798 | if (! eval { $coro->safe_cancel }) { |
780 | warn "unable to cancel thread: $@"; |
799 | warn "unable to cancel thread: $@"; |
781 | } |
800 | } |
|
|
801 | |
|
|
802 | However, what you never should do is first try to cancel "safely" and |
|
|
803 | if that fails, cancel the "hard" way with C<< ->cancel >>. That makes |
|
|
804 | no sense: either you rely on being able to execute cleanup code in your |
|
|
805 | thread context, or you don't. If you do, then C<< ->safe_cancel >> is the |
|
|
806 | only way, and if you don't, then C<< ->cancel >> is always faster and more |
|
|
807 | direct. |
782 | |
808 | |
783 | =item $coro->schedule_to |
809 | =item $coro->schedule_to |
784 | |
810 | |
785 | Puts the current coro to sleep (like C<Coro::schedule>), but instead |
811 | Puts the current coro to sleep (like C<Coro::schedule>), but instead |
786 | of continuing with the next coro from the ready queue, always switch to |
812 | of continuing with the next coro from the ready queue, always switch to |
… | |
… | |
805 | inside the coro at the next convenient point in time. Otherwise |
831 | inside the coro at the next convenient point in time. Otherwise |
806 | clears the exception object. |
832 | clears the exception object. |
807 | |
833 | |
808 | Coro will check for the exception each time a schedule-like-function |
834 | Coro will check for the exception each time a schedule-like-function |
809 | returns, i.e. after each C<schedule>, C<cede>, C<< Coro::Semaphore->down |
835 | returns, i.e. after each C<schedule>, C<cede>, C<< Coro::Semaphore->down |
810 | >>, C<< Coro::Handle->readable >> and so on. Most of these functions |
836 | >>, C<< Coro::Handle->readable >> and so on. Most of those functions (all |
811 | detect this case and return early in case an exception is pending. |
837 | that are part of Coro itself) detect this case and return early in case an |
|
|
838 | exception is pending. |
812 | |
839 | |
813 | The exception object will be thrown "as is" with the specified scalar in |
840 | The exception object will be thrown "as is" with the specified scalar in |
814 | C<$@>, i.e. if it is a string, no line number or newline will be appended |
841 | C<$@>, i.e. if it is a string, no line number or newline will be appended |
815 | (unlike with C<die>). |
842 | (unlike with C<die>). |
816 | |
843 | |
817 | This can be used as a softer means than C<cancel> to ask a coro to |
844 | This can be used as a softer means than either C<cancel> or C<safe_cancel |
818 | end itself, although there is no guarantee that the exception will lead to |
845 | >to ask a coro to end itself, although there is no guarantee that the |
819 | termination, and if the exception isn't caught it might well end the whole |
846 | exception will lead to termination, and if the exception isn't caught it |
820 | program. |
847 | might well end the whole program. |
821 | |
848 | |
822 | You might also think of C<throw> as being the moral equivalent of |
849 | You might also think of C<throw> as being the moral equivalent of |
823 | C<kill>ing a coro with a signal (in this case, a scalar). |
850 | C<kill>ing a coro with a signal (in this case, a scalar). |
824 | |
851 | |
825 | =item $coro->join |
852 | =item $coro->join |
… | |
… | |
843 | }; |
870 | }; |
844 | |
871 | |
845 | &schedule while $current; |
872 | &schedule while $current; |
846 | } |
873 | } |
847 | |
874 | |
848 | wantarray ? @{$self->{_status}} : $self->{_status}[0]; |
875 | wantarray ? @{$self->{_status}} : $self->{_status}[0] |
849 | } |
876 | } |
850 | |
877 | |
851 | =item $coro->on_destroy (\&cb) |
878 | =item $coro->on_destroy (\&cb) |
852 | |
879 | |
853 | Registers a callback that is called when this coro thread gets destroyed, |
880 | Registers a callback that is called when this coro thread gets destroyed, |