… | |
… | |
30 | |
30 | |
31 | =cut |
31 | =cut |
32 | |
32 | |
33 | package Coro; |
33 | package Coro; |
34 | |
34 | |
35 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
35 | use strict; |
|
|
36 | no warnings "uninitialized"; |
36 | |
37 | |
37 | use Coro::State; |
38 | use Coro::State; |
38 | |
39 | |
39 | use vars qw($idle $main $current); |
|
|
40 | |
|
|
41 | use base Exporter; |
40 | use base Exporter::; |
42 | |
41 | |
|
|
42 | our $idle; # idle coroutine |
|
|
43 | our $main; # main coroutine |
|
|
44 | our $current; # current coroutine |
|
|
45 | |
43 | $VERSION = '1.0'; |
46 | our $VERSION = '2.5'; |
44 | |
47 | |
45 | @EXPORT = qw(async cede schedule terminate current); |
48 | our @EXPORT = qw(async cede schedule terminate current); |
46 | %EXPORT_TAGS = ( |
49 | our %EXPORT_TAGS = ( |
47 | 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)], |
48 | ); |
51 | ); |
49 | @EXPORT_OK = @{$EXPORT_TAGS{prio}}; |
52 | our @EXPORT_OK = @{$EXPORT_TAGS{prio}}; |
50 | |
53 | |
51 | { |
54 | { |
52 | my @async; |
55 | my @async; |
53 | my $init; |
56 | my $init; |
54 | |
57 | |
55 | # this way of handling attributes simply is NOT scalable ;() |
58 | # this way of handling attributes simply is NOT scalable ;() |
56 | sub import { |
59 | sub import { |
|
|
60 | no strict 'refs'; |
|
|
61 | |
57 | Coro->export_to_level(1, @_); |
62 | Coro->export_to_level(1, @_); |
|
|
63 | |
58 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
64 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
59 | *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { |
65 | *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { |
60 | my ($package, $ref) = (shift, shift); |
66 | my ($package, $ref) = (shift, shift); |
61 | my @attrs; |
67 | my @attrs; |
62 | for (@_) { |
68 | for (@_) { |
… | |
… | |
135 | |
141 | |
136 | # the next line destroys the _coro_state, but keeps the |
142 | # the next line destroys the _coro_state, but keeps the |
137 | # process itself intact (we basically make it a zombie |
143 | # process itself intact (we basically make it a zombie |
138 | # process that always runs the manager thread, so it's possible |
144 | # process that always runs the manager thread, so it's possible |
139 | # to transfer() to this process). |
145 | # to transfer() to this process). |
140 | $coro->{_coro_state} = $manager->{_coro_state}; |
146 | $coro->{_coro_state}->_clone_state_from ($manager->{_coro_state}); |
141 | } |
147 | } |
142 | &schedule; |
148 | &schedule; |
143 | } |
149 | } |
144 | }; |
150 | }; |
145 | |
151 | |
… | |
… | |
156 | =item async { ... } [@args...] |
162 | =item async { ... } [@args...] |
157 | |
163 | |
158 | Create a new asynchronous process and return it's process object |
164 | Create a new asynchronous process and return it's process object |
159 | (usually unused). When the sub returns the new process is automatically |
165 | (usually unused). When the sub returns the new process is automatically |
160 | terminated. |
166 | terminated. |
|
|
167 | |
|
|
168 | When the coroutine dies, the program will exit, just as in the main |
|
|
169 | program. |
161 | |
170 | |
162 | # create a new coroutine that just prints its arguments |
171 | # create a new coroutine that just prints its arguments |
163 | async { |
172 | async { |
164 | print "@_\n"; |
173 | print "@_\n"; |
165 | } 1,2,3,4; |
174 | } 1,2,3,4; |
… | |
… | |
223 | } |
232 | } |
224 | |
233 | |
225 | sub new { |
234 | sub new { |
226 | my $class = shift; |
235 | my $class = shift; |
227 | bless { |
236 | bless { |
228 | _coro_state => (new Coro::State $_[0] && \&_newcoro, @_), |
237 | _coro_state => (new Coro::State \&_newcoro, @_), |
229 | }, $class; |
238 | }, $class; |
230 | } |
239 | } |
231 | |
240 | |
232 | =item $process->ready |
241 | =item $process->ready |
233 | |
242 | |
… | |
… | |
235 | |
244 | |
236 | =cut |
245 | =cut |
237 | |
246 | |
238 | =item $process->cancel (arg...) |
247 | =item $process->cancel (arg...) |
239 | |
248 | |
240 | 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 |
241 | status (default: the empty list). |
250 | status (default: the empty list). |
242 | |
251 | |
243 | =cut |
252 | =cut |
244 | |
253 | |
245 | sub cancel { |
254 | sub cancel { |
… | |
… | |
265 | &schedule; |
274 | &schedule; |
266 | } |
275 | } |
267 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
276 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
268 | } |
277 | } |
269 | |
278 | |
270 | =item $oldprio = $process->prio($newprio) |
279 | =item $oldprio = $process->prio ($newprio) |
271 | |
280 | |
272 | 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 |
273 | process. Higher priority processes get run before lower priority |
282 | process. Higher priority processes get run before lower priority |
274 | processes. Priorities are small signed integers (currently -4 .. +3), |
283 | processes. Priorities are small signed integers (currently -4 .. +3), |
275 | 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 |
… | |
… | |
290 | 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. |
291 | |
300 | |
292 | =cut |
301 | =cut |
293 | |
302 | |
294 | sub prio { |
303 | sub prio { |
295 | my $old = $_[0]{prio}; |
304 | shift->{_coro_state}->prio (@_) |
296 | $_[0]{prio} = $_[1] if @_ > 1; |
|
|
297 | $old; |
|
|
298 | } |
305 | } |
299 | |
306 | |
300 | =item $newprio = $process->nice($change) |
307 | =item $newprio = $process->nice ($change) |
301 | |
308 | |
302 | 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. |
303 | higher values mean lower priority, just as in unix). |
310 | higher values mean lower priority, just as in unix). |
304 | |
311 | |
305 | =cut |
312 | =cut |
306 | |
313 | |
307 | sub nice { |
314 | sub nice { |
308 | $_[0]{prio} -= $_[1]; |
315 | shift->{_coro_state}->nice (@_) |
309 | } |
316 | } |
310 | |
317 | |
311 | =item $olddesc = $process->desc($newdesc) |
318 | =item $olddesc = $process->desc ($newdesc) |
312 | |
319 | |
313 | 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 |
314 | 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. |
315 | |
322 | |
316 | =cut |
323 | =cut |
… | |
… | |
337 | to allow per-thread schedulers, but Coro::State does not yet allow |
344 | to allow per-thread schedulers, but Coro::State does not yet allow |
338 | this). |
345 | this). |
339 | |
346 | |
340 | =head1 SEE ALSO |
347 | =head1 SEE ALSO |
341 | |
348 | |
342 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
349 | Support/Utility: L<Coro::Cont>, L<Coro::Specific>, L<Coro::State>, L<Coro::Util>. |
343 | L<Coro::Signal>, L<Coro::State>, L<Coro::Timer>, L<Coro::Event>, |
350 | |
344 | L<Coro::Handle>, L<Coro::RWLock>, L<Coro::Socket>. |
351 | Locking/IPC: L<Coro::Signal>, L<Coro::Channel>, L<Coro::Semaphore>, L<Coro::SemaphoreSet>, L<Coro::RWLock>. |
|
|
352 | |
|
|
353 | Event/IO: L<Coro::Timer>, L<Coro::Event>, L<Coro::Handle>, L<Coro::Socket>, L<Coro::Select>. |
|
|
354 | |
|
|
355 | Embedding: L<Coro:MakeMaker> |
345 | |
356 | |
346 | =head1 AUTHOR |
357 | =head1 AUTHOR |
347 | |
358 | |
348 | Marc Lehmann <pcg@goof.com> |
359 | Marc Lehmann <schmorp@schmorp.de> |
349 | http://home.schmorp.de/ |
360 | http://home.schmorp.de/ |
350 | |
361 | |
351 | =cut |
362 | =cut |
352 | |
363 | |