/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2002 Israfil consulting Services Corporation This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org $Id: NSMultiReaderLock.java 893 2006-02-16 13:22:23Z cgruber $ */ 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 $ */ 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(); } @Override 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. } } @Override 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; } } @Override public String toString() { throw new UnsupportedOperationException("Not Yet Implemented"); } 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. * * 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.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. * * 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. * */