1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
160
|
/*
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<Thread, Integer> _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.
*
*/
|