View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2002 Israfil consulting Services Corporation
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  
18  $Id: NSMultiReaderLock.java 893 2006-02-16 13:22:23 +0000 (Thu, 16 Feb 2006) cgruber $
19  
20  */
21  
22  package net.wotonomy.foundation;
23  
24  import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock;
25  
26  /***
27  * A Read-Write lock that allows unlimited number of calling threads to
28  * acquire read locks, but only one thread to acquire a write lock.  It 
29  * is also reentrant, allowing each thread to re-acquire it's lock 
30  * recursively.  For that reason it is somewhat slower, as there is a 
31  * hash lookup when attempting to acquire and release reader locks. Of 
32  * course a writer lock is quite a bit slower than a reader lock.  A 
33  * write lock is mutally exclusive with read locks, though a thread 
34  * that has obtained a read-lock may be promoted to a write lock and 
35  * vice versa when conditions permit.
36  *
37  * @author cgruber@israfil.net
38  * @author $Author: cgruber $
39  * @version $Revision: 893 $
40  */
41  public class NSMultiReaderLock extends ReentrantWriterPreferenceReadWriteLock implements NSLocking {
42  
43      NSMutableDictionary _readerSuspended = new NSMutableDictionary();
44  
45      public NSMultiReaderLock() {
46      }
47  
48      public void lockForReading() {
49         try {
50              readerLock_.acquire();
51          } catch (InterruptedException interruptedexception) {
52              // Null behavior, as notify() is already called
53              // by acquire();            
54              // We may want to log here.  
55          }
56      }
57  
58      public void unlockForReading() {
59          readerLock_.release();
60      }
61  
62      public void lock() {
63          lockForWriting();
64      }
65  
66      public void lockForWriting() {
67         try {
68              writerLock_.acquire();
69          } catch (InterruptedException interruptedexception) {
70              // Null behavior, as notify() is already called
71              // by acquire();            
72              // We may want to log here.  
73          }
74      }
75  
76      public void unlock() {
77          unlockForWriting();
78      }
79  
80      public void unlockForWriting() {
81          writerLock_.release();
82      }
83  
84      /*** @see com.webobjects.foundation.NSMultiReaderLock#suspendReaderLock() */
85      public void suspendReaderLocks() {
86          Thread thisThread = Thread.currentThread();
87          Integer suspendedReaders = (Integer)_readerSuspended.get(thisThread);
88          if (suspendedReaders != null && suspendedReaders.intValue() > 0) return;
89          // logic is to override startRead / endRead and ensure that the suspension
90          // isn't improperly stopped.
91  		throw new UnsupportedOperationException("Not Yet Implemented");
92      }
93  
94      /*** @see com.webobjects.foundation.NSMultiReaderLock#retrieveReaderLock() */
95      public void retrieveReaderLocks() {
96          Thread thisThread = Thread.currentThread();
97          Integer suspendedReaders = (Integer)_readerSuspended.get(thisThread);
98          if (suspendedReaders != null && suspendedReaders.intValue() > 0) return;
99          // logic is to override startRead / endRead and ensure that the suspension
100         // isn't improperly stopped.
101         throw new UnsupportedOperationException("Not Yet Implemented");
102     }
103 
104     public boolean tryLockForWriting() {
105         try {
106             return writerLock_.attempt(0);
107         } catch (InterruptedException interruptedexception) {
108             // notify() is already called by attempt();
109             // We may want to log here.  
110             return false;
111         }
112     }
113 
114     public boolean tryLockForReading() {
115         try {
116             return readerLock_.attempt(0);
117         } catch (InterruptedException interruptedexception) {
118             // notify() is already called by attempt();
119             // We may want to log here.  
120             return false;
121         }
122     }
123 
124     public String toString() {
125 		throw new UnsupportedOperationException("Not Yet Implemented");
126     }
127 
128     protected String _padString(long l, int i) {
129 		throw new UnsupportedOperationException("Not Yet Implemented");
130     }
131 
132     protected String _padString(String s, int i, boolean flag) {
133 		throw new UnsupportedOperationException("Not Yet Implemented");
134     }
135 
136 
137 }
138 /*
139  * $Log$
140  * Revision 1.2  2006/02/16 13:15:00  cgruber
141  * Check in all sources in eclipse-friendly maven-enabled packages.
142  *
143  * Revision 1.2  2003/08/06 23:07:52  chochos
144  * general code cleanup (mostly, removing unused imports)
145  *
146  * Revision 1.1  2002/07/14 21:56:16  mpowers
147  * Contributions from cgruber.
148  *
149  * Revision 1.2  2002/06/26 00:40:22  cgruber
150  * Add implementation, using ReentrantWriterPreferenceReadWriteLock
151  * as a base.
152  * 
153  * suspendReaderLocks and retreiveReaderLocks is the one
154  * that's likeliest to be a pain.
155  *
156  * Revision 1.1  2002/06/25 07:52:57  cgruber
157  * 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.
158  *
159  */