… | |
… | |
50 | |
50 | |
51 | our $idle; # idle handler |
51 | our $idle; # idle handler |
52 | our $main; # main coroutine |
52 | our $main; # main coroutine |
53 | our $current; # current coroutine |
53 | our $current; # current coroutine |
54 | |
54 | |
55 | our $VERSION = '3.7'; |
55 | our $VERSION = '4.0'; |
56 | |
56 | |
57 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); |
57 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); |
58 | our %EXPORT_TAGS = ( |
58 | our %EXPORT_TAGS = ( |
59 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
59 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
60 | ); |
60 | ); |
… | |
… | |
113 | reasons. If performance is not essential you are encouraged to use the |
113 | reasons. If performance is not essential you are encouraged to use the |
114 | C<Coro::current> function instead. |
114 | C<Coro::current> function instead. |
115 | |
115 | |
116 | =cut |
116 | =cut |
117 | |
117 | |
|
|
118 | $main->{desc} = "[main::]"; |
|
|
119 | |
118 | # maybe some other module used Coro::Specific before... |
120 | # maybe some other module used Coro::Specific before... |
119 | $main->{specific} = $current->{specific} |
121 | $main->{_specific} = $current->{_specific} |
120 | if $current; |
122 | if $current; |
121 | |
123 | |
122 | _set_current $main; |
124 | _set_current $main; |
123 | |
125 | |
124 | sub current() { $current } |
126 | sub current() { $current } |
… | |
… | |
149 | # free coroutine data and mark as destructed |
151 | # free coroutine data and mark as destructed |
150 | $self->_destroy |
152 | $self->_destroy |
151 | or return; |
153 | or return; |
152 | |
154 | |
153 | # call all destruction callbacks |
155 | # call all destruction callbacks |
154 | $_->(@{$self->{status}}) |
156 | $_->(@{$self->{_status}}) |
155 | for @{(delete $self->{destroy_cb}) || []}; |
157 | for @{(delete $self->{_on_destroy}) || []}; |
156 | } |
158 | } |
157 | |
159 | |
158 | # this coroutine is necessary because a coroutine |
160 | # this coroutine is necessary because a coroutine |
159 | # cannot destroy itself. |
161 | # cannot destroy itself. |
160 | my @destroy; |
162 | my @destroy; |
161 | my $manager; |
163 | my $manager; |
162 | |
164 | |
163 | $manager = new Coro sub { |
165 | $manager = new Coro sub { |
164 | $current->desc ("[coro manager]"); |
|
|
165 | |
|
|
166 | while () { |
166 | while () { |
167 | (shift @destroy)->_cancel |
167 | (shift @destroy)->_cancel |
168 | while @destroy; |
168 | while @destroy; |
169 | |
169 | |
170 | &schedule; |
170 | &schedule; |
171 | } |
171 | } |
172 | }; |
172 | }; |
173 | |
173 | $manager->desc ("[coro manager]"); |
174 | $manager->prio (PRIO_MAX); |
174 | $manager->prio (PRIO_MAX); |
175 | |
175 | |
176 | # static methods. not really. |
176 | # static methods. not really. |
177 | |
177 | |
178 | =back |
178 | =back |
… | |
… | |
186 | =item async { ... } [@args...] |
186 | =item async { ... } [@args...] |
187 | |
187 | |
188 | Create a new asynchronous coroutine and return it's coroutine object |
188 | Create a new asynchronous coroutine and return it's coroutine object |
189 | (usually unused). When the sub returns the new coroutine is automatically |
189 | (usually unused). When the sub returns the new coroutine is automatically |
190 | terminated. |
190 | terminated. |
|
|
191 | |
|
|
192 | See the C<Coro::State::new> constructor for info about the coroutine |
|
|
193 | environment. |
191 | |
194 | |
192 | Calling C<exit> in a coroutine will do the same as calling exit outside |
195 | Calling C<exit> in a coroutine will do the same as calling exit outside |
193 | the coroutine. Likewise, when the coroutine dies, the program will exit, |
196 | the coroutine. Likewise, when the coroutine dies, the program will exit, |
194 | just as it would in the main program. |
197 | just as it would in the main program. |
195 | |
198 | |
… | |
… | |
224 | The pool size is limited to 8 idle coroutines (this can be adjusted by |
227 | The pool size is limited to 8 idle coroutines (this can be adjusted by |
225 | changing $Coro::POOL_SIZE), and there can be as many non-idle coros as |
228 | changing $Coro::POOL_SIZE), and there can be as many non-idle coros as |
226 | required. |
229 | required. |
227 | |
230 | |
228 | If you are concerned about pooled coroutines growing a lot because a |
231 | If you are concerned about pooled coroutines growing a lot because a |
229 | single C<async_pool> used a lot of stackspace you can e.g. C<async_pool { |
232 | single C<async_pool> used a lot of stackspace you can e.g. C<async_pool |
230 | terminate }> once per second or so to slowly replenish the pool. |
233 | { terminate }> once per second or so to slowly replenish the pool. In |
|
|
234 | addition to that, when the stacks used by a handler grows larger than 16kb |
|
|
235 | (adjustable with $Coro::POOL_RSS) it will also exit. |
231 | |
236 | |
232 | =cut |
237 | =cut |
233 | |
238 | |
234 | our $POOL_SIZE = 8; |
239 | our $POOL_SIZE = 8; |
235 | our $MAX_POOL_RSS = 64 * 1024; |
240 | our $POOL_RSS = 16 * 1024; |
236 | our @pool; |
241 | our @async_pool; |
237 | |
242 | |
238 | sub pool_handler { |
243 | sub pool_handler { |
|
|
244 | my $cb; |
|
|
245 | |
239 | while () { |
246 | while () { |
240 | $current->{desc} = "[async_pool]"; |
|
|
241 | |
|
|
242 | eval { |
247 | eval { |
243 | my ($cb, @arg) = @{ delete $current->{_invoke} or return }; |
248 | while () { |
244 | $cb->(@arg); |
249 | _pool_1 $cb; |
|
|
250 | &$cb; |
|
|
251 | _pool_2 $cb; |
|
|
252 | &schedule; |
|
|
253 | } |
245 | }; |
254 | }; |
|
|
255 | |
|
|
256 | last if $@ eq "\3terminate\2\n"; |
246 | warn $@ if $@; |
257 | warn $@ if $@; |
247 | |
|
|
248 | last if @pool >= $POOL_SIZE || $current->rss >= $MAX_POOL_RSS; |
|
|
249 | |
|
|
250 | push @pool, $current; |
|
|
251 | $current->{desc} = "[async_pool idle]"; |
|
|
252 | $current->save (Coro::State::SAVE_DEF); |
|
|
253 | $current->prio (0); |
|
|
254 | schedule; |
|
|
255 | } |
258 | } |
256 | } |
259 | } |
257 | |
260 | |
258 | sub async_pool(&@) { |
261 | sub async_pool(&@) { |
259 | # this is also inlined into the unlock_scheduler |
262 | # this is also inlined into the unlock_scheduler |
260 | my $coro = (pop @pool) || new Coro \&pool_handler;; |
263 | my $coro = (pop @async_pool) || new Coro \&pool_handler; |
261 | |
264 | |
262 | $coro->{_invoke} = [@_]; |
265 | $coro->{_invoke} = [@_]; |
263 | $coro->ready; |
266 | $coro->ready; |
264 | |
267 | |
265 | $coro |
268 | $coro |
… | |
… | |
308 | |
311 | |
309 | =item terminate [arg...] |
312 | =item terminate [arg...] |
310 | |
313 | |
311 | Terminates the current coroutine with the given status values (see L<cancel>). |
314 | Terminates the current coroutine with the given status values (see L<cancel>). |
312 | |
315 | |
|
|
316 | =item killall |
|
|
317 | |
|
|
318 | Kills/terminates/cancels all coroutines except the currently running |
|
|
319 | one. This is useful after a fork, either in the child or the parent, as |
|
|
320 | usually only one of them should inherit the running coroutines. |
|
|
321 | |
313 | =cut |
322 | =cut |
314 | |
323 | |
315 | sub terminate { |
324 | sub terminate { |
316 | $current->cancel (@_); |
325 | $current->cancel (@_); |
|
|
326 | } |
|
|
327 | |
|
|
328 | sub killall { |
|
|
329 | for (Coro::State::list) { |
|
|
330 | $_->cancel |
|
|
331 | if $_ != $current && UNIVERSAL::isa $_, "Coro"; |
|
|
332 | } |
317 | } |
333 | } |
318 | |
334 | |
319 | =back |
335 | =back |
320 | |
336 | |
321 | # dynamic methods |
337 | # dynamic methods |
… | |
… | |
331 | Create a new coroutine and return it. When the sub returns the coroutine |
347 | Create a new coroutine and return it. When the sub returns the coroutine |
332 | automatically terminates as if C<terminate> with the returned values were |
348 | automatically terminates as if C<terminate> with the returned values were |
333 | called. To make the coroutine run you must first put it into the ready queue |
349 | called. To make the coroutine run you must first put it into the ready queue |
334 | by calling the ready method. |
350 | by calling the ready method. |
335 | |
351 | |
336 | See C<async> for additional discussion. |
352 | See C<async> and C<Coro::State::new> for additional info about the |
|
|
353 | coroutine environment. |
337 | |
354 | |
338 | =cut |
355 | =cut |
339 | |
356 | |
340 | sub _run_coro { |
357 | sub _run_coro { |
341 | terminate &{+shift}; |
358 | terminate &{+shift}; |
… | |
… | |
365 | |
382 | |
366 | =cut |
383 | =cut |
367 | |
384 | |
368 | sub cancel { |
385 | sub cancel { |
369 | my $self = shift; |
386 | my $self = shift; |
370 | $self->{status} = [@_]; |
387 | $self->{_status} = [@_]; |
371 | |
388 | |
372 | if ($current == $self) { |
389 | if ($current == $self) { |
373 | push @destroy, $self; |
390 | push @destroy, $self; |
374 | $manager->ready; |
391 | $manager->ready; |
375 | &schedule while 1; |
392 | &schedule while 1; |
… | |
… | |
379 | } |
396 | } |
380 | |
397 | |
381 | =item $coroutine->join |
398 | =item $coroutine->join |
382 | |
399 | |
383 | Wait until the coroutine terminates and return any values given to the |
400 | Wait until the coroutine terminates and return any values given to the |
384 | C<terminate> or C<cancel> functions. C<join> can be called multiple times |
401 | C<terminate> or C<cancel> functions. C<join> can be called concurrently |
385 | from multiple coroutine. |
402 | from multiple coroutines. |
386 | |
403 | |
387 | =cut |
404 | =cut |
388 | |
405 | |
389 | sub join { |
406 | sub join { |
390 | my $self = shift; |
407 | my $self = shift; |
391 | |
408 | |
392 | unless ($self->{status}) { |
409 | unless ($self->{_status}) { |
393 | my $current = $current; |
410 | my $current = $current; |
394 | |
411 | |
395 | push @{$self->{destroy_cb}}, sub { |
412 | push @{$self->{_on_destroy}}, sub { |
396 | $current->ready; |
413 | $current->ready; |
397 | undef $current; |
414 | undef $current; |
398 | }; |
415 | }; |
399 | |
416 | |
400 | &schedule while $current; |
417 | &schedule while $current; |
401 | } |
418 | } |
402 | |
419 | |
403 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
420 | wantarray ? @{$self->{_status}} : $self->{_status}[0]; |
404 | } |
421 | } |
405 | |
422 | |
406 | =item $coroutine->on_destroy (\&cb) |
423 | =item $coroutine->on_destroy (\&cb) |
407 | |
424 | |
408 | Registers a callback that is called when this coroutine gets destroyed, |
425 | Registers a callback that is called when this coroutine gets destroyed, |
… | |
… | |
412 | =cut |
429 | =cut |
413 | |
430 | |
414 | sub on_destroy { |
431 | sub on_destroy { |
415 | my ($self, $cb) = @_; |
432 | my ($self, $cb) = @_; |
416 | |
433 | |
417 | push @{ $self->{destroy_cb} }, $cb; |
434 | push @{ $self->{_on_destroy} }, $cb; |
418 | } |
435 | } |
419 | |
436 | |
420 | =item $oldprio = $coroutine->prio ($newprio) |
437 | =item $oldprio = $coroutine->prio ($newprio) |
421 | |
438 | |
422 | Sets (or gets, if the argument is missing) the priority of the |
439 | Sets (or gets, if the argument is missing) the priority of the |
… | |
… | |
446 | |
463 | |
447 | =item $olddesc = $coroutine->desc ($newdesc) |
464 | =item $olddesc = $coroutine->desc ($newdesc) |
448 | |
465 | |
449 | Sets (or gets in case the argument is missing) the description for this |
466 | Sets (or gets in case the argument is missing) the description for this |
450 | coroutine. This is just a free-form string you can associate with a coroutine. |
467 | coroutine. This is just a free-form string you can associate with a coroutine. |
|
|
468 | |
|
|
469 | This method simply sets the C<< $coroutine->{desc} >> member to the given string. You |
|
|
470 | can modify this member directly if you wish. |
451 | |
471 | |
452 | =cut |
472 | =cut |
453 | |
473 | |
454 | sub desc { |
474 | sub desc { |
455 | my $old = $_[0]{desc}; |
475 | my $old = $_[0]{desc}; |
… | |
… | |
531 | |
551 | |
532 | # we create a special coro because we want to cede, |
552 | # we create a special coro because we want to cede, |
533 | # to reduce pressure on the coro pool (because most callbacks |
553 | # to reduce pressure on the coro pool (because most callbacks |
534 | # return immediately and can be reused) and because we cannot cede |
554 | # return immediately and can be reused) and because we cannot cede |
535 | # inside an event callback. |
555 | # inside an event callback. |
536 | our $unblock_scheduler = async { |
556 | our $unblock_scheduler = new Coro sub { |
537 | $current->desc ("[unblock_sub scheduler]"); |
|
|
538 | while () { |
557 | while () { |
539 | while (my $cb = pop @unblock_queue) { |
558 | while (my $cb = pop @unblock_queue) { |
540 | # this is an inlined copy of async_pool |
559 | # this is an inlined copy of async_pool |
541 | my $coro = (pop @pool or new Coro \&pool_handler); |
560 | my $coro = (pop @async_pool) || new Coro \&pool_handler; |
542 | |
561 | |
543 | $coro->{_invoke} = $cb; |
562 | $coro->{_invoke} = $cb; |
544 | $coro->ready; |
563 | $coro->ready; |
545 | cede; # for short-lived callbacks, this reduces pressure on the coro pool |
564 | cede; # for short-lived callbacks, this reduces pressure on the coro pool |
546 | } |
565 | } |
547 | schedule; # sleep well |
566 | schedule; # sleep well |
548 | } |
567 | } |
549 | }; |
568 | }; |
|
|
569 | $unblock_scheduler->desc ("[unblock_sub scheduler]"); |
550 | |
570 | |
551 | sub unblock_sub(&) { |
571 | sub unblock_sub(&) { |
552 | my $cb = shift; |
572 | my $cb = shift; |
553 | |
573 | |
554 | sub { |
574 | sub { |