… | |
… | |
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 + 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 | |
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.06; |
41 | $VERSION = 0.12; |
37 | |
42 | |
38 | @EXPORT = qw(async yield schedule terminate); |
43 | @EXPORT = qw(async cede 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, @_); |
… | |
… | |
52 | my @attrs; |
55 | my @attrs; |
53 | for (@_) { |
56 | for (@_) { |
54 | if ($_ eq "Coro") { |
57 | if ($_ eq "Coro") { |
55 | push @async, $ref; |
58 | push @async, $ref; |
56 | } else { |
59 | } else { |
57 | push @attrs, @_; |
60 | push @attrs, $_; |
58 | } |
61 | } |
59 | } |
62 | } |
60 | return $old ? $old->($package, $name, @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. |
… | |
… | |
98 | our $idle = new Coro sub { |
103 | our $idle = new Coro sub { |
99 | print STDERR "FATAL: deadlock detected\n"; |
104 | print STDERR "FATAL: deadlock detected\n"; |
100 | exit(51); |
105 | exit(51); |
101 | }; |
106 | }; |
102 | |
107 | |
|
|
108 | # this coroutine is necessary because a coroutine |
|
|
109 | # cannot destroy itself. |
|
|
110 | my @destroy; |
|
|
111 | my $manager = new Coro sub { |
|
|
112 | while() { |
|
|
113 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
|
|
114 | &schedule; |
|
|
115 | } |
|
|
116 | }; |
|
|
117 | |
103 | # we really need priorities... |
118 | # we really need priorities... |
104 | ## my @ready; #d# |
|
|
105 | our @ready = (); # the ready queue. hehe, rather broken ;) |
119 | my @ready; # the ready queue. hehe, rather broken ;) |
106 | |
120 | |
107 | # static methods. not really. |
121 | # static methods. not really. |
108 | |
122 | |
109 | =head2 STATIC METHODS |
123 | =head2 STATIC METHODS |
110 | |
124 | |
… | |
… | |
128 | |
142 | |
129 | =cut |
143 | =cut |
130 | |
144 | |
131 | sub async(&@) { |
145 | sub async(&@) { |
132 | my $pid = new Coro @_; |
146 | my $pid = new Coro @_; |
|
|
147 | $manager->ready; # this ensures that the stack is cloned from the manager |
133 | $pid->ready; |
148 | $pid->ready; |
134 | $pid; |
149 | $pid; |
135 | } |
150 | } |
136 | |
151 | |
137 | =item schedule |
152 | =item schedule |
… | |
… | |
148 | # should be done using priorities :( |
163 | # should be done using priorities :( |
149 | ($prev, $current) = ($current, shift @ready || $idle); |
164 | ($prev, $current) = ($current, shift @ready || $idle); |
150 | Coro::State::transfer($prev, $current); |
165 | Coro::State::transfer($prev, $current); |
151 | } |
166 | } |
152 | |
167 | |
153 | =item yield |
168 | =item cede |
154 | |
169 | |
155 | Yield to other processes. This function puts the current process into the |
170 | "Cede" to other processes. This function puts the current process into the |
156 | ready queue and calls C<schedule>. |
171 | ready queue and calls C<schedule>, which has the effect of giving up the |
|
|
172 | current "timeslice" to other coroutines of the same or higher priority. |
157 | |
173 | |
158 | =cut |
174 | =cut |
159 | |
175 | |
160 | sub yield { |
176 | sub cede { |
161 | $current->ready; |
177 | $current->ready; |
162 | &schedule; |
178 | &schedule; |
163 | } |
179 | } |
164 | |
180 | |
165 | =item terminate |
181 | =item terminate |
… | |
… | |
169 | Future versions of this function will allow result arguments. |
185 | Future versions of this function will allow result arguments. |
170 | |
186 | |
171 | =cut |
187 | =cut |
172 | |
188 | |
173 | sub terminate { |
189 | sub terminate { |
174 | $current->{_results} = [@_]; |
190 | push @destroy, $current; |
|
|
191 | $manager->ready; |
175 | &schedule; |
192 | &schedule; |
|
|
193 | # NORETURN |
176 | } |
194 | } |
177 | |
195 | |
178 | =back |
196 | =back |
179 | |
197 | |
180 | # dynamic methods |
198 | # dynamic methods |
… | |
… | |
221 | |
239 | |
222 | =cut |
240 | =cut |
223 | |
241 | |
224 | 1; |
242 | 1; |
225 | |
243 | |
226 | =head1 BUGS |
244 | =head1 BUGS/LIMITATIONS |
227 | |
245 | |
228 | - could be faster, especially when the core would introduce special |
246 | - could be faster, especially when the core would introduce special |
229 | support for coroutines (like it does for threads). |
247 | support for coroutines (like it does for threads). |
230 | - there is still a memleak on coroutine termination that I could not |
248 | - there is still a memleak on coroutine termination that I could not |
231 | identify. Could be as small as a single SV. |
249 | identify. Could be as small as a single SV. |
232 | - this module is not well-tested. |
250 | - this module is not well-tested. |
|
|
251 | - if variables or arguments "disappear" (become undef) or become |
|
|
252 | corrupted please contact the author so he cen iron out the |
|
|
253 | remaining bugs. |
|
|
254 | - this module is not thread-safe. You must only ever use this module from |
|
|
255 | the same thread (this requirement might be loosened in the future to |
|
|
256 | allow per-thread schedulers, but Coro::State does not yet allow this). |
233 | |
257 | |
234 | =head1 SEE ALSO |
258 | =head1 SEE ALSO |
235 | |
259 | |
236 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
260 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
237 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
261 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |