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

Comparing Coro/Coro.pm (file contents):
Revision 1.305 by root, Thu Aug 4 19:37:58 2011 UTC vs.
Revision 1.327 by root, Sun Jun 1 22:00:45 2014 UTC

195 195
196 async { 196 async {
197 Coro::terminate "return value 1", "return value 2"; 197 Coro::terminate "return value 1", "return value 2";
198 }; 198 };
199 199
200And yet another way is to C<< ->cancel >> (or C<< ->safe_cancel >>) the 200Yet another way is to C<< ->cancel >> (or C<< ->safe_cancel >>) the coro
201coro thread from another thread: 201thread from another thread:
202 202
203 my $coro = async { 203 my $coro = async {
204 exit 1; 204 exit 1;
205 }; 205 };
206 206
218So, cancelling a thread that runs in an XS event loop might not be the 218So, 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 219best 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 220when a thread is in a C<tie> method or an C<AUTOLOAD> for example) is
221safe. 221safe.
222 222
223Lastly, a coro thread object that isn't referenced is C<< ->cancel >>'ed 223Last not least, a coro thread object that isn't referenced is C<<
224automatically - just like other objects in Perl. This is not such a common 224->cancel >>'ed automatically - just like other objects in Perl. This
225case, however - a running thread is referencedy b C<$Coro::current>, a 225is not such a common case, however - a running thread is referencedy by
226thread ready to run is referenced by the ready queue, a thread waiting 226C<$Coro::current>, a thread ready to run is referenced by the ready queue,
227on a lock or semaphore is referenced by being in some wait list and so 227a thread waiting on a lock or semaphore is referenced by being in some
228on. But a thread that isn't in any of those queues gets cancelled: 228wait list and so on. But a thread that isn't in any of those queues gets
229cancelled:
229 230
230 async { 231 async {
231 schedule; # cede to other coros, don't go into the ready queue 232 schedule; # cede to other coros, don't go into the ready queue
232 }; 233 };
233 234
234 cede; 235 cede;
235 # now the async above is destroyed, as it is not referenced by anything. 236 # now the async above is destroyed, as it is not referenced by anything.
237
238A slightly embellished example might make it clearer:
239
240 async {
241 my $guard = Guard::guard { print "destroyed\n" };
242 schedule while 1;
243 };
244
245 cede;
246
247Superficially one might not expect any output - since the C<async>
248implements an endless loop, the C<$guard> will not be cleaned up. However,
249since the thread object returned by C<async> is not stored anywhere, the
250thread is initially referenced because it is in the ready queue, when it
251runs it is referenced by C<$Coro::current>, but when it calls C<schedule>,
252it gets C<cancel>ed causing the guard object to be destroyed (see the next
253section), and printing it's message.
254
255If this seems a bit drastic, remember that this only happens when nothing
256references the thread anymore, which means there is no way to further
257execute it, ever. The only options at this point are leaking the thread,
258or cleaning it up, which brings us to...
236 259
237=item 5. Cleanup 260=item 5. Cleanup
238 261
239Threads will allocate various resources. Most but not all will be returned 262Threads will allocate various resources. Most but not all will be returned
240when a thread terminates, during clean-up. 263when a thread terminates, during clean-up.
259 282
260 my $sem = new Coro::Semaphore; 283 my $sem = new Coro::Semaphore;
261 284
262 async { 285 async {
263 my $lock_guard = $sem->guard; 286 my $lock_guard = $sem->guard;
264 # if we reutrn, or die or get cancelled, here, 287 # if we return, or die or get cancelled, here,
265 # then the semaphore will be "up"ed. 288 # then the semaphore will be "up"ed.
266 }; 289 };
267 290
268The C<Guard::guard> function comes in handy for any custom cleanup you 291The C<Guard::guard> function comes in handy for any custom cleanup you
269might want to do (but you cannot switch to other coroutines form those 292might want to do (but you cannot switch to other coroutines from those
270code blocks): 293code blocks):
271 294
272 async { 295 async {
273 my $window = new Gtk2::Window "toplevel"; 296 my $window = new Gtk2::Window "toplevel";
274 # The window will not be cleaned up automatically, even when $window 297 # The window will not be cleaned up automatically, even when $window
291=item 6. Viva La Zombie Muerte 314=item 6. Viva La Zombie Muerte
292 315
293Even after a thread has terminated and cleaned up its resources, the Coro 316Even after a thread has terminated and cleaned up its resources, the Coro
294object still is there and stores the return values of the thread. 317object still is there and stores the return values of the thread.
295 318
296The means the Coro object gets freed automatically when the thread has 319When there are no other references, it will simply be cleaned up and
297terminated and cleaned up and there arenot other references. 320freed.
298 321
299If there are, the Coro object will stay around, and you can call C<< 322If there areany references, the Coro object will stay around, and you
300->join >> as many times as you wish to retrieve the result values: 323can call C<< ->join >> as many times as you wish to retrieve the result
324values:
301 325
302 async { 326 async {
303 print "hi\n"; 327 print "hi\n";
304 1 328 1
305 }; 329 };
342 366
343our $idle; # idle handler 367our $idle; # idle handler
344our $main; # main coro 368our $main; # main coro
345our $current; # current coro 369our $current; # current coro
346 370
347our $VERSION = 6.05; 371our $VERSION = 6.39;
348 372
349our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); 373our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait);
350our %EXPORT_TAGS = ( 374our %EXPORT_TAGS = (
351 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 375 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
352); 376);
357=over 4 381=over 4
358 382
359=item $Coro::main 383=item $Coro::main
360 384
361This variable stores the Coro object that represents the main 385This variable stores the Coro object that represents the main
362program. While you cna C<ready> it and do most other things you can do to 386program. While you can C<ready> it and do most other things you can do to
363coro, it is mainly useful to compare again C<$Coro::current>, to see 387coro, it is mainly useful to compare again C<$Coro::current>, to see
364whether you are running in the main program or not. 388whether you are running in the main program or not.
365 389
366=cut 390=cut
367 391
628 Coro::on_enter { 652 Coro::on_enter {
629 # on entering the thread, we set an VTALRM handler to cede 653 # on entering the thread, we set an VTALRM handler to cede
630 $SIG{VTALRM} = sub { cede }; 654 $SIG{VTALRM} = sub { cede };
631 # and then start the interval timer 655 # and then start the interval timer
632 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01; 656 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01;
633 }; 657 };
634 Coro::on_leave { 658 Coro::on_leave {
635 # on leaving the thread, we stop the interval timer again 659 # on leaving the thread, we stop the interval timer again
636 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0; 660 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0;
637 }; 661 };
638 662
639 &{+shift}; 663 &{+shift};
640 } 664 }
641 665
642 # use like this: 666 # use like this:
643 timeslice { 667 timeslice {
644 # The following is an endless loop that would normally 668 # The following is an endless loop that would normally
645 # monopolise the process. Since it runs in a timesliced 669 # monopolise the process. Since it runs in a timesliced
646 # environment, it will regularly cede to other threads. 670 # environment, it will regularly cede to other threads.
647 while () { } 671 while () { }
648 }; 672 };
649 673
650 674
651=item killall 675=item killall
652 676
653Kills/terminates/cancels all coros except the currently running one. 677Kills/terminates/cancels all coros except the currently running one.
769the thread is inside a C callback that doesn't expect to be canceled, 793the thread is inside a C callback that doesn't expect to be canceled,
770bad things can happen, or if the cancelled thread insists on running 794bad things can happen, or if the cancelled thread insists on running
771complicated cleanup handlers that rely on its thread context, things will 795complicated cleanup handlers that rely on its thread context, things will
772not work. 796not work.
773 797
774Any cleanup code being run (e.g. from C<guard> blocks) will be run without 798Any cleanup code being run (e.g. from C<guard> blocks, destructors and so
775a thread context, and is not allowed to switch to other threads. On the 799on) will be run without a thread context, and is not allowed to switch
800to other threads. A common mistake is to call C<< ->cancel >> from a
801destructor called by die'ing inside the thread to be cancelled for
802example.
803
776plus side, C<< ->cancel >> will always clean up the thread, no matter 804On the plus side, C<< ->cancel >> will always clean up the thread, no
777what. If your cleanup code is complex or you want to avoid cancelling a 805matter what. If your cleanup code is complex or you want to avoid
778C-thread that doesn't know how to clean up itself, it can be better to C<< 806cancelling a C-thread that doesn't know how to clean up itself, it can be
779->throw >> an exception, or use C<< ->safe_cancel >>. 807better to C<< ->throw >> an exception, or use C<< ->safe_cancel >>.
780 808
781The arguments to C<< ->cancel >> are not copied, but instead will 809The arguments to C<< ->cancel >> are not copied, but instead will
782be referenced directly (e.g. if you pass C<$var> and after the call 810be referenced directly (e.g. if you pass C<$var> and after the call
783change that variable, then you might change the return values passed to 811change that variable, then you might change the return values passed to
784e.g. C<join>, so don't do that). 812e.g. C<join>, so don't do that).
790 818
791=item $coro->safe_cancel ($arg...) 819=item $coro->safe_cancel ($arg...)
792 820
793Works mostly like C<< ->cancel >>, but is inherently "safer", and 821Works mostly like C<< ->cancel >>, but is inherently "safer", and
794consequently, can fail with an exception in cases the thread is not in a 822consequently, can fail with an exception in cases the thread is not in a
795cancellable state. 823cancellable state. Essentially, C<< ->safe_cancel >> is a C<< ->cancel >>
824with extra checks before canceling.
796 825
797This method works a bit like throwing an exception that cannot be caught 826It works a bit like throwing an exception that cannot be caught -
798- specifically, it will clean up the thread from within itself, so 827specifically, it will clean up the thread from within itself, so all
799all cleanup handlers (e.g. C<guard> blocks) are run with full thread 828cleanup handlers (e.g. C<guard> blocks) are run with full thread
800context and can block if they wish. The downside is that there is no 829context and can block if they wish. The downside is that there is no
801guarantee that the thread can be cancelled when you call this method, and 830guarantee that the thread can be cancelled when you call this method, and
802therefore, it might fail. It is also considerably slower than C<cancel> or 831therefore, it might fail. It is also considerably slower than C<cancel> or
803C<terminate>. 832C<terminate>.
804 833
890that is, after it's resources have been freed but before it is joined. The 919that is, after it's resources have been freed but before it is joined. The
891callback gets passed the terminate/cancel arguments, if any, and I<must 920callback gets passed the terminate/cancel arguments, if any, and I<must
892not> die, under any circumstances. 921not> die, under any circumstances.
893 922
894There can be any number of C<on_destroy> callbacks per coro, and there is 923There can be any number of C<on_destroy> callbacks per coro, and there is
895no way currently to remove a callback once added. 924currently no way to remove a callback once added.
896 925
897=item $oldprio = $coro->prio ($newprio) 926=item $oldprio = $coro->prio ($newprio)
898 927
899Sets (or gets, if the argument is missing) the priority of the 928Sets (or gets, if the argument is missing) the priority of the
900coro thread. Higher priority coro get run before lower priority 929coro thread. Higher priority coro get run before lower priority
927coro thread. This is just a free-form string you can associate with a 956coro thread. This is just a free-form string you can associate with a
928coro. 957coro.
929 958
930This method simply sets the C<< $coro->{desc} >> member to the given 959This method simply sets the C<< $coro->{desc} >> member to the given
931string. You can modify this member directly if you wish, and in fact, this 960string. You can modify this member directly if you wish, and in fact, this
932is often preferred to indicate major processing states that cna then be 961is often preferred to indicate major processing states that can then be
933seen for example in a L<Coro::Debug> session: 962seen for example in a L<Coro::Debug> session:
934 963
935 sub my_long_function { 964 sub my_long_function {
936 local $Coro::current->{desc} = "now in my_long_function"; 965 local $Coro::current->{desc} = "now in my_long_function";
937 ... 966 ...
1106But from within a coro, you often just want to write this: 1135But from within a coro, you often just want to write this:
1107 1136
1108 my $status = wait_for_child $pid; 1137 my $status = wait_for_child $pid;
1109 1138
1110Coro offers two functions specifically designed to make this easy, 1139Coro offers two functions specifically designed to make this easy,
1111C<Coro::rouse_cb> and C<Coro::rouse_wait>. 1140C<rouse_cb> and C<rouse_wait>.
1112 1141
1113The first function, C<rouse_cb>, generates and returns a callback that, 1142The first function, C<rouse_cb>, generates and returns a callback that,
1114when invoked, will save its arguments and notify the coro that 1143when invoked, will save its arguments and notify the coro that
1115created the callback. 1144created the callback.
1116 1145
1122function mentioned above: 1151function mentioned above:
1123 1152
1124 sub wait_for_child($) { 1153 sub wait_for_child($) {
1125 my ($pid) = @_; 1154 my ($pid) = @_;
1126 1155
1127 my $watcher = AnyEvent->child (pid => $pid, cb => Coro::rouse_cb); 1156 my $watcher = AnyEvent->child (pid => $pid, cb => rouse_cb);
1128 1157
1129 my ($rpid, $rstatus) = Coro::rouse_wait; 1158 my ($rpid, $rstatus) = rouse_wait;
1130 $rstatus 1159 $rstatus
1131 } 1160 }
1132 1161
1133In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough, 1162In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough,
1134you can roll your own, using C<schedule>: 1163you can roll your own, using C<schedule> and C<ready>:
1135 1164
1136 sub wait_for_child($) { 1165 sub wait_for_child($) {
1137 my ($pid) = @_; 1166 my ($pid) = @_;
1138 1167
1139 # store the current coro in $current, 1168 # store the current coro in $current,
1142 my ($done, $rstatus); 1171 my ($done, $rstatus);
1143 1172
1144 # pass a closure to ->child 1173 # pass a closure to ->child
1145 my $watcher = AnyEvent->child (pid => $pid, cb => sub { 1174 my $watcher = AnyEvent->child (pid => $pid, cb => sub {
1146 $rstatus = $_[1]; # remember rstatus 1175 $rstatus = $_[1]; # remember rstatus
1147 $done = 1; # mark $rstatus as valud 1176 $done = 1; # mark $rstatus as valid
1177 $current->ready; # wake up the waiting thread
1148 }); 1178 });
1149 1179
1150 # wait until the closure has been called 1180 # wait until the closure has been called
1151 schedule while !$done; 1181 schedule while !$done;
1152 1182

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines