CADI.Common.Util
Class Mutex

java.lang.Object
  extended by CADI.Common.Util.Mutex

public class Mutex
extends java.lang.Object

This class implements a mutex where multiple threads can get read permission but only and only thread is allowed to get the write permission.

Usage example:
getReadLock | getWriteLock
releaseLock

Version:
beta 0.3
Author:
Group on Interactive Coding of Images (GICI)

Field Summary
private  java.util.ArrayList<java.lang.Thread> threads
          Contains a list of the threads that grants permission to access to the resource.
private  boolean writeLock
          Indicates if the write access set.
 
Constructor Summary
Mutex()
          Constructor.
 
Method Summary
private  void addThread(java.lang.Thread thread)
          Adds a thread to the list of threads.
private  boolean findThread(java.lang.Thread thread)
          Checks whether a thread is or not in the list of threads.
 void getReadLock()
          Gets a lock that grants permission to read.
 void getWriteLock()
          Gets a lock that grants permission to write.
 void releaseLock()
          Release the lock (read or write).
private  void removeThread(java.lang.Thread thread)
          Removes a thread from the list of the threads.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

threads

private java.util.ArrayList<java.lang.Thread> threads
Contains a list of the threads that grants permission to access to the resource.


writeLock

private boolean writeLock
Indicates if the write access set.

Constructor Detail

Mutex

public Mutex()
Constructor.

Method Detail

getReadLock

public void getReadLock()
Gets a lock that grants permission to read.


getWriteLock

public void getWriteLock()
Gets a lock that grants permission to write. When the write permission is granted, only and only one thread has this permission.


releaseLock

public void releaseLock()
Release the lock (read or write).


findThread

private boolean findThread(java.lang.Thread thread)
Checks whether a thread is or not in the list of threads.

Parameters:
thread - the thread to find.
Returns:
true if the thread is in the thread list. Otherwise, returns false.

addThread

private void addThread(java.lang.Thread thread)
Adds a thread to the list of threads.

Parameters:
thread - the thread to be added to the list.

removeThread

private void removeThread(java.lang.Thread thread)
Removes a thread from the list of the threads.

Parameters:
thread - the thread to be removed from the thread list.


CADI - Copyright (C) 2007
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Group on Interactive Coding of Images (GICI) (contact)