… | |
… | |
23 | This module collection manages coroutines. Coroutines are similar to |
23 | This module collection manages coroutines. Coroutines are similar to |
24 | Threads but don't run in parallel. |
24 | Threads but don't run in parallel. |
25 | |
25 | |
26 | This module is still experimental, see the BUGS section below. |
26 | This module is still experimental, see the BUGS section below. |
27 | |
27 | |
|
|
28 | In this module, coroutines are defined as "callchain + lexical variables |
|
|
29 | + @_ + $_ + $@ + $^W), that is, a coroutine has it's own callchain, it's |
|
|
30 | own set of lexicals and it's own set of perl's most important global |
|
|
31 | variables. |
|
|
32 | |
28 | =cut |
33 | =cut |
29 | |
34 | |
30 | package Coro; |
35 | package Coro; |
31 | |
36 | |
32 | use Coro::State; |
37 | use Coro::State; |
33 | |
38 | |
34 | use base Exporter; |
39 | use base Exporter; |
35 | |
40 | |
36 | $VERSION = 0.09; |
41 | $VERSION = 0.10; |
37 | |
42 | |
38 | @EXPORT = qw(async yield schedule terminate); |
43 | @EXPORT = qw(async yield schedule terminate current); |
39 | @EXPORT_OK = qw($current); |
44 | @EXPORT_OK = qw($current); |
40 | |
45 | |
41 | { |
46 | { |
42 | use subs 'async'; |
|
|
43 | |
|
|
44 | my @async; |
47 | my @async; |
45 | |
48 | |
46 | # this way of handling attributes simply is NOT scalable ;() |
49 | # this way of handling attributes simply is NOT scalable ;() |
47 | sub import { |
50 | sub import { |
48 | Coro->export_to_level(1, @_); |
51 | Coro->export_to_level(1, @_); |
… | |
… | |
60 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
63 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
61 | }; |
64 | }; |
62 | } |
65 | } |
63 | |
66 | |
64 | sub INIT { |
67 | sub INIT { |
65 | async pop @async while @async; |
68 | &async(pop @async) while @async; |
66 | } |
69 | } |
67 | } |
70 | } |
68 | |
71 | |
69 | =item $main |
72 | =item $main |
70 | |
73 | |
… | |
… | |
72 | |
75 | |
73 | =cut |
76 | =cut |
74 | |
77 | |
75 | our $main = new Coro; |
78 | our $main = new Coro; |
76 | |
79 | |
77 | =item $current |
80 | =item $current (or as function: current) |
78 | |
81 | |
79 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
82 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
80 | |
83 | |
81 | =cut |
84 | =cut |
82 | |
85 | |
… | |
… | |
84 | if ($current) { |
87 | if ($current) { |
85 | $main->{specific} = $current->{specific}; |
88 | $main->{specific} = $current->{specific}; |
86 | } |
89 | } |
87 | |
90 | |
88 | our $current = $main; |
91 | our $current = $main; |
|
|
92 | |
|
|
93 | sub current() { $current } |
89 | |
94 | |
90 | =item $idle |
95 | =item $idle |
91 | |
96 | |
92 | The coroutine to switch to when no other coroutine is running. The default |
97 | The coroutine to switch to when no other coroutine is running. The default |
93 | implementation prints "FATAL: deadlock detected" and exits. |
98 | implementation prints "FATAL: deadlock detected" and exits. |
… | |
… | |
232 | - if variables or arguments "disappear" (become undef) or become |
237 | - if variables or arguments "disappear" (become undef) or become |
233 | corrupted please contact the author so he cen iron out the |
238 | corrupted please contact the author so he cen iron out the |
234 | remaining bugs. |
239 | remaining bugs. |
235 | - this module is not thread-safe. You must only ever use this module from |
240 | - this module is not thread-safe. You must only ever use this module from |
236 | the same thread (this requirement might be loosened in the future to |
241 | the same thread (this requirement might be loosened in the future to |
237 | allow per-thread schedulers, but Coro::Satte does not yet allow this). |
242 | allow per-thread schedulers, but Coro::State does not yet allow this). |
238 | |
243 | |
239 | =head1 SEE ALSO |
244 | =head1 SEE ALSO |
240 | |
245 | |
241 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
246 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
242 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
247 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |