… | |
… | |
19 | cede; |
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 | |
|
|
26 | This module is still experimental, see the BUGS section below. |
|
|
27 | |
25 | |
28 | In this module, coroutines are defined as "callchain + lexical variables |
26 | In this module, coroutines are defined as "callchain + lexical variables |
29 | + @_ + $_ + $@ + $^W + C stack), that is, a coroutine has it's own |
27 | + @_ + $_ + $@ + $^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 |
28 | callchain, it's own set of lexicals and it's own set of perl's most |
31 | important global variables. |
29 | important global variables. |
32 | |
30 | |
33 | =cut |
31 | =cut |
34 | |
32 | |
35 | package Coro; |
33 | package Coro; |
36 | |
34 | |
|
|
35 | no warnings qw(uninitialized); |
|
|
36 | |
37 | use Coro::State; |
37 | use Coro::State; |
38 | |
38 | |
39 | use base Exporter; |
39 | use base Exporter; |
40 | |
40 | |
41 | $VERSION = 0.49; |
41 | $VERSION = 0.52; |
42 | |
42 | |
43 | @EXPORT = qw(async cede schedule terminate current); |
43 | @EXPORT = qw(async cede schedule terminate current); |
44 | %EXPORT_TAGS = ( |
44 | %EXPORT_TAGS = ( |
45 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
45 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
46 | ); |
46 | ); |
… | |
… | |
114 | }; |
114 | }; |
115 | |
115 | |
116 | # this coroutine is necessary because a coroutine |
116 | # this coroutine is necessary because a coroutine |
117 | # cannot destroy itself. |
117 | # cannot destroy itself. |
118 | my @destroy; |
118 | my @destroy; |
|
|
119 | my $manager; |
119 | my $manager = new Coro sub { |
120 | $manager = new Coro sub { |
120 | while() { |
121 | while() { |
121 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
122 | # by overwriting the state object with the manager we destroy it |
|
|
123 | # while still being able to schedule this coroutine (in case it has |
|
|
124 | # been readied multiple times. this is harmless since the manager |
|
|
125 | # can be called as many times as neccessary and will always |
|
|
126 | # remove itself from the runqueue |
|
|
127 | while (@destroy) { |
|
|
128 | my $coro = pop @destroy; |
|
|
129 | $coro->{status} ||= []; |
|
|
130 | $_->ready for @{delete $coro->{join} || []}; |
|
|
131 | $coro->{_coro_state} = $manager->{_coro_state}; |
|
|
132 | } |
122 | &schedule; |
133 | &schedule; |
123 | } |
134 | } |
124 | }; |
135 | }; |
125 | |
136 | |
126 | # static methods. not really. |
137 | # static methods. not really. |
… | |
… | |
168 | ready queue and calls C<schedule>, which has the effect of giving up the |
179 | ready queue and calls C<schedule>, which has the effect of giving up the |
169 | current "timeslice" to other coroutines of the same or higher priority. |
180 | current "timeslice" to other coroutines of the same or higher priority. |
170 | |
181 | |
171 | =cut |
182 | =cut |
172 | |
183 | |
173 | =item terminate |
184 | =item terminate [arg...] |
174 | |
185 | |
175 | Terminates the current process. |
186 | Terminates the current process. |
176 | |
187 | |
177 | Future versions of this function will allow result arguments. |
188 | Future versions of this function will allow result arguments. |
178 | |
189 | |
179 | =cut |
190 | =cut |
180 | |
191 | |
181 | sub terminate { |
192 | sub terminate { |
|
|
193 | $current->{status} = [@_]; |
182 | $current->cancel; |
194 | $current->cancel; |
183 | &schedule; |
195 | &schedule; |
184 | die; # NORETURN |
196 | die; # NORETURN |
185 | } |
197 | } |
186 | |
198 | |
… | |
… | |
195 | =over 4 |
207 | =over 4 |
196 | |
208 | |
197 | =item new Coro \&sub [, @args...] |
209 | =item new Coro \&sub [, @args...] |
198 | |
210 | |
199 | Create a new process and return it. When the sub returns the process |
211 | Create a new process and return it. When the sub returns the process |
200 | automatically terminates. To start the process you must first put it into |
212 | automatically terminates as if C<terminate> with the returned values were |
|
|
213 | called. To make the process run you must first put it into the ready queue |
201 | the ready queue by calling the ready method. |
214 | by calling the ready method. |
202 | |
|
|
203 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
204 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
205 | |
215 | |
206 | =cut |
216 | =cut |
207 | |
217 | |
208 | sub _newcoro { |
218 | sub _newcoro { |
209 | terminate &{+shift}; |
219 | terminate &{+shift}; |
… | |
… | |
216 | }, $class; |
226 | }, $class; |
217 | } |
227 | } |
218 | |
228 | |
219 | =item $process->ready |
229 | =item $process->ready |
220 | |
230 | |
221 | Put the current process into the ready queue. |
231 | Put the given process into the ready queue. |
222 | |
232 | |
223 | =cut |
233 | =cut |
224 | |
234 | |
225 | =item $process->cancel |
235 | =item $process->cancel |
226 | |
236 | |
… | |
… | |
229 | =cut |
239 | =cut |
230 | |
240 | |
231 | sub cancel { |
241 | sub cancel { |
232 | push @destroy, $_[0]; |
242 | push @destroy, $_[0]; |
233 | $manager->ready; |
243 | $manager->ready; |
|
|
244 | &schedule if $current == $_[0]; |
|
|
245 | } |
|
|
246 | |
|
|
247 | =item $process->join |
|
|
248 | |
|
|
249 | Wait until the coroutine terminates and return any values given to the |
|
|
250 | C<terminate> function. C<join> can be called multiple times from multiple |
|
|
251 | processes. |
|
|
252 | |
|
|
253 | =cut |
|
|
254 | |
|
|
255 | sub join { |
|
|
256 | my $self = shift; |
|
|
257 | unless ($self->{status}) { |
|
|
258 | push @{$self->{join}}, $current; |
|
|
259 | &schedule; |
|
|
260 | } |
|
|
261 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
234 | } |
262 | } |
235 | |
263 | |
236 | =item $oldprio = $process->prio($newprio) |
264 | =item $oldprio = $process->prio($newprio) |
237 | |
265 | |
238 | Sets the priority of the process. Higher priority processes get run before |
266 | Sets (or gets, if the argument is missing) the priority of the |
|
|
267 | process. Higher priority processes get run before lower priority |
239 | lower priority processes. Priorities are smalled signed integer (currently |
268 | processes. Priorities are smalled signed integer (currently -4 .. +3), |
240 | -4 .. +3), that you can refer to using PRIO_xxx constants (use the import |
269 | that you can refer to using PRIO_xxx constants (use the import tag :prio |
241 | tag :prio to get then): |
270 | to get then): |
242 | |
271 | |
243 | PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN |
272 | PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN |
244 | 3 > 1 > 0 > -1 > -3 > -4 |
273 | 3 > 1 > 0 > -1 > -3 > -4 |
245 | |
274 | |
246 | # set priority to HIGH |
275 | # set priority to HIGH |
… | |
… | |
271 | |
300 | |
272 | sub nice { |
301 | sub nice { |
273 | $_[0]{prio} -= $_[1]; |
302 | $_[0]{prio} -= $_[1]; |
274 | } |
303 | } |
275 | |
304 | |
|
|
305 | =item $olddesc = $process->desc($newdesc) |
|
|
306 | |
|
|
307 | Sets (or gets in case the argument is missing) the description for this |
|
|
308 | process. This is just a free-form string you can associate with a process. |
|
|
309 | |
|
|
310 | =cut |
|
|
311 | |
|
|
312 | sub desc { |
|
|
313 | my $old = $_[0]{desc}; |
|
|
314 | $_[0]{desc} = $_[1] if @_ > 1; |
|
|
315 | $old; |
|
|
316 | } |
|
|
317 | |
276 | =back |
318 | =back |
277 | |
319 | |
278 | =cut |
320 | =cut |
279 | |
321 | |
280 | 1; |
322 | 1; |
281 | |
323 | |
282 | =head1 BUGS/LIMITATIONS |
324 | =head1 BUGS/LIMITATIONS |
283 | |
325 | |
284 | - you must make very sure that no coro is still active on global destruction. |
326 | - you must make very sure that no coro is still active on global destruction. |
285 | very bad things might happen otherwise (usually segfaults). |
327 | very bad things might happen otherwise (usually segfaults). |
286 | - this module is not thread-safe. You must only ever use this module from |
328 | - this module is not thread-safe. You should only ever use this module from |
287 | the same thread (this requirement might be loosened in the future to |
329 | the same thread (this requirement might be loosened in the future to |
288 | allow per-thread schedulers, but Coro::State does not yet allow this). |
330 | allow per-thread schedulers, but Coro::State does not yet allow this). |
289 | |
331 | |
290 | =head1 SEE ALSO |
332 | =head1 SEE ALSO |
291 | |
333 | |