… | |
… | |
149 | for @{ delete $self->{_on_destroy} || [] }; |
149 | for @{ delete $self->{_on_destroy} || [] }; |
150 | } |
150 | } |
151 | |
151 | |
152 | # this coroutine is necessary because a coroutine |
152 | # this coroutine is necessary because a coroutine |
153 | # cannot destroy itself. |
153 | # cannot destroy itself. |
154 | my @destroy; |
154 | our @destroy; |
155 | my $manager; |
155 | our $manager; |
156 | |
156 | |
157 | $manager = new Coro sub { |
157 | $manager = new Coro sub { |
158 | while () { |
158 | while () { |
159 | (shift @destroy)->_cancel |
159 | (shift @destroy)->_cancel |
160 | while @destroy; |
160 | while @destroy; |
… | |
… | |
340 | program calls this function, there will be some one-time resource leak. |
340 | program calls this function, there will be some one-time resource leak. |
341 | |
341 | |
342 | =cut |
342 | =cut |
343 | |
343 | |
344 | sub terminate { |
344 | sub terminate { |
345 | $current->cancel (@_); |
345 | $current->{_status} = [@_]; |
|
|
346 | push @destroy, $current; |
|
|
347 | $manager->ready; |
|
|
348 | do { &schedule } while 1; |
346 | } |
349 | } |
347 | |
350 | |
348 | sub killall { |
351 | sub killall { |
349 | for (Coro::State::list) { |
352 | for (Coro::State::list) { |
350 | $_->cancel |
353 | $_->cancel |
… | |
… | |
399 | |
402 | |
400 | =cut |
403 | =cut |
401 | |
404 | |
402 | sub cancel { |
405 | sub cancel { |
403 | my $self = shift; |
406 | my $self = shift; |
404 | $self->{_status} = [@_]; |
|
|
405 | |
407 | |
406 | if ($current == $self) { |
408 | if ($current == $self) { |
407 | push @destroy, $self; |
409 | terminate @_; |
408 | $manager->ready; |
|
|
409 | &schedule while 1; |
|
|
410 | } else { |
410 | } else { |
|
|
411 | $self->{_status} = [@_]; |
411 | $self->_cancel; |
412 | $self->_cancel; |
412 | } |
413 | } |
413 | } |
414 | } |
414 | |
415 | |
415 | =item $coroutine->throw ([$scalar]) |
416 | =item $coroutine->throw ([$scalar]) |