1 |
+ |
/* |
2 |
+ |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
+ |
* Expert Group and released to the public domain. Use, modify, and |
4 |
+ |
* redistribute this code in any way without acknowledgement. |
5 |
+ |
*/ |
6 |
+ |
|
7 |
|
package java.util.concurrent; |
8 |
|
|
9 |
|
/** |
97 |
|
* @editor $Author$ |
98 |
|
* |
99 |
|
*/ |
100 |
< |
public class Semaphore { |
100 |
> |
public class Semaphore implements java.io.Serializable { |
101 |
> |
// todo SerialID |
102 |
> |
// uses default serialization, which happens be fine here. |
103 |
> |
|
104 |
> |
// Fields are package-private to allow the FairSemaphore variant |
105 |
> |
// to access. |
106 |
> |
|
107 |
> |
final ReentrantLock lock; |
108 |
> |
final Condition available; |
109 |
> |
long count; |
110 |
|
|
111 |
< |
private long permits = 0; |
111 |
> |
Semaphore(long permits, ReentrantLock lock) { |
112 |
> |
this.count = permits; |
113 |
> |
this.lock = lock; |
114 |
> |
available = lock.newCondition(); |
115 |
> |
} |
116 |
|
|
117 |
|
/** |
118 |
|
* Construct a <tt>Semaphore</tt> with the given number of |
120 |
|
* @param permits the initial number of permits available |
121 |
|
*/ |
122 |
|
public Semaphore(long permits) { |
123 |
< |
this.permits = permits; |
123 |
> |
this(permits, new ReentrantLock()); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
153 |
|
* |
154 |
|
* @see Thread#interrupt |
155 |
|
*/ |
156 |
< |
public void acquire() throws InterruptedException {} |
156 |
> |
public void acquire() throws InterruptedException { |
157 |
> |
lock.lockInterruptibly(); |
158 |
> |
try { |
159 |
> |
while (count <= 0) available.await(); |
160 |
> |
--count; |
161 |
> |
} |
162 |
> |
catch (InterruptedException ie) { |
163 |
> |
available.signal(); |
164 |
> |
throw ie; |
165 |
> |
} |
166 |
> |
finally { |
167 |
> |
lock.unlock(); |
168 |
> |
} |
169 |
> |
} |
170 |
|
|
171 |
|
/** |
172 |
|
* Acquires a permit from this semaphore, blocking until one is |
188 |
|
* thread does return from this method its interrupt status will be set. |
189 |
|
* |
190 |
|
*/ |
191 |
< |
public void acquireUninterruptibly() {} |
191 |
> |
public void acquireUninterruptibly() { |
192 |
> |
lock.lock(); |
193 |
> |
try { |
194 |
> |
while (count <= 0) available.awaitUninterruptibly(); |
195 |
> |
--count; |
196 |
> |
} |
197 |
> |
finally { |
198 |
> |
lock.unlock(); |
199 |
> |
} |
200 |
> |
} |
201 |
|
|
202 |
|
/** |
203 |
|
* Acquires a permit from this semaphore, only if one is available at the |
213 |
|
* otherwise. |
214 |
|
*/ |
215 |
|
public boolean tryAcquire() { |
216 |
< |
return false; |
216 |
> |
lock.lock(); |
217 |
> |
try { |
218 |
> |
if (count > 0) { |
219 |
> |
--count; |
220 |
> |
return true; |
221 |
> |
} |
222 |
> |
return false; |
223 |
> |
} |
224 |
> |
finally { |
225 |
> |
lock.unlock(); |
226 |
> |
} |
227 |
|
} |
228 |
|
|
229 |
|
/** |
270 |
|
*/ |
271 |
|
public boolean tryAcquire(long timeout, TimeUnit granularity) |
272 |
|
throws InterruptedException { |
273 |
< |
return false; |
273 |
> |
lock.lockInterruptibly(); |
274 |
> |
long nanos = granularity.toNanos(timeout); |
275 |
> |
try { |
276 |
> |
for (;;) { |
277 |
> |
if (count > 0) { |
278 |
> |
--count; |
279 |
> |
return true; |
280 |
> |
} |
281 |
> |
if (nanos <= 0) |
282 |
> |
return false; |
283 |
> |
nanos = available.awaitNanos(nanos); |
284 |
> |
} |
285 |
> |
} |
286 |
> |
catch (InterruptedException ie) { |
287 |
> |
available.signal(); |
288 |
> |
throw ie; |
289 |
> |
} |
290 |
> |
finally { |
291 |
> |
lock.unlock(); |
292 |
> |
} |
293 |
|
} |
294 |
|
|
295 |
|
/** |
304 |
|
* Correct usage of a semaphore is established by programming convention |
305 |
|
* in the application. |
306 |
|
*/ |
307 |
< |
public void release() {} |
307 |
> |
public void release() { |
308 |
> |
lock.lock(); |
309 |
> |
try { |
310 |
> |
++count; |
311 |
> |
available.signal(); |
312 |
> |
} |
313 |
> |
finally { |
314 |
> |
lock.unlock(); |
315 |
> |
} |
316 |
> |
} |
317 |
> |
|
318 |
|
|
319 |
|
/** |
320 |
|
* Return the current number of permits available in this semaphore. |
322 |
|
* @return the number of permits available in this semaphore. |
323 |
|
*/ |
324 |
|
public long availablePermits() { |
325 |
< |
return permits; |
325 |
> |
lock.lock(); |
326 |
> |
try { |
327 |
> |
return count; |
328 |
> |
} |
329 |
> |
finally { |
330 |
> |
lock.unlock(); |
331 |
> |
} |
332 |
|
} |
333 |
|
} |
334 |
|
|