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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*
File: WriterPreferenceReadWriteLock.java
Originally written by Doug Lea and released into the public domain.
This may be used for any purposes whatsoever without acknowledgment.
Thanks for the assistance and support of Sun Microsystems Labs,
and everyone contributing, testing, and using this code.
History:
Date Who What
11Jun1998 dl Create public version
5Aug1998 dl replaced int counters with longs
25aug1998 dl record writer thread
3May1999 dl add notifications on interrupt/timeout
*/
package EDU.oswego.cs.dl.util.concurrent;
/**
* A ReadWriteLock that prefers waiting writers over
* waiting readers when there is contention. This class
* is adapted from the versions described in CPJ, improving
* on the ones there a bit by segregating reader and writer
* wait queues, which is typically more efficient.
* <p>
* The locks are <em>NOT</em> reentrant. In particular,
* even though it may appear to usually work OK,
* a thread holding a read lock should not attempt to
* re-acquire it. Doing so risks lockouts when there are
* also waiting writers.
* <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
**/
public class WriterPreferenceReadWriteLock implements ReadWriteLock {
protected long activeReaders_ = 0;
protected Thread activeWriter_ = null;
protected long waitingReaders_ = 0;
protected long waitingWriters_ = 0;
protected final ReaderLock readerLock_ = new ReaderLock();
protected final WriterLock writerLock_ = new WriterLock();
public Sync writeLock() { return writerLock_; }
public Sync readLock() { return readerLock_; }
/*
A bunch of small synchronized methods are needed
to allow communication from the Lock objects
back to this object, that serves as controller
*/
protected synchronized void cancelledWaitingReader() { --waitingReaders_; }
protected synchronized void cancelledWaitingWriter() { --waitingWriters_; }
/** Override this method to change to reader preference **/
protected boolean allowReader() {
return activeWriter_ == null && waitingWriters_ == 0;
}
protected synchronized boolean startRead() {
boolean allowRead = allowReader();
if (allowRead) ++activeReaders_;
return allowRead;
}
protected synchronized boolean startWrite() {
// The allowWrite expression cannot be modified without
// also changing startWrite, so is hard-wired
boolean allowWrite = (activeWriter_ == null && activeReaders_ == 0);
if (allowWrite) activeWriter_ = Thread.currentThread();
return allowWrite;
}
/*
Each of these variants is needed to maintain atomicity
of wait counts during wait loops. They could be
made faster by manually inlining each other. We hope that
compilers do this for us though.
*/
protected synchronized boolean startReadFromNewReader() {
boolean pass = startRead();
if (!pass) ++waitingReaders_;
return pass;
}
protected synchronized boolean startWriteFromNewWriter() {
boolean pass = startWrite();
if (!pass) ++waitingWriters_;
return pass;
}
protected synchronized boolean startReadFromWaitingReader() {
boolean pass = startRead();
if (pass) --waitingReaders_;
return pass;
}
protected synchronized boolean startWriteFromWaitingWriter() {
boolean pass = startWrite();
if (pass) --waitingWriters_;
return pass;
}
/**
* Called upon termination of a read.
* Returns the object to signal to wake up a waiter, or null if no such
**/
protected synchronized Signaller endRead() {
if (--activeReaders_ == 0 && waitingWriters_ > 0)
return writerLock_;
else
return null;
}
/**
* Called upon termination of a write.
* Returns the object to signal to wake up a waiter, or null if no such
**/
protected synchronized Signaller endWrite() {
activeWriter_ = null;
if (waitingReaders_ > 0 && allowReader())
return readerLock_;
else if (waitingWriters_ > 0)
return writerLock_;
else
return null;
}
/**
* Reader and Writer requests are maintained in two different
* wait sets, by two different objects. These objects do not
* know whether the wait sets need notification since they
* don't know preference rules. So, each supports a
* method that can be selected by main controlling object
* to perform the notifications. This base class simplifies mechanics.
**/
protected abstract class Signaller { // base for ReaderLock and WriterLock
abstract void signalWaiters();
}
protected class ReaderLock extends Signaller implements Sync {
public void acquire() throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
InterruptedException ie = null;
synchronized(this) {
if (!startReadFromNewReader()) {
for (;;) {
try {
ReaderLock.this.wait();
if (startReadFromWaitingReader())
return;
}
catch(InterruptedException ex){
cancelledWaitingReader();
ie = ex;
break;
}
}
}
}
if (ie != null) {
// fall through outside synch on interrupt.
// This notification is not really needed here,
// but may be in plausible subclasses
writerLock_.signalWaiters();
throw ie;
}
}
public void release() {
Signaller s = endRead();
if (s != null) s.signalWaiters();
}
synchronized void signalWaiters() { ReaderLock.this.notifyAll(); }
public boolean attempt(long msecs) throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
InterruptedException ie = null;
synchronized(this) {
if (msecs <= 0)
return startRead();
else if (startReadFromNewReader())
return true;
else {
long waitTime = msecs;
long start = System.currentTimeMillis();
for (;;) {
try { ReaderLock.this.wait(waitTime); }
catch(InterruptedException ex){
cancelledWaitingReader();
ie = ex;
break;
}
if (startReadFromWaitingReader())
return true;
else {
waitTime = msecs - (System.currentTimeMillis() - start);
if (waitTime <= 0) {
cancelledWaitingReader();
break;
}
}
}
}
}
// safeguard on interrupt or timeout:
writerLock_.signalWaiters();
if (ie != null) throw ie;
else return false; // timed out
}
}
protected class WriterLock extends Signaller implements Sync {
public void acquire() throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
InterruptedException ie = null;
synchronized(this) {
if (!startWriteFromNewWriter()) {
for (;;) {
try {
WriterLock.this.wait();
if (startWriteFromWaitingWriter())
return;
}
catch(InterruptedException ex){
cancelledWaitingWriter();
WriterLock.this.notify();
ie = ex;
break;
}
}
}
}
if (ie != null) {
// Fall through outside synch on interrupt.
// On exception, we may need to signal readers.
// It is not worth checking here whether it is strictly necessary.
readerLock_.signalWaiters();
throw ie;
}
}
public void release(){
Signaller s = endWrite();
if (s != null) s.signalWaiters();
}
synchronized void signalWaiters() { WriterLock.this.notify(); }
public boolean attempt(long msecs) throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
InterruptedException ie = null;
synchronized(this) {
if (msecs <= 0)
return startWrite();
else if (startWriteFromNewWriter())
return true;
else {
long waitTime = msecs;
long start = System.currentTimeMillis();
for (;;) {
try { WriterLock.this.wait(waitTime); }
catch(InterruptedException ex){
cancelledWaitingWriter();
WriterLock.this.notify();
ie = ex;
break;
}
if (startWriteFromWaitingWriter())
return true;
else {
waitTime = msecs - (System.currentTimeMillis() - start);
if (waitTime <= 0) {
cancelledWaitingWriter();
WriterLock.this.notify();
break;
}
}
}
}
}
readerLock_.signalWaiters();
if (ie != null) throw ie;
else return false; // timed out
}
}
}
|