… | |
… | |
32 | |
32 | |
33 | no warnings; |
33 | no warnings; |
34 | |
34 | |
35 | use Coro (); |
35 | use Coro (); |
36 | |
36 | |
37 | $VERSION = 0.5; |
37 | $VERSION = 4.741; |
38 | |
38 | |
39 | =item new [inital count] |
39 | =item new [inital count] |
40 | |
40 | |
41 | Creates a new sempahore set with the given initial lock count for each |
41 | Creates a new semaphore set with the given initial lock count for each |
42 | individual semaphore. See L<Coro::Semaphore>. |
42 | individual semaphore. See L<Coro::Semaphore>. |
43 | |
43 | |
44 | =cut |
44 | =cut |
45 | |
45 | |
46 | sub new { |
46 | sub new { |
47 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
47 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
48 | } |
48 | } |
49 | |
49 | |
50 | =item $sem->down($id) |
50 | =item $sem->down ($id) |
51 | |
51 | |
52 | Decrement the counter, therefore "locking" the named semaphore. This |
52 | Decrement the counter, therefore "locking" the named semaphore. This |
53 | method waits until the semaphore is available if the counter is zero. |
53 | method waits until the semaphore is available if the counter is zero. |
54 | |
54 | |
|
|
55 | =item $status = $sem->timed_down ($id, $timeout) |
|
|
56 | |
|
|
57 | Like C<down>, but returns false if semaphore couldn't be acquired within |
|
|
58 | $timeout seconds, otherwise true. |
|
|
59 | |
55 | =cut |
60 | =cut |
56 | |
61 | |
57 | sub down { |
62 | sub down { |
|
|
63 | while () { |
58 | my $sem = ($_[0][1]{$_[1]} ||= [$_[0][0]]); |
64 | my $sem = ($_[0][1]{$_[1]} ||= [$_[0][0]]); |
|
|
65 | |
59 | while ($sem->[0] <= 0) { |
66 | if ($sem->[0] > 0) { |
|
|
67 | --$sem->[0]; |
|
|
68 | return 1; |
|
|
69 | } |
|
|
70 | |
60 | push @{$sem->[1]}, $Coro::current; |
71 | push @{$sem->[1]}, $Coro::current; |
61 | Coro::schedule; |
72 | &Coro::schedule; |
62 | } |
73 | } |
63 | --$sem->[0]; |
|
|
64 | } |
74 | } |
65 | |
75 | |
|
|
76 | sub timed_down { |
|
|
77 | require Coro::Timer; |
|
|
78 | my $timeout = Coro::Timer::timeout ($_[2]); |
|
|
79 | |
|
|
80 | while () { |
|
|
81 | my $sem = ($_[0][1]{$_[1]} ||= [$_[0][0]]); |
|
|
82 | |
|
|
83 | if ($sem->[0] > 0) { |
|
|
84 | --$sem->[0]; |
|
|
85 | return 1; |
|
|
86 | } |
|
|
87 | |
|
|
88 | if ($timeout) { |
|
|
89 | # ugly as hell. |
|
|
90 | for (0..$#{$sem->[1]}) { |
|
|
91 | if ($sem->[1][$_] == $Coro::current) { |
|
|
92 | splice @{$sem->[1]}, $_, 1; |
|
|
93 | return 0; |
|
|
94 | } |
|
|
95 | } |
|
|
96 | die; |
|
|
97 | } |
|
|
98 | |
|
|
99 | push @{$sem->[1]}, $Coro::current; |
|
|
100 | &Coro::schedule; |
|
|
101 | } |
|
|
102 | } |
|
|
103 | |
66 | =item $sem->up($id) |
104 | =item $sem->up ($id) |
67 | |
105 | |
68 | Unlock the semaphore again. |
106 | Unlock the semaphore again. |
69 | |
107 | |
70 | =cut |
108 | =cut |
71 | |
109 | |
72 | sub up { |
110 | sub up { |
73 | my $sem = $_[0][1]{$_[1]}; |
111 | my $sem = $_[0][1]{$_[1]}; |
|
|
112 | |
74 | if (++$sem->[0] > 0) { |
113 | if (++$sem->[0] > 0) { |
75 | (shift @{$sem->[1]})->ready if @{$sem->[1]}; |
114 | (shift @{$sem->[1]})->ready if @{$sem->[1]}; |
76 | } |
115 | } |
|
|
116 | |
77 | delete $_[0][1]{$_[1]} if $sem->[0] == $_[0][0]; |
117 | delete $_[0][1]{$_[1]} if $sem->[0] == $_[0][0] && !@{$sem->[1] || []}; |
78 | } |
118 | } |
79 | |
119 | |
80 | =item $sem->try |
120 | =item $sem->try |
81 | |
121 | |
82 | Try to C<down> the semaphore. Returns true when this was possible, |
122 | Try to C<down> the semaphore. Returns true when this was possible, |
… | |
… | |
92 | } else { |
132 | } else { |
93 | return 0; |
133 | return 0; |
94 | } |
134 | } |
95 | } |
135 | } |
96 | |
136 | |
97 | =item $sem->waiters($id) |
137 | =item $sem->waiters ($id) |
98 | |
138 | |
99 | In scalar context, returns the number of coroutines waiting for this |
139 | In scalar context, returns the number of coroutines waiting for this |
100 | semaphore. |
140 | semaphore. |
101 | |
141 | |
102 | =cut |
142 | =cut |
103 | |
143 | |
104 | sub waiters { |
144 | sub waiters { |
|
|
145 | my $sem = $_[0][1]{$_[1]} |
|
|
146 | or return; |
105 | @{$_[0][1]{$_[1]}}; |
147 | @{ $_[0][1]{$_[1]}[1] || []} |
106 | } |
148 | } |
107 | |
149 | |
108 | =item $guard = $sem->guard($id) |
150 | =item $guard = $sem->guard ($id) |
109 | |
151 | |
110 | This method calls C<down> and then creates a guard object. When the guard |
152 | This method calls C<down> and then creates a guard object. When the guard |
111 | object is destroyed it automatically calls C<up>. |
153 | object is destroyed it automatically calls C<up>. |
|
|
154 | |
|
|
155 | =item $guard = $sem->timed_guard ($id, $timeout) |
|
|
156 | |
|
|
157 | Like C<guard>, but returns undef if semaphore couldn't be acquired within |
|
|
158 | $timeout seconds, otherwise the guard object. |
112 | |
159 | |
113 | =cut |
160 | =cut |
114 | |
161 | |
115 | sub guard { |
162 | sub guard { |
116 | &down; |
163 | &down; |
117 | # double indirection because bless works on the referenced |
|
|
118 | # object, not (only) on the reference itself. |
|
|
119 | bless [@_], Coro::SemaphoreSet::Guard::; |
164 | bless [@_], Coro::SemaphoreSet::guard::; |
120 | } |
165 | } |
121 | |
166 | |
|
|
167 | sub timed_guard { |
|
|
168 | &timed_down |
|
|
169 | ? bless [$_[0], $_[1]], Coro::SemaphoreSet::guard:: |
|
|
170 | : (); |
|
|
171 | } |
|
|
172 | |
122 | sub Coro::SemaphoreSet::Guard::DESTROY { |
173 | sub Coro::SemaphoreSet::guard::DESTROY { |
123 | &up(@{$_[0]}); |
174 | &up(@{$_[0]}); |
124 | } |
175 | } |
125 | |
176 | |
126 | 1; |
177 | 1; |
127 | |
178 | |
128 | =back |
179 | =back |
129 | |
180 | |
130 | =head1 AUTHOR |
181 | =head1 AUTHOR |
131 | |
182 | |
132 | Marc Lehmann <pcg@goof.com> |
183 | Marc Lehmann <schmorp@schmorp.de> |
133 | http://www.goof.com/pcg/marc/ |
184 | http://home.schmorp.de/ |
134 | |
185 | |
135 | =cut |
186 | =cut |
136 | |
187 | |