… | |
… | |
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 ], transfer::; |
13 | my $trans = bless [ $self ], transfer::; |
14 | Scalar::Util::weaken($trans->[0]); |
|
|
15 | |
14 | |
16 | push @{$self->{wait}}, $trans; |
15 | push @{$self->{wait}}, $trans; |
17 | Scalar::Util::weaken($self->{wait}[-1]); |
16 | Scalar::Util::weaken($self->{wait}[-1]); |
18 | |
17 | |
19 | --$self->{conns}; |
18 | if (--$self->{conns} >= 0) { |
20 | $self->wake_next; |
19 | $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 | if ($self->{conns} >= 0) { |
|
|
29 | while(@{$self->{wait}}) { |
28 | while(@{$self->{wait}}) { |
30 | my $transfer = shift @{$self->{wait}}; |
29 | my $transfer = shift @{$self->{wait}}; |
31 | if ($transfer) { |
30 | if ($transfer) { |
32 | $transfer->wake; |
31 | $transfer->wake; |
33 | last; |
32 | last; |
34 | } |
|
|
35 | } |
33 | } |
36 | } |
34 | } |
37 | } |
35 | } |
38 | |
36 | |
39 | sub waiters { |
37 | sub waiters { |
… | |
… | |
53 | sub try { |
51 | sub try { |
54 | my $self = shift; |
52 | my $self = shift; |
55 | |
53 | |
56 | unless ($self->[2]) { |
54 | unless ($self->[2]) { |
57 | my $timeout = Coro::Timer::timeout $_[0]; |
55 | my $timeout = Coro::Timer::timeout $_[0]; |
58 | $self->[1] = $Coro::current; |
56 | local $self->[1] = $Coro::current; |
59 | |
57 | |
60 | Coro::schedule; |
58 | Coro::schedule; |
61 | |
|
|
62 | undef $self->[1]; |
|
|
63 | } |
59 | } |
64 | |
60 | |
65 | return $self->[2]; |
61 | return $self->[2]; |
66 | } |
62 | } |
67 | |
63 | |
68 | sub DESTROY { |
64 | sub DESTROY { |
69 | my $self = shift; |
65 | my $self = shift; |
70 | $self->[0]{conns}++; |
66 | $self->[0]{conns}++; |
71 | $self->[0]->wake_next; |
67 | $self->[0]->wake_next if $self->[2]; |
72 | } |
68 | } |
73 | |
69 | |
74 | package conn; |
70 | package conn; |
75 | |
71 | |
76 | our %blockuri; |
72 | our %blockuri; |