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

Comparing Coro/Coro.pm (file contents):
Revision 1.316 by root, Wed Mar 6 06:00:08 2013 UTC vs.
Revision 1.349 by root, Tue Aug 14 16:51:37 2018 UTC

366 366
367our $idle; # idle handler 367our $idle; # idle handler
368our $main; # main coro 368our $main; # main coro
369our $current; # current coro 369our $current; # current coro
370 370
371our $VERSION = 6.28; 371our $VERSION = 6.52;
372 372
373our @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);
374our %EXPORT_TAGS = ( 374our %EXPORT_TAGS = (
375 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)],
376); 376);
498C<async> does. As the coro is being reused, stuff like C<on_destroy> 498C<async> does. As the coro is being reused, stuff like C<on_destroy>
499will not work in the expected way, unless you call terminate or cancel, 499will not work in the expected way, unless you call terminate or cancel,
500which somehow defeats the purpose of pooling (but is fine in the 500which somehow defeats the purpose of pooling (but is fine in the
501exceptional case). 501exceptional case).
502 502
503The priority will be reset to C<0> after each run, tracing will be 503The priority will be reset to C<0> after each run, all C<swap_sv> calls
504disabled, the description will be reset and the default output filehandle 504will be undone, tracing will be disabled, the description will be reset
505gets restored, so you can change all these. Otherwise the coro will 505and the default output filehandle gets restored, so you can change all
506be re-used "as-is": most notably if you change other per-coro global 506these. Otherwise the coro will be re-used "as-is": most notably if you
507stuff such as C<$/> you I<must needs> revert that change, which is most 507change other per-coro global stuff such as C<$/> you I<must needs> revert
508simply done by using local as in: C<< local $/ >>. 508that change, which is most simply done by using local as in: C<< local $/
509>>.
509 510
510The idle pool size is limited to C<8> idle coros (this can be 511The idle pool size is limited to C<8> idle coros (this can be
511adjusted by changing $Coro::POOL_SIZE), but there can be as many non-idle 512adjusted by changing $Coro::POOL_SIZE), but there can be as many non-idle
512coros as required. 513coros as required.
513 514
637 # at this place, the timezone is Antarctica/South_Pole, 638 # at this place, the timezone is Antarctica/South_Pole,
638 # without disturbing the TZ of any other coro. 639 # without disturbing the TZ of any other coro.
639 }; 640 };
640 641
641This can be used to localise about any resource (locale, uid, current 642This can be used to localise about any resource (locale, uid, current
642working directory etc.) to a block, despite the existance of other 643working directory etc.) to a block, despite the existence of other
643coros. 644coros.
644 645
645Another interesting example implements time-sliced multitasking using 646Another interesting example implements time-sliced multitasking using
646interval timers (this could obviously be optimised, but does the job): 647interval timers (this could obviously be optimised, but does the job):
647 648
652 Coro::on_enter { 653 Coro::on_enter {
653 # on entering the thread, we set an VTALRM handler to cede 654 # on entering the thread, we set an VTALRM handler to cede
654 $SIG{VTALRM} = sub { cede }; 655 $SIG{VTALRM} = sub { cede };
655 # and then start the interval timer 656 # and then start the interval timer
656 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01; 657 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01;
657 }; 658 };
658 Coro::on_leave { 659 Coro::on_leave {
659 # on leaving the thread, we stop the interval timer again 660 # on leaving the thread, we stop the interval timer again
660 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0; 661 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0;
661 }; 662 };
662 663
663 &{+shift}; 664 &{+shift};
664 } 665 }
665 666
666 # use like this: 667 # use like this:
667 timeslice { 668 timeslice {
668 # The following is an endless loop that would normally 669 # The following is an endless loop that would normally
669 # monopolise the process. Since it runs in a timesliced 670 # monopolise the process. Since it runs in a timesliced
670 # environment, it will regularly cede to other threads. 671 # environment, it will regularly cede to other threads.
671 while () { } 672 while () { }
672 }; 673 };
673 674
674 675
675=item killall 676=item killall
676 677
677Kills/terminates/cancels all coros except the currently running one. 678Kills/terminates/cancels all coros except the currently running one.
753=item $state->is_new 754=item $state->is_new
754 755
755Returns true iff this Coro object is "new", i.e. has never been run 756Returns true iff this Coro object is "new", i.e. has never been run
756yet. Those states basically consist of only the code reference to call and 757yet. Those states basically consist of only the code reference to call and
757the arguments, but consumes very little other resources. New states will 758the arguments, but consumes very little other resources. New states will
758automatically get assigned a perl interpreter when they are transfered to. 759automatically get assigned a perl interpreter when they are transferred to.
759 760
760=item $state->is_zombie 761=item $state->is_zombie
761 762
762Returns true iff the Coro object has been cancelled, i.e. 763Returns true iff the Coro object has been cancelled, i.e.
763it's resources freed because they were C<cancel>'ed, C<terminate>'d, 764it's resources freed because they were C<cancel>'ed, C<terminate>'d,
781=item $is_suspended = $coro->is_suspended 782=item $is_suspended = $coro->is_suspended
782 783
783Returns true iff this Coro object has been suspended. Suspended Coros will 784Returns true iff this Coro object has been suspended. Suspended Coros will
784not ever be scheduled. 785not ever be scheduled.
785 786
786=item $coro->cancel (arg...) 787=item $coro->cancel ($arg...)
787 788
788Terminates the given Coro thread and makes it return the given arguments as 789Terminate the given Coro thread and make it return the given arguments as
789status (default: an empty list). Never returns if the Coro is the 790status (default: an empty list). Never returns if the Coro is the
790current Coro. 791current Coro.
791 792
792This is a rather brutal way to free a coro, with some limitations - if 793This is a rather brutal way to free a coro, with some limitations - if
793the thread is inside a C callback that doesn't expect to be canceled, 794the thread is inside a C callback that doesn't expect to be canceled,
794bad things can happen, or if the cancelled thread insists on running 795bad things can happen, or if the cancelled thread insists on running
795complicated cleanup handlers that rely on its thread context, things will 796complicated cleanup handlers that rely on its thread context, things will
796not work. 797not work.
797 798
798Any cleanup code being run (e.g. from C<guard> blocks) will be run without 799Any cleanup code being run (e.g. from C<guard> blocks, destructors and so
799a thread context, and is not allowed to switch to other threads. On the 800on) will be run without a thread context, and is not allowed to switch
801to other threads. A common mistake is to call C<< ->cancel >> from a
802destructor called by die'ing inside the thread to be cancelled for
803example.
804
800plus side, C<< ->cancel >> will always clean up the thread, no matter 805On the plus side, C<< ->cancel >> will always clean up the thread, no
801what. If your cleanup code is complex or you want to avoid cancelling a 806matter what. If your cleanup code is complex or you want to avoid
802C-thread that doesn't know how to clean up itself, it can be better to C<< 807cancelling a C-thread that doesn't know how to clean up itself, it can be
803->throw >> an exception, or use C<< ->safe_cancel >>. 808better to C<< ->throw >> an exception, or use C<< ->safe_cancel >>.
804 809
805The arguments to C<< ->cancel >> are not copied, but instead will 810The arguments to C<< ->cancel >> are not copied, but instead will
806be referenced directly (e.g. if you pass C<$var> and after the call 811be referenced directly (e.g. if you pass C<$var> and after the call
807change that variable, then you might change the return values passed to 812change that variable, then you might change the return values passed to
808e.g. C<join>, so don't do that). 813e.g. C<join>, so don't do that).
814 819
815=item $coro->safe_cancel ($arg...) 820=item $coro->safe_cancel ($arg...)
816 821
817Works mostly like C<< ->cancel >>, but is inherently "safer", and 822Works mostly like C<< ->cancel >>, but is inherently "safer", and
818consequently, can fail with an exception in cases the thread is not in a 823consequently, can fail with an exception in cases the thread is not in a
819cancellable state. 824cancellable state. Essentially, C<< ->safe_cancel >> is a C<< ->cancel >>
825with extra checks before canceling.
820 826
821This method works a bit like throwing an exception that cannot be caught 827It works a bit like throwing an exception that cannot be caught -
822- specifically, it will clean up the thread from within itself, so 828specifically, it will clean up the thread from within itself, so all
823all cleanup handlers (e.g. C<guard> blocks) are run with full thread 829cleanup handlers (e.g. C<guard> blocks) are run with full thread
824context and can block if they wish. The downside is that there is no 830context and can block if they wish. The downside is that there is no
825guarantee that the thread can be cancelled when you call this method, and 831guarantee that the thread can be cancelled when you call this method, and
826therefore, it might fail. It is also considerably slower than C<cancel> or 832therefore, it might fail. It is also considerably slower than C<cancel> or
827C<terminate>. 833C<terminate>.
828 834
829A thread is in a safe-cancellable state if it either hasn't been run yet, 835A thread is in a safe-cancellable state if it either has never been run
836yet, has already been canceled/terminated or otherwise destroyed, or has
830or it has no C context attached and is inside an SLF function. 837no C context attached and is inside an SLF function.
831 838
839The first two states are trivial - a thread that hasnot started or has
840already finished is safe to cancel.
841
832The latter two basically mean that the thread isn't currently inside a 842The last state basically means that the thread isn't currently inside a
833perl callback called from some C function (usually via some XS modules) 843perl callback called from some C function (usually via some XS modules)
834and isn't currently executing inside some C function itself (via Coro's XS 844and isn't currently executing inside some C function itself (via Coro's XS
835API). 845API).
836 846
837This call returns true when it could cancel the thread, or croaks with an 847This call returns true when it could cancel the thread, or croaks with an
914that is, after it's resources have been freed but before it is joined. The 924that is, after it's resources have been freed but before it is joined. The
915callback gets passed the terminate/cancel arguments, if any, and I<must 925callback gets passed the terminate/cancel arguments, if any, and I<must
916not> die, under any circumstances. 926not> die, under any circumstances.
917 927
918There can be any number of C<on_destroy> callbacks per coro, and there is 928There can be any number of C<on_destroy> callbacks per coro, and there is
919no way currently to remove a callback once added. 929currently no way to remove a callback once added.
920 930
921=item $oldprio = $coro->prio ($newprio) 931=item $oldprio = $coro->prio ($newprio)
922 932
923Sets (or gets, if the argument is missing) the priority of the 933Sets (or gets, if the argument is missing) the priority of the
924coro thread. Higher priority coro get run before lower priority 934coro thread. Higher priority coro get run before lower priority
1016otherwise you might suffer from crashes or worse. The only event library 1026otherwise you might suffer from crashes or worse. The only event library
1017currently known that is safe to use without C<unblock_sub> is L<EV> (but 1027currently known that is safe to use without C<unblock_sub> is L<EV> (but
1018you might still run into deadlocks if all event loops are blocked). 1028you might still run into deadlocks if all event loops are blocked).
1019 1029
1020Coro will try to catch you when you block in the event loop 1030Coro will try to catch you when you block in the event loop
1021("FATAL:$Coro::IDLE blocked itself"), but this is just best effort and 1031("FATAL: $Coro::idle blocked itself"), but this is just best effort and
1022only works when you do not run your own event loop. 1032only works when you do not run your own event loop.
1023 1033
1024This function allows your callbacks to block by executing them in another 1034This function allows your callbacks to block by executing them in another
1025coro where it is safe to block. One example where blocking is handy 1035coro where it is safe to block. One example where blocking is handy
1026is when you use the L<Coro::AIO|Coro::AIO> functions to save results to 1036is when you use the L<Coro::AIO|Coro::AIO> functions to save results to
1117It is very common for a coro to wait for some callback to be 1127It is very common for a coro to wait for some callback to be
1118called. This occurs naturally when you use coro in an otherwise 1128called. This occurs naturally when you use coro in an otherwise
1119event-based program, or when you use event-based libraries. 1129event-based program, or when you use event-based libraries.
1120 1130
1121These typically register a callback for some event, and call that callback 1131These typically register a callback for some event, and call that callback
1122when the event occured. In a coro, however, you typically want to 1132when the event occurred. In a coro, however, you typically want to
1123just wait for the event, simplyifying things. 1133just wait for the event, simplyifying things.
1124 1134
1125For example C<< AnyEvent->child >> registers a callback to be called when 1135For example C<< AnyEvent->child >> registers a callback to be called when
1126a specific child has exited: 1136a specific child has exited:
1127 1137
1256processes. What makes it so bad is that on non-windows platforms, you can 1266processes. What makes it so bad is that on non-windows platforms, you can
1257actually take advantage of custom hardware for this purpose (as evidenced 1267actually take advantage of custom hardware for this purpose (as evidenced
1258by the forks module, which gives you the (i-) threads API, just much 1268by the forks module, which gives you the (i-) threads API, just much
1259faster). 1269faster).
1260 1270
1261Sharing data is in the i-threads model is done by transfering data 1271Sharing data is in the i-threads model is done by transferring data
1262structures between threads using copying semantics, which is very slow - 1272structures between threads using copying semantics, which is very slow -
1263shared data simply does not exist. Benchmarks using i-threads which are 1273shared data simply does not exist. Benchmarks using i-threads which are
1264communication-intensive show extremely bad behaviour with i-threads (in 1274communication-intensive show extremely bad behaviour with i-threads (in
1265fact, so bad that Coro, which cannot take direct advantage of multiple 1275fact, so bad that Coro, which cannot take direct advantage of multiple
1266CPUs, is often orders of magnitude faster because it shares data using 1276CPUs, is often orders of magnitude faster because it shares data using
1296 1306
1297XS API: L<Coro::MakeMaker>. 1307XS API: L<Coro::MakeMaker>.
1298 1308
1299Low level Configuration, Thread Environment, Continuations: L<Coro::State>. 1309Low level Configuration, Thread Environment, Continuations: L<Coro::State>.
1300 1310
1301=head1 AUTHOR 1311=head1 AUTHOR/SUPPORT/CONTACT
1302 1312
1303 Marc Lehmann <schmorp@schmorp.de> 1313 Marc A. Lehmann <schmorp@schmorp.de>
1304 http://home.schmorp.de/ 1314 http://software.schmorp.de/pkg/Coro.html
1305 1315
1306=cut 1316=cut
1307 1317

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines