… | |
… | |
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 { |
58 | my $sem = ($_[0][1]{$_[1]} ||= [$_[0][0]]); |
63 | my $sem = ($_[0][1]{$_[1]} ||= [$_[0][0]]); |
59 | while ($sem->[0] <= 0) { |
64 | while ($sem->[0] <= 0) { |
60 | push @{$sem->[1]}, $Coro::current; |
65 | push @{$sem->[1]}, $Coro::current; |
61 | Coro::schedule; |
66 | Coro::schedule; |
62 | } |
67 | } |
63 | --$sem->[0]; |
68 | --$sem->[0]; |
|
|
69 | } |
|
|
70 | |
|
|
71 | sub timed_down { |
|
|
72 | require Coro::Timer; |
|
|
73 | my $timeout = Coro::Timer::timeout($_[2]); |
|
|
74 | |
|
|
75 | my $sem = ($_[0][1]{$_[1]} ||= [$_[0][0]]); |
|
|
76 | while ($sem->[0] <= 0) { |
|
|
77 | push @{$sem->[1]}, $Coro::current; |
|
|
78 | Coro::schedule; |
|
|
79 | $timeout and return; |
|
|
80 | } |
|
|
81 | --$sem->[0]; |
|
|
82 | return 1; |
64 | } |
83 | } |
65 | |
84 | |
66 | =item $sem->up($id) |
85 | =item $sem->up($id) |
67 | |
86 | |
68 | Unlock the semaphore again. |
87 | Unlock the semaphore again. |