More changes to Monitor to attempt to catch a bug.

This commit is contained in:
wcrisman
2014-07-11 15:51:51 -07:00
parent 66487c3b25
commit b11dc14fb3

View File

@@ -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,16 +373,15 @@ 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//
}//if//
}//if//
while(threadData.currentLock.lockingThreadData != null) {
@@ -388,22 +393,9 @@ private static void resume(ThreadData threadData) {
}//catch//
}//while//
Debug.log("MONITOR: Resuming lock (after wait): " + Integer.toHexString(threadData.currentLock.lock.hashCode()));
threadData.currentLock.lockingThreadData = threadData;
}//else//
}//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//
}//else//
}//synchronized//
}//if//
}//if//