Public Member Functions | |
ReadPrioritaryFIFOWriteLock () | |
void | acquireRead () throws InterruptedException |
synchronized void | releaseRead () |
void | acquireWrite () throws InterruptedException |
synchronized void | releaseWrite () |
final synchronized boolean | isReadLocked () |
final synchronized boolean | isWriteLocked () |
If a reader holds the lock, all incoming readers are allowed to acquire the lock until a write arrives. A writer must wait for all current readers to release the lock before acquiring it.
When a writer has the lock, everybody else is blocked.
When a writer release the lock, there is a writer priority so if another writer is waiting it will have the lock even if it arrived later than readers. Writers are prioritary against readers but all writers get the lock in a FIFO order.
Definition at line 44 of file ReadPrioritaryFIFOWriteLock.java.
|
Creates a new Definition at line 64 of file ReadPrioritaryFIFOWriteLock.java. 00065 { 00066 activeReaders = 0; 00067 activeWriter = false; 00068 waitingReaders = 0; 00069 waitingWriters = 0; 00070 readSync = new Object(); 00071 writeWaitingQueue = new ArrayList(); 00072 }
|
|
Acquires the lock for a read.
Definition at line 80 of file ReadPrioritaryFIFOWriteLock.java. 00081 { 00082 synchronized (this) 00083 { 00084 if ((waitingWriters == 0) && !activeWriter) 00085 { // No active or pending writer 00086 activeReaders++; 00087 return; 00088 } 00089 } 00090 00091 // Beyond this point, we have to wait 00092 synchronized (readSync) 00093 { 00094 // It becomes a little bit tricky here but a writer could have 00095 // released the lock between the first test at the beginning of 00096 // this function and this point. Therefore we have to recheck if 00097 // the lock is not completely idle else we'll never wake up ! 00098 synchronized (this) 00099 { 00100 if (!activeWriter) 00101 { // No active or pending writer 00102 activeReaders++; 00103 return; 00104 } 00105 } 00106 00107 waitingReaders++; 00108 try 00109 { 00110 readSync.wait(); 00111 } 00112 catch (InterruptedException ie) 00113 { 00114 waitingReaders--; // roll back state 00115 throw ie; 00116 } 00117 waitingReaders--; 00118 } 00119 synchronized (this) 00120 { 00121 activeReaders++; 00122 } 00123 }
|
|
Acquires the lock for a write.
Definition at line 149 of file ReadPrioritaryFIFOWriteLock.java. Referenced by org.objectweb.cjdbc.controller.loadbalancer.raidb2.RAIDb2ec.disableBackend(), org.objectweb.cjdbc.controller.loadbalancer.raidb1.RAIDb1ec.disableBackend(), org.objectweb.cjdbc.controller.loadbalancer.raidb2.RAIDb2ec.enableBackend(), and org.objectweb.cjdbc.controller.loadbalancer.raidb1.RAIDb1ec.enableBackend(). 00150 { 00151 synchronized (Thread.currentThread()) 00152 { 00153 synchronized (this) 00154 { 00155 if ((activeReaders == 0) && !activeWriter) 00156 { 00157 activeWriter = true; 00158 return; 00159 } 00160 else 00161 { 00162 waitingWriters++; 00163 writeWaitingQueue.add(Thread.currentThread()); 00164 } 00165 } 00166 try 00167 { 00168 Thread.currentThread().wait(); 00169 } 00170 catch (InterruptedException ie) 00171 { 00172 releaseWrite(); 00173 throw ie; 00174 } 00175 } 00176 }
|
|
Tests if the lock is currently held by at least one reader.
Definition at line 211 of file ReadPrioritaryFIFOWriteLock.java. 00212 {
00213 return activeReaders > 0;
00214 }
|
|
Tests if the lock is currently held by a writer.
Definition at line 221 of file ReadPrioritaryFIFOWriteLock.java. 00222 {
00223 return activeWriter;
00224 }
|
|
Releases a lock previously acquired for reading. Definition at line 128 of file ReadPrioritaryFIFOWriteLock.java. 00129 { 00130 activeReaders--; 00131 if ((activeReaders == 0) && (waitingWriters > 0)) 00132 { // Wake up first waiting write if any 00133 Object thread = writeWaitingQueue.remove(0); 00134 synchronized (thread) 00135 { 00136 thread.notify(); 00137 activeWriter = true; 00138 waitingWriters--; 00139 } 00140 } 00141 }
|
|
Releases a lock previously acquired for writing. Definition at line 181 of file ReadPrioritaryFIFOWriteLock.java. Referenced by org.objectweb.cjdbc.controller.loadbalancer.raidb2.RAIDb2ec.disableBackend(), org.objectweb.cjdbc.controller.loadbalancer.raidb1.RAIDb1ec.disableBackend(), org.objectweb.cjdbc.controller.loadbalancer.raidb2.RAIDb2ec.enableBackend(), and org.objectweb.cjdbc.controller.loadbalancer.raidb1.RAIDb1ec.enableBackend(). 00182 { 00183 activeWriter = false; 00184 00185 // Writer priority 00186 if (waitingWriters > 0) 00187 { // Wake up first waiting write if any 00188 Object thread = writeWaitingQueue.remove(0); 00189 synchronized (thread) 00190 { 00191 thread.notify(); 00192 activeWriter = true; 00193 waitingWriters--; 00194 } 00195 } 00196 00197 // Wake up readers 00198 else 00199 synchronized (readSync) 00200 { 00201 if (waitingReaders > 0) 00202 readSync.notifyAll(); 00203 } 00204 }
|