org.allcolor.yahp.converter
Class CMutex

java.lang.Object
  extended by org.allcolor.yahp.converter.CMutex

public final class CMutex
extends java.lang.Object

This is a simple mutex (Mutual Exclusion) class, which permits to synchronize Thread access to some parts of the code. Example usage : CMutex m = new CMutex(); private void mymethod() { m.acquire(); try { do something... } finally { m.release(); } }

Version:
0.94
Author:
Quentin Anciaux

Nested Class Summary
private static class CMutex.CMutexComparator
          This class is used to sort the mutex when acquiring multiple
 
Field Summary
private  int acquire
          number of time the owner has acquired this mutex
private  int id
          id of the mutex, used for sorting
private static java.lang.Object IDLOCK
          used for serial access to the static id (sid)
private  java.lang.Object LOCK
          used to implement the locking mechanism
private  java.lang.ref.WeakReference owner
          owner of the mutex
private static int sid
          used to create unique id
 
Constructor Summary
CMutex()
          Creates a new CMutex object.
 
Method Summary
 void acquire()
          Acquires this mutex
 void acquire(long timeout)
          Acquires this mutex
static void acquireMultiple(CMutex[] mutex)
          Acquires an array of mutex
static void acquireMultiple(long timeout, CMutex[] mutex)
          Acquires an array of mutex
 boolean isOwner()
          return true if Thread.currentThread is the owner of the mutex.
 void release()
          Releases the mutex
static void releaseMultiple(CMutex[] mutex)
          Releases an array of mutex previously acquired
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IDLOCK

private static final java.lang.Object IDLOCK
used for serial access to the static id (sid)


sid

private static int sid
used to create unique id


acquire

private int acquire
number of time the owner has acquired this mutex


id

private int id
id of the mutex, used for sorting


LOCK

private final java.lang.Object LOCK
used to implement the locking mechanism


owner

private java.lang.ref.WeakReference owner
owner of the mutex

Constructor Detail

CMutex

public CMutex()
Creates a new CMutex object.

Method Detail

acquireMultiple

public static final void acquireMultiple(CMutex[] mutex)
Acquires an array of mutex

Parameters:
mutex - the mutex array to acquire

acquireMultiple

public static final void acquireMultiple(long timeout,
                                         CMutex[] mutex)
                                  throws java.lang.RuntimeException
Acquires an array of mutex

Parameters:
timeout - maximum time to wait
mutex - the mutex array to acquire
Throws:
java.lang.RuntimeException - if timeout is reached

releaseMultiple

public static final void releaseMultiple(CMutex[] mutex)
Releases an array of mutex previously acquired

Parameters:
mutex - the mutex array to release

acquire

public final void acquire()
Acquires this mutex


acquire

public final void acquire(long timeout)
                   throws java.lang.RuntimeException
Acquires this mutex

Parameters:
timeout - maximum time to wait for acquiring
Throws:
java.lang.RuntimeException - if timeout is reached and no acquire was possible.

isOwner

public final boolean isOwner()
return true if Thread.currentThread is the owner of the mutex.

Returns:
true if Thread.currentThread is the owner of the mutex.

release

public final void release()
Releases the mutex

Throws:
java.lang.IllegalMonitorStateException - If currentThread is not the owner.