ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
(Generate patch)

Comparing Coro/Coro.pm (file contents):
Revision 1.294 by root, Fri May 6 21:15:17 2011 UTC vs.
Revision 1.295 by root, Tue May 10 19:55:48 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines