make GamePtr AutoCloseable and use where possible

This commit is contained in:
Eric House 2019-02-12 06:13:36 -08:00
parent c706205cd1
commit dc1431fd08
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,
makeJNIHandler() );
if ( success ) {
m_jniGamePtr = m_jniThreadRef.getGamePtr();
m_jniGamePtr = m_jniThreadRef.getGamePtr(); // .retain()?
Assert.assertNotNull( m_jniGamePtr );
}
}
@ -2785,37 +2785,44 @@ public class BoardDelegate extends DelegateBase
public static void setupRematchFor( Activity activity, long rowID )
{
GamePtr gamePtr = null;
GameSummary summary = null;
CurGameInfo gi = null;
try ( JNIThread thread = JNIThread.getRetained( rowID ) ) {
if ( null != thread ) {
gamePtr = thread.getGamePtr().retain();
try ( GamePtr gamePtr = thread.getGamePtr().retain() ) {
summary = thread.getSummary();
gi = thread.getGI();
setupRematchFor( activity, gamePtr, summary, gi );
}
} else {
try ( GameLock lock = GameLock.tryLockRO( rowID ) ) {
if ( null != lock ) {
summary = DBUtils.getSummary( activity, lock );
gi = new CurGameInfo( activity );
gamePtr = GameUtils.loadMakeGame( activity, gi, lock );
try ( GamePtr gamePtr = GameUtils
.loadMakeGame( activity, gi, lock ) ) {
setupRematchFor( activity, gamePtr, summary, gi );
}
} else {
DbgUtils.toastNoLock( TAG, activity, rowID,
"setupRematchFor(%d)", rowID );
}
}
}
}
}
private static void setupRematchFor( Activity activity, GamePtr gamePtr,
GameSummary summary, CurGameInfo gi )
{
if ( null != gamePtr ) {
doRematchIf( activity, null, rowID, DBUtils.GROUPID_UNSPEC,
summary, gi, gamePtr );
gamePtr.release();
doRematchIf( activity, null, gamePtr.getRowid(),
DBUtils.GROUPID_UNSPEC, summary, gi, gamePtr );
} else {
Log.w( TAG, "setupRematchFor(): unable to lock game" );
}
}
}
// Return true if anything sent
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 );
m_parent = parent;
m_jniThread = thread;
m_jniGamePtr = thread.getGamePtr();
m_jniGamePtr = thread.getGamePtr(); // .retain()?
m_gi = thread.getGI();
m_isSolo = CurGameInfo.DeviceRole.SERVER_STANDALONE == m_gi.serverRole;
m_connTypes = connTypes;

View file

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

View file

@ -536,18 +536,27 @@ public class GameConfigDelegate extends DelegateBase
if ( null == m_giOrig ) {
m_giOrig = new CurGameInfo( m_activity );
XwJNI.GamePtr gamePtr = null;
if ( null != m_jniThread ) {
gamePtr = m_jniThread.getGamePtr().retain();
try ( XwJNI.GamePtr gamePtr = m_jniThread
.getGamePtr().retain() ) {
loadGame( gamePtr );
}
} else {
try ( GameLock lock = GameLock.tryLockRO( m_rowid ) ) {
if ( null != lock ) {
gamePtr = GameUtils.loadMakeGame( m_activity, m_giOrig,
lock );
try ( XwJNI.GamePtr gamePtr = GameUtils.
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 ) {
Assert.assertFalse( BuildConfig.DEBUG );
} else {
@ -587,8 +596,6 @@ public class GameConfigDelegate extends DelegateBase
buildDisabledsMap( gamePtr );
setDisableds();
gamePtr.release();
m_car = new CommsAddrRec( m_carOrig );
setTitle();
@ -631,7 +638,6 @@ public class GameConfigDelegate extends DelegateBase
setBoardsizeSpinner();
}
}
} // loadGame
private void showTimerSet( boolean show )

View file

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

View file

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

View file

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