More changes to Monitor to attempt to catch a bug.
This commit is contained in:
@@ -118,6 +118,7 @@ public static boolean lock(Monitor monitor, long timeout, boolean allowSuspend,
|
||||
}//if//
|
||||
|
||||
if(threadData.currentLock == monitor) {
|
||||
Debug.log("MONITOR: Incrementing lock count: " + Integer.toHexString(monitor.lock.hashCode()));
|
||||
threadData.lockCount++;
|
||||
threadData.stack = new RuntimeException();
|
||||
|
||||
@@ -274,7 +275,7 @@ public static void unlock(Monitor monitor) {
|
||||
//Reduce the lock count and clear the lock if zero.//
|
||||
if(--threadData.lockCount == 0) {
|
||||
synchronized(monitor.lock) {
|
||||
threadData.currentLock.lockingThreadData = null;
|
||||
monitor.lockingThreadData = null;
|
||||
monitor.lock.notify();
|
||||
Debug.log("MONITOR: Unlocking lock: " + Integer.toHexString(monitor.lock.hashCode()));
|
||||
}//synchronized//
|
||||
@@ -285,6 +286,9 @@ public static void unlock(Monitor monitor) {
|
||||
|
||||
resume(threadData);
|
||||
}//if//
|
||||
else {
|
||||
Debug.log("MONITOR: Decremented lock count: " + Integer.toHexString(monitor.lock.hashCode()));
|
||||
}//else//
|
||||
//}//if//
|
||||
}//unlock()//
|
||||
/**
|
||||
@@ -309,6 +313,7 @@ private static void suspend(ThreadData threadData) {
|
||||
if(threadData.currentLock != null) {
|
||||
//Allow another thread to lock the previously held monitor.//
|
||||
synchronized(threadData.currentLock.lock) {
|
||||
Debug.log("MONITOR: Suspended lock: " + Integer.toHexString(threadData.currentLock.lock.hashCode()));
|
||||
threadData.currentLock.lockingThreadData = null;
|
||||
threadData.currentLock.lock.notify();
|
||||
}//synchronized//
|
||||
@@ -354,6 +359,7 @@ private static void resume(ThreadData threadData) {
|
||||
//Wait until we can lock on the restored monitor.//
|
||||
synchronized(threadData.currentLock.lock) {
|
||||
if(threadData.currentLock.lockingThreadData == null) {
|
||||
Debug.log("MONITOR: Resuming lock (no wait): " + Integer.toHexString(threadData.currentLock.lock.hashCode()));
|
||||
threadData.currentLock.lockingThreadData = threadData;
|
||||
}//if//
|
||||
else {
|
||||
@@ -367,42 +373,28 @@ private static void resume(ThreadData threadData) {
|
||||
}//catch//
|
||||
|
||||
//If we couldn't get the lock in 60 seconds then display the stack of the
|
||||
if(threadData.currentLock.lockingThreadData == null) {
|
||||
threadData.currentLock.lockingThreadData = threadData;
|
||||
}//if//
|
||||
else {
|
||||
if(threadData.currentLock.lockingThreadData != null) {
|
||||
Throwable stack = threadData.currentLock.lockingThreadData.stack;
|
||||
|
||||
//Note: This access to stack is a bit unsafe, but should be fine since stack is immutable.//
|
||||
if(stack != null) {
|
||||
Debug.log(stack, "Possible deadlock detected while attempting to resume a lock. Last locking thread's stack follows (RuntimeException):");
|
||||
Debug.log(new RuntimeException(), "Resume attempt stack (RuntimeException):");
|
||||
Debug.log("Possible deadlock detected while attempting to resume a lock. Last locking thread's stack follows (RuntimeException):", stack);
|
||||
Debug.log("Resume attempt stack (RuntimeException):", new RuntimeException());
|
||||
}//if//
|
||||
|
||||
while(threadData.currentLock.lockingThreadData != null) {
|
||||
try {
|
||||
threadData.currentLock.lock.wait(0);
|
||||
}//try//
|
||||
catch(InterruptedException e) {
|
||||
Debug.handle(e);
|
||||
}//catch//
|
||||
}//while//
|
||||
|
||||
threadData.currentLock.lockingThreadData = threadData;
|
||||
}//else//
|
||||
}//if//
|
||||
}//if//
|
||||
else {
|
||||
while(threadData.currentLock.lockingThreadData != null) {
|
||||
try {
|
||||
threadData.currentLock.lock.wait(0);
|
||||
}//try//
|
||||
catch(InterruptedException e) {
|
||||
Debug.handle(e);
|
||||
}//catch//
|
||||
}//while//
|
||||
|
||||
threadData.currentLock.lockingThreadData = threadData;
|
||||
}//else//
|
||||
while(threadData.currentLock.lockingThreadData != null) {
|
||||
try {
|
||||
threadData.currentLock.lock.wait(0);
|
||||
}//try//
|
||||
catch(InterruptedException e) {
|
||||
Debug.handle(e);
|
||||
}//catch//
|
||||
}//while//
|
||||
|
||||
Debug.log("MONITOR: Resuming lock (after wait): " + Integer.toHexString(threadData.currentLock.lock.hashCode()));
|
||||
threadData.currentLock.lockingThreadData = threadData;
|
||||
}//else//
|
||||
}//synchronized//
|
||||
}//if//
|
||||
|
||||
Reference in New Issue
Block a user