… | |
… | |
36 | |
36 | |
37 | use Coro::State; |
37 | use Coro::State; |
38 | |
38 | |
39 | use base Exporter; |
39 | use base Exporter; |
40 | |
40 | |
41 | $VERSION = 0.12; |
41 | $VERSION = 0.45; |
42 | |
42 | |
43 | @EXPORT = qw(async cede schedule terminate current); |
43 | @EXPORT = qw(async cede schedule terminate current); |
44 | @EXPORT_OK = qw($current); |
44 | @EXPORT_OK = qw($current); |
45 | |
45 | |
46 | { |
46 | { |
47 | my @async; |
47 | my @async; |
|
|
48 | my $init; |
48 | |
49 | |
49 | # this way of handling attributes simply is NOT scalable ;() |
50 | # this way of handling attributes simply is NOT scalable ;() |
50 | sub import { |
51 | sub import { |
51 | Coro->export_to_level(1, @_); |
52 | Coro->export_to_level(1, @_); |
52 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
53 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
… | |
… | |
54 | my ($package, $ref) = (shift, shift); |
55 | my ($package, $ref) = (shift, shift); |
55 | my @attrs; |
56 | my @attrs; |
56 | for (@_) { |
57 | for (@_) { |
57 | if ($_ eq "Coro") { |
58 | if ($_ eq "Coro") { |
58 | push @async, $ref; |
59 | push @async, $ref; |
|
|
60 | unless ($init++) { |
|
|
61 | eval q{ |
|
|
62 | sub INIT { |
|
|
63 | &async(pop @async) while @async; |
|
|
64 | } |
|
|
65 | }; |
|
|
66 | } |
59 | } else { |
67 | } else { |
60 | push @attrs, $_; |
68 | push @attrs, $_; |
61 | } |
69 | } |
62 | } |
70 | } |
63 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
71 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
64 | }; |
72 | }; |
65 | } |
73 | } |
66 | |
74 | |
67 | sub INIT { |
|
|
68 | &async(pop @async) while @async; |
|
|
69 | } |
|
|
70 | } |
75 | } |
71 | |
76 | |
72 | =item $main |
77 | =item $main |
73 | |
78 | |
74 | This coroutine represents the main program. |
79 | This coroutine represents the main program. |
… | |
… | |
185 | Future versions of this function will allow result arguments. |
190 | Future versions of this function will allow result arguments. |
186 | |
191 | |
187 | =cut |
192 | =cut |
188 | |
193 | |
189 | sub terminate { |
194 | sub terminate { |
190 | push @destroy, $current; |
195 | $current->cancel; |
191 | $manager->ready; |
|
|
192 | &schedule; |
196 | &schedule; |
193 | # NORETURN |
197 | die; # NORETURN |
194 | } |
198 | } |
195 | |
199 | |
196 | =back |
200 | =back |
197 | |
201 | |
198 | # dynamic methods |
202 | # dynamic methods |
… | |
… | |
231 | |
235 | |
232 | =cut |
236 | =cut |
233 | |
237 | |
234 | sub ready { |
238 | sub ready { |
235 | push @ready, $_[0]; |
239 | push @ready, $_[0]; |
|
|
240 | } |
|
|
241 | |
|
|
242 | =item $process->cancel |
|
|
243 | |
|
|
244 | Like C<terminate>, but terminates the specified process instead. |
|
|
245 | |
|
|
246 | =cut |
|
|
247 | |
|
|
248 | sub cancel { |
|
|
249 | push @destroy, $_[0]; |
|
|
250 | $manager->ready; |
236 | } |
251 | } |
237 | |
252 | |
238 | =back |
253 | =back |
239 | |
254 | |
240 | =cut |
255 | =cut |
… | |
… | |
256 | allow per-thread schedulers, but Coro::State does not yet allow this). |
271 | allow per-thread schedulers, but Coro::State does not yet allow this). |
257 | |
272 | |
258 | =head1 SEE ALSO |
273 | =head1 SEE ALSO |
259 | |
274 | |
260 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
275 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
261 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
276 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>, |
|
|
277 | L<Coro::Handle>, L<Coro::Socket>. |
262 | |
278 | |
263 | =head1 AUTHOR |
279 | =head1 AUTHOR |
264 | |
280 | |
265 | Marc Lehmann <pcg@goof.com> |
281 | Marc Lehmann <pcg@goof.com> |
266 | http://www.goof.com/pcg/marc/ |
282 | http://www.goof.com/pcg/marc/ |