--- Coro/Coro.pm 2007/09/19 22:33:08 1.129 +++ Coro/Coro.pm 2007/09/22 14:42:56 1.134 @@ -115,6 +115,8 @@ =cut +$main->{desc} = "[main::]"; + # maybe some other module used Coro::Specific before... $main->{specific} = $current->{specific} if $current; @@ -161,8 +163,6 @@ my $manager; $manager = new Coro sub { - $current->desc ("[coro manager]"); - while () { (shift @destroy)->_cancel while @destroy; @@ -170,7 +170,7 @@ &schedule; } }; - +$manager->desc ("[coro manager]"); $manager->prio (PRIO_MAX); # static methods. not really. @@ -226,37 +226,42 @@ required. If you are concerned about pooled coroutines growing a lot because a -single C used a lot of stackspace you can e.g. C once per second or so to slowly replenish the pool. +single C used a lot of stackspace you can e.g. C once per second or so to slowly replenish the pool. In +addition to that, when the stacks used by a handler grows larger than 16kb +(adjustable with $Coro::POOL_RSS) it will also exit. =cut our $POOL_SIZE = 8; -our @pool; +our $POOL_RSS = 16 * 1024; +our @async_pool; sub pool_handler { - while () { - $current->{desc} = "[async_pool]"; + my $cb; + while () { eval { - my ($cb, @arg) = @{ delete $current->{_invoke} or return }; - $cb->(@arg); - }; - warn $@ if $@; + while () { + $cb = &_pool_1 + or return; + + &$cb; - last if @pool >= $POOL_SIZE; + return if &_pool_2; - push @pool, $current; - $current->{desc} = "[async_pool idle]"; - $current->save (Coro::State::SAVE_DEF); - $current->prio (0); - schedule; + undef $cb; + schedule; + } + }; + + warn $@ if $@; } } sub async_pool(&@) { # this is also inlined into the unlock_scheduler - my $coro = (pop @pool) || new Coro \&pool_handler;; + my $coro = (pop @async_pool) || new Coro \&pool_handler;; $coro->{_invoke} = [@_]; $coro->ready; @@ -532,12 +537,11 @@ # to reduce pressure on the coro pool (because most callbacks # return immediately and can be reused) and because we cannot cede # inside an event callback. -our $unblock_scheduler = async { - $current->desc ("[unblock_sub scheduler]"); +our $unblock_scheduler = new Coro sub { while () { while (my $cb = pop @unblock_queue) { # this is an inlined copy of async_pool - my $coro = (pop @pool or new Coro \&pool_handler); + my $coro = (pop @async_pool) || new Coro \&pool_handler; $coro->{_invoke} = $cb; $coro->ready; @@ -546,6 +550,7 @@ schedule; # sleep well } }; +$unblock_scheduler->desc ("[unblock_sub scheduler]"); sub unblock_sub(&) { my $cb = shift;