… | |
… | |
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 |
28 | In this module, coroutines are defined as "callchain + lexical variables |
29 | + @_ + $_ + $@ + $^W), that is, a coroutine has it's own callchain, it's |
29 | + @_ + $_ + $@ + $^W + C stack), that is, a coroutine has it's own |
30 | own set of lexicals and it's own set of perl's most important global |
30 | callchain, it's own set of lexicals and it's own set of perl's most |
31 | variables. |
31 | important global variables. |
32 | |
32 | |
33 | =cut |
33 | =cut |
34 | |
34 | |
35 | package Coro; |
35 | package Coro; |
36 | |
36 | |
37 | use Coro::State; |
37 | use Coro::State; |
38 | |
38 | |
39 | use base Exporter; |
39 | use base Exporter; |
40 | |
40 | |
41 | $VERSION = 0.10; |
41 | $VERSION = 0.12; |
42 | |
42 | |
43 | @EXPORT = qw(async yield schedule terminate current); |
43 | @EXPORT = qw(async cede schedule terminate current); |
44 | @EXPORT_OK = qw($current); |
44 | @EXPORT_OK = qw($current); |
45 | |
45 | |
46 | { |
46 | { |
47 | my @async; |
47 | my @async; |
48 | |
48 | |
… | |
… | |
103 | our $idle = new Coro sub { |
103 | our $idle = new Coro sub { |
104 | print STDERR "FATAL: deadlock detected\n"; |
104 | print STDERR "FATAL: deadlock detected\n"; |
105 | exit(51); |
105 | exit(51); |
106 | }; |
106 | }; |
107 | |
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 | |
108 | # we really need priorities... |
118 | # we really need priorities... |
109 | my @ready; # the ready queue. hehe, rather broken ;) |
119 | my @ready; # the ready queue. hehe, rather broken ;) |
110 | |
120 | |
111 | # static methods. not really. |
121 | # static methods. not really. |
112 | |
122 | |
… | |
… | |
132 | |
142 | |
133 | =cut |
143 | =cut |
134 | |
144 | |
135 | sub async(&@) { |
145 | sub async(&@) { |
136 | my $pid = new Coro @_; |
146 | my $pid = new Coro @_; |
|
|
147 | $manager->ready; # this ensures that the stack is cloned from the manager |
137 | $pid->ready; |
148 | $pid->ready; |
138 | $pid; |
149 | $pid; |
139 | } |
150 | } |
140 | |
151 | |
141 | =item schedule |
152 | =item schedule |
… | |
… | |
152 | # should be done using priorities :( |
163 | # should be done using priorities :( |
153 | ($prev, $current) = ($current, shift @ready || $idle); |
164 | ($prev, $current) = ($current, shift @ready || $idle); |
154 | Coro::State::transfer($prev, $current); |
165 | Coro::State::transfer($prev, $current); |
155 | } |
166 | } |
156 | |
167 | |
157 | =item yield |
168 | =item cede |
158 | |
169 | |
159 | 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 |
160 | 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. |
161 | |
173 | |
162 | =cut |
174 | =cut |
163 | |
175 | |
164 | sub yield { |
176 | sub cede { |
165 | $current->ready; |
177 | $current->ready; |
166 | &schedule; |
178 | &schedule; |
167 | } |
179 | } |
168 | |
180 | |
169 | =item terminate |
181 | =item terminate |
… | |
… | |
173 | Future versions of this function will allow result arguments. |
185 | Future versions of this function will allow result arguments. |
174 | |
186 | |
175 | =cut |
187 | =cut |
176 | |
188 | |
177 | sub terminate { |
189 | sub terminate { |
178 | $current->{_results} = [@_]; |
190 | push @destroy, $current; |
|
|
191 | $manager->ready; |
179 | &schedule; |
192 | &schedule; |
|
|
193 | # NORETURN |
180 | } |
194 | } |
181 | |
195 | |
182 | =back |
196 | =back |
183 | |
197 | |
184 | # dynamic methods |
198 | # dynamic methods |
… | |
… | |
242 | allow per-thread schedulers, but Coro::State does not yet allow this). |
256 | allow per-thread schedulers, but Coro::State does not yet allow this). |
243 | |
257 | |
244 | =head1 SEE ALSO |
258 | =head1 SEE ALSO |
245 | |
259 | |
246 | 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>, |
247 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
261 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>, |
|
|
262 | L<Coro::L<Coro::Handle>, L<Coro::Socket>. |
248 | |
263 | |
249 | =head1 AUTHOR |
264 | =head1 AUTHOR |
250 | |
265 | |
251 | Marc Lehmann <pcg@goof.com> |
266 | Marc Lehmann <pcg@goof.com> |
252 | http://www.goof.com/pcg/marc/ |
267 | http://www.goof.com/pcg/marc/ |