diff options
| author | Benjamin Culkin <scorpress@gmail.com> | 2024-05-20 17:58:16 -0400 |
|---|---|---|
| committer | Benjamin Culkin <scorpress@gmail.com> | 2024-05-20 17:58:16 -0400 |
| commit | 40a9d99496e098562f090fb7ffce9e749011b131 (patch) | |
| tree | 437df24d65470582e943e494a52db8ed65a881ae /projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMultiReaderLock.java | |
| parent | ff072dfe782f6f22123cd4ba050828d35c0d0fbd (diff) | |
Formatting pass
Diffstat (limited to 'projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMultiReaderLock.java')
| -rw-r--r-- | projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMultiReaderLock.java | 228 |
1 files changed, 113 insertions, 115 deletions
diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMultiReaderLock.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMultiReaderLock.java index abed576..3492141 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMultiReaderLock.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMultiReaderLock.java @@ -24,136 +24,134 @@ package net.wotonomy.foundation; import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock; /** -* A Read-Write lock that allows unlimited number of calling threads to -* acquire read locks, but only one thread to acquire a write lock. It -* is also reentrant, allowing each thread to re-acquire it's lock -* recursively. For that reason it is somewhat slower, as there is a -* hash lookup when attempting to acquire and release reader locks. Of -* course a writer lock is quite a bit slower than a reader lock. A -* write lock is mutally exclusive with read locks, though a thread -* that has obtained a read-lock may be promoted to a write lock and -* vice versa when conditions permit. -* -* @author cgruber@israfil.net -* @author $Author: cgruber $ -* @version $Revision: 893 $ -*/ + * A Read-Write lock that allows unlimited number of calling threads to acquire + * read locks, but only one thread to acquire a write lock. It is also + * reentrant, allowing each thread to re-acquire it's lock recursively. For that + * reason it is somewhat slower, as there is a hash lookup when attempting to + * acquire and release reader locks. Of course a writer lock is quite a bit + * slower than a reader lock. A write lock is mutally exclusive with read locks, + * though a thread that has obtained a read-lock may be promoted to a write lock + * and vice versa when conditions permit. + * + * @author cgruber@israfil.net + * @author $Author: cgruber $ + * @version $Revision: 893 $ + */ public class NSMultiReaderLock extends ReentrantWriterPreferenceReadWriteLock implements NSLocking { - NSMutableDictionary _readerSuspended = new NSMutableDictionary(); - - public NSMultiReaderLock() { - } - - public void lockForReading() { - try { - readerLock_.acquire(); - } catch (InterruptedException interruptedexception) { - // Null behavior, as notify() is already called - // by acquire(); - // We may want to log here. - } - } - - public void unlockForReading() { - readerLock_.release(); - } - - public void lock() { - lockForWriting(); - } - - public void lockForWriting() { - try { - writerLock_.acquire(); - } catch (InterruptedException interruptedexception) { - // Null behavior, as notify() is already called - // by acquire(); - // We may want to log here. - } - } - - public void unlock() { - unlockForWriting(); - } - - public void unlockForWriting() { - writerLock_.release(); - } - - /** @see com.webobjects.foundation.NSMultiReaderLock#suspendReaderLock() */ - public void suspendReaderLocks() { - Thread thisThread = Thread.currentThread(); - Integer suspendedReaders = (Integer)_readerSuspended.get(thisThread); - if (suspendedReaders != null && suspendedReaders.intValue() > 0) return; - // logic is to override startRead / endRead and ensure that the suspension - // isn't improperly stopped. + NSMutableDictionary _readerSuspended = new NSMutableDictionary(); + + public NSMultiReaderLock() { + } + + public void lockForReading() { + try { + readerLock_.acquire(); + } catch (InterruptedException interruptedexception) { + // Null behavior, as notify() is already called + // by acquire(); + // We may want to log here. + } + } + + public void unlockForReading() { + readerLock_.release(); + } + + public void lock() { + lockForWriting(); + } + + public void lockForWriting() { + try { + writerLock_.acquire(); + } catch (InterruptedException interruptedexception) { + // Null behavior, as notify() is already called + // by acquire(); + // We may want to log here. + } + } + + public void unlock() { + unlockForWriting(); + } + + public void unlockForWriting() { + writerLock_.release(); + } + + /** @see com.webobjects.foundation.NSMultiReaderLock#suspendReaderLock() */ + public void suspendReaderLocks() { + Thread thisThread = Thread.currentThread(); + Integer suspendedReaders = (Integer) _readerSuspended.get(thisThread); + if (suspendedReaders != null && suspendedReaders.intValue() > 0) + return; + // logic is to override startRead / endRead and ensure that the suspension + // isn't improperly stopped. throw new UnsupportedOperationException("Not Yet Implemented"); - } - - /** @see com.webobjects.foundation.NSMultiReaderLock#retrieveReaderLock() */ - public void retrieveReaderLocks() { - Thread thisThread = Thread.currentThread(); - Integer suspendedReaders = (Integer)_readerSuspended.get(thisThread); - if (suspendedReaders != null && suspendedReaders.intValue() > 0) return; - // logic is to override startRead / endRead and ensure that the suspension - // isn't improperly stopped. - throw new UnsupportedOperationException("Not Yet Implemented"); - } - - public boolean tryLockForWriting() { - try { - return writerLock_.attempt(0); - } catch (InterruptedException interruptedexception) { - // notify() is already called by attempt(); - // We may want to log here. - return false; - } - } - - public boolean tryLockForReading() { - try { - return readerLock_.attempt(0); - } catch (InterruptedException interruptedexception) { - // notify() is already called by attempt(); - // We may want to log here. - return false; - } - } - - public String toString() { + } + + /** @see com.webobjects.foundation.NSMultiReaderLock#retrieveReaderLock() */ + public void retrieveReaderLocks() { + Thread thisThread = Thread.currentThread(); + Integer suspendedReaders = (Integer) _readerSuspended.get(thisThread); + if (suspendedReaders != null && suspendedReaders.intValue() > 0) + return; + // logic is to override startRead / endRead and ensure that the suspension + // isn't improperly stopped. throw new UnsupportedOperationException("Not Yet Implemented"); - } - - protected String _padString(long l, int i) { + } + + public boolean tryLockForWriting() { + try { + return writerLock_.attempt(0); + } catch (InterruptedException interruptedexception) { + // notify() is already called by attempt(); + // We may want to log here. + return false; + } + } + + public boolean tryLockForReading() { + try { + return readerLock_.attempt(0); + } catch (InterruptedException interruptedexception) { + // notify() is already called by attempt(); + // We may want to log here. + return false; + } + } + + public String toString() { throw new UnsupportedOperationException("Not Yet Implemented"); - } + } - protected String _padString(String s, int i, boolean flag) { + protected String _padString(long l, int i) { throw new UnsupportedOperationException("Not Yet Implemented"); - } + } + protected String _padString(String s, int i, boolean flag) { + throw new UnsupportedOperationException("Not Yet Implemented"); + } } /* - * $Log$ - * Revision 1.2 2006/02/16 13:15:00 cgruber - * Check in all sources in eclipse-friendly maven-enabled packages. + * $Log$ Revision 1.2 2006/02/16 13:15:00 cgruber Check in all sources in + * eclipse-friendly maven-enabled packages. * - * Revision 1.2 2003/08/06 23:07:52 chochos - * general code cleanup (mostly, removing unused imports) + * Revision 1.2 2003/08/06 23:07:52 chochos general code cleanup (mostly, + * removing unused imports) * - * Revision 1.1 2002/07/14 21:56:16 mpowers - * Contributions from cgruber. + * Revision 1.1 2002/07/14 21:56:16 mpowers Contributions from cgruber. * - * Revision 1.2 2002/06/26 00:40:22 cgruber - * Add implementation, using ReentrantWriterPreferenceReadWriteLock - * as a base. + * Revision 1.2 2002/06/26 00:40:22 cgruber Add implementation, using + * ReentrantWriterPreferenceReadWriteLock as a base. * - * suspendReaderLocks and retreiveReaderLocks is the one - * that's likeliest to be a pain. + * suspendReaderLocks and retreiveReaderLocks is the one that's likeliest to be + * a pain. * - * Revision 1.1 2002/06/25 07:52:57 cgruber - * Add quite a few abstract classes, interfaces, and classes. All API consistent with WebObjects, but with no implementation, nor any private or package access members from the original. + * Revision 1.1 2002/06/25 07:52:57 cgruber Add quite a few abstract classes, + * interfaces, and classes. All API consistent with WebObjects, but with no + * implementation, nor any private or package access members from the original. * */ |
