… | |
… | |
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 | while (@destroy) { |
|
|
130 | my $coro = pop @destroy; |
|
|
131 | $coro->{status} ||= []; |
|
|
132 | $_->ready for @{delete $coro->{join} || []}; |
|
|
133 | $coro->{_coro_state} = $manager->{_coro_state}; |
|
|
134 | } |
122 | &schedule; |
135 | &schedule; |
123 | } |
136 | } |
124 | }; |
137 | }; |
125 | |
138 | |
126 | # static methods. not really. |
139 | # static methods. not really. |
… | |
… | |
168 | 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 |
169 | current "timeslice" to other coroutines of the same or higher priority. |
182 | current "timeslice" to other coroutines of the same or higher priority. |
170 | |
183 | |
171 | =cut |
184 | =cut |
172 | |
185 | |
173 | =item terminate |
186 | =item terminate [arg...] |
174 | |
187 | |
175 | Terminates the current process. |
188 | Terminates the current process. |
176 | |
189 | |
177 | Future versions of this function will allow result arguments. |
190 | Future versions of this function will allow result arguments. |
178 | |
191 | |
179 | =cut |
192 | =cut |
180 | |
193 | |
181 | sub terminate { |
194 | sub terminate { |
|
|
195 | $current->{status} = [@_]; |
182 | $current->cancel; |
196 | $current->cancel; |
183 | &schedule; |
197 | &schedule; |
184 | die; # NORETURN |
198 | die; # NORETURN |
185 | } |
199 | } |
186 | |
200 | |
… | |
… | |
195 | =over 4 |
209 | =over 4 |
196 | |
210 | |
197 | =item new Coro \&sub [, @args...] |
211 | =item new Coro \&sub [, @args...] |
198 | |
212 | |
199 | 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 |
200 | automatically terminates. To start the process you must first put it into |
214 | automatically terminates as if C<terminate> with the returned values were |
201 | the ready queue by calling the ready method. |
215 | called. To start the process you must first put it into the ready queue by |
|
|
216 | calling the ready method. |
202 | |
217 | |
203 | The coderef you submit MUST NOT be a closure that refers to variables |
218 | The coderef you submit MUST NOT be a closure that refers to variables |
204 | in an outer scope. This does NOT work. Pass arguments into it instead. |
219 | in an outer scope. This does NOT work. Pass arguments into it instead. |
205 | |
220 | |
206 | =cut |
221 | =cut |
… | |
… | |
216 | }, $class; |
231 | }, $class; |
217 | } |
232 | } |
218 | |
233 | |
219 | =item $process->ready |
234 | =item $process->ready |
220 | |
235 | |
221 | Put the current process into the ready queue. |
236 | Put the given process into the ready queue. |
222 | |
237 | |
223 | =cut |
238 | =cut |
224 | |
239 | |
225 | =item $process->cancel |
240 | =item $process->cancel |
226 | |
241 | |
… | |
… | |
229 | =cut |
244 | =cut |
230 | |
245 | |
231 | sub cancel { |
246 | sub cancel { |
232 | push @destroy, $_[0]; |
247 | push @destroy, $_[0]; |
233 | $manager->ready; |
248 | $manager->ready; |
|
|
249 | &schedule if $current == $_[0]; |
|
|
250 | } |
|
|
251 | |
|
|
252 | =item $process->join |
|
|
253 | |
|
|
254 | Wait until the coroutine terminates and return any values given to the |
|
|
255 | C<terminate> function. C<join> can be called multiple times from multiple |
|
|
256 | processes. |
|
|
257 | |
|
|
258 | =cut |
|
|
259 | |
|
|
260 | sub join { |
|
|
261 | my $self = shift; |
|
|
262 | unless ($self->{status}) { |
|
|
263 | push @{$self->{join}}, $current; |
|
|
264 | &schedule; |
|
|
265 | } |
|
|
266 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
234 | } |
267 | } |
235 | |
268 | |
236 | =item $oldprio = $process->prio($newprio) |
269 | =item $oldprio = $process->prio($newprio) |
237 | |
270 | |
238 | Sets the priority of the process. Higher priority processes get run before |
271 | Sets the priority of the process. Higher priority processes get run before |
… | |
… | |
279 | |
312 | |
280 | 1; |
313 | 1; |
281 | |
314 | |
282 | =head1 BUGS/LIMITATIONS |
315 | =head1 BUGS/LIMITATIONS |
283 | |
316 | |
284 | - could be faster, especially when the core would introduce special |
317 | - you must make very sure that no coro is still active on global destruction. |
285 | support for coroutines (like it does for threads). |
318 | 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 |
319 | - 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 |
320 | 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). |
321 | allow per-thread schedulers, but Coro::State does not yet allow this). |
295 | |
322 | |
296 | =head1 SEE ALSO |
323 | =head1 SEE ALSO |