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

Comparing cvsroot/Coro/Coro.pm (file contents):
Revision 1.293 by root, Sat Apr 30 05:20:03 2011 UTC vs.
Revision 1.295 by root, Tue May 10 19:55:48 2011 UTC

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
201And yet another way is to C<< ->cancel >> the coro thread from another 201And yet another way is to C<< ->cancel >> (or C<< ->safe_cancel >>) the
202thread: 202coro 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; # also accepts values for ->join to retrieve
209 209
210Cancellation I<can> be dangerous - it's a bit like calling C<exit> without 210Cancellation I<can> be dangerous - it's a bit like calling C<exit> without
211actually exiting, and might leave C libraries and XS modules in a weird 211actually exiting, and might leave C libraries and XS modules in a weird
212state. Unlike other thread implementations, however, Coro is exceptionally 212state. Unlike other thread implementations, however, Coro is exceptionally
213safe with regards to cancellation, as perl will always be in a consistent 213safe with regards to cancellation, as perl will always be in a consistent
214state. 214state, and for those cases where you want to do truly marvellous things
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
217C<< ->safe_cancel >> method.
215 218
216So, 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
217best idea, but any other combination that deals with perl only (cancelling 220best idea, but any other combination that deals with perl only (cancelling
218when 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
219safe. 222safe.
728Returns true iff this Coro object has been suspended. Suspended Coros will 731Returns true iff this Coro object has been suspended. Suspended Coros will
729not ever be scheduled. 732not ever be scheduled.
730 733
731=item $coro->cancel (arg...) 734=item $coro->cancel (arg...)
732 735
733Terminates the given Coro object and makes it return the given arguments as 736Terminates the given Coro thread and makes it return the given arguments as
734status (default: an empty list). Never returns if the Coro is the 737status (default: an empty list). Never returns if the Coro is the
735current Coro. 738current Coro.
736 739
737The arguments are not copied, but instead will be referenced directly 740This is a rather brutal way to free a coro, with some limitations - if
738(e.g. if you pass C<$var> and after the call change that variable, then 741the thread is inside a C callback that doesn't expect to be canceled,
739you might change the return values passed to e.g. C<join>, so don't do 742bad things can happen, or if the cancelled thread insists on running
740that). 743complicated cleanup handlers that rely on it'S thread context, things will
744not work.
745
746Any cleanup code being run (e.g. from C<guard> blocks) will be run without
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 >>.
752
753The arguments to C<< ->cancel >> are not copied, but instead will
754be referenced directly (e.g. if you pass C<$var> and after the call
755change that variable, then you might change the return values passed to
756e.g. C<join>, so don't do that).
741 757
742The resources of the Coro are usually freed (or destructed) before this 758The resources of the Coro are usually freed (or destructed) before this
743call 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
744in 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
745Coro object. 761Coro object.
746 762
763=item $coro->safe_cancel ($arg...)
764
765Works mostly like C<< ->cancel >>, but is inherently "safer", and
766consequently, can fail with an exception in cases the thread is not in a
767cancellable state.
768
769This method works a bit like throwing an exception that cannot be caught
770- specifically, it will clean up the thread from within itself, so
771all cleanup handlers (e.g. C<guard> blocks) are run with full thread
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>.
776
777A thread is in a safe-cancellable state if it either hasn't been run yet,
778or it has no C context attached and is inside an SLF function.
779
780The latter two basically mean that the thread isn't currently inside a
781perl callback called from some C function (usually via some XS modules)
782and isn't currently executing inside some C function itself (via Coro's XS
783API).
784
785This call returns true when it could cancel the thread, or croaks with an
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:
797
798 if (! eval { $coro->safe_cancel }) {
799 warn "unable to cancel thread: $@";
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.
808
747=item $coro->schedule_to 809=item $coro->schedule_to
748 810
749Puts the current coro to sleep (like C<Coro::schedule>), but instead 811Puts the current coro to sleep (like C<Coro::schedule>), but instead
750of 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
751the given coro object (regardless of priority etc.). The readyness 813the given coro object (regardless of priority etc.). The readyness
769inside the coro at the next convenient point in time. Otherwise 831inside the coro at the next convenient point in time. Otherwise
770clears the exception object. 832clears the exception object.
771 833
772Coro will check for the exception each time a schedule-like-function 834Coro will check for the exception each time a schedule-like-function
773returns, 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
774>>, C<< Coro::Handle->readable >> and so on. Most of these functions 836>>, C<< Coro::Handle->readable >> and so on. Most of those functions (all
775detect 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.
776 839
777The 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
778C<$@>, 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
779(unlike with C<die>). 842(unlike with C<die>).
780 843
781This 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
782end 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
783termination, 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
784program. 847might well end the whole program.
785 848
786You 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
787C<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).
788 851
789=item $coro->join 852=item $coro->join
807 }; 870 };
808 871
809 &schedule while $current; 872 &schedule while $current;
810 } 873 }
811 874
812 wantarray ? @{$self->{_status}} : $self->{_status}[0]; 875 wantarray ? @{$self->{_status}} : $self->{_status}[0]
813} 876}
814 877
815=item $coro->on_destroy (\&cb) 878=item $coro->on_destroy (\&cb)
816 879
817Registers 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