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

Comparing cvsroot/Coro/Coro.pm (file contents):
Revision 1.298 by root, Sat May 21 16:35:38 2011 UTC vs.
Revision 1.311 by root, Fri Nov 30 08:12:37 2012 UTC

342 342
343our $idle; # idle handler 343our $idle; # idle handler
344our $main; # main coro 344our $main; # main coro
345our $current; # current coro 345our $current; # current coro
346 346
347our $VERSION = 5.372; 347our $VERSION = "6.10";
348 348
349our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); 349our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait);
350our %EXPORT_TAGS = ( 350our %EXPORT_TAGS = (
351 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 351 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
352); 352);
766current Coro. 766current Coro.
767 767
768This is a rather brutal way to free a coro, with some limitations - if 768This 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, 769the 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 770bad things can happen, or if the cancelled thread insists on running
771complicated cleanup handlers that rely on it'S thread context, things will 771complicated cleanup handlers that rely on its thread context, things will
772not work. 772not work.
773 773
774Any cleanup code being run (e.g. from C<guard> blocks) will be run without 774Any cleanup code being run (e.g. from C<guard> blocks) will be run without
775a thread context, and is not allowed to switch to other threads. On the 775a thread context, and is not allowed to switch to other threads. On the
776plus side, C<< ->cancel >> will always clean up the thread, no matter 776plus side, C<< ->cancel >> will always clean up the thread, no matter
1129 my ($rpid, $rstatus) = Coro::rouse_wait; 1129 my ($rpid, $rstatus) = Coro::rouse_wait;
1130 $rstatus 1130 $rstatus
1131 } 1131 }
1132 1132
1133In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough, 1133In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough,
1134you can roll your own, using C<schedule>: 1134you can roll your own, using C<schedule> and C<ready>:
1135 1135
1136 sub wait_for_child($) { 1136 sub wait_for_child($) {
1137 my ($pid) = @_; 1137 my ($pid) = @_;
1138 1138
1139 # store the current coro in $current, 1139 # store the current coro in $current,
1142 my ($done, $rstatus); 1142 my ($done, $rstatus);
1143 1143
1144 # pass a closure to ->child 1144 # pass a closure to ->child
1145 my $watcher = AnyEvent->child (pid => $pid, cb => sub { 1145 my $watcher = AnyEvent->child (pid => $pid, cb => sub {
1146 $rstatus = $_[1]; # remember rstatus 1146 $rstatus = $_[1]; # remember rstatus
1147 $done = 1; # mark $rstatus as valud 1147 $done = 1; # mark $rstatus as valid
1148 $current->ready; # wake up the waiting thread
1148 }); 1149 });
1149 1150
1150 # wait until the closure has been called 1151 # wait until the closure has been called
1151 schedule while !$done; 1152 schedule while !$done;
1152 1153

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines