… | |
… | |
38 | |
38 | |
39 | use vars qw($idle $main $current); |
39 | use vars qw($idle $main $current); |
40 | |
40 | |
41 | use base Exporter; |
41 | use base Exporter; |
42 | |
42 | |
43 | $VERSION = "0.9"; |
43 | $VERSION = 0.96; |
44 | |
44 | |
45 | @EXPORT = qw(async cede schedule terminate current); |
45 | @EXPORT = qw(async cede schedule terminate current); |
46 | %EXPORT_TAGS = ( |
46 | %EXPORT_TAGS = ( |
47 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
47 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
48 | ); |
48 | ); |
… | |
… | |
130 | # remove itself from the runqueue |
130 | # remove itself from the runqueue |
131 | while (@destroy) { |
131 | while (@destroy) { |
132 | my $coro = pop @destroy; |
132 | my $coro = pop @destroy; |
133 | $coro->{status} ||= []; |
133 | $coro->{status} ||= []; |
134 | $_->ready for @{delete $coro->{join} || []}; |
134 | $_->ready for @{delete $coro->{join} || []}; |
|
|
135 | |
|
|
136 | # the next line destroys the _coro_state, but keeps the |
|
|
137 | # process itself intact (we basically make it a zombie |
|
|
138 | # process that always runs the manager thread, so it's possible |
|
|
139 | # to transfer() to this process). |
135 | $coro->{_coro_state} = $manager->{_coro_state}; |
140 | $coro->{_coro_state} = $manager->{_coro_state}; |
136 | } |
141 | } |
137 | &schedule; |
142 | &schedule; |
138 | } |
143 | } |
139 | }; |
144 | }; |
… | |
… | |
184 | |
189 | |
185 | =cut |
190 | =cut |
186 | |
191 | |
187 | =item terminate [arg...] |
192 | =item terminate [arg...] |
188 | |
193 | |
189 | Terminates the current process. |
194 | Terminates the current process with the given status values (see L<cancel>). |
190 | |
|
|
191 | Future versions of this function will allow result arguments. |
|
|
192 | |
195 | |
193 | =cut |
196 | =cut |
194 | |
197 | |
195 | sub terminate { |
198 | sub terminate { |
196 | $current->{status} = [@_]; |
|
|
197 | $current->cancel; |
199 | $current->cancel (@_); |
198 | &schedule; |
|
|
199 | die; # NORETURN |
|
|
200 | } |
200 | } |
201 | |
201 | |
202 | =back |
202 | =back |
203 | |
203 | |
204 | # dynamic methods |
204 | # dynamic methods |
… | |
… | |
233 | |
233 | |
234 | Put the given process into the ready queue. |
234 | Put the given process into the ready queue. |
235 | |
235 | |
236 | =cut |
236 | =cut |
237 | |
237 | |
238 | =item $process->cancel |
238 | =item $process->cancel (arg...) |
239 | |
239 | |
240 | Like C<terminate>, but terminates the specified process instead. |
240 | Temrinates the given process and makes it return the given arguments as |
|
|
241 | status (default: the empty list). |
241 | |
242 | |
242 | =cut |
243 | =cut |
243 | |
244 | |
244 | sub cancel { |
245 | sub cancel { |
|
|
246 | my $self = shift; |
|
|
247 | $self->{status} = [@_]; |
245 | push @destroy, $_[0]; |
248 | push @destroy, $self; |
246 | $manager->ready; |
249 | $manager->ready; |
247 | &schedule if $current == $_[0]; |
250 | &schedule if $current == $self; |
248 | } |
251 | } |
249 | |
252 | |
250 | =item $process->join |
253 | =item $process->join |
251 | |
254 | |
252 | Wait until the coroutine terminates and return any values given to the |
255 | Wait until the coroutine terminates and return any values given to the |
253 | C<terminate> function. C<join> can be called multiple times from multiple |
256 | C<terminate> or C<cancel> functions. C<join> can be called multiple times |
254 | processes. |
257 | from multiple processes. |
255 | |
258 | |
256 | =cut |
259 | =cut |
257 | |
260 | |
258 | sub join { |
261 | sub join { |
259 | my $self = shift; |
262 | my $self = shift; |