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

Comparing Coro/README (file contents):
Revision 1.33 by root, Tue Mar 4 06:13:24 2014 UTC vs.
Revision 1.40 by root, Thu Aug 31 16:28:49 2017 UTC

417 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
418 "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
419 terminate or cancel, which somehow defeats the purpose of pooling 419 terminate or cancel, which somehow defeats the purpose of pooling
420 (but is fine in the exceptional case). 420 (but is fine in the exceptional case).
421 421
422 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
423 disabled, the description will be reset and the default output 423 will be undone, tracing will be disabled, the description will be
424 filehandle gets restored, so you can change all these. Otherwise the 424 reset and the default output filehandle gets restored, so you can
425 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
426 per-coro global stuff such as $/ you *must needs* revert that 426 notably if you change other per-coro global stuff such as $/ you
427 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 $/".
428 429
429 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
430 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
431 coros as required. 432 coros as required.
432 433
533 # at this place, the timezone is Antarctica/South_Pole, 534 # at this place, the timezone is Antarctica/South_Pole,
534 # without disturbing the TZ of any other coro. 535 # without disturbing the TZ of any other coro.
535 }; 536 };
536 537
537 This can be used to localise about any resource (locale, uid, 538 This can be used to localise about any resource (locale, uid,
538 current working directory etc.) to a block, despite the existance of 539 current working directory etc.) to a block, despite the existence of
539 other coros. 540 other coros.
540 541
541 Another interesting example implements time-sliced multitasking 542 Another interesting example implements time-sliced multitasking
542 using interval timers (this could obviously be optimised, but does 543 using interval timers (this could obviously be optimised, but does
543 the job): 544 the job):
625 $state->is_new 626 $state->is_new
626 Returns true iff this Coro object is "new", i.e. has never been run 627 Returns true iff this Coro object is "new", i.e. has never been run
627 yet. Those states basically consist of only the code reference to 628 yet. Those states basically consist of only the code reference to
628 call and the arguments, but consumes very little other resources. 629 call and the arguments, but consumes very little other resources.
629 New states will automatically get assigned a perl interpreter when 630 New states will automatically get assigned a perl interpreter when
630 they are transfered to. 631 they are transferred to.
631 632
632 $state->is_zombie 633 $state->is_zombie
633 Returns true iff the Coro object has been cancelled, i.e. it's 634 Returns true iff the Coro object has been cancelled, i.e. it's
634 resources freed because they were "cancel"'ed, "terminate"'d, 635 resources freed because they were "cancel"'ed, "terminate"'d,
635 "safe_cancel"'ed or simply went out of scope. 636 "safe_cancel"'ed or simply went out of scope.
650 651
651 $is_suspended = $coro->is_suspended 652 $is_suspended = $coro->is_suspended
652 Returns true iff this Coro object has been suspended. Suspended 653 Returns true iff this Coro object has been suspended. Suspended
653 Coros will not ever be scheduled. 654 Coros will not ever be scheduled.
654 655
655 $coro->cancel (arg...) 656 $coro->cancel ($arg...)
656 Terminates the given Coro thread and makes it return the given 657 Terminate the given Coro thread and make it return the given
657 arguments as status (default: an empty list). Never returns if the 658 arguments as status (default: an empty list). Never returns if the
658 Coro is the current Coro. 659 Coro is the current Coro.
659 660
660 This is a rather brutal way to free a coro, with some limitations - 661 This is a rather brutal way to free a coro, with some limitations -
661 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
662 canceled, bad things can happen, or if the cancelled thread insists 663 canceled, bad things can happen, or if the cancelled thread insists
663 on running complicated cleanup handlers that rely on its thread 664 on running complicated cleanup handlers that rely on its thread
664 context, things will not work. 665 context, things will not work.
665 666
666 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
667 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
668 threads. On the plus side, "->cancel" will always clean up the 673 On the plus side, "->cancel" will always clean up the thread, no
669 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
670 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
671 itself, it can be better to "->throw" an exception, or use 676 can be better to "->throw" an exception, or use "->safe_cancel".
672 "->safe_cancel".
673 677
674 The arguments to "->cancel" are not copied, but instead will be 678 The arguments to "->cancel" are not copied, but instead will be
675 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
676 that variable, then you might change the return values passed to 680 that variable, then you might change the return values passed to
677 e.g. "join", so don't do that). 681 e.g. "join", so don't do that).
682 actually destruct the Coro object. 686 actually destruct the Coro object.
683 687
684 $coro->safe_cancel ($arg...) 688 $coro->safe_cancel ($arg...)
685 Works mostly like "->cancel", but is inherently "safer", and 689 Works mostly like "->cancel", but is inherently "safer", and
686 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
687 in a cancellable state. 691 in a cancellable state. Essentially, "->safe_cancel" is a "->cancel"
692 with extra checks before canceling.
688 693
689 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 -
690 caught - specifically, it will clean up the thread from within 695 specifically, it will clean up the thread from within itself, so all
691 itself, so all cleanup handlers (e.g. "guard" blocks) are run with 696 cleanup handlers (e.g. "guard" blocks) are run with full thread
692 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
693 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
694 this method, and therefore, it might fail. It is also considerably 699 method, and therefore, it might fail. It is also considerably slower
695 slower than "cancel" or "terminate". 700 than "cancel" or "terminate".
696 701
697 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 has never been
703 run yet, has already been canceled/terminated or otherwise
698 yet, or it has no C context attached and is inside an SLF function. 704 destroyed, or has no C context attached and is inside an SLF
705 function.
699 706
707 The first two states are trivial - a thread that hasnot started or
708 has already finished is safe to cancel.
709
700 The latter two basically mean that the thread isn't currently inside 710 The last state basically means that the thread isn't currently
701 a perl callback called from some C function (usually via some XS 711 inside a perl callback called from some C function (usually via some
702 modules) and isn't currently executing inside some C function itself 712 XS modules) and isn't currently executing inside some C function
703 (via Coro's XS API). 713 itself (via Coro's XS API).
704 714
705 This call returns true when it could cancel the thread, or croaks 715 This call returns true when it could cancel the thread, or croaks
706 with an error otherwise (i.e. it either returns true or doesn't 716 with an error otherwise (i.e. it either returns true or doesn't
707 return at all). 717 return at all).
708 718
853 otherwise you might suffer from crashes or worse. The only event 863 otherwise you might suffer from crashes or worse. The only event
854 library currently known that is safe to use without "unblock_sub" is 864 library currently known that is safe to use without "unblock_sub" is
855 EV (but you might still run into deadlocks if all event loops are 865 EV (but you might still run into deadlocks if all event loops are
856 blocked). 866 blocked).
857 867
858 Coro will try to catch you when you block in the event loop 868 Coro will try to catch you when you block in the event loop ("FATAL:
859 ("FATAL:$Coro::IDLE blocked itself"), but this is just best effort 869 $Coro::idle blocked itself"), but this is just best effort and only
860 and only works when you do not run your own event loop. 870 works when you do not run your own event loop.
861 871
862 This function allows your callbacks to block by executing them in 872 This function allows your callbacks to block by executing them in
863 another coro where it is safe to block. One example where blocking 873 another coro where it is safe to block. One example where blocking
864 is handy is when you use the Coro::AIO functions to save results to 874 is handy is when you use the Coro::AIO functions to save results to
865 disk, for example. 875 disk, for example.
902 It is very common for a coro to wait for some callback to be called. 912 It is very common for a coro to wait for some callback to be called.
903 This occurs naturally when you use coro in an otherwise event-based 913 This occurs naturally when you use coro in an otherwise event-based
904 program, or when you use event-based libraries. 914 program, or when you use event-based libraries.
905 915
906 These typically register a callback for some event, and call that 916 These typically register a callback for some event, and call that
907 callback when the event occured. In a coro, however, you typically want 917 callback when the event occurred. In a coro, however, you typically want
908 to just wait for the event, simplyifying things. 918 to just wait for the event, simplyifying things.
909 919
910 For example "AnyEvent->child" registers a callback to be called when a 920 For example "AnyEvent->child" registers a callback to be called when a
911 specific child has exited: 921 specific child has exited:
912 922
1031 processes. What makes it so bad is that on non-windows platforms, you 1041 processes. What makes it so bad is that on non-windows platforms, you
1032 can actually take advantage of custom hardware for this purpose (as 1042 can actually take advantage of custom hardware for this purpose (as
1033 evidenced by the forks module, which gives you the (i-) threads API, 1043 evidenced by the forks module, which gives you the (i-) threads API,
1034 just much faster). 1044 just much faster).
1035 1045
1036 Sharing data is in the i-threads model is done by transfering data 1046 Sharing data is in the i-threads model is done by transferring data
1037 structures between threads using copying semantics, which is very slow - 1047 structures between threads using copying semantics, which is very slow -
1038 shared data simply does not exist. Benchmarks using i-threads which are 1048 shared data simply does not exist. Benchmarks using i-threads which are
1039 communication-intensive show extremely bad behaviour with i-threads (in 1049 communication-intensive show extremely bad behaviour with i-threads (in
1040 fact, so bad that Coro, which cannot take direct advantage of multiple 1050 fact, so bad that Coro, which cannot take direct advantage of multiple
1041 CPUs, is often orders of magnitude faster because it shares data using 1051 CPUs, is often orders of magnitude faster because it shares data using
1070 1080
1071 XS API: Coro::MakeMaker. 1081 XS API: Coro::MakeMaker.
1072 1082
1073 Low level Configuration, Thread Environment, Continuations: Coro::State. 1083 Low level Configuration, Thread Environment, Continuations: Coro::State.
1074 1084
1075AUTHOR 1085AUTHOR/SUPPORT/CONTACT
1076 Marc Lehmann <schmorp@schmorp.de> 1086 Marc A. Lehmann <schmorp@schmorp.de>
1077 http://home.schmorp.de/ 1087 http://software.schmorp.de/pkg/Coro.html
1078 1088

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines