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

Comparing Coro/Coro.pm (file contents):
Revision 1.297 by root, Thu May 12 23:55:39 2011 UTC vs.
Revision 1.349 by root, Tue Aug 14 16:51:37 2018 UTC

16 cede; # yield to coro 16 cede; # yield to coro
17 print "3\n"; 17 print "3\n";
18 cede; # and again 18 cede; # and again
19 19
20 # use locking 20 # use locking
21 use Coro::Semaphore;
22 my $lock = new Coro::Semaphore; 21 my $lock = new Coro::Semaphore;
23 my $locked; 22 my $locked;
24 23
25 $lock->down; 24 $lock->down;
26 $locked = 1; 25 $locked = 1;
196 195
197 async { 196 async {
198 Coro::terminate "return value 1", "return value 2"; 197 Coro::terminate "return value 1", "return value 2";
199 }; 198 };
200 199
201And 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
202coro thread from another thread: 201thread from another thread:
203 202
204 my $coro = async { 203 my $coro = async {
205 exit 1; 204 exit 1;
206 }; 205 };
207 206
219So, 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
220best idea, but any other combination that deals with perl only (cancelling 219best idea, but any other combination that deals with perl only (cancelling
221when 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
222safe. 221safe.
223 222
224Lastly, 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<<
225automatically - just like other objects in Perl. This is not such a common 224->cancel >>'ed automatically - just like other objects in Perl. This
226case, however - a running thread is referencedy b C<$Coro::current>, a 225is not such a common case, however - a running thread is referencedy by
227thread 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,
228on 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
229on. 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:
230 230
231 async { 231 async {
232 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
233 }; 233 };
234 234
235 cede; 235 cede;
236 # 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...
237 259
238=item 5. Cleanup 260=item 5. Cleanup
239 261
240Threads will allocate various resources. Most but not all will be returned 262Threads will allocate various resources. Most but not all will be returned
241when a thread terminates, during clean-up. 263when a thread terminates, during clean-up.
260 282
261 my $sem = new Coro::Semaphore; 283 my $sem = new Coro::Semaphore;
262 284
263 async { 285 async {
264 my $lock_guard = $sem->guard; 286 my $lock_guard = $sem->guard;
265 # if we reutrn, or die or get cancelled, here, 287 # if we return, or die or get cancelled, here,
266 # then the semaphore will be "up"ed. 288 # then the semaphore will be "up"ed.
267 }; 289 };
268 290
269The 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
270might 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
271code blocks): 293code blocks):
272 294
273 async { 295 async {
274 my $window = new Gtk2::Window "toplevel"; 296 my $window = new Gtk2::Window "toplevel";
275 # The window will not be cleaned up automatically, even when $window 297 # The window will not be cleaned up automatically, even when $window
292=item 6. Viva La Zombie Muerte 314=item 6. Viva La Zombie Muerte
293 315
294Even 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
295object still is there and stores the return values of the thread. 317object still is there and stores the return values of the thread.
296 318
297The means the Coro object gets freed automatically when the thread has 319When there are no other references, it will simply be cleaned up and
298terminated and cleaned up and there arenot other references. 320freed.
299 321
300If 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
301->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:
302 325
303 async { 326 async {
304 print "hi\n"; 327 print "hi\n";
305 1 328 1
306 }; 329 };
343 366
344our $idle; # idle handler 367our $idle; # idle handler
345our $main; # main coro 368our $main; # main coro
346our $current; # current coro 369our $current; # current coro
347 370
348our $VERSION = 5.372; 371our $VERSION = 6.52;
349 372
350our @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);
351our %EXPORT_TAGS = ( 374our %EXPORT_TAGS = (
352 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)],
353); 376);
358=over 4 381=over 4
359 382
360=item $Coro::main 383=item $Coro::main
361 384
362This variable stores the Coro object that represents the main 385This variable stores the Coro object that represents the main
363program. 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
364coro, 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
365whether you are running in the main program or not. 388whether you are running in the main program or not.
366 389
367=cut 390=cut
368 391
475C<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>
476will 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,
477which somehow defeats the purpose of pooling (but is fine in the 500which somehow defeats the purpose of pooling (but is fine in the
478exceptional case). 501exceptional case).
479 502
480The 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
481disabled, the description will be reset and the default output filehandle 504will be undone, tracing will be disabled, the description will be reset
482gets restored, so you can change all these. Otherwise the coro will 505and the default output filehandle gets restored, so you can change all
483be 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
484stuff 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
485simply done by using local as in: C<< local $/ >>. 508that change, which is most simply done by using local as in: C<< local $/
509>>.
486 510
487The 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
488adjusted 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
489coros as required. 513coros as required.
490 514
614 # at this place, the timezone is Antarctica/South_Pole, 638 # at this place, the timezone is Antarctica/South_Pole,
615 # without disturbing the TZ of any other coro. 639 # without disturbing the TZ of any other coro.
616 }; 640 };
617 641
618This can be used to localise about any resource (locale, uid, current 642This can be used to localise about any resource (locale, uid, current
619working directory etc.) to a block, despite the existance of other 643working directory etc.) to a block, despite the existence of other
620coros. 644coros.
621 645
622Another interesting example implements time-sliced multitasking using 646Another interesting example implements time-sliced multitasking using
623interval timers (this could obviously be optimised, but does the job): 647interval timers (this could obviously be optimised, but does the job):
624 648
629 Coro::on_enter { 653 Coro::on_enter {
630 # on entering the thread, we set an VTALRM handler to cede 654 # on entering the thread, we set an VTALRM handler to cede
631 $SIG{VTALRM} = sub { cede }; 655 $SIG{VTALRM} = sub { cede };
632 # and then start the interval timer 656 # and then start the interval timer
633 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01; 657 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01;
634 }; 658 };
635 Coro::on_leave { 659 Coro::on_leave {
636 # on leaving the thread, we stop the interval timer again 660 # on leaving the thread, we stop the interval timer again
637 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0; 661 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0;
638 }; 662 };
639 663
640 &{+shift}; 664 &{+shift};
641 } 665 }
642 666
643 # use like this: 667 # use like this:
644 timeslice { 668 timeslice {
645 # The following is an endless loop that would normally 669 # The following is an endless loop that would normally
646 # monopolise the process. Since it runs in a timesliced 670 # monopolise the process. Since it runs in a timesliced
647 # environment, it will regularly cede to other threads. 671 # environment, it will regularly cede to other threads.
648 while () { } 672 while () { }
649 }; 673 };
650 674
651 675
652=item killall 676=item killall
653 677
654Kills/terminates/cancels all coros except the currently running one. 678Kills/terminates/cancels all coros except the currently running one.
730=item $state->is_new 754=item $state->is_new
731 755
732Returns 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
733yet. 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
734the arguments, but consumes very little other resources. New states will 758the arguments, but consumes very little other resources. New states will
735automatically get assigned a perl interpreter when they are transfered to. 759automatically get assigned a perl interpreter when they are transferred to.
736 760
737=item $state->is_zombie 761=item $state->is_zombie
738 762
739Returns true iff the Coro object has been cancelled, i.e. 763Returns true iff the Coro object has been cancelled, i.e.
740it'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,
758=item $is_suspended = $coro->is_suspended 782=item $is_suspended = $coro->is_suspended
759 783
760Returns true iff this Coro object has been suspended. Suspended Coros will 784Returns true iff this Coro object has been suspended. Suspended Coros will
761not ever be scheduled. 785not ever be scheduled.
762 786
763=item $coro->cancel (arg...) 787=item $coro->cancel ($arg...)
764 788
765Terminates 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
766status (default: an empty list). Never returns if the Coro is the 790status (default: an empty list). Never returns if the Coro is the
767current Coro. 791current Coro.
768 792
769This 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
770the 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,
771bad things can happen, or if the cancelled thread insists on running 795bad things can happen, or if the cancelled thread insists on running
772complicated cleanup handlers that rely on it'S thread context, things will 796complicated cleanup handlers that rely on its thread context, things will
773not work. 797not work.
774 798
775Any 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
776a 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
777plus side, C<< ->cancel >> will always clean up the thread, no matter 805On the plus side, C<< ->cancel >> will always clean up the thread, no
778what. 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
779C-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
780->throw >> an exception, or use C<< ->safe_cancel >>. 808better to C<< ->throw >> an exception, or use C<< ->safe_cancel >>.
781 809
782The arguments to C<< ->cancel >> are not copied, but instead will 810The arguments to C<< ->cancel >> are not copied, but instead will
783be 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
784change that variable, then you might change the return values passed to 812change that variable, then you might change the return values passed to
785e.g. C<join>, so don't do that). 813e.g. C<join>, so don't do that).
791 819
792=item $coro->safe_cancel ($arg...) 820=item $coro->safe_cancel ($arg...)
793 821
794Works mostly like C<< ->cancel >>, but is inherently "safer", and 822Works mostly like C<< ->cancel >>, but is inherently "safer", and
795consequently, 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
796cancellable state. 824cancellable state. Essentially, C<< ->safe_cancel >> is a C<< ->cancel >>
825with extra checks before canceling.
797 826
798This method works a bit like throwing an exception that cannot be caught 827It works a bit like throwing an exception that cannot be caught -
799- specifically, it will clean up the thread from within itself, so 828specifically, it will clean up the thread from within itself, so all
800all cleanup handlers (e.g. C<guard> blocks) are run with full thread 829cleanup handlers (e.g. C<guard> blocks) are run with full thread
801context 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
802guarantee 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
803therefore, 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
804C<terminate>. 833C<terminate>.
805 834
806A 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
807or it has no C context attached and is inside an SLF function. 837no C context attached and is inside an SLF function.
808 838
839The first two states are trivial - a thread that hasnot started or has
840already finished is safe to cancel.
841
809The 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
810perl callback called from some C function (usually via some XS modules) 843perl callback called from some C function (usually via some XS modules)
811and 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
812API). 845API).
813 846
814This 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
891that 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
892callback gets passed the terminate/cancel arguments, if any, and I<must 925callback gets passed the terminate/cancel arguments, if any, and I<must
893not> die, under any circumstances. 926not> die, under any circumstances.
894 927
895There 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
896no way currently to remove a callback once added. 929currently no way to remove a callback once added.
897 930
898=item $oldprio = $coro->prio ($newprio) 931=item $oldprio = $coro->prio ($newprio)
899 932
900Sets (or gets, if the argument is missing) the priority of the 933Sets (or gets, if the argument is missing) the priority of the
901coro thread. Higher priority coro get run before lower priority 934coro thread. Higher priority coro get run before lower priority
928coro thread. This is just a free-form string you can associate with a 961coro thread. This is just a free-form string you can associate with a
929coro. 962coro.
930 963
931This method simply sets the C<< $coro->{desc} >> member to the given 964This method simply sets the C<< $coro->{desc} >> member to the given
932string. You can modify this member directly if you wish, and in fact, this 965string. You can modify this member directly if you wish, and in fact, this
933is often preferred to indicate major processing states that cna then be 966is often preferred to indicate major processing states that can then be
934seen for example in a L<Coro::Debug> session: 967seen for example in a L<Coro::Debug> session:
935 968
936 sub my_long_function { 969 sub my_long_function {
937 local $Coro::current->{desc} = "now in my_long_function"; 970 local $Coro::current->{desc} = "now in my_long_function";
938 ... 971 ...
993otherwise you might suffer from crashes or worse. The only event library 1026otherwise you might suffer from crashes or worse. The only event library
994currently 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
995you might still run into deadlocks if all event loops are blocked). 1028you might still run into deadlocks if all event loops are blocked).
996 1029
997Coro 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
998("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
999only works when you do not run your own event loop. 1032only works when you do not run your own event loop.
1000 1033
1001This function allows your callbacks to block by executing them in another 1034This function allows your callbacks to block by executing them in another
1002coro 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
1003is 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
1094It 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
1095called. This occurs naturally when you use coro in an otherwise 1128called. This occurs naturally when you use coro in an otherwise
1096event-based program, or when you use event-based libraries. 1129event-based program, or when you use event-based libraries.
1097 1130
1098These typically register a callback for some event, and call that callback 1131These typically register a callback for some event, and call that callback
1099when the event occured. In a coro, however, you typically want to 1132when the event occurred. In a coro, however, you typically want to
1100just wait for the event, simplyifying things. 1133just wait for the event, simplyifying things.
1101 1134
1102For example C<< AnyEvent->child >> registers a callback to be called when 1135For example C<< AnyEvent->child >> registers a callback to be called when
1103a specific child has exited: 1136a specific child has exited:
1104 1137
1107But from within a coro, you often just want to write this: 1140But from within a coro, you often just want to write this:
1108 1141
1109 my $status = wait_for_child $pid; 1142 my $status = wait_for_child $pid;
1110 1143
1111Coro offers two functions specifically designed to make this easy, 1144Coro offers two functions specifically designed to make this easy,
1112C<Coro::rouse_cb> and C<Coro::rouse_wait>. 1145C<rouse_cb> and C<rouse_wait>.
1113 1146
1114The first function, C<rouse_cb>, generates and returns a callback that, 1147The first function, C<rouse_cb>, generates and returns a callback that,
1115when invoked, will save its arguments and notify the coro that 1148when invoked, will save its arguments and notify the coro that
1116created the callback. 1149created the callback.
1117 1150
1123function mentioned above: 1156function mentioned above:
1124 1157
1125 sub wait_for_child($) { 1158 sub wait_for_child($) {
1126 my ($pid) = @_; 1159 my ($pid) = @_;
1127 1160
1128 my $watcher = AnyEvent->child (pid => $pid, cb => Coro::rouse_cb); 1161 my $watcher = AnyEvent->child (pid => $pid, cb => rouse_cb);
1129 1162
1130 my ($rpid, $rstatus) = Coro::rouse_wait; 1163 my ($rpid, $rstatus) = rouse_wait;
1131 $rstatus 1164 $rstatus
1132 } 1165 }
1133 1166
1134In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough, 1167In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough,
1135you can roll your own, using C<schedule>: 1168you can roll your own, using C<schedule> and C<ready>:
1136 1169
1137 sub wait_for_child($) { 1170 sub wait_for_child($) {
1138 my ($pid) = @_; 1171 my ($pid) = @_;
1139 1172
1140 # store the current coro in $current, 1173 # store the current coro in $current,
1143 my ($done, $rstatus); 1176 my ($done, $rstatus);
1144 1177
1145 # pass a closure to ->child 1178 # pass a closure to ->child
1146 my $watcher = AnyEvent->child (pid => $pid, cb => sub { 1179 my $watcher = AnyEvent->child (pid => $pid, cb => sub {
1147 $rstatus = $_[1]; # remember rstatus 1180 $rstatus = $_[1]; # remember rstatus
1148 $done = 1; # mark $rstatus as valud 1181 $done = 1; # mark $rstatus as valid
1182 $current->ready; # wake up the waiting thread
1149 }); 1183 });
1150 1184
1151 # wait until the closure has been called 1185 # wait until the closure has been called
1152 schedule while !$done; 1186 schedule while !$done;
1153 1187
1232processes. 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
1233actually take advantage of custom hardware for this purpose (as evidenced 1267actually take advantage of custom hardware for this purpose (as evidenced
1234by 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
1235faster). 1269faster).
1236 1270
1237Sharing 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
1238structures between threads using copying semantics, which is very slow - 1272structures between threads using copying semantics, which is very slow -
1239shared data simply does not exist. Benchmarks using i-threads which are 1273shared data simply does not exist. Benchmarks using i-threads which are
1240communication-intensive show extremely bad behaviour with i-threads (in 1274communication-intensive show extremely bad behaviour with i-threads (in
1241fact, so bad that Coro, which cannot take direct advantage of multiple 1275fact, so bad that Coro, which cannot take direct advantage of multiple
1242CPUs, is often orders of magnitude faster because it shares data using 1276CPUs, is often orders of magnitude faster because it shares data using
1272 1306
1273XS API: L<Coro::MakeMaker>. 1307XS API: L<Coro::MakeMaker>.
1274 1308
1275Low level Configuration, Thread Environment, Continuations: L<Coro::State>. 1309Low level Configuration, Thread Environment, Continuations: L<Coro::State>.
1276 1310
1277=head1 AUTHOR 1311=head1 AUTHOR/SUPPORT/CONTACT
1278 1312
1279 Marc Lehmann <schmorp@schmorp.de> 1313 Marc A. Lehmann <schmorp@schmorp.de>
1280 http://home.schmorp.de/ 1314 http://software.schmorp.de/pkg/Coro.html
1281 1315
1282=cut 1316=cut
1283 1317

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines