make GamePtr AutoCloseable and use where possible

This commit is contained in:
Eric House 2019-02-12 06:13:36 -08:00
parent b1a4b1a030
commit eafe6cd06f
7 changed files with 277 additions and 247 deletions

View file

@ -2074,7 +2074,7 @@ public class BoardDelegate extends DelegateBase
success = m_jniThreadRef.configure( m_activity, m_view, m_utils, this, success = m_jniThreadRef.configure( m_activity, m_view, m_utils, this,
makeJNIHandler() ); makeJNIHandler() );
if ( success ) { if ( success ) {
m_jniGamePtr = m_jniThreadRef.getGamePtr(); m_jniGamePtr = m_jniThreadRef.getGamePtr(); // .retain()?
Assert.assertNotNull( m_jniGamePtr ); Assert.assertNotNull( m_jniGamePtr );
} }
} }
@ -2785,37 +2785,44 @@ public class BoardDelegate extends DelegateBase
public static void setupRematchFor( Activity activity, long rowID ) public static void setupRematchFor( Activity activity, long rowID )
{ {
GamePtr gamePtr = null;
GameSummary summary = null; GameSummary summary = null;
CurGameInfo gi = null; CurGameInfo gi = null;
try ( JNIThread thread = JNIThread.getRetained( rowID ) ) { try ( JNIThread thread = JNIThread.getRetained( rowID ) ) {
if ( null != thread ) { if ( null != thread ) {
gamePtr = thread.getGamePtr().retain(); try ( GamePtr gamePtr = thread.getGamePtr().retain() ) {
summary = thread.getSummary(); summary = thread.getSummary();
gi = thread.getGI(); gi = thread.getGI();
setupRematchFor( activity, gamePtr, summary, gi );
}
} else { } else {
try ( GameLock lock = GameLock.tryLockRO( rowID ) ) { try ( GameLock lock = GameLock.tryLockRO( rowID ) ) {
if ( null != lock ) { if ( null != lock ) {
summary = DBUtils.getSummary( activity, lock ); summary = DBUtils.getSummary( activity, lock );
gi = new CurGameInfo( activity ); gi = new CurGameInfo( activity );
gamePtr = GameUtils.loadMakeGame( activity, gi, lock ); try ( GamePtr gamePtr = GameUtils
.loadMakeGame( activity, gi, lock ) ) {
setupRematchFor( activity, gamePtr, summary, gi );
}
} else { } else {
DbgUtils.toastNoLock( TAG, activity, rowID, DbgUtils.toastNoLock( TAG, activity, rowID,
"setupRematchFor(%d)", rowID ); "setupRematchFor(%d)", rowID );
} }
} }
} }
}
}
private static void setupRematchFor( Activity activity, GamePtr gamePtr,
GameSummary summary, CurGameInfo gi )
{
if ( null != gamePtr ) { if ( null != gamePtr ) {
doRematchIf( activity, null, rowID, DBUtils.GROUPID_UNSPEC, doRematchIf( activity, null, gamePtr.getRowid(),
summary, gi, gamePtr ); DBUtils.GROUPID_UNSPEC, summary, gi, gamePtr );
gamePtr.release();
} else { } else {
Log.w( TAG, "setupRematchFor(): unable to lock game" ); Log.w( TAG, "setupRematchFor(): unable to lock game" );
} }
} }
}
// Return true if anything sent // Return true if anything sent
private boolean tryRematchInvites( boolean force ) private boolean tryRematchInvites( boolean force )

View file

