diff options
Diffstat (limited to 'java/src/Mutex.java')
-rw-r--r-- | java/src/Mutex.java | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/java/src/Mutex.java b/java/src/Mutex.java index 8daab4ff9eb..1b796062ae8 100644 --- a/java/src/Mutex.java +++ b/java/src/Mutex.java @@ -61,13 +61,15 @@ public class Mutex { this.monitor_.timedWait (); this.monitor_.inUse (true); + setOwner(); } /** - * Acquire the mutex. Note that the call will return if <timeout> - * amount of time expires. - *@param tv amount of time (TimeValue) to wait before returning - * (unless operation completes before) + * Acquire the mutex. + * Throws a TimeoutException if the mutex isn't acquired before the + * given absolute time timeout. + *@param tv time (TimeValue) to wait until before throwing a + * TimeoutException (unless the mutex is acquired before that) *@exception TimeoutException wait timed out exception *@exception InterruptedException exception during wait */ @@ -76,17 +78,32 @@ public class Mutex { this.monitor_.timedWait (tv); this.monitor_.inUse (true); + setOwner(); } /** - * Release the mutex. + * Release the mutex. This is safe for non-owners to call. */ public synchronized void release () { - this.monitor_.inUse (false); - this.monitor_.signal (); + if (isOwner()) { + this.monitor_.inUse (false); + this.monitor_.signal (); + } } private TimedWaitMAdapter monitor_ = new TimedWaitMAdapter (this); // The monitor (adapter) to wait on + + // Keep track of the owner. Allow subclasses to redefine this + // behavior + private Object owner_ = null; + + protected void setOwner() { + this.owner_ = Thread.currentThread().toString(); + } + + protected boolean isOwner() { + return Thread.currentThread().toString().equals(this.owner_); + } } |