From 4cdafbd510c77c5b339e8dd8fe5c7b3c6eaeacc2 Mon Sep 17 00:00:00 2001 From: Andy2 Date: Fri, 4 Mar 2011 18:36:33 -0800 Subject: [PATCH] reduce the length of the sleep when waiting for lock; add code to capture stack trace at lock time that'll be printed out along with the current thread's trace when I can't get a lock in one second. I'll comment that out shortly but want it for debugging -- though it's untested: can't repro any failure to lock now. --- .../src/org/eehouse/android/xw4/GameUtils.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java index a5b057912..6d6a29345 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java @@ -48,6 +48,7 @@ public class GameUtils { private String m_path; private boolean m_isForWrite; private int m_lockCount; + StackTraceElement[] m_lockTrace; // This will leak empty ReentrantReadWriteLock instances for // now. @@ -75,6 +76,11 @@ public class GameUtils { s_locks.put( m_path, this ); ++m_lockCount; gotIt = true; + + StackTraceElement[] trace = Thread.currentThread(). + getStackTrace(); + m_lockTrace = new StackTraceElement[trace.length]; + System.arraycopy( trace, 0, m_lockTrace, 0, trace.length ); } else if ( this == owner && ! m_isForWrite ) { Assert.assertTrue( 0 == m_lockCount ); ++m_lockCount; @@ -86,6 +92,7 @@ public class GameUtils { public GameLock lock() { + long stopTime = System.currentTimeMillis() + 1000; Utils.logf( "GameLock.lock(%s)", m_path ); // Utils.printStack(); for ( ; ; ) { @@ -95,11 +102,15 @@ public class GameUtils { Utils.logf( "GameLock.lock() failed; sleeping" ); // Utils.printStack(); try { - Thread.sleep( 100 ); // milliseconds + Thread.sleep( 25 ); // milliseconds } catch( InterruptedException ie ) { Utils.logf( "GameLock.lock(): %s", ie.toString() ); break; } + if ( System.currentTimeMillis() >= stopTime ) { + Utils.printStack( m_lockTrace ); + Assert.fail(); + } } Utils.logf( "GameLock.lock(%s) done", m_path ); return this;