1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
53
54
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
71
72
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
90
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
100
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
109
110 return false;
111 }
112 }
113
114 public boolean tryLockForReading() {
115 try {
116 return readerLock_.attempt(0);
117 } catch (InterruptedException interruptedexception) {
118
119
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159