… | |
… | |
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.2'; |
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). |
… | |
… | |
313 | unless ($self->{status}) { |
316 | unless ($self->{status}) { |
314 | push @{$self->{join}}, $current; |
317 | push @{$self->{join}}, $current; |
315 | &schedule; |
318 | &schedule; |
316 | } |
319 | } |
317 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
320 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
|
|
321 | } |
|
|
322 | |
|
|
323 | =item $coroutine->on_destroy (\&cb) |
|
|
324 | |
|
|
325 | Registers a callback that is called when this coroutine gets destroyed, |
|
|
326 | but before it is joined. The callback gets passed the terminate arguments, |
|
|
327 | if any. |
|
|
328 | |
|
|
329 | =cut |
|
|
330 | |
|
|
331 | sub on_destroy { |
|
|
332 | my ($self, $cb) = @_; |
|
|
333 | |
|
|
334 | push @{ $self->{destroy_cb} }, $cb; |
318 | } |
335 | } |
319 | |
336 | |
320 | =item $oldprio = $coroutine->prio ($newprio) |
337 | =item $oldprio = $coroutine->prio ($newprio) |
321 | |
338 | |
322 | Sets (or gets, if the argument is missing) the priority of the |
339 | Sets (or gets, if the argument is missing) the priority of the |