01: /*
02: * HA-JDBC: High-Availability JDBC
03: * Copyright (c) 2004-2007 Paul Ferraro
04: *
05: * This library is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as published by the
07: * Free Software Foundation; either version 2.1 of the License, or (at your
08: * option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13: * for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public License
16: * along with this library; if not, write to the Free Software Foundation,
17: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * Contact: ferraro@users.sourceforge.net
20: */
21: package net.sf.hajdbc.util.concurrent;
22:
23: import java.util.concurrent.Semaphore;
24: import java.util.concurrent.TimeUnit;
25: import java.util.concurrent.locks.Condition;
26: import java.util.concurrent.locks.Lock;
27:
28: /**
29: * An implementation of {@link java.util.concurrent.lock.Lock} using a semaphore.
30: * Unlike the {@link java.util.concurrent.lock.ReentrantLock} this lock can be locked and unlocked by different threads.
31: * Lock upgrading and downgrading are not supported. Conditions are also not supported.
32: *
33: * @author Paul Ferraro
34: */
35: public class SemaphoreLock implements Lock {
36: private Semaphore semaphore;
37: private int permits;
38:
39: public SemaphoreLock() {
40: this (new Semaphore(1, true), 1);
41: }
42:
43: public SemaphoreLock(Semaphore semaphore, int permits) {
44: this .semaphore = semaphore;
45: this .permits = permits;
46: }
47:
48: /**
49: * @see java.util.concurrent.locks.Lock#lock()
50: */
51: @Override
52: public void lock() {
53: this .semaphore.acquireUninterruptibly(this .permits);
54: }
55:
56: /**
57: * @see java.util.concurrent.locks.Lock#lockInterruptibly()
58: */
59: @Override
60: public void lockInterruptibly() throws InterruptedException {
61: this .semaphore.acquire(this .permits);
62: }
63:
64: /**
65: * @see java.util.concurrent.locks.Lock#newCondition()
66: */
67: @Override
68: public Condition newCondition() {
69: throw new UnsupportedOperationException();
70: }
71:
72: /**
73: * @see java.util.concurrent.locks.Lock#tryLock()
74: */
75: @Override
76: public boolean tryLock() {
77: return this .semaphore.tryAcquire(this .permits);
78: }
79:
80: /**
81: * @see java.util.concurrent.locks.Lock#tryLock(long, java.util.concurrent.TimeUnit)
82: */
83: @Override
84: public boolean tryLock(long time, TimeUnit unit)
85: throws InterruptedException {
86: return this .semaphore.tryAcquire(this .permits, time, unit);
87: }
88:
89: /**
90: * @see java.util.concurrent.locks.Lock#unlock()
91: */
92: @Override
93: public void unlock() {
94: this.semaphore.release(this.permits);
95: }
96: }
|