@ -298,7 +298,7 @@ public class BoardView extends View implements BoardHandler, SyncedDraw {
Assert.assertTrue( null != parent || !BuildConfig.DEBUG ); Assert.assertTrue( null != parent || !BuildConfig.DEBUG );
m_parent = parent; m_parent = parent;
m_jniThread = thread; m_jniThread = thread;
m_jniGamePtr = thread.getGamePtr(); m_jniGamePtr = thread.getGamePtr(); // .retain()?
m_gi = thread.getGI(); m_gi = thread.getGI();
m_isSolo = CurGameInfo.DeviceRole.SERVER_STANDALONE == m_gi.serverRole; m_isSolo = CurGameInfo.DeviceRole.SERVER_STANDALONE == m_gi.serverRole;
m_connTypes = connTypes; m_connTypes = connTypes;

View file

@ -670,10 +670,11 @@ public class DelegateBase implements DlgClickNotify,
Log.d( TAG, "onStatusClicked(%d)", rowid ); Log.d( TAG, "onStatusClicked(%d)", rowid );
try ( GameLock lock = GameLock.tryLockRO( rowid ) ) { try ( GameLock lock = GameLock.tryLockRO( rowid ) ) {
if ( null != lock ) { if ( null != lock ) {
GamePtr gamePtr = GameUtils.loadMakeGame( getActivity(), lock ); try ( GamePtr gamePtr = GameUtils
.loadMakeGame( getActivity(), lock ) ) {
if ( null != gamePtr ) { if ( null != gamePtr ) {
onStatusClicked( gamePtr ); onStatusClicked( gamePtr );
gamePtr.release(); }
} }
} }
} }

View file

@ -536,18 +536,27 @@ public class GameConfigDelegate extends DelegateBase
if ( null == m_giOrig ) { if ( null == m_giOrig ) {
m_giOrig = new CurGameInfo( m_activity ); m_giOrig = new CurGameInfo( m_activity );
XwJNI.GamePtr gamePtr = null;
if ( null != m_jniThread ) { if ( null != m_jniThread ) {
gamePtr = m_jniThread.getGamePtr().retain(); try ( XwJNI.GamePtr gamePtr = m_jniThread
.getGamePtr().retain() ) {
loadGame( gamePtr );
}
} else { } else {
try ( GameLock lock = GameLock.tryLockRO( m_rowid ) ) { try ( GameLock lock = GameLock.tryLockRO( m_rowid ) ) {
if ( null != lock ) { if ( null != lock ) {
gamePtr = GameUtils.loadMakeGame( m_activity, m_giOrig, try ( XwJNI.GamePtr gamePtr = GameUtils.
lock ); loadMakeGame( m_activity, m_giOrig, lock ) ) {
loadGame( gamePtr );
}
}
}
} }
} }
} }
// Exists only to be called from inside two try-with-resource blocks above
private void loadGame( XwJNI.GamePtr gamePtr )
{
if ( null == gamePtr ) { if ( null == gamePtr ) {
Assert.assertFalse( BuildConfig.DEBUG ); Assert.assertFalse( BuildConfig.DEBUG );
} else { } else {
@ -587,8 +596,6 @@ public class GameConfigDelegate extends DelegateBase
buildDisabledsMap( gamePtr ); buildDisabledsMap( gamePtr );
setDisableds(); setDisableds();
gamePtr.release();
m_car = new CommsAddrRec( m_carOrig ); m_car = new CommsAddrRec( m_carOrig );
setTitle(); setTitle();
@ -631,7 +638,6 @@ public class GameConfigDelegate extends DelegateBase
setBoardsizeSpinner(); setBoardsizeSpinner();
} }
}
} // loadGame } // loadGame
private void showTimerSet( boolean show ) private void showTimerSet( boolean show )

View file

