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

Comparing Coro/Coro.pm (file contents):
Revision 1.309 by root, Sat Oct 6 21:25:24 2012 UTC vs.
Revision 1.357 by root, Wed Jul 21 06:36:11 2021 UTC

72 72
73=over 4 73=over 4
74 74
75=item 1. Creation 75=item 1. Creation
76 76
77The first thing in the life of a coro thread is it's creation - 77The first thing in the life of a coro thread is its creation -
78obviously. The typical way to create a thread is to call the C<async 78obviously. The typical way to create a thread is to call the C<async
79BLOCK> function: 79BLOCK> function:
80 80
81 async { 81 async {
82 # thread code goes here 82 # thread code goes here
91This creates a new coro thread and puts it into the ready queue, meaning 91This creates a new coro thread and puts it into the ready queue, meaning
92it will run as soon as the CPU is free for it. 92it will run as soon as the CPU is free for it.
93 93
94C<async> will return a Coro object - you can store this for future 94C<async> will return a Coro object - you can store this for future
95reference or ignore it - a thread that is running, ready to run or waiting 95reference or ignore it - a thread that is running, ready to run or waiting
96for some event is alive on it's own. 96for some event is alive on its own.
97 97
98Another way to create a thread is to call the C<new> constructor with a 98Another way to create a thread is to call the C<new> constructor with a
99code-reference: 99code-reference:
100 100
101 new Coro sub { 101 new Coro sub {
188 188
189 my $hello_world = $coro->join; 189 my $hello_world = $coro->join;
190 190
191 print $hello_world; 191 print $hello_world;
192 192
193Another way to terminate is to call C<< Coro::terminate >>, which at any 193Another way to terminate is to call C<< Coro::terminate >>, which works at
194subroutine call nesting level: 194any subroutine call nesting level:
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.
236 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 its 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...
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.
241 264
242Cleanup is quite similar to throwing an uncaught exception: perl will 265Cleanup is quite similar to throwing an uncaught exception: perl will
243work it's way up through all subroutine calls and blocks. On it's way, it 266work its way up through all subroutine calls and blocks. On its way, it
244will release all C<my> variables, undo all C<local>'s and free any other 267will release all C<my> variables, undo all C<local>'s and free any other
245resources truly local to the thread. 268resources truly local to the thread.
246 269
247So, a common way to free resources is to keep them referenced only by my 270So, a common way to free resources is to keep them referenced only by my
248variables: 271variables:
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
275 # gets freed, so use a guard to ensure it's destruction 298 # gets freed, so use a guard to ensure its destruction
276 # in case of an error: 299 # in case of an error:
277 my $window_guard = Guard::guard { $window->destroy }; 300 my $window_guard = Guard::guard { $window->destroy };
278 301
279 # we are safe here 302 # we are safe here
280 }; 303 };
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.09; 371our $VERSION = 6.57;
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
474C<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>
475will 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,
476which somehow defeats the purpose of pooling (but is fine in the 500which somehow defeats the purpose of pooling (but is fine in the
477exceptional case). 501exceptional case).
478 502
479The 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
480disabled, the description will be reset and the default output filehandle 504will be undone, tracing will be disabled, the description will be reset
481gets restored, so you can change all these. Otherwise the coro will 505and the default output filehandle gets restored, so you can change all
482be 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
483stuff 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
484simply done by using local as in: C<< local $/ >>. 508that change, which is most simply done by using local as in: C<< local $/
509>>.
485 510
486The 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
487adjusted 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
488coros as required. 513coros as required.
489 514
613 # at this place, the timezone is Antarctica/South_Pole, 638 # at this place, the timezone is Antarctica/South_Pole,
614 # without disturbing the TZ of any other coro. 639 # without disturbing the TZ of any other coro.
615 }; 640 };
616 641
617This can be used to localise about any resource (locale, uid, current 642This can be used to localise about any resource (locale, uid, current
618working directory etc.) to a block, despite the existance of other 643working directory etc.) to a block, despite the existence of other
619coros. 644coros.
620 645
621Another interesting example implements time-sliced multitasking using 646Another interesting example implements time-sliced multitasking using
622interval timers (this could obviously be optimised, but does the job): 647interval timers (this could obviously be optimised, but does the job):
623 648
628 Coro::on_enter { 653 Coro::on_enter {
629 # on entering the thread, we set an VTALRM handler to cede 654 # on entering the thread, we set an VTALRM handler to cede
630 $SIG{VTALRM} = sub { cede }; 655 $SIG{VTALRM} = sub { cede };
631 # and then start the interval timer 656 # and then start the interval timer
632 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01; 657 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01;
633 }; 658 };
634 Coro::on_leave { 659 Coro::on_leave {
635 # on leaving the thread, we stop the interval timer again 660 # on leaving the thread, we stop the interval timer again
636 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0; 661 Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0;
637 }; 662 };
638 663
639 &{+shift}; 664 &{+shift};
640 } 665 }
641 666
642 # use like this: 667 # use like this:
643 timeslice { 668 timeslice {
644 # The following is an endless loop that would normally 669 # The following is an endless loop that would normally
645 # monopolise the process. Since it runs in a timesliced 670 # monopolise the process. Since it runs in a timesliced
646 # environment, it will regularly cede to other threads. 671 # environment, it will regularly cede to other threads.
647 while () { } 672 while () { }
648 }; 673 };
649 674
650 675
651=item killall 676=item killall
652 677
653Kills/terminates/cancels all coros except the currently running one. 678Kills/terminates/cancels all coros except the currently running one.
729=item $state->is_new 754=item $state->is_new
730 755
731Returns 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
732yet. 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
733the arguments, but consumes very little other resources. New states will 758the arguments, but consumes very little other resources. New states will
734automatically get assigned a perl interpreter when they are transfered to. 759automatically get assigned a perl interpreter when they are transferred to.
735 760
736=item $state->is_zombie 761=item $state->is_zombie
737 762
738Returns true iff the Coro object has been cancelled, i.e. 763Returns true iff the Coro object has been cancelled, i.e.
739it's resources freed because they were C<cancel>'ed, C<terminate>'d, 764its resources freed because they were C<cancel>'ed, C<terminate>'d,
740C<safe_cancel>'ed or simply went out of scope. 765C<safe_cancel>'ed or simply went out of scope.
741 766
742The name "zombie" stems from UNIX culture, where a process that has 767The name "zombie" stems from UNIX culture, where a process that has
743exited and only stores and exit status and no other resources is called a 768exited and only stores and exit status and no other resources is called a
744"zombie". 769"zombie".
757=item $is_suspended = $coro->is_suspended 782=item $is_suspended = $coro->is_suspended
758 783
759Returns true iff this Coro object has been suspended. Suspended Coros will 784Returns true iff this Coro object has been suspended. Suspended Coros will
760not ever be scheduled. 785not ever be scheduled.
761 786
762=item $coro->cancel (arg...) 787=item $coro->cancel ($arg...)
763 788
764Terminates 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
765status (default: an empty list). Never returns if the Coro is the 790status (default: an empty list). Never returns if the Coro is the
766current Coro. 791current Coro.
767 792
768This 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
769the 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,
770bad things can happen, or if the cancelled thread insists on running 795bad things can happen, or if the cancelled thread insists on running
771complicated cleanup handlers that rely on its thread context, things will 796complicated cleanup handlers that rely on its thread context, things will
772not work. 797not work.
773 798
774Any 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
775a 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
776plus side, C<< ->cancel >> will always clean up the thread, no matter 805On 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 806matter 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<< 807cancelling a C-thread that doesn't know how to clean up itself, it can be
779->throw >> an exception, or use C<< ->safe_cancel >>. 808better to C<< ->throw >> an exception, or use C<< ->safe_cancel >>.
780 809
781The arguments to C<< ->cancel >> are not copied, but instead will 810The arguments to C<< ->cancel >> are not copied, but instead will
782be 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
783change that variable, then you might change the return values passed to 812change that variable, then you might change the return values passed to
784e.g. C<join>, so don't do that). 813e.g. C<join>, so don't do that).
790 819
791=item $coro->safe_cancel ($arg...) 820=item $coro->safe_cancel ($arg...)
792 821
793Works mostly like C<< ->cancel >>, but is inherently "safer", and 822Works mostly like C<< ->cancel >>, but is inherently "safer", and
794consequently, 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
795cancellable state. 824cancellable state. Essentially, C<< ->safe_cancel >> is a C<< ->cancel >>
825with extra checks before canceling.
796 826
797This method works a bit like throwing an exception that cannot be caught 827It works a bit like throwing an exception that cannot be caught -
798- specifically, it will clean up the thread from within itself, so 828specifically, it will clean up the thread from within itself, so all
799all cleanup handlers (e.g. C<guard> blocks) are run with full thread 829cleanup 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 830context 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 831guarantee 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 832therefore, it might fail. It is also considerably slower than C<cancel> or
803C<terminate>. 833C<terminate>.
804 834
805A 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
806or it has no C context attached and is inside an SLF function. 837no C context attached and is inside an SLF function.
807 838
839The first two states are trivial - a thread that hasnot started or has
840already finished is safe to cancel.
841
808The 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
809perl callback called from some C function (usually via some XS modules) 843perl callback called from some C function (usually via some XS modules)
810and 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
811API). 845API).
812 846
813This 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
885return once the C<$coro> terminates. 919return once the C<$coro> terminates.
886 920
887=item $coro->on_destroy (\&cb) 921=item $coro->on_destroy (\&cb)
888 922
889Registers a callback that is called when this coro thread gets destroyed, 923Registers a callback that is called when this coro thread gets destroyed,
890that is, after it's resources have been freed but before it is joined. The 924that is, after its resources have been freed but before it is joined. The
891callback gets passed the terminate/cancel arguments, if any, and I<must 925callback gets passed the terminate/cancel arguments, if any, and I<must
892not> die, under any circumstances. 926not> die, under any circumstances.
893 927
894There 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
895no way currently to remove a callback once added. 929currently no way to remove a callback once added.
896 930
897=item $oldprio = $coro->prio ($newprio) 931=item $oldprio = $coro->prio ($newprio)
898 932
899Sets (or gets, if the argument is missing) the priority of the 933Sets (or gets, if the argument is missing) the priority of the
900coro thread. Higher priority coro get run before lower priority 934coro thread. Higher priority coro get run before lower priority
927coro 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
928coro. 962coro.
929 963
930This method simply sets the C<< $coro->{desc} >> member to the given 964This 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 965string. 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 966is often preferred to indicate major processing states that can then be
933seen for example in a L<Coro::Debug> session: 967seen for example in a L<Coro::Debug> session:
934 968
935 sub my_long_function { 969 sub my_long_function {
936 local $Coro::current->{desc} = "now in my_long_function"; 970 local $Coro::current->{desc} = "now in my_long_function";
937 ... 971 ...
992otherwise you might suffer from crashes or worse. The only event library 1026otherwise you might suffer from crashes or worse. The only event library
993currently 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
994you might still run into deadlocks if all event loops are blocked). 1028you might still run into deadlocks if all event loops are blocked).
995 1029
996Coro 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
997("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
998only works when you do not run your own event loop. 1032only works when you do not run your own event loop.
999 1033
1000This function allows your callbacks to block by executing them in another 1034This function allows your callbacks to block by executing them in another
1001coro 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
1002is 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
1051 1085
1052Create and return a "rouse callback". That's a code reference that, 1086Create and return a "rouse callback". That's a code reference that,
1053when called, will remember a copy of its arguments and notify the owner 1087when called, will remember a copy of its arguments and notify the owner
1054coro of the callback. 1088coro of the callback.
1055 1089
1090Only the first invocation will store agruments and signal any waiter -
1091further calls will effectively be ignored, but it is ok to try.
1092
1056See the next function. 1093Also see the next function.
1057 1094
1058=item @args = rouse_wait [$cb] 1095=item @args = rouse_wait [$cb]
1059 1096
1060Wait for the specified rouse callback (or the last one that was created in 1097Wait for the specified rouse callback to be invoked (or if the argument is
1061this coro). 1098missing, use the most recently created callback in the current coro).
1062 1099
1063As soon as the callback is invoked (or when the callback was invoked 1100As soon as the callback is invoked (or when the callback was invoked
1064before C<rouse_wait>), it will return the arguments originally passed to 1101before C<rouse_wait>), it will return the arguments originally passed to
1065the rouse callback. In scalar context, that means you get the I<last> 1102the rouse callback. In scalar context, that means you get the I<last>
1066argument, just as if C<rouse_wait> had a C<return ($a1, $a2, $a3...)> 1103argument, just as if C<rouse_wait> had a C<return ($a1, $a2, $a3...)>
1067statement at the end. 1104statement at the end.
1068 1105
1106You are only allowed to wait once for a given rouse callback.
1107
1069See the section B<HOW TO WAIT FOR A CALLBACK> for an actual usage example. 1108See the section B<HOW TO WAIT FOR A CALLBACK> for an actual usage example.
1109
1110As of Coro 6.57, you can reliably wait for a rouse callback in a different
1111thread than from where it was created.
1070 1112
1071=back 1113=back
1072 1114
1073=cut 1115=cut
1074 1116
1080 1122
1081 # some modules have their new predefined in State.xs, some don't 1123 # some modules have their new predefined in State.xs, some don't
1082 *{"Coro::$module\::new"} = $old 1124 *{"Coro::$module\::new"} = $old
1083 if $old; 1125 if $old;
1084 1126
1085 goto &{"Coro::$module\::new"}; 1127 goto &{"Coro::$module\::new"}
1086 }; 1128 };
1087} 1129}
1088 1130
10891; 11311;
1090 1132
1093It is very common for a coro to wait for some callback to be 1135It is very common for a coro to wait for some callback to be
1094called. This occurs naturally when you use coro in an otherwise 1136called. This occurs naturally when you use coro in an otherwise
1095event-based program, or when you use event-based libraries. 1137event-based program, or when you use event-based libraries.
1096 1138
1097These typically register a callback for some event, and call that callback 1139These typically register a callback for some event, and call that callback
1098when the event occured. In a coro, however, you typically want to 1140when the event occurred. In a coro, however, you typically want to
1099just wait for the event, simplyifying things. 1141just wait for the event, simplyifying things.
1100 1142
1101For example C<< AnyEvent->child >> registers a callback to be called when 1143For example C<< AnyEvent->child >> registers a callback to be called when
1102a specific child has exited: 1144a specific child has exited:
1103 1145
1106But from within a coro, you often just want to write this: 1148But from within a coro, you often just want to write this:
1107 1149
1108 my $status = wait_for_child $pid; 1150 my $status = wait_for_child $pid;
1109 1151
1110Coro offers two functions specifically designed to make this easy, 1152Coro offers two functions specifically designed to make this easy,
1111C<Coro::rouse_cb> and C<Coro::rouse_wait>. 1153C<rouse_cb> and C<rouse_wait>.
1112 1154
1113The first function, C<rouse_cb>, generates and returns a callback that, 1155The first function, C<rouse_cb>, generates and returns a callback that,
1114when invoked, will save its arguments and notify the coro that 1156when invoked, will save its arguments and notify the coro that
1115created the callback. 1157created the callback.
1116 1158
1122function mentioned above: 1164function mentioned above:
1123 1165
1124 sub wait_for_child($) { 1166 sub wait_for_child($) {
1125 my ($pid) = @_; 1167 my ($pid) = @_;
1126 1168
1127 my $watcher = AnyEvent->child (pid => $pid, cb => Coro::rouse_cb); 1169 my $watcher = AnyEvent->child (pid => $pid, cb => rouse_cb);
1128 1170
1129 my ($rpid, $rstatus) = Coro::rouse_wait; 1171 my ($rpid, $rstatus) = rouse_wait;
1130 $rstatus 1172 $rstatus
1131 } 1173 }
1132 1174
1133In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough, 1175In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough,
1134you can roll your own, using C<schedule>: 1176you can roll your own, using C<schedule> and C<ready>:
1135 1177
1136 sub wait_for_child($) { 1178 sub wait_for_child($) {
1137 my ($pid) = @_; 1179 my ($pid) = @_;
1138 1180
1139 # store the current coro in $current, 1181 # store the current coro in $current,
1142 my ($done, $rstatus); 1184 my ($done, $rstatus);
1143 1185
1144 # pass a closure to ->child 1186 # pass a closure to ->child
1145 my $watcher = AnyEvent->child (pid => $pid, cb => sub { 1187 my $watcher = AnyEvent->child (pid => $pid, cb => sub {
1146 $rstatus = $_[1]; # remember rstatus 1188 $rstatus = $_[1]; # remember rstatus
1147 $done = 1; # mark $rstatus as valud 1189 $done = 1; # mark $rstatus as valid
1190 $current->ready; # wake up the waiting thread
1148 }); 1191 });
1149 1192
1150 # wait until the closure has been called 1193 # wait until the closure has been called
1151 schedule while !$done; 1194 schedule while !$done;
1152 1195
1231processes. What makes it so bad is that on non-windows platforms, you can 1274processes. What makes it so bad is that on non-windows platforms, you can
1232actually take advantage of custom hardware for this purpose (as evidenced 1275actually take advantage of custom hardware for this purpose (as evidenced
1233by the forks module, which gives you the (i-) threads API, just much 1276by the forks module, which gives you the (i-) threads API, just much
1234faster). 1277faster).
1235 1278
1236Sharing data is in the i-threads model is done by transfering data 1279Sharing data is in the i-threads model is done by transferring data
1237structures between threads using copying semantics, which is very slow - 1280structures between threads using copying semantics, which is very slow -
1238shared data simply does not exist. Benchmarks using i-threads which are 1281shared data simply does not exist. Benchmarks using i-threads which are
1239communication-intensive show extremely bad behaviour with i-threads (in 1282communication-intensive show extremely bad behaviour with i-threads (in
1240fact, so bad that Coro, which cannot take direct advantage of multiple 1283fact, so bad that Coro, which cannot take direct advantage of multiple
1241CPUs, is often orders of magnitude faster because it shares data using 1284CPUs, is often orders of magnitude faster because it shares data using
1271 1314
1272XS API: L<Coro::MakeMaker>. 1315XS API: L<Coro::MakeMaker>.
1273 1316
1274Low level Configuration, Thread Environment, Continuations: L<Coro::State>. 1317Low level Configuration, Thread Environment, Continuations: L<Coro::State>.
1275 1318
1276=head1 AUTHOR 1319=head1 AUTHOR/SUPPORT/CONTACT
1277 1320
1278 Marc Lehmann <schmorp@schmorp.de> 1321 Marc A. Lehmann <schmorp@schmorp.de>
1279 http://home.schmorp.de/ 1322 http://software.schmorp.de/pkg/Coro.html
1280 1323
1281=cut 1324=cut
1282 1325

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines