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

Comparing Coro/README (file contents):
Revision 1.31 by root, Fri Dec 7 23:23:15 2012 UTC vs.
Revision 1.38 by root, Wed Jun 22 20:24:38 2016 UTC

186 186
187 async { 187 async {
188 Coro::terminate "return value 1", "return value 2"; 188 Coro::terminate "return value 1", "return value 2";
189 }; 189 };
190 190
191 And yet another way is to "->cancel" (or "->safe_cancel") the coro 191 Yet another way is to "->cancel" (or "->safe_cancel") the coro
192 thread from another thread: 192 thread from another thread:
193 193
194 my $coro = async { 194 my $coro = async {
195 exit 1; 195 exit 1;
196 }; 196 };
209 So, cancelling a thread that runs in an XS event loop might not be 209 So, cancelling a thread that runs in an XS event loop might not be
210 the best idea, but any other combination that deals with perl only 210 the best idea, but any other combination that deals with perl only
211 (cancelling when a thread is in a "tie" method or an "AUTOLOAD" for 211 (cancelling when a thread is in a "tie" method or an "AUTOLOAD" for
212 example) is safe. 212 example) is safe.
213 213
214 Lastly, a coro thread object that isn't referenced is "->cancel"'ed 214 Last not least, a coro thread object that isn't referenced is
215 automatically - just like other objects in Perl. This is not such a 215 "->cancel"'ed automatically - just like other objects in Perl. This
216 common case, however - a running thread is referencedy b 216 is not such a common case, however - a running thread is referencedy
217 $Coro::current, a thread ready to run is referenced by the ready 217 by $Coro::current, a thread ready to run is referenced by the ready
218 queue, a thread waiting on a lock or semaphore is referenced by 218 queue, a thread waiting on a lock or semaphore is referenced by
219 being in some wait list and so on. But a thread that isn't in any of 219 being in some wait list and so on. But a thread that isn't in any of
220 those queues gets cancelled: 220 those queues gets cancelled:
221 221
222 async { 222 async {
223 schedule; # cede to other coros, don't go into the ready queue 223 schedule; # cede to other coros, don't go into the ready queue
224 }; 224 };
225 225
226 cede; 226 cede;
227 # now the async above is destroyed, as it is not referenced by anything. 227 # now the async above is destroyed, as it is not referenced by anything.
228
229 A slightly embellished example might make it clearer:
230
231 async {
232 my $guard = Guard::guard { print "destroyed\n" };
233 schedule while 1;
234 };
235
236 cede;
237
238 Superficially one might not expect any output - since the "async"
239 implements an endless loop, the $guard will not be cleaned up.
240 However, since the thread object returned by "async" is not stored
241 anywhere, the thread is initially referenced because it is in the
242 ready queue, when it runs it is referenced by $Coro::current, but
243 when it calls "schedule", it gets "cancel"ed causing the guard
244 object to be destroyed (see the next section), and printing it's
245 message.
246
247 If this seems a bit drastic, remember that this only happens when
248 nothing references the thread anymore, which means there is no way
249 to further execute it, ever. The only options at this point are
250 leaking the thread, or cleaning it up, which brings us to...
228 251
229 5. Cleanup 252 5. Cleanup
230 Threads will allocate various resources. Most but not all will be 253 Threads will allocate various resources. Most but not all will be
231 returned when a thread terminates, during clean-up. 254 returned when a thread terminates, during clean-up.
232 255
250 273
251 my $sem = new Coro::Semaphore; 274 my $sem = new Coro::Semaphore;
252 275
253 async { 276 async {
254 my $lock_guard = $sem->guard; 277 my $lock_guard = $sem->guard;
255 # if we reutrn, or die or get cancelled, here, 278 # if we return, or die or get cancelled, here,
256 # then the semaphore will be "up"ed. 279 # then the semaphore will be "up"ed.
257 }; 280 };
258 281
259 The "Guard::guard" function comes in handy for any custom cleanup 282 The "Guard::guard" function comes in handy for any custom cleanup
260 you might want to do (but you cannot switch to other coroutines form 283 you might want to do (but you cannot switch to other coroutines from
261 those code blocks): 284 those code blocks):
262 285
263 async { 286 async {
264 my $window = new Gtk2::Window "toplevel"; 287 my $window = new Gtk2::Window "toplevel";
265 # The window will not be cleaned up automatically, even when $window 288 # The window will not be cleaned up automatically, even when $window
282 6. Viva La Zombie Muerte 305 6. Viva La Zombie Muerte
283 Even after a thread has terminated and cleaned up its resources, the 306 Even after a thread has terminated and cleaned up its resources, the
284 Coro object still is there and stores the return values of the 307 Coro object still is there and stores the return values of the
285 thread. 308 thread.
286 309
287 The means the Coro object gets freed automatically when the thread 310 When there are no other references, it will simply be cleaned up and
288 has terminated and cleaned up and there arenot other references. 311 freed.
289 312
290 If there are, the Coro object will stay around, and you can call 313 If there areany references, the Coro object will stay around, and
291 "->join" as many times as you wish to retrieve the result values: 314 you can call "->join" as many times as you wish to retrieve the
315 result values:
292 316
293 async { 317 async {
294 print "hi\n"; 318 print "hi\n";
295 1 319 1
296 }; 320 };
393 program, as "async" does. As the coro is being reused, stuff like 417 program, as "async" does. As the coro is being reused, stuff like
394 "on_destroy" will not work in the expected way, unless you call 418 "on_destroy" will not work in the expected way, unless you call
395 terminate or cancel, which somehow defeats the purpose of pooling 419 terminate or cancel, which somehow defeats the purpose of pooling
396 (but is fine in the exceptional case). 420 (but is fine in the exceptional case).
397 421
398 The priority will be reset to 0 after each run, tracing will be 422 The priority will be reset to 0 after each run, all "swap_sv" calls
399 disabled, the description will be reset and the default output 423 will be undone, tracing will be disabled, the description will be
400 filehandle gets restored, so you can change all these. Otherwise the 424 reset and the default output filehandle gets restored, so you can
401 coro will be re-used "as-is": most notably if you change other 425 change all these. Otherwise the coro will be re-used "as-is": most
402 per-coro global stuff such as $/ you *must needs* revert that 426 notably if you change other per-coro global stuff such as $/ you
403 change, which is most simply done by using local as in: "local $/". 427 *must needs* revert that change, which is most simply done by using
428 local as in: "local $/".
404 429
405 The idle pool size is limited to 8 idle coros (this can be adjusted 430 The idle pool size is limited to 8 idle coros (this can be adjusted
406 by changing $Coro::POOL_SIZE), but there can be as many non-idle 431 by changing $Coro::POOL_SIZE), but there can be as many non-idle
407 coros as required. 432 coros as required.
408 433
525 Coro::on_enter { 550 Coro::on_enter {
526 # on entering the thread, we set an VTALRM handler to cede 551 # on entering the thread, we set an VTALRM handler to cede
527 $SIG{VTALRM} = sub { cede }; 552 $SIG{VTALRM} = sub { cede };
528 # and then start the interval timer 553 # and then start the interval timer
529 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01; 554 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01;
530 }; 555 };
531 Coro::on_leave { 556 Coro::on_leave {
532 # on leaving the thread, we stop the interval timer again 557 # on leaving the thread, we stop the interval timer again
533 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0; 558 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0;
534 }; 559 };
535 560
536 &{+shift}; 561 &{+shift};
537 } 562 }
538 563
539 # use like this: 564 # use like this:
540 timeslice { 565 timeslice {
541 # The following is an endless loop that would normally 566 # The following is an endless loop that would normally
542 # monopolise the process. Since it runs in a timesliced 567 # monopolise the process. Since it runs in a timesliced
637 if the thread is inside a C callback that doesn't expect to be 662 if the thread is inside a C callback that doesn't expect to be
638 canceled, bad things can happen, or if the cancelled thread insists 663 canceled, bad things can happen, or if the cancelled thread insists
639 on running complicated cleanup handlers that rely on its thread 664 on running complicated cleanup handlers that rely on its thread
640 context, things will not work. 665 context, things will not work.
641 666
642 Any cleanup code being run (e.g. from "guard" blocks) will be run 667 Any cleanup code being run (e.g. from "guard" blocks, destructors
643 without a thread context, and is not allowed to switch to other 668 and so on) will be run without a thread context, and is not allowed
669 to switch to other threads. A common mistake is to call "->cancel"
670 from a destructor called by die'ing inside the thread to be
671 cancelled for example.
672
644 threads. On the plus side, "->cancel" will always clean up the 673 On the plus side, "->cancel" will always clean up the thread, no
645 thread, no matter what. If your cleanup code is complex or you want 674 matter what. If your cleanup code is complex or you want to avoid
646 to avoid cancelling a C-thread that doesn't know how to clean up 675 cancelling a C-thread that doesn't know how to clean up itself, it
647 itself, it can be better to "->throw" an exception, or use 676 can be better to "->throw" an exception, or use "->safe_cancel".
648 "->safe_cancel".
649 677
650 The arguments to "->cancel" are not copied, but instead will be 678 The arguments to "->cancel" are not copied, but instead will be
651 referenced directly (e.g. if you pass $var and after the call change 679 referenced directly (e.g. if you pass $var and after the call change
652 that variable, then you might change the return values passed to 680 that variable, then you might change the return values passed to
653 e.g. "join", so don't do that). 681 e.g. "join", so don't do that).
658 actually destruct the Coro object. 686 actually destruct the Coro object.
659 687
660 $coro->safe_cancel ($arg...) 688 $coro->safe_cancel ($arg...)
661 Works mostly like "->cancel", but is inherently "safer", and 689 Works mostly like "->cancel", but is inherently "safer", and
662 consequently, can fail with an exception in cases the thread is not 690 consequently, can fail with an exception in cases the thread is not
663 in a cancellable state. 691 in a cancellable state. Essentially, "->safe_cancel" is a "->cancel"
692 with extra checks before canceling.
664 693
665 This method works a bit like throwing an exception that cannot be 694 It works a bit like throwing an exception that cannot be caught -
666 caught - specifically, it will clean up the thread from within 695 specifically, it will clean up the thread from within itself, so all
667 itself, so all cleanup handlers (e.g. "guard" blocks) are run with 696 cleanup handlers (e.g. "guard" blocks) are run with full thread
668 full thread context and can block if they wish. The downside is that 697 context and can block if they wish. The downside is that there is no
669 there is no guarantee that the thread can be cancelled when you call 698 guarantee that the thread can be cancelled when you call this
670 this method, and therefore, it might fail. It is also considerably 699 method, and therefore, it might fail. It is also considerably slower
671 slower than "cancel" or "terminate". 700 than "cancel" or "terminate".
672 701
673 A thread is in a safe-cancellable state if it either hasn't been run 702 A thread is in a safe-cancellable state if it either hasn't been run
674 yet, or it has no C context attached and is inside an SLF function. 703 yet, or it has no C context attached and is inside an SLF function.
675 704
676 The latter two basically mean that the thread isn't currently inside 705 The latter two basically mean that the thread isn't currently inside
754 destroyed, that is, after it's resources have been freed but before 783 destroyed, that is, after it's resources have been freed but before
755 it is joined. The callback gets passed the terminate/cancel 784 it is joined. The callback gets passed the terminate/cancel
756 arguments, if any, and *must not* die, under any circumstances. 785 arguments, if any, and *must not* die, under any circumstances.
757 786
758 There can be any number of "on_destroy" callbacks per coro, and 787 There can be any number of "on_destroy" callbacks per coro, and
759 there is no way currently to remove a callback once added. 788 there is currently no way to remove a callback once added.
760 789
761 $oldprio = $coro->prio ($newprio) 790 $oldprio = $coro->prio ($newprio)
762 Sets (or gets, if the argument is missing) the priority of the coro 791 Sets (or gets, if the argument is missing) the priority of the coro
763 thread. Higher priority coro get run before lower priority coros. 792 thread. Higher priority coro get run before lower priority coros.
764 Priorities are small signed integers (currently -4 .. +3), that you 793 Priorities are small signed integers (currently -4 .. +3), that you
829 otherwise you might suffer from crashes or worse. The only event 858 otherwise you might suffer from crashes or worse. The only event
830 library currently known that is safe to use without "unblock_sub" is 859 library currently known that is safe to use without "unblock_sub" is
831 EV (but you might still run into deadlocks if all event loops are 860 EV (but you might still run into deadlocks if all event loops are
832 blocked). 861 blocked).
833 862
834 Coro will try to catch you when you block in the event loop 863 Coro will try to catch you when you block in the event loop ("FATAL:
835 ("FATAL:$Coro::IDLE blocked itself"), but this is just best effort 864 $Coro::idle blocked itself"), but this is just best effort and only
836 and only works when you do not run your own event loop. 865 works when you do not run your own event loop.
837 866
838 This function allows your callbacks to block by executing them in 867 This function allows your callbacks to block by executing them in
839 another coro where it is safe to block. One example where blocking 868 another coro where it is safe to block. One example where blocking
840 is handy is when you use the Coro::AIO functions to save results to 869 is handy is when you use the Coro::AIO functions to save results to
841 disk, for example. 870 disk, for example.
891 But from within a coro, you often just want to write this: 920 But from within a coro, you often just want to write this:
892 921
893 my $status = wait_for_child $pid; 922 my $status = wait_for_child $pid;
894 923
895 Coro offers two functions specifically designed to make this easy, 924 Coro offers two functions specifically designed to make this easy,
896 "Coro::rouse_cb" and "Coro::rouse_wait". 925 "rouse_cb" and "rouse_wait".
897 926
898 The first function, "rouse_cb", generates and returns a callback that, 927 The first function, "rouse_cb", generates and returns a callback that,
899 when invoked, will save its arguments and notify the coro that created 928 when invoked, will save its arguments and notify the coro that created
900 the callback. 929 the callback.
901 930
907 function mentioned above: 936 function mentioned above:
908 937
909 sub wait_for_child($) { 938 sub wait_for_child($) {
910 my ($pid) = @_; 939 my ($pid) = @_;
911 940
912 my $watcher = AnyEvent->child (pid => $pid, cb => Coro::rouse_cb); 941 my $watcher = AnyEvent->child (pid => $pid, cb => rouse_cb);
913 942
914 my ($rpid, $rstatus) = Coro::rouse_wait; 943 my ($rpid, $rstatus) = rouse_wait;
915 $rstatus 944 $rstatus
916 } 945 }
917 946
918 In the case where "rouse_cb" and "rouse_wait" are not flexible enough, 947 In the case where "rouse_cb" and "rouse_wait" are not flexible enough,
919 you can roll your own, using "schedule" and "ready": 948 you can roll your own, using "schedule" and "ready":
1046 1075
1047 XS API: Coro::MakeMaker. 1076 XS API: Coro::MakeMaker.
1048 1077
1049 Low level Configuration, Thread Environment, Continuations: Coro::State. 1078 Low level Configuration, Thread Environment, Continuations: Coro::State.
1050 1079
1051AUTHOR 1080AUTHOR/SUPPORT/CONTACT
1052 Marc Lehmann <schmorp@schmorp.de> 1081 Marc A. Lehmann <schmorp@schmorp.de>
1053 http://home.schmorp.de/ 1082 http://software.schmorp.de/pkg/Coro.html
1054 1083

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines