… | |
… | |
38 | |
38 | |
39 | use Coro::State; |
39 | use Coro::State; |
40 | |
40 | |
41 | use base Exporter; |
41 | use base Exporter; |
42 | |
42 | |
43 | $VERSION = 0.51; |
43 | $VERSION = 0.52; |
44 | |
44 | |
45 | @EXPORT = qw(async cede schedule terminate current); |
45 | @EXPORT = qw(async cede schedule terminate current); |
46 | %EXPORT_TAGS = ( |
46 | %EXPORT_TAGS = ( |
47 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
47 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
48 | ); |
48 | ); |
… | |
… | |
124 | # by overwriting the state object with the manager we destroy it |
124 | # by overwriting the state object with the manager we destroy it |
125 | # while still being able to schedule this coroutine (in case it has |
125 | # while still being able to schedule this coroutine (in case it has |
126 | # been readied multiple times. this is harmless since the manager |
126 | # been readied multiple times. this is harmless since the manager |
127 | # can be called as many times as neccessary and will always |
127 | # can be called as many times as neccessary and will always |
128 | # remove itself from the runqueue |
128 | # remove itself from the runqueue |
|
|
129 | while (@destroy) { |
|
|
130 | my $coro = pop @destroy; |
|
|
131 | $coro->{status} ||= []; |
|
|
132 | $_->ready for @{delete $coro->{join} || []}; |
129 | (pop @destroy)->{_coro_state} = $manager->{_coro_state} while @destroy; |
133 | $coro->{_coro_state} = $manager->{_coro_state}; |
|
|
134 | } |
130 | &schedule; |
135 | &schedule; |
131 | } |
136 | } |
132 | }; |
137 | }; |
133 | |
138 | |
134 | # static methods. not really. |
139 | # static methods. not really. |
… | |
… | |
176 | ready queue and calls C<schedule>, which has the effect of giving up the |
181 | 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. |
182 | current "timeslice" to other coroutines of the same or higher priority. |
178 | |
183 | |
179 | =cut |
184 | =cut |
180 | |
185 | |
181 | =item terminate |
186 | =item terminate [arg...] |
182 | |
187 | |
183 | Terminates the current process. |
188 | Terminates the current process. |
184 | |
189 | |
185 | Future versions of this function will allow result arguments. |
190 | Future versions of this function will allow result arguments. |
186 | |
191 | |
187 | =cut |
192 | =cut |
188 | |
193 | |
189 | sub terminate { |
194 | sub terminate { |
|
|
195 | $current->{status} = [@_]; |
190 | $current->cancel; |
196 | $current->cancel; |
191 | &schedule; |
197 | &schedule; |
192 | die; # NORETURN |
198 | die; # NORETURN |
193 | } |
199 | } |
194 | |
200 | |
… | |
… | |
203 | =over 4 |
209 | =over 4 |
204 | |
210 | |
205 | =item new Coro \&sub [, @args...] |
211 | =item new Coro \&sub [, @args...] |
206 | |
212 | |
207 | Create a new process and return it. When the sub returns the process |
213 | Create a new process and return it. When the sub returns the process |
208 | automatically terminates. To start the process you must first put it into |
214 | automatically terminates as if C<terminate> with the returned values were |
|
|
215 | called. To make the process run you must first put it into the ready queue |
209 | the ready queue by calling the ready method. |
216 | by calling the ready method. |
210 | |
|
|
211 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
212 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
213 | |
217 | |
214 | =cut |
218 | =cut |
215 | |
219 | |
216 | sub _newcoro { |
220 | sub _newcoro { |
217 | terminate &{+shift}; |
221 | terminate &{+shift}; |
… | |
… | |
240 | push @destroy, $_[0]; |
244 | push @destroy, $_[0]; |
241 | $manager->ready; |
245 | $manager->ready; |
242 | &schedule if $current == $_[0]; |
246 | &schedule if $current == $_[0]; |
243 | } |
247 | } |
244 | |
248 | |
|
|
249 | =item $process->join |
|
|
250 | |
|
|
251 | Wait until the coroutine terminates and return any values given to the |
|
|
252 | C<terminate> function. C<join> can be called multiple times from multiple |
|
|
253 | processes. |
|
|
254 | |
|
|
255 | =cut |
|
|
256 | |
|
|
257 | sub join { |
|
|
258 | my $self = shift; |
|
|
259 | unless ($self->{status}) { |
|
|
260 | push @{$self->{join}}, $current; |
|
|
261 | &schedule; |
|
|
262 | } |
|
|
263 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
|
|
264 | } |
|
|
265 | |
245 | =item $oldprio = $process->prio($newprio) |
266 | =item $oldprio = $process->prio($newprio) |
246 | |
267 | |
247 | Sets the priority of the process. Higher priority processes get run before |
268 | Sets (or gets, if the argument is missing) the priority of the |
|
|
269 | process. Higher priority processes get run before lower priority |
248 | lower priority processes. Priorities are smalled signed integer (currently |
270 | processes. Priorities are smalled signed integer (currently -4 .. +3), |
249 | -4 .. +3), that you can refer to using PRIO_xxx constants (use the import |
271 | that you can refer to using PRIO_xxx constants (use the import tag :prio |
250 | tag :prio to get then): |
272 | to get then): |
251 | |
273 | |
252 | PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN |
274 | PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN |
253 | 3 > 1 > 0 > -1 > -3 > -4 |
275 | 3 > 1 > 0 > -1 > -3 > -4 |
254 | |
276 | |
255 | # set priority to HIGH |
277 | # set priority to HIGH |
… | |
… | |
278 | |
300 | |
279 | =cut |
301 | =cut |
280 | |
302 | |
281 | sub nice { |
303 | sub nice { |
282 | $_[0]{prio} -= $_[1]; |
304 | $_[0]{prio} -= $_[1]; |
|
|
305 | } |
|
|
306 | |
|
|
307 | =item $olddesc = $process->desc($newdesc) |
|
|
308 | |
|
|
309 | Sets (or gets in case the argument is missing) the description for this |
|
|
310 | process. This is just a free-form string you can associate with a process. |
|
|
311 | |
|
|
312 | =cut |
|
|
313 | |
|
|
314 | sub desc { |
|
|
315 | my $old = $_[0]{desc}; |
|
|
316 | $_[0]{desc} = $_[1] if @_ > 1; |
|
|
317 | $old; |
283 | } |
318 | } |
284 | |
319 | |
285 | =back |
320 | =back |
286 | |
321 | |
287 | =cut |
322 | =cut |