… | |
… | |
14 | |
14 | |
15 | sub some_func : Coro { |
15 | sub some_func : Coro { |
16 | # some more async code |
16 | # some more async code |
17 | } |
17 | } |
18 | |
18 | |
19 | yield; |
19 | cede; |
20 | |
20 | |
21 | =head1 DESCRIPTION |
21 | =head1 DESCRIPTION |
22 | |
22 | |
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 | |
|
|
39 | WARNING: Unless you really know what you are doing, do NOT do context |
|
|
40 | switches inside callbacks from the XS level. The reason for this is |
|
|
41 | similar to the reason above: A callback calls a perl function, this |
|
|
42 | perl function does a context switch, some other callback is called, the |
|
|
43 | original function returns from it - to what? To the wrong XS function, |
|
|
44 | with totally different return values. Unfortunately, this includes |
|
|
45 | callbacks done by perl itself (tie'd variables!). |
|
|
46 | |
|
|
47 | The only workaround for this is to do coroutines on C level. |
|
|
48 | |
28 | =cut |
49 | =cut |
29 | |
50 | |
30 | package Coro; |
51 | package Coro; |
31 | |
52 | |
32 | use Coro::State; |
53 | use Coro::State; |
33 | |
54 | |
34 | use base Exporter; |
55 | use base Exporter; |
35 | |
56 | |
36 | $VERSION = 0.06; |
57 | $VERSION = 0.10; |
37 | |
58 | |
38 | @EXPORT = qw(async yield schedule terminate); |
59 | @EXPORT = qw(async cede schedule terminate current); |
39 | @EXPORT_OK = qw($current); |
60 | @EXPORT_OK = qw($current); |
40 | |
61 | |
41 | { |
62 | { |
42 | use subs 'async'; |
|
|
43 | |
|
|
44 | my @async; |
63 | my @async; |
45 | |
64 | |
46 | # this way of handling attributes simply is NOT scalable ;() |
65 | # this way of handling attributes simply is NOT scalable ;() |
47 | sub import { |
66 | sub import { |
48 | Coro->export_to_level(1, @_); |
67 | Coro->export_to_level(1, @_); |
… | |
… | |
52 | my @attrs; |
71 | my @attrs; |
53 | for (@_) { |
72 | for (@_) { |
54 | if ($_ eq "Coro") { |
73 | if ($_ eq "Coro") { |
55 | push @async, $ref; |
74 | push @async, $ref; |
56 | } else { |
75 | } else { |
57 | push @attrs, @_; |
76 | push @attrs, $_; |
58 | } |
77 | } |
59 | } |
78 | } |
60 | return $old ? $old->($package, $name, @attrs) : @attrs; |
79 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
61 | }; |
80 | }; |
62 | } |
81 | } |
63 | |
82 | |
64 | sub INIT { |
83 | sub INIT { |
65 | async pop @async while @async; |
84 | &async(pop @async) while @async; |
66 | } |
85 | } |
67 | } |
86 | } |
68 | |
87 | |
69 | =item $main |
88 | =item $main |
70 | |
89 | |
… | |
… | |
72 | |
91 | |
73 | =cut |
92 | =cut |
74 | |
93 | |
75 | our $main = new Coro; |
94 | our $main = new Coro; |
76 | |
95 | |
77 | =item $current |
96 | =item $current (or as function: current) |
78 | |
97 | |
79 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
98 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
80 | |
99 | |
81 | =cut |
100 | =cut |
82 | |
101 | |
… | |
… | |
84 | if ($current) { |
103 | if ($current) { |
85 | $main->{specific} = $current->{specific}; |
104 | $main->{specific} = $current->{specific}; |
86 | } |
105 | } |
87 | |
106 | |
88 | our $current = $main; |
107 | our $current = $main; |
|
|
108 | |
|
|
109 | sub current() { $current } |
89 | |
110 | |
90 | =item $idle |
111 | =item $idle |
91 | |
112 | |
92 | The coroutine to switch to when no other coroutine is running. The default |
113 | The coroutine to switch to when no other coroutine is running. The default |
93 | implementation prints "FATAL: deadlock detected" and exits. |
114 | implementation prints "FATAL: deadlock detected" and exits. |
… | |
… | |
99 | print STDERR "FATAL: deadlock detected\n"; |
120 | print STDERR "FATAL: deadlock detected\n"; |
100 | exit(51); |
121 | exit(51); |
101 | }; |
122 | }; |
102 | |
123 | |
103 | # we really need priorities... |
124 | # we really need priorities... |
104 | ## my @ready; #d# |
|
|
105 | our @ready = (); # the ready queue. hehe, rather broken ;) |
125 | my @ready; # the ready queue. hehe, rather broken ;) |
106 | |
126 | |
107 | # static methods. not really. |
127 | # static methods. not really. |
108 | |
128 | |
109 | =head2 STATIC METHODS |
129 | =head2 STATIC METHODS |
110 | |
130 | |
… | |
… | |
148 | # should be done using priorities :( |
168 | # should be done using priorities :( |
149 | ($prev, $current) = ($current, shift @ready || $idle); |
169 | ($prev, $current) = ($current, shift @ready || $idle); |
150 | Coro::State::transfer($prev, $current); |
170 | Coro::State::transfer($prev, $current); |
151 | } |
171 | } |
152 | |
172 | |
153 | =item yield |
173 | =item cede |
154 | |
174 | |
155 | Yield to other processes. This function puts the current process into the |
175 | "Cede" to other processes. This function puts the current process into the |
156 | ready queue and calls C<schedule>. |
176 | ready queue and calls C<schedule>, which has the effect of giving up the |
|
|
177 | current "timeslice" to other coroutines of the same or higher priority. |
157 | |
178 | |
158 | =cut |
179 | =cut |
159 | |
180 | |
160 | sub yield { |
181 | sub cede { |
161 | $current->ready; |
182 | $current->ready; |
162 | &schedule; |
183 | &schedule; |
163 | } |
184 | } |
164 | |
185 | |
165 | =item terminate |
186 | =item terminate |
… | |
… | |
169 | Future versions of this function will allow result arguments. |
190 | Future versions of this function will allow result arguments. |
170 | |
191 | |
171 | =cut |
192 | =cut |
172 | |
193 | |
173 | sub terminate { |
194 | sub terminate { |
|
|
195 | my $self = $current; |
174 | $current->{_results} = [@_]; |
196 | $self->{_results} = [@_]; |
175 | &schedule; |
197 | $current = shift @ready || $idle; |
|
|
198 | Coro::State::transfer(delete $self->{_coro_state}, $current); |
|
|
199 | # cannot return |
|
|
200 | die; |
176 | } |
201 | } |
177 | |
202 | |
178 | =back |
203 | =back |
179 | |
204 | |
180 | # dynamic methods |
205 | # dynamic methods |
… | |
… | |
221 | |
246 | |
222 | =cut |
247 | =cut |
223 | |
248 | |
224 | 1; |
249 | 1; |
225 | |
250 | |
226 | =head1 BUGS |
251 | =head1 BUGS/LIMITATIONS |
227 | |
252 | |
228 | - could be faster, especially when the core would introduce special |
253 | - could be faster, especially when the core would introduce special |
229 | support for coroutines (like it does for threads). |
254 | support for coroutines (like it does for threads). |
230 | - there is still a memleak on coroutine termination that I could not |
255 | - there is still a memleak on coroutine termination that I could not |
231 | identify. Could be as small as a single SV. |
256 | identify. Could be as small as a single SV. |
232 | - this module is not well-tested. |
257 | - this module is not well-tested. |
|
|
258 | - if variables or arguments "disappear" (become undef) or become |
|
|
259 | corrupted please contact the author so he cen iron out the |
|
|
260 | remaining bugs. |
|
|
261 | - this module is not thread-safe. You must only ever use this module from |
|
|
262 | the same thread (this requirement might be loosened in the future to |
|
|
263 | allow per-thread schedulers, but Coro::State does not yet allow this). |
233 | |
264 | |
234 | =head1 SEE ALSO |
265 | =head1 SEE ALSO |
235 | |
266 | |
236 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
267 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
237 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
268 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |