… | |
… | |
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.5; |
43 | $VERSION = 0.51; |
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 | ); |
… | |
… | |
116 | }; |
116 | }; |
117 | |
117 | |
118 | # this coroutine is necessary because a coroutine |
118 | # this coroutine is necessary because a coroutine |
119 | # cannot destroy itself. |
119 | # cannot destroy itself. |
120 | my @destroy; |
120 | my @destroy; |
|
|
121 | my $manager; |
121 | my $manager = new Coro sub { |
122 | $manager = new Coro sub { |
122 | while() { |
123 | while() { |
123 | 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; |
124 | &schedule; |
130 | &schedule; |
125 | } |
131 | } |
126 | }; |
132 | }; |
127 | |
133 | |
128 | # static methods. not really. |
134 | # static methods. not really. |