uk.org.ogsadai.common
Class FileLock

java.lang.Object
  |
  +--uk.org.ogsadai.common.FileLock

public class FileLock
extends java.lang.Object

This class implements a Multiple Reader, Single Writer (MRSW) mutual exclusion locking facility.

Threads wishing to obtain a lock on a file should call getReadLock or getWriteLock, and should call returnReadLock or returnWriteLock to relinquish the lock.

The MRSW lock permits either one thread to hold a write lock, or any number of threads to hold read locks. For example, if two threads have read locks and another thread calls getWriteLock to obtain a write lock, this thread waits until both reading threads have relinquished their read locks via returnReadLock.

Author:
The OGSA-DAI Project Team

Constructor Summary
FileLock()
           
 
Method Summary
 int countReaders(java.lang.String fileName)
          Return the number of threads with a read lock on a file.
 int countWriters(java.lang.String fileName)
          Return the number of threads with a write lock on a file.
 void doLocking(java.lang.String fileName, boolean lock, boolean read)
          Perform a locking or unlocking operation on a file.
 void getReadLock(java.lang.String fileName)
          Block until a read lock is available on a file, and obtain it.
 void getWriteLock(java.lang.String fileName)
          Block until a write lock is available on a file, and obtain it.
 void returnReadLock(java.lang.String fileName)
          Relinquish a read lock on a file.
 void returnWriteLock(java.lang.String fileName)
          Relinquish a write lock on a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileLock

public FileLock()
Method Detail

doLocking

public void doLocking(java.lang.String fileName,
                      boolean lock,
                      boolean read)
               throws CommonSystemException
Perform a locking or unlocking operation on a file.

Parameters:
fileName - the file to lock or unlock
lock - true to lock the file, false to unlock the file
read - true if the lock is a read lock, false if the lock is a write lock
Throws:
CommonSystemException - if an error occurs while waiting for a lock

getReadLock

public void getReadLock(java.lang.String fileName)
                 throws CommonSystemException
Block until a read lock is available on a file, and obtain it.

Parameters:
fileName - the file to lock
Throws:
CommonSystemException - if an error occurs while waiting for the lock

returnReadLock

public void returnReadLock(java.lang.String fileName)
                    throws CommonSystemException
Relinquish a read lock on a file.

Parameters:
fileName - the file to unlock
Throws:
CommonSystemException - if there is no existing read lock

getWriteLock

public void getWriteLock(java.lang.String fileName)
                  throws CommonSystemException
Block until a write lock is available on a file, and obtain it.

Parameters:
fileName - the file to lock
Throws:
CommonSystemException - if an error occurs while waiting for the lock

returnWriteLock

public void returnWriteLock(java.lang.String fileName)
                     throws CommonSystemException
Relinquish a write lock on a file.

Parameters:
fileName - the file to unlock
Throws:
CommonSystemException - if there is no existing write lock

countReaders

public int countReaders(java.lang.String fileName)
Return the number of threads with a read lock on a file.

Parameters:
fileName - the file
Returns:
the number of readers

countWriters

public int countWriters(java.lang.String fileName)
Return the number of threads with a write lock on a file.

Parameters:
fileName - the file
Returns:
the number of writers