… | |
… | |
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.49; |
43 | $VERSION = 0.51; |
42 | |
44 | |
43 | @EXPORT = qw(async cede schedule terminate current); |
45 | @EXPORT = qw(async cede schedule terminate current); |
44 | %EXPORT_TAGS = ( |
46 | %EXPORT_TAGS = ( |
45 | 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)], |
46 | ); |
48 | ); |
… | |
… | |
114 | }; |
116 | }; |
115 | |
117 | |
116 | # this coroutine is necessary because a coroutine |
118 | # this coroutine is necessary because a coroutine |
117 | # cannot destroy itself. |
119 | # cannot destroy itself. |
118 | my @destroy; |
120 | my @destroy; |
|
|
121 | my $manager; |
119 | my $manager = new Coro sub { |
122 | $manager = new Coro sub { |
120 | while() { |
123 | while() { |
121 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
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 |
|
|
126 | # been readied multiple times. this is harmless since the manager |
|
|
127 | # can be called as many times as neccessary and will always |
|
|
128 | # remove itself from the runqueue |
|
|
129 | (pop @destroy)->{_coro_state} = $manager->{_coro_state} while @destroy; |
122 | &schedule; |
130 | &schedule; |
123 | } |
131 | } |
124 | }; |
132 | }; |
125 | |
133 | |
126 | # static methods. not really. |
134 | # static methods. not really. |
… | |
… | |
216 | }, $class; |
224 | }, $class; |
217 | } |
225 | } |
218 | |
226 | |
219 | =item $process->ready |
227 | =item $process->ready |
220 | |
228 | |
221 | Put the current process into the ready queue. |
229 | Put the given process into the ready queue. |
222 | |
230 | |
223 | =cut |
231 | =cut |
224 | |
232 | |
225 | =item $process->cancel |
233 | =item $process->cancel |
226 | |
234 | |
… | |
… | |
229 | =cut |
237 | =cut |
230 | |
238 | |
231 | sub cancel { |
239 | sub cancel { |
232 | push @destroy, $_[0]; |
240 | push @destroy, $_[0]; |
233 | $manager->ready; |
241 | $manager->ready; |
|
|
242 | &schedule if $current == $_[0]; |
234 | } |
243 | } |
235 | |
244 | |
236 | =item $oldprio = $process->prio($newprio) |
245 | =item $oldprio = $process->prio($newprio) |
237 | |
246 | |
238 | Sets the priority of the process. Higher priority processes get run before |
247 | Sets the priority of the process. Higher priority processes get run before |
… | |
… | |
279 | |
288 | |
280 | 1; |
289 | 1; |
281 | |
290 | |
282 | =head1 BUGS/LIMITATIONS |
291 | =head1 BUGS/LIMITATIONS |
283 | |
292 | |
284 | - could be faster, especially when the core would introduce special |
293 | - you must make very sure that no coro is still active on global destruction. |
285 | support for coroutines (like it does for threads). |
294 | very bad things might happen otherwise (usually segfaults). |
286 | - there is still a memleak on coroutine termination that I could not |
|
|
287 | identify. Could be as small as a single SV. |
|
|
288 | - this module is not well-tested. |
|
|
289 | - if variables or arguments "disappear" (become undef) or become |
|
|
290 | corrupted please contact the author so he cen iron out the |
|
|
291 | remaining bugs. |
|
|
292 | - this module is not thread-safe. You must only ever use this module from |
295 | - this module is not thread-safe. You must only ever use this module from |
293 | the same thread (this requirement might be loosened in the future to |
296 | the same thread (this requirement might be loosened in the future to |
294 | allow per-thread schedulers, but Coro::State does not yet allow this). |
297 | allow per-thread schedulers, but Coro::State does not yet allow this). |
295 | |
298 | |
296 | =head1 SEE ALSO |
299 | =head1 SEE ALSO |