… | |
… | |
54 | method waits until the semaphore is available if the counter is zero. |
54 | method waits until the semaphore is available if the counter is zero. |
55 | |
55 | |
56 | =cut |
56 | =cut |
57 | |
57 | |
58 | sub down { |
58 | sub down { |
|
|
59 | # Coro::Semaphore::down increases the refcount, which we check in _may_delete |
59 | Coro::Semaphore::down ($_[0][1]{$_[1]} ||= Coro::Semaphore::_alloc $_[0][0]); |
60 | Coro::Semaphore::down ($_[0][1]{$_[1]} ||= Coro::Semaphore::_alloc $_[0][0]); |
60 | } |
61 | } |
61 | |
62 | |
62 | #ub timed_down { |
63 | #ub timed_down { |
63 | # require Coro::Timer; |
64 | # require Coro::Timer; |
… | |
… | |
87 | # } |
88 | # } |
88 | # |
89 | # |
89 | |
90 | |
90 | =item $semset->up ($id) |
91 | =item $semset->up ($id) |
91 | |
92 | |
92 | Unlock the semaphore again. If the semaphore then reaches the default |
93 | Unlock the semaphore again. If the semaphore reaches the default count for |
93 | count for this set and has no waiters, the space allocated for it will be |
94 | this set and has no waiters, the space allocated for it will be freed. |
94 | freed. |
|
|
95 | |
95 | |
96 | =cut |
96 | =cut |
97 | |
97 | |
98 | sub up { |
98 | sub up { |
99 | my ($self, $id) = @_; |
99 | my ($self, $id) = @_; |
… | |
… | |
101 | my $sem = $self->[1]{$id} ||= Coro::Semaphore::_alloc $self->[0]; |
101 | my $sem = $self->[1]{$id} ||= Coro::Semaphore::_alloc $self->[0]; |
102 | |
102 | |
103 | Coro::Semaphore::up $sem; |
103 | Coro::Semaphore::up $sem; |
104 | |
104 | |
105 | delete $self->[1]{$id} |
105 | delete $self->[1]{$id} |
106 | if $self->[0] == Coro::Semaphore::count $sem |
106 | if _may_delete $sem, $self->[0], 1; |
107 | and !Coro::Semaphore::waiters $sem; |
|
|
108 | } |
107 | } |
109 | |
108 | |
110 | =item $semset->try ($id) |
109 | =item $semset->try ($id) |
111 | |
110 | |
112 | Try to C<down> the semaphore. Returns true when this was possible, |
111 | Try to C<down> the semaphore. Returns true when this was possible, |