… | |
… | |
366 | |
366 | |
367 | our $idle; # idle handler |
367 | our $idle; # idle handler |
368 | our $main; # main coro |
368 | our $main; # main coro |
369 | our $current; # current coro |
369 | our $current; # current coro |
370 | |
370 | |
371 | our $VERSION = 6.23; |
371 | our $VERSION = 6.33; |
372 | |
372 | |
373 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); |
373 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); |
374 | our %EXPORT_TAGS = ( |
374 | our %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 | ); |
… | |
… | |
914 | that is, after it's resources have been freed but before it is joined. The |
914 | that is, after it's resources have been freed but before it is joined. The |
915 | callback gets passed the terminate/cancel arguments, if any, and I<must |
915 | callback gets passed the terminate/cancel arguments, if any, and I<must |
916 | not> die, under any circumstances. |
916 | not> die, under any circumstances. |
917 | |
917 | |
918 | There can be any number of C<on_destroy> callbacks per coro, and there is |
918 | There can be any number of C<on_destroy> callbacks per coro, and there is |
919 | no way currently to remove a callback once added. |
919 | currently no way to remove a callback once added. |
920 | |
920 | |
921 | =item $oldprio = $coro->prio ($newprio) |
921 | =item $oldprio = $coro->prio ($newprio) |
922 | |
922 | |
923 | Sets (or gets, if the argument is missing) the priority of the |
923 | Sets (or gets, if the argument is missing) the priority of the |
924 | coro thread. Higher priority coro get run before lower priority |
924 | coro thread. Higher priority coro get run before lower priority |
… | |
… | |
1130 | But from within a coro, you often just want to write this: |
1130 | But from within a coro, you often just want to write this: |
1131 | |
1131 | |
1132 | my $status = wait_for_child $pid; |
1132 | my $status = wait_for_child $pid; |
1133 | |
1133 | |
1134 | Coro offers two functions specifically designed to make this easy, |
1134 | Coro offers two functions specifically designed to make this easy, |
1135 | C<Coro::rouse_cb> and C<Coro::rouse_wait>. |
1135 | C<rouse_cb> and C<rouse_wait>. |
1136 | |
1136 | |
1137 | The first function, C<rouse_cb>, generates and returns a callback that, |
1137 | The first function, C<rouse_cb>, generates and returns a callback that, |
1138 | when invoked, will save its arguments and notify the coro that |
1138 | when invoked, will save its arguments and notify the coro that |
1139 | created the callback. |
1139 | created the callback. |
1140 | |
1140 | |
… | |
… | |
1146 | function mentioned above: |
1146 | function mentioned above: |
1147 | |
1147 | |
1148 | sub wait_for_child($) { |
1148 | sub wait_for_child($) { |
1149 | my ($pid) = @_; |
1149 | my ($pid) = @_; |
1150 | |
1150 | |
1151 | my $watcher = AnyEvent->child (pid => $pid, cb => Coro::rouse_cb); |
1151 | my $watcher = AnyEvent->child (pid => $pid, cb => rouse_cb); |
1152 | |
1152 | |
1153 | my ($rpid, $rstatus) = Coro::rouse_wait; |
1153 | my ($rpid, $rstatus) = rouse_wait; |
1154 | $rstatus |
1154 | $rstatus |
1155 | } |
1155 | } |
1156 | |
1156 | |
1157 | In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough, |
1157 | In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough, |
1158 | you can roll your own, using C<schedule> and C<ready>: |
1158 | you can roll your own, using C<schedule> and C<ready>: |