[jboss-cvs] JBossCache/src/org/jboss/cache/lock ...
Manik Surtani
msurtani at jboss.com
Wed Jan 3 10:33:10 EST 2007
User: msurtani
Date: 07/01/03 10:33:10
Modified: src/org/jboss/cache/lock ReadWriteLockWithUpgrade.java
Log:
Improved notification mechanism, added a notification interceptor
Revision Changes Path
1.5 +97 -62 JBossCache/src/org/jboss/cache/lock/ReadWriteLockWithUpgrade.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ReadWriteLockWithUpgrade.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/lock/ReadWriteLockWithUpgrade.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- ReadWriteLockWithUpgrade.java 8 Dec 2006 18:50:49 -0000 1.4
+++ ReadWriteLockWithUpgrade.java 3 Jan 2007 15:33:10 -0000 1.5
@@ -7,15 +7,14 @@
package org.jboss.cache.lock;
-import java.util.Map;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
-import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
/*
* <p> This class is similar to PreferredWriterReadWriteLock except that
@@ -66,12 +65,12 @@
protected static final Object dummy_ = new Object();
protected final ReaderLock readerLock_ = new ReaderLock();
protected final WriterLock writerLock_ = new WriterLock();
- protected static final Log log_=LogFactory.getLog(ReadWriteLockWithUpgrade.class);
+ protected static final Log log_ = LogFactory.getLog(ReadWriteLockWithUpgrade.class);
public String toString()
{
- StringBuffer sb=new StringBuffer();
+ StringBuffer sb = new StringBuffer();
sb.append("activeReaders=").append(activeReaders_).append(", activeWriter=").append(activeWriter_);
sb.append(", waitingReaders=").append(waitingReaders_).append(", waitingWriters=").append(waitingWriters_);
sb.append(", waitingUpgrader=").append(waitingUpgrader_);
@@ -101,14 +100,16 @@
if (activeReaders_ == 0)
throw new RuntimeException("No reader lock available for upgrade");
- synchronized (writerLock_) {
- if(waitingUpgrader_ >=1) {
- String errStr="upgradeLockAttempt(): more than one reader trying to simultaneously upgrade to write lock";
+ synchronized (writerLock_)
+ {
+ if (waitingUpgrader_ >= 1)
+ {
+ String errStr = "upgradeLockAttempt(): more than one reader trying to simultaneously upgrade to write lock";
log_.error(errStr);
throw new UpgradeException(errStr);
}
waitingUpgrader_++;
- upgraderLocal_.put(this,dummy_);
+ upgraderLocal_.put(this, dummy_);
}
// If there is only one reader left, switch to write lock immediately.
@@ -116,18 +117,24 @@
// lock. There is still a chance for deadlock when there are two reader locks
// and suddenly the second lock is released when this lock is released and acquired
// as is else case. Solution is to let it timeout.
- if (activeReaders_ == 1) {
+ if (activeReaders_ == 1)
+ {
resetWaitingUpgrader();
return changeLock();
- } else {
+ }
+ else
+ {
readerLock_.unlock();
- try {
- if (!writerLock_.tryLock(msecs, TimeUnit.MILLISECONDS)) {
+ try
+ {
+ if (!writerLock_.tryLock(msecs, TimeUnit.MILLISECONDS))
+ {
log_.error("upgradeLock(): failed");
resetWaitingUpgrader();
- if(!readerLock_.tryLock(msecs, TimeUnit.MILLISECONDS)) {
- String errStr="ReadWriteLockWithUpgrade.upgradeLockAttempt():" +
+ if (!readerLock_.tryLock(msecs, TimeUnit.MILLISECONDS))
+ {
+ String errStr = "ReadWriteLockWithUpgrade.upgradeLockAttempt():" +
" failed to upgrade to write lock and also failed to re-obtain the read lock";
log_.error(errStr);
throw new IllegalStateException(errStr);
@@ -135,7 +142,9 @@
return null;
}
resetWaitingUpgrader();
- } catch (InterruptedException ex) {
+ }
+ catch (InterruptedException ex)
+ {
resetWaitingUpgrader();
return null;
}
@@ -144,8 +153,10 @@
}
}
- private void resetWaitingUpgrader() {
- synchronized (writerLock_) {
+ private void resetWaitingUpgrader()
+ {
+ synchronized (writerLock_)
+ {
waitingUpgrader_--;
upgraderLocal_.remove(this);
}
@@ -155,7 +166,8 @@
{
--activeReaders_;
- if (!startWrite()) {
+ if (!startWrite())
+ {
// Something is wrong.
return null;
}
@@ -189,7 +201,8 @@
protected synchronized boolean startRead()
{
boolean allowRead = allowReader();
- if (allowRead) {
+ if (allowRead)
+ {
++activeReaders_;
}
return allowRead;
@@ -276,6 +289,7 @@
static interface Signaller
{ // base for ReaderLock and WriterLock
+
void signalWaiters();
}
@@ -322,7 +336,8 @@
public void unlock()
{
Signaller s = endRead();
- if (s != null) {
+ if (s != null)
+ {
s.signalWaiters();
}
}
@@ -337,28 +352,35 @@
if (Thread.interrupted()) throw new InterruptedException();
long msecs = unit.toMillis(time);
InterruptedException ie = null;
- synchronized (this) {
+ synchronized (this)
+ {
if (msecs <= 0)
return startRead();
else if (startReadFromNewReader())
return true;
- else {
+ else
+ {
long waitTime = msecs;
long start = System.currentTimeMillis();
- while(true) {
- try {
+ while (true)
+ {
+ try
+ {
ReaderLock.this.wait(waitTime);
}
- catch(InterruptedException ex) {
+ catch (InterruptedException ex)
+ {
cancelledWaitingReader();
- ie=ex;
+ ie = ex;
break;
}
- if(startReadFromWaitingReader())
+ if (startReadFromWaitingReader())
return true;
- else {
- waitTime=msecs - (System.currentTimeMillis() - start);
- if(waitTime <= 0) {
+ else
+ {
+ waitTime = msecs - (System.currentTimeMillis() - start);
+ if (waitTime <= 0)
+ {
cancelledWaitingReader();
break;
}
@@ -398,50 +420,65 @@
InterruptedException ie = null;
long msecs = unit.toMillis(time);
- synchronized (WriterLock.this) {
- if (msecs <= 0) {
+ synchronized (WriterLock.this)
+ {
+ if (msecs <= 0)
+ {
// Upgrade thread has prioirty.
- if (waitingUpgrader_ != 0) {
- if (upgraderLocal_.get(ReadWriteLockWithUpgrade.this) != null) {
+ if (waitingUpgrader_ != 0)
+ {
+ if (upgraderLocal_.get(ReadWriteLockWithUpgrade.this) != null)
+ {
log_.info("attempt(): upgrade to write lock");
return startWrite();
}
else
return false;
- } else
+ }
+ else
return startWrite();
- } else if (startWriteFromNewWriter())
+ }
+ else if (startWriteFromNewWriter())
return true;
- else {
+ else
+ {
long waitTime = msecs;
long start = System.currentTimeMillis();
- while(true) {
- try {
+ while (true)
+ {
+ try
+ {
WriterLock.this.wait(waitTime);
}
- catch(InterruptedException ex) {
+ catch (InterruptedException ex)
+ {
cancelledWaitingWriter();
WriterLock.this.notifyAll();
- ie=ex;
+ ie = ex;
break;
}
- if(waitingUpgrader_ != 0) { // Has upgrade request
- if(upgraderLocal_.get(ReadWriteLockWithUpgrade.this) != null) { // Upgrade thread
- if(startWriteFromWaitingWriter())
+ if (waitingUpgrader_ != 0)
+ { // Has upgrade request
+ if (upgraderLocal_.get(ReadWriteLockWithUpgrade.this) != null)
+ { // Upgrade thread
+ if (startWriteFromWaitingWriter())
return true;
}
- else { // Normal write thread, go back to wait.
+ else
+ { // Normal write thread, go back to wait.
continue;
}
}
- else { // Every one is normal write thread. Compete; if fail go back to wait.
- if(startWriteFromWaitingWriter())
+ else
+ { // Every one is normal write thread. Compete; if fail go back to wait.
+ if (startWriteFromWaitingWriter())
return true;
}
- waitTime=msecs - (System.currentTimeMillis() - start);
- if(waitTime <= 0) {
+ waitTime = msecs - (System.currentTimeMillis() - start);
+ if (waitTime <= 0)
+ {
cancelledWaitingWriter();
WriterLock.this.notifyAll();
break;
@@ -460,7 +497,5 @@
}
-
-
}
More information about the jboss-cvs-commits
mailing list