… | |
… | |
316 | }; |
316 | }; |
317 | |
317 | |
318 | GLOBAL VARIABLES |
318 | GLOBAL VARIABLES |
319 | $Coro::main |
319 | $Coro::main |
320 | This variable stores the Coro object that represents the main |
320 | This variable stores the Coro object that represents the main |
321 | program. While you cna "ready" it and do most other things you can |
321 | program. While you can "ready" it and do most other things you can |
322 | do to coro, it is mainly useful to compare again $Coro::current, to |
322 | do to coro, it is mainly useful to compare again $Coro::current, to |
323 | see whether you are running in the main program or not. |
323 | see whether you are running in the main program or not. |
324 | |
324 | |
325 | $Coro::current |
325 | $Coro::current |
326 | The Coro object representing the current coro (the last coro that |
326 | The Coro object representing the current coro (the last coro that |
… | |
… | |
790 | with a coro. |
790 | with a coro. |
791 | |
791 | |
792 | This method simply sets the "$coro->{desc}" member to the given |
792 | This method simply sets the "$coro->{desc}" member to the given |
793 | string. You can modify this member directly if you wish, and in |
793 | string. You can modify this member directly if you wish, and in |
794 | fact, this is often preferred to indicate major processing states |
794 | fact, this is often preferred to indicate major processing states |
795 | that cna then be seen for example in a Coro::Debug session: |
795 | that can then be seen for example in a Coro::Debug session: |
796 | |
796 | |
797 | sub my_long_function { |
797 | sub my_long_function { |
798 | local $Coro::current->{desc} = "now in my_long_function"; |
798 | local $Coro::current->{desc} = "now in my_long_function"; |
799 | ... |
799 | ... |
800 | $Coro::current->{desc} = "my_long_function: phase 1"; |
800 | $Coro::current->{desc} = "my_long_function: phase 1"; |
… | |
… | |
914 | my ($rpid, $rstatus) = Coro::rouse_wait; |
914 | my ($rpid, $rstatus) = Coro::rouse_wait; |
915 | $rstatus |
915 | $rstatus |
916 | } |
916 | } |
917 | |
917 | |
918 | In the case where "rouse_cb" and "rouse_wait" are not flexible enough, |
918 | In the case where "rouse_cb" and "rouse_wait" are not flexible enough, |
919 | you can roll your own, using "schedule": |
919 | you can roll your own, using "schedule" and "ready": |
920 | |
920 | |
921 | sub wait_for_child($) { |
921 | sub wait_for_child($) { |
922 | my ($pid) = @_; |
922 | my ($pid) = @_; |
923 | |
923 | |
924 | # store the current coro in $current, |
924 | # store the current coro in $current, |
… | |
… | |
927 | my ($done, $rstatus); |
927 | my ($done, $rstatus); |
928 | |
928 | |
929 | # pass a closure to ->child |
929 | # pass a closure to ->child |
930 | my $watcher = AnyEvent->child (pid => $pid, cb => sub { |
930 | my $watcher = AnyEvent->child (pid => $pid, cb => sub { |
931 | $rstatus = $_[1]; # remember rstatus |
931 | $rstatus = $_[1]; # remember rstatus |
932 | $done = 1; # mark $rstatus as valud |
932 | $done = 1; # mark $rstatus as valid |
|
|
933 | $current->ready; # wake up the waiting thread |
933 | }); |
934 | }); |
934 | |
935 | |
935 | # wait until the closure has been called |
936 | # wait until the closure has been called |
936 | schedule while !$done; |
937 | schedule while !$done; |
937 | |
938 | |