org.eclipse.rse.services.clientserver
Class SystemReentrantMutex
java.lang.Object
org.eclipse.rse.services.clientserver.SystemReentrantMutex
-
public class SystemReentrantMutex
- extends
Object
A reentrant Exclusion Lock for Threads that need to access a resource in a
serialized manner. If the request for the lock is running on the same thread
who is currently holding the lock, it will "borrow" the lock, and the call to
waitForLock() will go through. A SystemOperationMonitor is accepted in order
to support cancellation when waiting for the Mutex. This is a clone of
Mutex
with some modification to make sure the
sequential calls to waitForLock() method in the same thread will not be
blocked.
Usage Example:
private SystemReentrantMutex fooMutex = new SystemReentrantMutex();
boolean doFooSerialized()(ISystemOperationMonitor monitor) {
int mutexLockStatus = SystemReentrantMutex.LOCK_STATUS_NOLOCK;
mutexLockStatus = fooMutex.waitForLock(monitor, 1000);
if (mutexLockStatus != SystemReentrantMutex.LOCK_STATUS_NOLOCK) {
try {
return doFoo();
} finally {
//We only release the mutex if we acquire it, not borrowed it.
if (mutexLockStatus == SystemReentrantMutex.LOCK_STATUS_AQUIRED)
{
fooMutex.release();
}
}
}
return false;
}
-
Since:
- 3.0
Methods inherited from class java.lang.
Object
|
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
|
LOCK_STATUS_NOLOCK
public static final int LOCK_STATUS_NOLOCK
-
See Also:
-
Constant Field Values
LOCK_STATUS_AQUIRED
public static final int LOCK_STATUS_AQUIRED
-
See Also:
-
Constant Field Values
LOCK_STATUS_BORROWED
public static final int LOCK_STATUS_BORROWED
-
See Also:
-
Constant Field Values
SystemReentrantMutex
public SystemReentrantMutex()
- Creates an instance of SystemMutex.
waitForLock
public int waitForLock(
ISystemOperationMonitor monitor,
long timeout)
- Try to acquire the lock maintained by this mutex.
If the thread needs to wait before it can acquire the mutex, it
will wait in a first-come-first-serve fashion. In case a progress
monitor was given, it will be updated and checked for cancel every
second.
-
-
Parameters:
-
monitor
- SystemOperationMonitor. May be null
. -
timeout
- Maximum wait time given in milliseconds.
-
Returns:
-
LOCK_STATUS_AQUIRED
if the lock was acquired successfully.
LOCK_STATUS_BORROWED
if the lock was borrowed.
LOCK_STATUS_NOLOCK
if otherwise.
release
public void release()
- Release this mutex's lock.
May only be called by the same thread that originally acquired
the SystemMutex.
-
isLocked
public boolean isLocked()
- Return this Mutex's lock status.
-
-
Returns:
-
true
if this mutex is currently acquired by a thread.
interruptAll
public void interruptAll()
- Interrupt all threads waiting for the Lock, causing their
waitForLock(ISystemOperationMonitor, long)
method to return
false
.
This should be called if the resource that the Threads are
contending for, becomes unavailable for some other reason.
-
Copyright (c) IBM Corporation and others 2000, 2008. All Rights Reserved.