@ -124,21 +124,21 @@ public class GameUtils {
{ {
CurGameInfo gi = new CurGameInfo( context ); CurGameInfo gi = new CurGameInfo( context );
CommsAddrRec addr = null; CommsAddrRec addr = null;
GamePtr gamePtr = loadMakeGame( context, gi, lockSrc );
String[] dictNames = gi.dictNames(); String[] dictNames = gi.dictNames();
DictUtils.DictPairs pairs = DictUtils.openDicts( context, dictNames ); DictUtils.DictPairs pairs = DictUtils.openDicts( context, dictNames );
try ( GamePtr gamePtr = loadMakeGame( context, gi, lockSrc ) ) {
if ( XwJNI.game_hasComms( gamePtr ) ) { if ( XwJNI.game_hasComms( gamePtr ) ) {
addr = new CommsAddrRec(); addr = new CommsAddrRec();
XwJNI.comms_getAddr( gamePtr, addr ); XwJNI.comms_getAddr( gamePtr, addr );
} }
gamePtr.release(); }
gamePtr = XwJNI.initNew( gi, dictNames, pairs.m_bytes, pairs.m_paths, try ( GamePtr gamePtr = XwJNI
.initNew( gi, dictNames, pairs.m_bytes, pairs.m_paths,
gi.langName( context ), (UtilCtxt)null, gi.langName( context ), (UtilCtxt)null,
(DrawCtx)null, CommonPrefs.get( context ), (DrawCtx)null, CommonPrefs.get( context ),
(TransportProcs)null ); (TransportProcs)null ) ) {
if ( juggle ) { if ( juggle ) {
gi.juggle(); gi.juggle();
@ -157,9 +157,9 @@ public class GameUtils {
} else { } else {
saveGame( context, gamePtr, gi, lockDest, true ); saveGame( context, gamePtr, gi, lockDest, true );
} }
summarizeAndRelease( context, lockDest, gamePtr, gi ); summarize( context, lockDest, gamePtr, gi );
DBUtils.saveThumbnail( context, lockDest, null ); DBUtils.saveThumbnail( context, lockDest, null );
}
return lockDest; return lockDest;
} // resetGame } // resetGame
@ -196,17 +196,13 @@ public class GameUtils {
return result; return result;
} }
private static GameSummary summarizeAndRelease( Context context, private static GameSummary summarize( Context context, GameLock lock,
GameLock lock, GamePtr gamePtr, CurGameInfo gi )
GamePtr gamePtr,
CurGameInfo gi )
{ {
GameSummary summary = new GameSummary( gi ); GameSummary summary = new GameSummary( gi );
XwJNI.game_summarize( gamePtr, summary ); XwJNI.game_summarize( gamePtr, summary );
DBUtils.saveSummary( context, lock, summary ); DBUtils.saveSummary( context, lock, summary );
gamePtr.release();
return summary; return summary;
} }
@ -214,9 +210,10 @@ public class GameUtils {
{ {
GameSummary result = null; GameSummary result = null;
CurGameInfo gi = new CurGameInfo( context ); CurGameInfo gi = new CurGameInfo( context );
GamePtr gamePtr = loadMakeGame( context, gi, lock ); try ( GamePtr gamePtr = loadMakeGame( context, gi, lock ) ) {
if ( null != gamePtr ) { if ( null != gamePtr ) {
result = summarizeAndRelease( context, lock, gamePtr, gi ); result = summarize( context, lock, gamePtr, gi );
}
} }
return result; return result;
} }
@ -411,14 +408,14 @@ public class GameUtils {
try ( GameLock lock = GameLock.tryLockRO( rowid ) ) { try ( GameLock lock = GameLock.tryLockRO( rowid ) ) {
if ( null != lock ) { if ( null != lock ) {
CurGameInfo gi = new CurGameInfo( context ); CurGameInfo gi = new CurGameInfo( context );
GamePtr gamePtr = loadMakeGame( context, gi, lock ); try ( GamePtr gamePtr = loadMakeGame( context, gi, lock ) ) {
if ( null != gamePtr ) { if ( null != gamePtr ) {
thumb = takeSnapshot( context, gamePtr, gi ); thumb = takeSnapshot( context, gamePtr, gi );
gamePtr.release();
DBUtils.saveThumbnail( context, lock, thumb ); DBUtils.saveThumbnail( context, lock, thumb );
} }
} }
} }
}
return thumb; return thumb;
} }
@ -925,7 +922,7 @@ public class GameUtils {
if ( null != lock ) { if ( null != lock ) {
CurGameInfo gi = new CurGameInfo( context ); CurGameInfo gi = new CurGameInfo( context );
FeedUtilsImpl feedImpl = new FeedUtilsImpl( context, rowid ); FeedUtilsImpl feedImpl = new FeedUtilsImpl( context, rowid );
GamePtr gamePtr = loadMakeGame( context, gi, feedImpl, sink, lock ); try ( GamePtr gamePtr = loadMakeGame( context, gi, feedImpl, sink, lock ) ) {
if ( null != gamePtr ) { if ( null != gamePtr ) {
XwJNI.comms_resendAll( gamePtr, false, false ); XwJNI.comms_resendAll( gamePtr, false, false );
@ -954,12 +951,13 @@ public class GameUtils {
} }
saveGame( context, gamePtr, gi, lock, false ); saveGame( context, gamePtr, gi, lock, false );
GameSummary summary = summarizeAndRelease( context, lock, GameSummary summary = summarize( context, lock,
gamePtr, gi ); gamePtr, gi );
if ( null != isLocalOut ) { if ( null != isLocalOut ) {
isLocalOut[0] = 0 <= summary.turn isLocalOut[0] = 0 <= summary.turn
&& gi.players[summary.turn].isLocal; && gi.players[summary.turn].isLocal;
} }
}
int flags = setFromFeedImpl( feedImpl ); int flags = setFromFeedImpl( feedImpl );
if ( GameSummary.MSG_FLAGS_NONE != flags ) { if ( GameSummary.MSG_FLAGS_NONE != flags ) {
@ -999,18 +997,18 @@ public class GameUtils {
DictUtils.DictPairs pairs = DictUtils.openDicts( context, DictUtils.DictPairs pairs = DictUtils.openDicts( context,
dictNames ); dictNames );
GamePtr gamePtr = try ( GamePtr gamePtr =
XwJNI.initFromStream( rowid, stream, gi, dictNames, XwJNI.initFromStream( rowid, stream, gi, dictNames,
pairs.m_bytes, pairs.m_paths, pairs.m_bytes, pairs.m_paths,
gi.langName( context ), null, gi.langName( context ), null,
null, CommonPrefs.get( context ), null ); null, CommonPrefs.get( context ), null ) ) {
// second time required as game_makeFromStream can overwrite // second time required as game_makeFromStream can overwrite
gi.replaceDicts( context, newDict ); gi.replaceDicts( context, newDict );
saveGame( context, gamePtr, gi, lock, false ); saveGame( context, gamePtr, gi, lock, false );
summarizeAndRelease( context, lock, gamePtr, gi ); summarize( context, lock, gamePtr, gi );
}
} else { } else {
DbgUtils.toastNoLock( TAG, context, rowid, DbgUtils.toastNoLock( TAG, context, rowid,
"replaceDicts(): rowid %d", "replaceDicts(): rowid %d",
@ -1042,7 +1040,6 @@ public class GameUtils {
String[] dictNames = gi.dictNames(); String[] dictNames = gi.dictNames();
DictUtils.DictPairs pairs = DictUtils.openDicts( context, dictNames ); DictUtils.DictPairs pairs = DictUtils.openDicts( context, dictNames );
String langName = gi.langName( context ); String langName = gi.langName( context );
GamePtr gamePtr = null;
boolean madeGame = false; boolean madeGame = false;
CommonPrefs cp = CommonPrefs.get( context ); CommonPrefs cp = CommonPrefs.get( context );
@ -1051,21 +1048,36 @@ public class GameUtils {
} else { } else {
byte[] stream = savedGame( context, lock ); byte[] stream = savedGame( context, lock );
// Will fail if there's nothing in the stream but a gi. // Will fail if there's nothing in the stream but a gi.
gamePtr = XwJNI.initFromStream( lock.getRowid(), stream, try ( GamePtr gamePtr = XwJNI
.initFromStream( lock.getRowid(), stream,
new CurGameInfo(context), new CurGameInfo(context),
dictNames, pairs.m_bytes, dictNames, pairs.m_bytes,
pairs.m_paths, langName, pairs.m_paths, langName,
null, null, cp, null ); null, null, cp, null ) ) {
madeGame = null != gamePtr; if ( null != gamePtr ) {
applyChanges( context, sink, gi, car, disab,
lock, gamePtr );
madeGame = true;
}
}
} }
if ( forceNew || !madeGame ) { if ( forceNew || !madeGame ) {
Assert.assertNull( gamePtr ); try ( GamePtr gamePtr = XwJNI.initNew( gi, dictNames, pairs.m_bytes,
gamePtr = XwJNI.initNew( gi, dictNames, pairs.m_bytes,
pairs.m_paths, langName, util, pairs.m_paths, langName, util,
(DrawCtx)null, cp, sink ); (DrawCtx)null, cp, sink ) ) {
if ( null != gamePtr ) {
applyChanges( context, sink, gi, car, disab, lock, gamePtr );
}
}
}
} }
private static void applyChanges( Context context, MultiMsgSink sink,
CurGameInfo gi, CommsAddrRec car,
Map<CommsConnType, boolean[]> disab,
GameLock lock, GamePtr gamePtr )
{
if ( null != car ) { if ( null != car ) {
XwJNI.comms_setAddr( gamePtr, car ); XwJNI.comms_setAddr( gamePtr, car );
} }
@ -1086,7 +1098,6 @@ public class GameUtils {
GameSummary summary = new GameSummary( gi ); GameSummary summary = new GameSummary( gi );
XwJNI.game_summarize( gamePtr, summary ); XwJNI.game_summarize( gamePtr, summary );
gamePtr.release();
DBUtils.saveSummary( context, lock, summary ); DBUtils.saveSummary( context, lock, summary );
} // applyChanges } // applyChanges
@ -1167,7 +1178,7 @@ public class GameUtils {
} else if ( DeviceRole.SERVER_STANDALONE != summary.serverRole ) { } else if ( DeviceRole.SERVER_STANDALONE != summary.serverRole ) {
int gameID = summary.gameID; int gameID = summary.gameID;
GamePtr gamePtr = loadMakeGame( context, lock ); try ( GamePtr gamePtr = loadMakeGame( context, lock ) ) {
if ( null != gamePtr ) { if ( null != gamePtr ) {
Assert.assertTrue( XwJNI.game_hasComms( gamePtr ) Assert.assertTrue( XwJNI.game_hasComms( gamePtr )
|| !BuildConfig.DEBUG ); || !BuildConfig.DEBUG );
@ -1199,8 +1210,7 @@ public class GameUtils {
if ( summary.inRelayGame() ) { if ( summary.inRelayGame() ) {
tellRelayDied( context, summary, informNow ); tellRelayDied( context, summary, informNow );
} }
}
gamePtr.release();
} }
} }
} }
@ -1275,11 +1285,10 @@ public class GameUtils {
if ( null != lock ) { if ( null != lock ) {
CurGameInfo gi = new CurGameInfo( m_context ); CurGameInfo gi = new CurGameInfo( m_context );
MultiMsgSink sink = new MultiMsgSink( m_context, rowid ); MultiMsgSink sink = new MultiMsgSink( m_context, rowid );
GamePtr gamePtr = loadMakeGame( m_context, gi, sink, lock ); try ( GamePtr gamePtr = loadMakeGame( m_context, gi, sink, lock ) ) {
if ( null != gamePtr ) { if ( null != gamePtr ) {
int nSent = XwJNI.comms_resendAll( gamePtr, true, int nSent = XwJNI.comms_resendAll( gamePtr, true,
m_filter, false ); m_filter, false );
gamePtr.release();
Log.d( TAG, "Resender.doInBackground(): sent %d " Log.d( TAG, "Resender.doInBackground(): sent %d "
+ "messages for rowid %d", nSent, rowid ); + "messages for rowid %d", nSent, rowid );
nSentTotal += sink.numSent(); nSentTotal += sink.numSent();
@ -1287,6 +1296,7 @@ public class GameUtils {
Log.d( TAG, "Resender.doInBackground(): loadMakeGame()" Log.d( TAG, "Resender.doInBackground(): loadMakeGame()"
+ " failed for rowid %d", rowid ); + " failed for rowid %d", rowid );
} }
}
} else { } else {
try ( JNIThread thread = JNIThread.getRetained( rowid ) ) { try ( JNIThread thread = JNIThread.getRetained( rowid ) ) {
if ( null != thread ) { if ( null != thread ) {

View file

@ -808,8 +808,8 @@ public class JNIThread extends Thread implements AutoCloseable {
private void retain_sync() private void retain_sync()
{ {
++m_refCount; ++m_refCount;
Log.i( TAG, "retain_sync(rowid=%d): m_refCount raised to %d", // Log.i( TAG, "retain_sync(rowid=%d): m_refCount raised to %d",
m_rowid, m_refCount ); // m_rowid, m_refCount );
} }
public JNIThread retain() public JNIThread retain()
@ -831,8 +831,8 @@ public class JNIThread extends Thread implements AutoCloseable {
stop = true; stop = true;
} }
} }
Log.i( TAG, "release(rowid=%d): m_refCount dropped to %d", // Log.i( TAG, "release(rowid=%d): m_refCount dropped to %d",
m_rowid, m_refCount ); // m_rowid, m_refCount );
if ( stop ) { if ( stop ) {
waitToStop( true ); waitToStop( true );

View file

@ -34,7 +34,7 @@ import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType;
public class XwJNI { public class XwJNI {
private static final String TAG = XwJNI.class.getSimpleName(); private static final String TAG = XwJNI.class.getSimpleName();
public static class GamePtr { public static class GamePtr implements AutoCloseable {
private int m_ptr = 0; private int m_ptr = 0;
private int m_refCount = 0; private int m_refCount = 0;
private long m_rowid; private long m_rowid;
@ -68,8 +68,8 @@ public class XwJNI {
public synchronized void release() public synchronized void release()
{ {
--m_refCount; --m_refCount;
Log.d( TAG, "release(this=%H, rowid=%d): refCount now %d", // Log.d( TAG, "release(this=%H, rowid=%d): refCount now %d",
this, m_rowid, m_refCount ); // this, m_rowid, m_refCount );
if ( 0 == m_refCount ) { if ( 0 == m_refCount ) {
if ( 0 != m_ptr ) { if ( 0 != m_ptr ) {
if ( !haveEnv( getJNI().m_ptr ) ) { if ( !haveEnv( getJNI().m_ptr ) ) {
@ -81,6 +81,12 @@ public class XwJNI {
} }
} }
@Override
public void close()
{
release();
}
// @Override // @Override
public void finalize() throws java.lang.Throwable public void finalize() throws java.lang.Throwable
{ {