… | |
… | |
32 | |
32 | |
33 | =cut |
33 | =cut |
34 | |
34 | |
35 | package Coro; |
35 | package Coro; |
36 | |
36 | |
|
|
37 | no warnings qw(uninitialized); |
|
|
38 | |
37 | use Coro::State; |
39 | use Coro::State; |
38 | |
40 | |
39 | use base Exporter; |
41 | use base Exporter; |
40 | |
42 | |
41 | $VERSION = 0.45; |
43 | $VERSION = 0.5; |
42 | |
44 | |
43 | @EXPORT = qw(async cede schedule terminate current); |
45 | @EXPORT = qw(async cede schedule terminate current); |
44 | @EXPORT_OK = qw($current); |
46 | %EXPORT_TAGS = ( |
|
|
47 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
|
|
48 | ); |
|
|
49 | @EXPORT_OK = @{$EXPORT_TAGS{prio}}; |
45 | |
50 | |
46 | { |
51 | { |
47 | my @async; |
52 | my @async; |
48 | my $init; |
53 | my $init; |
49 | |
54 | |
… | |
… | |
226 | =cut |
231 | =cut |
227 | |
232 | |
228 | sub cancel { |
233 | sub cancel { |
229 | push @destroy, $_[0]; |
234 | push @destroy, $_[0]; |
230 | $manager->ready; |
235 | $manager->ready; |
|
|
236 | &schedule if $current == $_[0]; |
|
|
237 | } |
|
|
238 | |
|
|
239 | =item $oldprio = $process->prio($newprio) |
|
|
240 | |
|
|
241 | Sets the priority of the process. Higher priority processes get run before |
|
|
242 | lower priority processes. Priorities are smalled signed integer (currently |
|
|
243 | -4 .. +3), that you can refer to using PRIO_xxx constants (use the import |
|
|
244 | tag :prio to get then): |
|
|
245 | |
|
|
246 | PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN |
|
|
247 | 3 > 1 > 0 > -1 > -3 > -4 |
|
|
248 | |
|
|
249 | # set priority to HIGH |
|
|
250 | current->prio(PRIO_HIGH); |
|
|
251 | |
|
|
252 | The idle coroutine ($Coro::idle) always has a lower priority than any |
|
|
253 | existing coroutine. |
|
|
254 | |
|
|
255 | Changing the priority of the current process will take effect immediately, |
|
|
256 | but changing the priority of processes in the ready queue (but not |
|
|
257 | running) will only take effect after the next schedule (of that |
|
|
258 | process). This is a bug that will be fixed in some future version. |
|
|
259 | |
|
|
260 | =cut |
|
|
261 | |
|
|
262 | sub prio { |
|
|
263 | my $old = $_[0]{prio}; |
|
|
264 | $_[0]{prio} = $_[1] if @_ > 1; |
|
|
265 | $old; |
|
|
266 | } |
|
|
267 | |
|
|
268 | =item $newprio = $process->nice($change) |
|
|
269 | |
|
|
270 | Similar to C<prio>, but subtract the given value from the priority (i.e. |
|
|
271 | higher values mean lower priority, just as in unix). |
|
|
272 | |
|
|
273 | =cut |
|
|
274 | |
|
|
275 | sub nice { |
|
|
276 | $_[0]{prio} -= $_[1]; |
231 | } |
277 | } |
232 | |
278 | |
233 | =back |
279 | =back |
234 | |
280 | |
235 | =cut |
281 | =cut |
236 | |
282 | |
237 | 1; |
283 | 1; |
238 | |
284 | |
239 | =head1 BUGS/LIMITATIONS |
285 | =head1 BUGS/LIMITATIONS |
240 | |
286 | |
241 | - could be faster, especially when the core would introduce special |
287 | - you must make very sure that no coro is still active on global destruction. |
242 | support for coroutines (like it does for threads). |
288 | very bad things might happen otherwise (usually segfaults). |
243 | - there is still a memleak on coroutine termination that I could not |
|
|
244 | identify. Could be as small as a single SV. |
|
|
245 | - this module is not well-tested. |
|
|
246 | - if variables or arguments "disappear" (become undef) or become |
|
|
247 | corrupted please contact the author so he cen iron out the |
|
|
248 | remaining bugs. |
|
|
249 | - this module is not thread-safe. You must only ever use this module from |
289 | - this module is not thread-safe. You must only ever use this module from |
250 | the same thread (this requirement might be loosened in the future to |
290 | the same thread (this requirement might be loosened in the future to |
251 | allow per-thread schedulers, but Coro::State does not yet allow this). |
291 | allow per-thread schedulers, but Coro::State does not yet allow this). |
252 | |
292 | |
253 | =head1 SEE ALSO |
293 | =head1 SEE ALSO |