… | |
… | |
41 | |
41 | |
42 | our $idle; # idle coroutine |
42 | our $idle; # idle coroutine |
43 | our $main; # main coroutine |
43 | our $main; # main coroutine |
44 | our $current; # current coroutine |
44 | our $current; # current coroutine |
45 | |
45 | |
46 | our $VERSION = 1.7; |
46 | our $VERSION = '2.5'; |
47 | |
47 | |
48 | our @EXPORT = qw(async cede schedule terminate current); |
48 | our @EXPORT = qw(async cede schedule terminate current); |
49 | our %EXPORT_TAGS = ( |
49 | our %EXPORT_TAGS = ( |
50 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
50 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
51 | ); |
51 | ); |
… | |
… | |
141 | |
141 | |
142 | # the next line destroys the _coro_state, but keeps the |
142 | # the next line destroys the _coro_state, but keeps the |
143 | # process itself intact (we basically make it a zombie |
143 | # process itself intact (we basically make it a zombie |
144 | # process that always runs the manager thread, so it's possible |
144 | # process that always runs the manager thread, so it's possible |
145 | # to transfer() to this process). |
145 | # to transfer() to this process). |
146 | $coro->{_coro_state} = $manager->{_coro_state}; |
146 | $coro->{_coro_state}->_clone_state_from ($manager->{_coro_state}); |
147 | } |
147 | } |
148 | &schedule; |
148 | &schedule; |
149 | } |
149 | } |
150 | }; |
150 | }; |
151 | |
151 | |
… | |
… | |
162 | =item async { ... } [@args...] |
162 | =item async { ... } [@args...] |
163 | |
163 | |
164 | Create a new asynchronous process and return it's process object |
164 | Create a new asynchronous process and return it's process object |
165 | (usually unused). When the sub returns the new process is automatically |
165 | (usually unused). When the sub returns the new process is automatically |
166 | terminated. |
166 | terminated. |
|
|
167 | |
|
|
168 | When the coroutine dies, the program will exit, just as in the main |
|
|
169 | program. |
167 | |
170 | |
168 | # create a new coroutine that just prints its arguments |
171 | # create a new coroutine that just prints its arguments |
169 | async { |
172 | async { |
170 | print "@_\n"; |
173 | print "@_\n"; |
171 | } 1,2,3,4; |
174 | } 1,2,3,4; |
… | |
… | |
229 | } |
232 | } |
230 | |
233 | |
231 | sub new { |
234 | sub new { |
232 | my $class = shift; |
235 | my $class = shift; |
233 | bless { |
236 | bless { |
234 | _coro_state => (new Coro::State $_[0] && \&_newcoro, @_), |
237 | _coro_state => (new Coro::State \&_newcoro, @_), |
235 | }, $class; |
238 | }, $class; |
236 | } |
239 | } |
237 | |
240 | |
238 | =item $process->ready |
241 | =item $process->ready |
239 | |
242 | |
… | |
… | |
241 | |
244 | |
242 | =cut |
245 | =cut |
243 | |
246 | |
244 | =item $process->cancel (arg...) |
247 | =item $process->cancel (arg...) |
245 | |
248 | |
246 | Temrinates the given process and makes it return the given arguments as |
249 | Terminates the given process and makes it return the given arguments as |
247 | status (default: the empty list). |
250 | status (default: the empty list). |
248 | |
251 | |
249 | =cut |
252 | =cut |
250 | |
253 | |
251 | sub cancel { |
254 | sub cancel { |
… | |
… | |
271 | &schedule; |
274 | &schedule; |
272 | } |
275 | } |
273 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
276 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
274 | } |
277 | } |
275 | |
278 | |
276 | =item $oldprio = $process->prio($newprio) |
279 | =item $oldprio = $process->prio ($newprio) |
277 | |
280 | |
278 | Sets (or gets, if the argument is missing) the priority of the |
281 | Sets (or gets, if the argument is missing) the priority of the |
279 | process. Higher priority processes get run before lower priority |
282 | process. Higher priority processes get run before lower priority |
280 | processes. Priorities are small signed integers (currently -4 .. +3), |
283 | processes. Priorities are small signed integers (currently -4 .. +3), |
281 | that you can refer to using PRIO_xxx constants (use the import tag :prio |
284 | that you can refer to using PRIO_xxx constants (use the import tag :prio |
… | |
… | |
296 | process). This is a bug that will be fixed in some future version. |
299 | process). This is a bug that will be fixed in some future version. |
297 | |
300 | |
298 | =cut |
301 | =cut |
299 | |
302 | |
300 | sub prio { |
303 | sub prio { |
301 | my $old = $_[0]{prio}; |
304 | shift->{_coro_state}->prio (@_) |
302 | $_[0]{prio} = $_[1] if @_ > 1; |
|
|
303 | $old; |
|
|
304 | } |
305 | } |
305 | |
306 | |
306 | =item $newprio = $process->nice($change) |
307 | =item $newprio = $process->nice ($change) |
307 | |
308 | |
308 | Similar to C<prio>, but subtract the given value from the priority (i.e. |
309 | Similar to C<prio>, but subtract the given value from the priority (i.e. |
309 | higher values mean lower priority, just as in unix). |
310 | higher values mean lower priority, just as in unix). |
310 | |
311 | |
311 | =cut |
312 | =cut |
312 | |
313 | |
313 | sub nice { |
314 | sub nice { |
314 | $_[0]{prio} -= $_[1]; |
315 | shift->{_coro_state}->nice (@_) |
315 | } |
316 | } |
316 | |
317 | |
317 | =item $olddesc = $process->desc($newdesc) |
318 | =item $olddesc = $process->desc ($newdesc) |
318 | |
319 | |
319 | Sets (or gets in case the argument is missing) the description for this |
320 | Sets (or gets in case the argument is missing) the description for this |
320 | process. This is just a free-form string you can associate with a process. |
321 | process. This is just a free-form string you can associate with a process. |
321 | |
322 | |
322 | =cut |
323 | =cut |