Author: chirino
Date: Thu Jul 24 09:30:14 2008
New Revision: 679452
URL: http://svn.apache.org/viewvc?rev=679452&view=rev
Log:
Simplified the WriteLock a bit
Modified:
activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/WriteLock.java
Modified:
activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/WriteLock.java
URL:
http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/WriteLock.java?rev=679452&r1=679451&r2=679452&view=diff
==============================================================================
---
activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/WriteLock.java
(original)
+++
activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/WriteLock.java
Thu Jul 24 09:30:14 2008
@@ -37,8 +37,7 @@
private static final Logger LOG = Logger.getLogger(WriteLock.class);
private final WriteLockProtocol protocol;
- private final ReentrantLock conditionLock = new ReentrantLock(false);
- private final Condition ownerCondition = conditionLock.newCondition();
+ private final Object mutex = new Object();
private final ReentrantLock threadLock = new ReentrantLock(false);
private long timeout = 30 * 1000L;
private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
@@ -49,25 +48,24 @@
if (LOG.isDebugEnabled()) {
LOG.debug("Owner of: " + protocol.getDir());
}
- fireOwnerCondition();
+ notifyMutex();
}
public void whenNotOwner() {
if (LOG.isDebugEnabled()) {
LOG.debug("Not owner of: " + protocol.getDir());
}
- fireOwnerCondition();
+ // What to do if we were locked but get one of these
notifications? In what scenarios would
+ // that be possible?
+ // Perhaps the client app needs to add a WhenOwnerListener to
handle unlocking this object.
+ notifyMutex();
}
});
}
- protected void fireOwnerCondition() {
- conditionLock.lock();
- try {
- ownerCondition.signal();
- }
- finally {
- conditionLock.unlock();
+ protected void notifyMutex() {
+ synchronized(mutex) {
+ mutex.notify();
}
}
@@ -116,50 +114,39 @@
}
}
}
-
+
+
public boolean tryLock(long timeout, TimeUnit timeUnit) throws
InterruptedException {
// TODO this implementation will typically wait too long!
- boolean unlockThreadLock = false;
- try {
- if (threadLock.tryLock(timeout, timeUnit)) {
- if (protocol.isOwner()) {
+ if (threadLock.tryLock(timeout, timeUnit)) {
+ if (protocol.isOwner()) {
+ return true;
+ }
+ try {
+ if (protocol.acquire()) {
return true;
}
- unlockThreadLock = true;
- try {
- if (protocol.acquire()) {
- unlockThreadLock = false;
- return true;
- }
- } catch (KeeperException e) {
- throw new RuntimeKeeperException("Failed to acquire remote
lock: " + e, e);
- }
-
- // now lets wait for the lock condition
-
- // Note lest make sure we unlock the threadLock
- // apart from if we manage to
- conditionLock.lock();
- try {
- ownerCondition.await(timeout, timeUnit);
+
+ // Wait till we get notified, we might become the owner
+ synchronized( mutex ) {
+ mutex.wait(timeUnit.toMillis(timeout));
if (protocol.isOwner()) {
- unlockThreadLock = false;
return true;
}
}
- finally {
- conditionLock.unlock();
+
+ } catch (KeeperException e) {
+ throw new RuntimeKeeperException("Failed to acquire remote
lock: " + e, e);
+ } finally {
+ if (!protocol.isOwner()) {
+ threadLock.unlock();
}
}
- return false;
- }
- finally {
- if (unlockThreadLock) {
- threadLock.unlock();
- }
}
+ return false;
}
+
public void unlock() {
try {
if (protocol.isOwner()) {