ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
(Generate patch)

Comparing Coro/Coro.pm (file contents):
Revision 1.98 by root, Mon Dec 4 21:56:00 2006 UTC vs.
Revision 1.102 by root, Fri Dec 29 11:37:49 2006 UTC

50 50
51our $idle; # idle handler 51our $idle; # idle handler
52our $main; # main coroutine 52our $main; # main coroutine
53our $current; # current coroutine 53our $current; # current coroutine
54 54
55our $VERSION = '3.1'; 55our $VERSION = '3.3';
56 56
57our @EXPORT = qw(async cede schedule terminate current unblock_sub); 57our @EXPORT = qw(async cede schedule terminate current unblock_sub);
58our %EXPORT_TAGS = ( 58our %EXPORT_TAGS = (
59 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 59 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
60); 60);
153 # been readied multiple times. this is harmless since the manager 153 # been readied multiple times. this is harmless since the manager
154 # can be called as many times as neccessary and will always 154 # can be called as many times as neccessary and will always
155 # remove itself from the runqueue 155 # remove itself from the runqueue
156 while (@destroy) { 156 while (@destroy) {
157 my $coro = pop @destroy; 157 my $coro = pop @destroy;
158
158 $coro->{status} ||= []; 159 $coro->{status} ||= [];
160
159 $_->ready for @{delete $coro->{join} || []}; 161 $_->ready for @{(delete $coro->{join} ) || []};
162 $_->(@{$coro->{status}}) for @{(delete $coro->{destroy_cb}) || []};
160 163
161 # the next line destroys the coro state, but keeps the 164 # the next line destroys the coro state, but keeps the
162 # coroutine itself intact (we basically make it a zombie 165 # coroutine itself intact (we basically make it a zombie
163 # coroutine that always runs the manager thread, so it's possible 166 # coroutine that always runs the manager thread, so it's possible
164 # to transfer() to this coroutine). 167 # to transfer() to this coroutine).
232 235
233"Cede" to other coroutines. This function puts the current coroutine into the 236"Cede" to other coroutines. This function puts the current coroutine into the
234ready queue and calls C<schedule>, which has the effect of giving up the 237ready queue and calls C<schedule>, which has the effect of giving up the
235current "timeslice" to other coroutines of the same or higher priority. 238current "timeslice" to other coroutines of the same or higher priority.
236 239
240=item Coro::cede_notself
241
242Works like cede, but is not exported by default and will cede to any
243coroutine, regardless of priority, once.
244
237=item terminate [arg...] 245=item terminate [arg...]
238 246
239Terminates the current coroutine with the given status values (see L<cancel>). 247Terminates the current coroutine with the given status values (see L<cancel>).
240 248
241=cut 249=cut
313 unless ($self->{status}) { 321 unless ($self->{status}) {
314 push @{$self->{join}}, $current; 322 push @{$self->{join}}, $current;
315 &schedule; 323 &schedule;
316 } 324 }
317 wantarray ? @{$self->{status}} : $self->{status}[0]; 325 wantarray ? @{$self->{status}} : $self->{status}[0];
326}
327
328=item $coroutine->on_destroy (\&cb)
329
330Registers a callback that is called when this coroutine gets destroyed,
331but before it is joined. The callback gets passed the terminate arguments,
332if any.
333
334=cut
335
336sub on_destroy {
337 my ($self, $cb) = @_;
338
339 push @{ $self->{destroy_cb} }, $cb;
318} 340}
319 341
320=item $oldprio = $coroutine->prio ($newprio) 342=item $oldprio = $coroutine->prio ($newprio)
321 343
322Sets (or gets, if the argument is missing) the priority of the 344Sets (or gets, if the argument is missing) the priority of the

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines