edu.ucla.stat.SOCR.JRI
Class Mutex

java.lang.Object
  extended by edu.ucla.stat.SOCR.JRI.Mutex

public class Mutex
extends java.lang.Object

This class implements a (not so) simple mutex. The initial state of the mutex is unlocked.


Field Summary
static boolean verbose
           
 
Constructor Summary
Mutex()
           
 
Method Summary
 void lock()
          locks the mutex.
 boolean lockWithTimeout(long to)
          locks the mutex.
 boolean safeLock()
          Locks the mutex.
 boolean safeLockWithTimeout(long to)
          Locks the mutex.
 java.lang.String toString()
           
 int tryLock()
          attempts to lock the mutex and returns information about its success.
 void unlock()
          unlocks the mutex.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

verbose

public static boolean verbose
Constructor Detail

Mutex

public Mutex()
Method Detail

lock

public void lock()
locks the mutex. If the mutex is already locked, waits until the mutex becomes free. Make sure the same thread doesn't issue two locks, because that will cause a deadlock. Use safeLock() instead if you wish to detect such deadlocks.


lockWithTimeout

public boolean lockWithTimeout(long to)
locks the mutex. If the mutex is already locked, waits until the mutex becomes free. Make sure the same thread doesn't issue two locks, because that will cause a deadlock.

Parameters:
to - timeout in milliseconds, see Object.wait().
Returns:
true if the lock was successful, false if not

tryLock

public int tryLock()
attempts to lock the mutex and returns information about its success.

Returns:
0 if the mutex was locked sucessfully
1 if the mutex is already locked by another thread
-1 is the mutex is already locked by the same thread (hence a call to lock() would cause a deadlock).

safeLock

public boolean safeLock()
Locks the mutex. It works like lock() except that it returns immediately if the same thread already owns the lock. It is safer to use this function rather than lock(), because lock can possibly cause a deadlock which won't be resolved.

Returns:
true is the mutex was successfully locked, false if deadlock was detected (i.e. the same thread has already the lock).

safeLockWithTimeout

public boolean safeLockWithTimeout(long to)
Locks the mutex. It works like lockWithTimeout(long) except that it returns immediately if the same thread already owns the lock. It is safer to use this function rather than lockWithTimeout(), because lock can possibly cause a deadlock which won't be resolved.

Returns:
true is the mutex was successfully locked, false if deadlock was detected or timeout elapsed.

unlock

public void unlock()
unlocks the mutex. It is possible to unlock an unlocked mutex, but a warning may be issued.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object