… | |
… | |
34 | |
34 | |
35 | =item $q = new Coro:Channel $maxsize |
35 | =item $q = new Coro:Channel $maxsize |
36 | |
36 | |
37 | Create a new channel with the given maximum size (unlimited if C<maxsize> |
37 | Create a new channel with the given maximum size (unlimited if C<maxsize> |
38 | is omitted). Giving a size of one gives you a traditional channel, i.e. a |
38 | is omitted). Giving a size of one gives you a traditional channel, i.e. a |
39 | queue that can store only a single element. |
39 | queue that can store only a single element (which means there will be no |
|
|
40 | buffering). To buffer one element you have to specify C<2>, and so on. |
40 | |
41 | |
41 | =cut |
42 | =cut |
42 | |
43 | |
43 | sub new { |
44 | sub new { |
44 | # [\@contents, [$getwait], $maxsize, [$putwait]]; |
45 | # [\@contents, [$getwait], $maxsize, [$putwait]]; |
… | |
… | |
51 | |
52 | |
52 | =cut |
53 | =cut |
53 | |
54 | |
54 | sub put { |
55 | sub put { |
55 | push @{$_[0][0]}, $_[1]; |
56 | push @{$_[0][0]}, $_[1]; |
56 | |
|
|
57 | (pop @{$_[0][1]})->ready if @{$_[0][1]}; |
57 | (pop @{$_[0][1]})->ready if @{$_[0][1]}; |
58 | |
58 | |
59 | while (@{$_[0][0]} >= $_[0][2]) { |
59 | while (@{$_[0][0]} >= $_[0][2]) { |
60 | push @{$_[0][3]}, $Coro::current; |
60 | push @{$_[0][3]}, $Coro::current; |
61 | &Coro::schedule; |
61 | &Coro::schedule; |
… | |
… | |
73 | will be returned. |
73 | will be returned. |
74 | |
74 | |
75 | =cut |
75 | =cut |
76 | |
76 | |
77 | sub get { |
77 | sub get { |
78 | (pop @{$_[0][3]})->ready if @{$_[0][3]}; |
|
|
79 | |
|
|
80 | while (!@{$_[0][0]}) { |
78 | while (!@{$_[0][0]}) { |
81 | push @{$_[0][1]}, $Coro::current; |
79 | push @{$_[0][1]}, $Coro::current; |
82 | &Coro::schedule; |
80 | &Coro::schedule; |
83 | } |
81 | } |
84 | |
82 | |
|
|
83 | (pop @{$_[0][3]})->ready if @{$_[0][3]}; |
85 | shift @{$_[0][0]} |
84 | shift @{$_[0][0]} |
86 | } |
85 | } |
87 | |
86 | |
88 | sub timed_get { |
87 | sub timed_get { |
89 | require Coro::Timer; |
88 | require Coro::Timer; |