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.297 by root, Thu May 12 23:55:39 2011 UTC vs.
Revision 1.313 by root, Fri Dec 7 22:37:24 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines