… | |
… | |
32 | |
32 | |
33 | no warnings qw(uninitialized); |
33 | no warnings qw(uninitialized); |
34 | |
34 | |
35 | use Coro (); |
35 | use Coro (); |
36 | |
36 | |
37 | $VERSION = 0.52; |
37 | $VERSION = 0.532; |
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 sempahore set with the given initial lock count for each |
42 | individual semaphore. See L<Coro::Semaphore>. |
42 | individual semaphore. See L<Coro::Semaphore>. |
… | |
… | |
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 | if ($timeout) { |
|
|
80 | # ugly as hell. |
|
|
81 | for (0..$#{$sem->[1]}) { |
|
|
82 | if ($sem->[1][$_] == $Coro::current) { |
|
|
83 | splice @{$sem->[1]}, $_, 1; |
|
|
84 | return; |
|
|
85 | } |
|
|
86 | } |
|
|
87 | die; |
|
|
88 | } |
|
|
89 | } |
|
|
90 | --$sem->[0]; |
|
|
91 | return 1; |
64 | } |
92 | } |
65 | |
93 | |
66 | =item $sem->up($id) |
94 | =item $sem->up($id) |
67 | |
95 | |
68 | Unlock the semaphore again. |
96 | Unlock the semaphore again. |
… | |
… | |
108 | =item $guard = $sem->guard($id) |
136 | =item $guard = $sem->guard($id) |
109 | |
137 | |
110 | This method calls C<down> and then creates a guard object. When the guard |
138 | This method calls C<down> and then creates a guard object. When the guard |
111 | object is destroyed it automatically calls C<up>. |
139 | object is destroyed it automatically calls C<up>. |
112 | |
140 | |
|
|
141 | =item $guard = $sem->timed_guard($id, $timeout) |
|
|
142 | |
|
|
143 | Like C<guard>, but returns undef if semaphore couldn't be acquired within |
|
|
144 | $timeout seconds, otherwise the guard object. |
|
|
145 | |
113 | =cut |
146 | =cut |
114 | |
147 | |
115 | sub guard { |
148 | sub guard { |
116 | &down; |
149 | &down; |
117 | # double indirection because bless works on the referenced |
|
|
118 | # object, not (only) on the reference itself. |
|
|
119 | bless [@_], Coro::SemaphoreSet::Guard::; |
150 | bless [@_], Coro::SemaphoreSet::guard::; |
120 | } |
151 | } |
121 | |
152 | |
|
|
153 | sub guard { |
|
|
154 | &timed_down |
|
|
155 | ? bless [$_[0], $_[1]], Coro::SemaphoreSet::guard:: |
|
|
156 | : (); |
|
|
157 | } |
|
|
158 | |
122 | sub Coro::SemaphoreSet::Guard::DESTROY { |
159 | sub Coro::SemaphoreSet::guard::DESTROY { |
123 | &up(@{$_[0]}); |
160 | &up(@{$_[0]}); |
124 | } |
161 | } |
125 | |
162 | |
126 | 1; |
163 | 1; |
127 | |
164 | |