… | |
… | |
50 | |
50 | |
51 | our $idle; # idle handler |
51 | our $idle; # idle handler |
52 | our $main; # main coroutine |
52 | our $main; # main coroutine |
53 | our $current; # current coroutine |
53 | our $current; # current coroutine |
54 | |
54 | |
55 | our $VERSION = '3.11'; |
55 | our $VERSION = '3.3'; |
56 | |
56 | |
57 | our @EXPORT = qw(async cede schedule terminate current unblock_sub); |
57 | our @EXPORT = qw(async cede schedule terminate current unblock_sub); |
58 | our %EXPORT_TAGS = ( |
58 | our %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 |
234 | ready queue and calls C<schedule>, which has the effect of giving up the |
237 | ready queue and calls C<schedule>, which has the effect of giving up the |
235 | current "timeslice" to other coroutines of the same or higher priority. |
238 | current "timeslice" to other coroutines of the same or higher priority. |
236 | |
239 | |
|
|
240 | =item Coro::cede_notself |
|
|
241 | |
|
|
242 | Works like cede, but is not exported by default and will cede to any |
|
|
243 | coroutine, regardless of priority, once. |
|
|
244 | |
237 | =item terminate [arg...] |
245 | =item terminate [arg...] |
238 | |
246 | |
239 | Terminates the current coroutine with the given status values (see L<cancel>). |
247 | Terminates 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 | |
|
|
330 | Registers a callback that is called when this coroutine gets destroyed, |
|
|
331 | but before it is joined. The callback gets passed the terminate arguments, |
|
|
332 | if any. |
|
|
333 | |
|
|
334 | =cut |
|
|
335 | |
|
|
336 | sub 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 | |
322 | Sets (or gets, if the argument is missing) the priority of the |
344 | Sets (or gets, if the argument is missing) the priority of the |