… | |
… | |
30 | |
30 | |
31 | =cut |
31 | =cut |
32 | |
32 | |
33 | package Coro; |
33 | package Coro; |
34 | |
34 | |
35 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
35 | use strict; |
|
|
36 | no warnings "uninitialized"; |
36 | |
37 | |
37 | use Coro::State; |
38 | use Coro::State; |
38 | |
39 | |
39 | use vars qw($idle $main $current); |
|
|
40 | |
|
|
41 | use base Exporter; |
40 | use base Exporter::; |
42 | |
41 | |
43 | $VERSION = "0.9"; |
42 | our $idle; # idle coroutine |
|
|
43 | our $main; # main coroutine |
|
|
44 | our $current; # current coroutine |
44 | |
45 | |
|
|
46 | our $VERSION = '2.0'; |
|
|
47 | |
45 | @EXPORT = qw(async cede schedule terminate current); |
48 | our @EXPORT = qw(async cede schedule terminate current); |
46 | %EXPORT_TAGS = ( |
49 | our %EXPORT_TAGS = ( |
47 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
50 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
48 | ); |
51 | ); |
49 | @EXPORT_OK = @{$EXPORT_TAGS{prio}}; |
52 | our @EXPORT_OK = @{$EXPORT_TAGS{prio}}; |
50 | |
53 | |
51 | { |
54 | { |
52 | my @async; |
55 | my @async; |
53 | my $init; |
56 | my $init; |
54 | |
57 | |
55 | # this way of handling attributes simply is NOT scalable ;() |
58 | # this way of handling attributes simply is NOT scalable ;() |
56 | sub import { |
59 | sub import { |
|
|
60 | no strict 'refs'; |
|
|
61 | |
57 | Coro->export_to_level(1, @_); |
62 | Coro->export_to_level(1, @_); |
|
|
63 | |
58 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
64 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
59 | *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { |
65 | *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { |
60 | my ($package, $ref) = (shift, shift); |
66 | my ($package, $ref) = (shift, shift); |
61 | my @attrs; |
67 | my @attrs; |
62 | for (@_) { |
68 | for (@_) { |
… | |
… | |
130 | # remove itself from the runqueue |
136 | # remove itself from the runqueue |
131 | while (@destroy) { |
137 | while (@destroy) { |
132 | my $coro = pop @destroy; |
138 | my $coro = pop @destroy; |
133 | $coro->{status} ||= []; |
139 | $coro->{status} ||= []; |
134 | $_->ready for @{delete $coro->{join} || []}; |
140 | $_->ready for @{delete $coro->{join} || []}; |
|
|
141 | |
|
|
142 | # the next line destroys the _coro_state, but keeps the |
|
|
143 | # process itself intact (we basically make it a zombie |
|
|
144 | # process that always runs the manager thread, so it's possible |
|
|
145 | # to transfer() to this process). |
135 | $coro->{_coro_state} = $manager->{_coro_state}; |
146 | $coro->{_coro_state} = $manager->{_coro_state}; |
136 | } |
147 | } |
137 | &schedule; |
148 | &schedule; |
138 | } |
149 | } |
139 | }; |
150 | }; |
… | |
… | |
184 | |
195 | |
185 | =cut |
196 | =cut |
186 | |
197 | |
187 | =item terminate [arg...] |
198 | =item terminate [arg...] |
188 | |
199 | |
189 | Terminates the current process. |
200 | Terminates the current process with the given status values (see L<cancel>). |
190 | |
|
|
191 | Future versions of this function will allow result arguments. |
|
|
192 | |
201 | |
193 | =cut |
202 | =cut |
194 | |
203 | |
195 | sub terminate { |
204 | sub terminate { |
196 | $current->{status} = [@_]; |
|
|
197 | $current->cancel; |
205 | $current->cancel (@_); |
198 | &schedule; |
|
|
199 | die; # NORETURN |
|
|
200 | } |
206 | } |
201 | |
207 | |
202 | =back |
208 | =back |
203 | |
209 | |
204 | # dynamic methods |
210 | # dynamic methods |
… | |
… | |
233 | |
239 | |
234 | Put the given process into the ready queue. |
240 | Put the given process into the ready queue. |
235 | |
241 | |
236 | =cut |
242 | =cut |
237 | |
243 | |
238 | =item $process->cancel |
244 | =item $process->cancel (arg...) |
239 | |
245 | |
240 | Like C<terminate>, but terminates the specified process instead. |
246 | Temrinates the given process and makes it return the given arguments as |
|
|
247 | status (default: the empty list). |
241 | |
248 | |
242 | =cut |
249 | =cut |
243 | |
250 | |
244 | sub cancel { |
251 | sub cancel { |
|
|
252 | my $self = shift; |
|
|
253 | $self->{status} = [@_]; |
245 | push @destroy, $_[0]; |
254 | push @destroy, $self; |
246 | $manager->ready; |
255 | $manager->ready; |
247 | &schedule if $current == $_[0]; |
256 | &schedule if $current == $self; |
248 | } |
257 | } |
249 | |
258 | |
250 | =item $process->join |
259 | =item $process->join |
251 | |
260 | |
252 | Wait until the coroutine terminates and return any values given to the |
261 | 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 |
262 | C<terminate> or C<cancel> functions. C<join> can be called multiple times |
254 | processes. |
263 | from multiple processes. |
255 | |
264 | |
256 | =cut |
265 | =cut |
257 | |
266 | |
258 | sub join { |
267 | sub join { |
259 | my $self = shift; |
268 | my $self = shift; |
… | |
… | |
334 | to allow per-thread schedulers, but Coro::State does not yet allow |
343 | to allow per-thread schedulers, but Coro::State does not yet allow |
335 | this). |
344 | this). |
336 | |
345 | |
337 | =head1 SEE ALSO |
346 | =head1 SEE ALSO |
338 | |
347 | |
339 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
348 | Support/Utility: L<Coro::Cont>, L<Coro::Specific>, L<Coro::State>, L<Coro::Util>. |
340 | L<Coro::Signal>, L<Coro::State>, L<Coro::Timer>, L<Coro::Event>, |
349 | |
341 | L<Coro::L<Coro::RWLock>, Handle>, L<Coro::Socket>. |
350 | Locking/IPC: L<Coro::Signal>, L<Coro::Channel>, L<Coro::Semaphore>, L<Coro::SemaphoreSet>, L<Coro::RWLock>. |
|
|
351 | |
|
|
352 | Event/IO: L<Coro::Timer>, L<Coro::Event>, L<Coro::Handle>, L<Coro::Socket>, L<Coro::Select>. |
|
|
353 | |
|
|
354 | Embedding: L<Coro:MakeMaker> |
342 | |
355 | |
343 | =head1 AUTHOR |
356 | =head1 AUTHOR |
344 | |
357 | |
345 | Marc Lehmann <pcg@goof.com> |
358 | Marc Lehmann <schmorp@schmorp.de> |
346 | http://www.goof.com/pcg/marc/ |
359 | http://home.schmorp.de/ |
347 | |
360 | |
348 | =cut |
361 | =cut |
349 | |
362 | |