… | |
… | |
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 |
|
|
24 | Threads but don't run in parallel. |
|
|
25 | |
|
|
26 | This module is still experimental, see the BUGS section below. |
|
|
27 | |
|
|
28 | In this module, coroutines are defined as "callchain + lexical variables |
|
|
29 | + @_ + $_ + $@ + $^W + C stack), that is, a coroutine has it's own |
|
|
30 | callchain, it's own set of lexicals and it's own set of perl's most |
|
|
31 | important global variables. |
|
|
32 | |
23 | =cut |
33 | =cut |
24 | |
34 | |
25 | package Coro; |
35 | package Coro; |
26 | |
36 | |
27 | use Coro::State; |
37 | use Coro::State; |
28 | |
38 | |
29 | use base Exporter; |
39 | use base Exporter; |
30 | |
40 | |
31 | $VERSION = 0.05; |
41 | $VERSION = 0.10; |
32 | |
42 | |
33 | @EXPORT = qw(async yield schedule terminate); |
43 | @EXPORT = qw(async cede schedule terminate current); |
34 | @EXPORT_OK = qw($current); |
44 | @EXPORT_OK = qw($current); |
35 | |
45 | |
36 | { |
46 | { |
37 | use subs 'async'; |
|
|
38 | |
|
|
39 | my @async; |
47 | my @async; |
40 | |
48 | |
41 | # this way of handling attributes simply is NOT scalable ;() |
49 | # this way of handling attributes simply is NOT scalable ;() |
42 | sub import { |
50 | sub import { |
43 | Coro->export_to_level(1, @_); |
51 | Coro->export_to_level(1, @_); |
… | |
… | |
47 | my @attrs; |
55 | my @attrs; |
48 | for (@_) { |
56 | for (@_) { |
49 | if ($_ eq "Coro") { |
57 | if ($_ eq "Coro") { |
50 | push @async, $ref; |
58 | push @async, $ref; |
51 | } else { |
59 | } else { |
52 | push @attrs, @_; |
60 | push @attrs, $_; |
53 | } |
61 | } |
54 | } |
62 | } |
55 | return $old ? $old->($package, $name, @attrs) : @attrs; |
63 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
56 | }; |
64 | }; |
57 | } |
65 | } |
58 | |
66 | |
59 | sub INIT { |
67 | sub INIT { |
60 | async pop @async while @async; |
68 | &async(pop @async) while @async; |
61 | } |
69 | } |
62 | } |
70 | } |
63 | |
71 | |
64 | =item $main |
72 | =item $main |
65 | |
73 | |
… | |
… | |
67 | |
75 | |
68 | =cut |
76 | =cut |
69 | |
77 | |
70 | our $main = new Coro; |
78 | our $main = new Coro; |
71 | |
79 | |
72 | =item $current |
80 | =item $current (or as function: current) |
73 | |
81 | |
74 | 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). |
75 | |
83 | |
76 | =cut |
84 | =cut |
77 | |
85 | |
… | |
… | |
79 | if ($current) { |
87 | if ($current) { |
80 | $main->{specific} = $current->{specific}; |
88 | $main->{specific} = $current->{specific}; |
81 | } |
89 | } |
82 | |
90 | |
83 | our $current = $main; |
91 | our $current = $main; |
|
|
92 | |
|
|
93 | sub current() { $current } |
84 | |
94 | |
85 | =item $idle |
95 | =item $idle |
86 | |
96 | |
87 | 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 |
88 | implementation prints "FATAL: deadlock detected" and exits. |
98 | implementation prints "FATAL: deadlock detected" and exits. |
… | |
… | |
94 | print STDERR "FATAL: deadlock detected\n"; |
104 | print STDERR "FATAL: deadlock detected\n"; |
95 | exit(51); |
105 | exit(51); |
96 | }; |
106 | }; |
97 | |
107 | |
98 | # we really need priorities... |
108 | # we really need priorities... |
99 | ## my @ready; #d# |
|
|
100 | our @ready = (); # the ready queue. hehe, rather broken ;) |
109 | my @ready; # the ready queue. hehe, rather broken ;) |
101 | |
110 | |
102 | # static methods. not really. |
111 | # static methods. not really. |
103 | |
112 | |
104 | =head2 STATIC METHODS |
113 | =head2 STATIC METHODS |
105 | |
114 | |
… | |
… | |
143 | # should be done using priorities :( |
152 | # should be done using priorities :( |
144 | ($prev, $current) = ($current, shift @ready || $idle); |
153 | ($prev, $current) = ($current, shift @ready || $idle); |
145 | Coro::State::transfer($prev, $current); |
154 | Coro::State::transfer($prev, $current); |
146 | } |
155 | } |
147 | |
156 | |
148 | =item yield |
157 | =item cede |
149 | |
158 | |
150 | Yield to other processes. This function puts the current process into the |
159 | "Cede" to other processes. This function puts the current process into the |
151 | ready queue and calls C<schedule>. |
160 | ready queue and calls C<schedule>, which has the effect of giving up the |
|
|
161 | current "timeslice" to other coroutines of the same or higher priority. |
152 | |
162 | |
153 | =cut |
163 | =cut |
154 | |
164 | |
155 | sub yield { |
165 | sub cede { |
156 | $current->ready; |
166 | $current->ready; |
157 | &schedule; |
167 | &schedule; |
158 | } |
168 | } |
159 | |
169 | |
160 | =item terminate |
170 | =item terminate |
… | |
… | |
163 | |
173 | |
164 | Future versions of this function will allow result arguments. |
174 | Future versions of this function will allow result arguments. |
165 | |
175 | |
166 | =cut |
176 | =cut |
167 | |
177 | |
|
|
178 | # this coroutine is necessary because a coroutine |
|
|
179 | # cannot destroy itself. |
|
|
180 | my @destroy; |
|
|
181 | my $terminate = new Coro sub { |
|
|
182 | while() { |
|
|
183 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
|
|
184 | &schedule; |
|
|
185 | } |
|
|
186 | }; |
|
|
187 | |
168 | sub terminate { |
188 | sub terminate { |
169 | $current->{_results} = [@_]; |
189 | push @destroy, $current; |
|
|
190 | $terminate->ready; |
170 | &schedule; |
191 | &schedule; |
|
|
192 | # NORETURN |
171 | } |
193 | } |
172 | |
194 | |
173 | =back |
195 | =back |
174 | |
196 | |
175 | # dynamic methods |
197 | # dynamic methods |
… | |
… | |
216 | |
238 | |
217 | =cut |
239 | =cut |
218 | |
240 | |
219 | 1; |
241 | 1; |
220 | |
242 | |
|
|
243 | =head1 BUGS/LIMITATIONS |
|
|
244 | |
|
|
245 | - could be faster, especially when the core would introduce special |
|
|
246 | support for coroutines (like it does for threads). |
|
|
247 | - there is still a memleak on coroutine termination that I could not |
|
|
248 | identify. Could be as small as a single SV. |
|
|
249 | - this module is not well-tested. |
|
|
250 | - if variables or arguments "disappear" (become undef) or become |
|
|
251 | corrupted please contact the author so he cen iron out the |
|
|
252 | remaining bugs. |
|
|
253 | - this module is not thread-safe. You must only ever use this module from |
|
|
254 | the same thread (this requirement might be loosened in the future to |
|
|
255 | allow per-thread schedulers, but Coro::State does not yet allow this). |
|
|
256 | |
221 | =head1 SEE ALSO |
257 | =head1 SEE ALSO |
222 | |
258 | |
223 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
259 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
224 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
260 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
225 | |
261 | |