… | |
… | |
342 | |
342 | |
343 | our $idle; # idle handler |
343 | our $idle; # idle handler |
344 | our $main; # main coro |
344 | our $main; # main coro |
345 | our $current; # current coro |
345 | our $current; # current coro |
346 | |
346 | |
347 | our $VERSION = 5.372; |
347 | our $VERSION = 6.23; |
348 | |
348 | |
349 | 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); |
350 | our %EXPORT_TAGS = ( |
350 | our %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 | ); |
… | |
… | |
357 | =over 4 |
357 | =over 4 |
358 | |
358 | |
359 | =item $Coro::main |
359 | =item $Coro::main |
360 | |
360 | |
361 | This variable stores the Coro object that represents the main |
361 | This variable stores the Coro object that represents the main |
362 | program. While you cna C<ready> it and do most other things you can do to |
362 | program. While you can C<ready> it and do most other things you can do to |
363 | coro, it is mainly useful to compare again C<$Coro::current>, to see |
363 | coro, it is mainly useful to compare again C<$Coro::current>, to see |
364 | whether you are running in the main program or not. |
364 | whether you are running in the main program or not. |
365 | |
365 | |
366 | =cut |
366 | =cut |
367 | |
367 | |
… | |
… | |
766 | current Coro. |
766 | current Coro. |
767 | |
767 | |
768 | 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 |
769 | 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, |
770 | 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 |
771 | complicated cleanup handlers that rely on it'S thread context, things will |
771 | complicated cleanup handlers that rely on its thread context, things will |
772 | not work. |
772 | not work. |
773 | |
773 | |
774 | 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 |
775 | 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 |
776 | 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 |
… | |
… | |
927 | coro thread. This is just a free-form string you can associate with a |
927 | coro thread. This is just a free-form string you can associate with a |
928 | coro. |
928 | coro. |
929 | |
929 | |
930 | This method simply sets the C<< $coro->{desc} >> member to the given |
930 | This method simply sets the C<< $coro->{desc} >> member to the given |
931 | string. You can modify this member directly if you wish, and in fact, this |
931 | string. You can modify this member directly if you wish, and in fact, this |
932 | is often preferred to indicate major processing states that cna then be |
932 | is often preferred to indicate major processing states that can then be |
933 | seen for example in a L<Coro::Debug> session: |
933 | seen for example in a L<Coro::Debug> session: |
934 | |
934 | |
935 | sub my_long_function { |
935 | sub my_long_function { |
936 | local $Coro::current->{desc} = "now in my_long_function"; |
936 | local $Coro::current->{desc} = "now in my_long_function"; |
937 | ... |
937 | ... |
… | |
… | |
1129 | my ($rpid, $rstatus) = Coro::rouse_wait; |
1129 | my ($rpid, $rstatus) = Coro::rouse_wait; |
1130 | $rstatus |
1130 | $rstatus |
1131 | } |
1131 | } |
1132 | |
1132 | |
1133 | 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, |
1134 | you can roll your own, using C<schedule>: |
1134 | you 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 | |