|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--uk.org.ogsadai.common.files.FileLock
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
.
Field Summary | |
private static java.lang.String |
COPYRIGHT_NOTICE
|
private static DAILogger |
LOG
|
private java.util.Map |
readers
|
private java.util.Map |
writers
|
Constructor Summary | |
FileLock()
|
Method Summary | |
private java.lang.String |
canonical(java.lang.String fileName)
Return the canonical version of the given filename, if at all possible. |
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. |
private void |
decrementReaders(java.lang.String fileName)
Decrement the number of threads with a read lock on a file. |
private void |
decrementWriters(java.lang.String fileName)
Decrement 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. |
private void |
incrementReaders(java.lang.String fileName)
Increment the number of threads with a read lock on a file. |
private void |
incrementWriters(java.lang.String fileName)
Increment the number of threads with a write lock on a file. |
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. |
private void |
setReaders(java.lang.String fileName,
int number)
Set the number of threads with a read lock on a file. |
private void |
setWriters(java.lang.String fileName,
int number)
Set the number of threads with a write lock on a file. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static final java.lang.String COPYRIGHT_NOTICE
private static DAILogger LOG
private java.util.Map readers
private java.util.Map writers
Constructor Detail |
public FileLock()
Method Detail |
public void doLocking(java.lang.String fileName, boolean lock, boolean read) throws FileLockException
fileName
- Name of file.lock
- true
to lock the file, false
to unlock the fileread
- true
if the lock is a read lock,
false
if the lock is a write lock
FileLockException
- if an error occurs while waiting for
a lock.public void getReadLock(java.lang.String fileName) throws FileLockException
fileName
- Name of file.
FileLockException
- if an error occurs while waiting for
the lock.public void returnReadLock(java.lang.String fileName) throws NoExistingFileLockException
fileName
- Name of file.
NoExistingFileLockException
- if there is no existing
read lock.public void getWriteLock(java.lang.String fileName) throws FileLockException
fileName
- Name of file.
FileLockException
- if an error occurs while waiting for
the lock.public void returnWriteLock(java.lang.String fileName) throws NoExistingFileLockException
fileName
- Name of file.
NoExistingFileLockException
- if there is no existing
write lock.public int countReaders(java.lang.String fileName)
fileName
- Name of file.
public int countWriters(java.lang.String fileName)
fileName
- Name of file.
private void setReaders(java.lang.String fileName, int number)
fileName
- Name of file.number
- Number of readers.private void setWriters(java.lang.String fileName, int number)
fileName
- Name of file.number
- Number of writers.private void incrementReaders(java.lang.String fileName)
fileName
- Name of file.private void decrementReaders(java.lang.String fileName)
fileName
- Name of file.
NoExistingFileLockException
- if there is no existing
read lock.private void incrementWriters(java.lang.String fileName)
fileName
- Name of file.private void decrementWriters(java.lang.String fileName)
fileName
- Name of file.
NoExistingFileLockException
- if there is no existing
write lock.private java.lang.String canonical(java.lang.String fileName)
fileName
- Name of file.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |