… | |
… | |
8 | } |
8 | } |
9 | |
9 | |
10 | sub start_transfer { |
10 | sub start_transfer { |
11 | my $self = shift; |
11 | my $self = shift; |
12 | |
12 | |
13 | my $trans = bless [ $self, $Coro::current ], transfer::; |
13 | my $trans = bless [ $self ], transfer::; |
14 | Scalar::Util::weaken($trans->[0]); |
14 | Scalar::Util::weaken($trans->[0]); |
15 | |
15 | |
16 | push @{$self->{wait}}, $trans; |
16 | push @{$self->{wait}}, $trans; |
|
|
17 | Scalar::Util::weaken($self->{wait}[-1]); |
17 | |
18 | |
18 | if (--$self->{conns} >= 0) { |
19 | --$self->{conns}; |
19 | $self->wake_next; |
20 | $self->wake_next; |
20 | } |
|
|
21 | |
21 | |
22 | $trans; |
22 | $trans; |
23 | } |
23 | } |
24 | |
24 | |
25 | sub wake_next { |
25 | sub wake_next { |
26 | my $self = shift; |
26 | my $self = shift; |
27 | |
27 | |
28 | return unless $self->{conns} >= 0; |
28 | if ($self->{conns} >= 0) { |
29 | |
29 | while(@{$self->{wait}}) { |
30 | (pop @{$self->{wait}})->wake if @{$self->{wait}}; |
30 | my $transfer = shift @{$self->{wait}}; |
|
|
31 | if ($transfer) { |
|
|
32 | $transfer->wake; |
|
|
33 | last; |
|
|
34 | } |
|
|
35 | } |
|
|
36 | } |
31 | } |
37 | } |
32 | |
38 | |
33 | sub waiters { |
39 | sub waiters { |
34 | map $_->[1], @{$_[0]{wait}}; |
40 | map $_->[1], @{$_[0]{wait}}; |
35 | } |
41 | } |
36 | |
42 | |
37 | package transfer; |
43 | package transfer; |
38 | |
44 | |
39 | use Coro::Timer (); |
45 | use Coro::Timer (); |
40 | |
46 | |
41 | sub try { |
|
|
42 | my $self = shift; |
|
|
43 | my $timeout = Coro::Timer::timeout $_[0]; |
|
|
44 | |
|
|
45 | Coro::schedule; |
|
|
46 | |
|
|
47 | return $self->[2]; |
|
|
48 | } |
|
|
49 | |
|
|
50 | sub wake { |
47 | sub wake { |
51 | my $self = shift; |
48 | my $self = shift; |
52 | $self->[2] = 1; |
49 | $self->[2] = 1; |
53 | $self->[1]->ready; |
50 | ref $self->[1] and $self->[1]->ready; |
|
|
51 | } |
|
|
52 | |
|
|
53 | sub try { |
|
|
54 | my $self = shift; |
|
|
55 | |
|
|
56 | unless ($self->[2]) { |
|
|
57 | my $timeout = Coro::Timer::timeout $_[0]; |
|
|
58 | $self->[1] = $Coro::current; |
|
|
59 | |
|
|
60 | Coro::schedule; |
|
|
61 | |
|
|
62 | undef $self->[1]; |
|
|
63 | } |
|
|
64 | |
|
|
65 | return $self->[2]; |
54 | } |
66 | } |
55 | |
67 | |
56 | sub DESTROY { |
68 | sub DESTROY { |
57 | my $self = shift; |
69 | my $self = shift; |
58 | $self->[0]{conns}++; |
70 | $self->[0]{conns}++; |