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