make run() wait for gamePtr before starting loop

Was seeing race condition on slowest device where JNIThread init hadn't
finished before the event loop tried to use the gamePtr. So use
wait()/notifyAll() to fix.
This commit is contained in:
Eric House 2018-12-18 06:55:23 -08:00
parent 285f6ad34a
commit 891c73a1b7

View file

@ -216,6 +216,8 @@ public class JNIThread extends Thread {
if ( null != m_jniGamePtr ) { if ( null != m_jniGamePtr ) {
Log.d( TAG, "configure(): m_jniGamePtr not null; that ok?" ); Log.d( TAG, "configure(): m_jniGamePtr not null; that ok?" );
} }
synchronized ( this ) {
m_jniGamePtr = null; m_jniGamePtr = null;
if ( null != stream ) { if ( null != stream ) {
m_jniGamePtr = XwJNI.initFromStream( m_rowid, stream, m_gi, m_jniGamePtr = XwJNI.initFromStream( m_rowid, stream, m_gi,
@ -231,8 +233,12 @@ public class JNIThread extends Thread {
utils, null, cp, m_xport ); utils, null, cp, m_xport );
} }
Assert.assertNotNull( m_jniGamePtr ); Assert.assertNotNull( m_jniGamePtr );
notifyAll();
}
m_lastSavedState = Arrays.hashCode( stream ); m_lastSavedState = Arrays.hashCode( stream );
} }
Log.d( TAG, "configure() => %b", success );
return success; return success;
} }
@ -417,13 +423,24 @@ public class JNIThread extends Thread {
} }
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
@Override
public void run() public void run()
{ {
Log.d( TAG, "run() starting" );
boolean[] barr = new boolean[2]; // scratch boolean boolean[] barr = new boolean[2]; // scratch boolean
for ( ; ; ) { for ( ; ; ) {
synchronized ( this ) { synchronized ( this ) {
if ( m_stopped ) { if ( m_stopped ) {
break; break;
} else if ( null == m_jniGamePtr ) {
try {
wait();
} catch ( InterruptedException iex ) {
Log.d( TAG, "exiting run() on interrupt: %s",
iex.getMessage() );
break;
}
continue;
} }
} }
@ -728,6 +745,7 @@ public class JNIThread extends Thread {
m_jniGamePtr.release(); m_jniGamePtr.release();
m_jniGamePtr = null; m_jniGamePtr = null;
} }
Log.d( TAG, "run() finished" );
} // run } // run
public void handleBkgrnd( JNICmd cmd, Object... args ) public void handleBkgrnd( JNICmd cmd, Object... args )
@ -815,7 +833,6 @@ public class JNIThread extends Thread {
result = s_instances.get( rowid ); result = s_instances.get( rowid );
if ( null == result && makeNew ) { if ( null == result && makeNew ) {
result = new JNIThread( new GameLock( rowid, true ).lock() ); result = new JNIThread( new GameLock( rowid, true ).lock() );
Assert.assertNotNull( result );
s_instances.put( rowid, result ); s_instances.put( rowid, result );
} }
if ( null != result ) { if ( null != result ) {