… | |
… | |
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 | |
|
|
33 | WARNING: When using this module, make sure that, at program end, no |
|
|
34 | coroutines are still running OR just call exit before falling off the |
|
|
35 | end. The reason for this is that some coroutine of yours might have called |
|
|
36 | into a C function, and falling off the end of main:: results in returning |
|
|
37 | to that C function instead if to the main C interpreter. |
|
|
38 | |
28 | =cut |
39 | =cut |
29 | |
40 | |
30 | package Coro; |
41 | package Coro; |
31 | |
42 | |
32 | use Coro::State; |
43 | use Coro::State; |
33 | |
44 | |
34 | use base Exporter; |
45 | use base Exporter; |
35 | |
46 | |
36 | $VERSION = 0.05; |
47 | $VERSION = 0.10; |
37 | |
48 | |
38 | @EXPORT = qw(async yield schedule terminate); |
49 | @EXPORT = qw(async yield schedule terminate current); |
39 | @EXPORT_OK = qw($current); |
50 | @EXPORT_OK = qw($current); |
40 | |
51 | |
41 | { |
52 | { |
42 | use subs 'async'; |
|
|
43 | |
|
|
44 | my @async; |
53 | my @async; |
45 | |
54 | |
46 | # this way of handling attributes simply is NOT scalable ;() |
55 | # this way of handling attributes simply is NOT scalable ;() |
47 | sub import { |
56 | sub import { |
48 | Coro->export_to_level(1, @_); |
57 | Coro->export_to_level(1, @_); |
… | |
… | |
52 | my @attrs; |
61 | my @attrs; |
53 | for (@_) { |
62 | for (@_) { |
54 | if ($_ eq "Coro") { |
63 | if ($_ eq "Coro") { |
55 | push @async, $ref; |
64 | push @async, $ref; |
56 | } else { |
65 | } else { |
57 | push @attrs, @_; |
66 | push @attrs, $_; |
58 | } |
67 | } |
59 | } |
68 | } |
60 | return $old ? $old->($package, $name, @attrs) : @attrs; |
69 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
61 | }; |
70 | }; |
62 | } |
71 | } |
63 | |
72 | |
64 | sub INIT { |
73 | sub INIT { |
65 | async pop @async while @async; |
74 | &async(pop @async) while @async; |
66 | } |
75 | } |
67 | } |
76 | } |
68 | |
77 | |
69 | =item $main |
78 | =item $main |
70 | |
79 | |
… | |
… | |
72 | |
81 | |
73 | =cut |
82 | =cut |
74 | |
83 | |
75 | our $main = new Coro; |
84 | our $main = new Coro; |
76 | |
85 | |
77 | =item $current |
86 | =item $current (or as function: current) |
78 | |
87 | |
79 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
88 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
80 | |
89 | |
81 | =cut |
90 | =cut |
82 | |
91 | |
… | |
… | |
84 | if ($current) { |
93 | if ($current) { |
85 | $main->{specific} = $current->{specific}; |
94 | $main->{specific} = $current->{specific}; |
86 | } |
95 | } |
87 | |
96 | |
88 | our $current = $main; |
97 | our $current = $main; |
|
|
98 | |
|
|
99 | sub current() { $current } |
89 | |
100 | |
90 | =item $idle |
101 | =item $idle |
91 | |
102 | |
92 | The coroutine to switch to when no other coroutine is running. The default |
103 | The coroutine to switch to when no other coroutine is running. The default |
93 | implementation prints "FATAL: deadlock detected" and exits. |
104 | implementation prints "FATAL: deadlock detected" and exits. |
… | |
… | |
99 | print STDERR "FATAL: deadlock detected\n"; |
110 | print STDERR "FATAL: deadlock detected\n"; |
100 | exit(51); |
111 | exit(51); |
101 | }; |
112 | }; |
102 | |
113 | |
103 | # we really need priorities... |
114 | # we really need priorities... |
104 | ## my @ready; #d# |
|
|
105 | our @ready = (); # the ready queue. hehe, rather broken ;) |
115 | my @ready; # the ready queue. hehe, rather broken ;) |
106 | |
116 | |
107 | # static methods. not really. |
117 | # static methods. not really. |
108 | |
118 | |
109 | =head2 STATIC METHODS |
119 | =head2 STATIC METHODS |
110 | |
120 | |
… | |
… | |
170 | |
180 | |
171 | =cut |
181 | =cut |
172 | |
182 | |
173 | sub terminate { |
183 | sub terminate { |
174 | $current->{_results} = [@_]; |
184 | $current->{_results} = [@_]; |
|
|
185 | delete $current->{_coro_state}; |
175 | &schedule; |
186 | &schedule; |
176 | } |
187 | } |
177 | |
188 | |
178 | =back |
189 | =back |
179 | |
190 | |
… | |
… | |
221 | |
232 | |
222 | =cut |
233 | =cut |
223 | |
234 | |
224 | 1; |
235 | 1; |
225 | |
236 | |
226 | =head1 BUGS |
237 | =head1 BUGS/LIMITATIONS |
227 | |
238 | |
228 | - could be faster, especially when the core would introduce special |
239 | - could be faster, especially when the core would introduce special |
229 | support for coroutines (like it does for threads). |
240 | support for coroutines (like it does for threads). |
230 | - there is still a memleak on coroutine termination that I could not |
241 | - there is still a memleak on coroutine termination that I could not |
231 | identify. Could be as small as a single SV. |
242 | identify. Could be as small as a single SV. |
232 | - this module is not well-tested. |
243 | - this module is not well-tested. |
|
|
244 | - if variables or arguments "disappear" (become undef) or become |
|
|
245 | corrupted please contact the author so he cen iron out the |
|
|
246 | remaining bugs. |
|
|
247 | - this module is not thread-safe. You must only ever use this module from |
|
|
248 | the same thread (this requirement might be loosened in the future to |
|
|
249 | allow per-thread schedulers, but Coro::State does not yet allow this). |
233 | |
250 | |
234 | =head1 SEE ALSO |
251 | =head1 SEE ALSO |
235 | |
252 | |
236 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
253 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
237 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
254 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |