From f90d76329b561f26bc4be84c1a50fcd3f0bfb334 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 30 Sep 2020 17:21:18 -0700 Subject: [PATCH] use semaphore to protect known players data Requires adding a common method called by platform code on creating a new dutil ctxt. --- xwords4/android/jni/Android.mk | 1 + xwords4/android/jni/utilwrapper.c | 3 +-- xwords4/common/config.mk | 2 ++ xwords4/common/dutil.h | 7 ++++++- xwords4/common/knownplyr.c | 4 ++++ xwords4/linux/lindutil.c | 4 +++- 6 files changed, 17 insertions(+), 4 deletions(-) diff --git a/xwords4/android/jni/Android.mk b/xwords4/android/jni/Android.mk index a5ed47bc4..deac3ef65 100644 --- a/xwords4/android/jni/Android.mk +++ b/xwords4/android/jni/Android.mk @@ -86,6 +86,7 @@ COMMON_SRC_FILES += \ $(COMMON_PATH)/dbgutil.c \ $(COMMON_PATH)/nli.c \ $(COMMON_PATH)/smsproto.c \ + $(COMMON_PATH)/dutil.c \ $(COMMON_PATH)/device.c \ $(COMMON_PATH)/knownplyr.c \ diff --git a/xwords4/android/jni/utilwrapper.c b/xwords4/android/jni/utilwrapper.c index c704bb54b..29e13cbb6 100644 --- a/xwords4/android/jni/utilwrapper.c +++ b/xwords4/android/jni/utilwrapper.c @@ -1006,6 +1006,7 @@ makeDUtil( MPFORMAL JNIEnv* env, JNIUtilCtxt* jniutil, void* closure ) { AndDUtil* dutil = (AndDUtil*)XP_CALLOC( mpool, sizeof(*dutil) ); + dutil_super_init( MPPARM(mpool) &dutil->dutil ); #ifdef MAP_THREAD_TO_ENV dutil->ti = ti; #endif @@ -1017,8 +1018,6 @@ makeDUtil( MPFORMAL JNIEnv* env, dutil->jdutil = (*env)->NewGlobalRef( env, jdutil ); } - MPASSIGN( dutil->dutil.mpool, mpool ); - DUtilVtable* vtable = &dutil->dutil.vtable; #define SET_DPROC(nam) vtable->m_dutil_##nam = and_dutil_##nam SET_DPROC(getCurSeconds); diff --git a/xwords4/common/config.mk b/xwords4/common/config.mk index e34e9c41a..2991d5907 100644 --- a/xwords4/common/config.mk +++ b/xwords4/common/config.mk @@ -49,6 +49,7 @@ COMMONSRC = \ $(COMMONDIR)/dictmgr.c \ $(COMMONDIR)/dbgutil.c \ $(COMMONDIR)/smsproto.c \ + $(COMMONDIR)/dutil.c \ $(COMMONDIR)/device.c \ # PENDING: define this in terms of above!!! @@ -88,6 +89,7 @@ COMMON5 = \ $(COMMONOBJDIR)/dictmgr.o \ $(COMMONOBJDIR)/dbgutil.o \ $(COMMONOBJDIR)/smsproto.o \ + $(COMMONOBJDIR)/dutil.o \ $(COMMONOBJDIR)/device.o \ $(COMMONOBJDIR)/knownplyr.o \ diff --git a/xwords4/common/dutil.h b/xwords4/common/dutil.h index 308e502ef..c21351a1a 100644 --- a/xwords4/common/dutil.h +++ b/xwords4/common/dutil.h @@ -84,11 +84,16 @@ struct XW_DUtilCtxt { DUtilVtable vtable; void* closure; void* devCtxt; /* owned by device.c */ - void* kpCtxt; /* owned by knownplyr.c */ +#ifdef XWFEATURE_KNOWNPLAYERS /* owned by knownplyr.c */ + void* kpCtxt; + pthread_mutex_t kpMutex; +#endif VTableMgr* vtMgr; MPSLOT }; +void dutil_super_init( MPFORMAL XW_DUtilCtxt* dutil ); + /* This one cheats: direct access */ #define dutil_getVTManager(duc) (duc)->vtMgr diff --git a/xwords4/common/knownplyr.c b/xwords4/common/knownplyr.c index 536588b75..e299428e5 100644 --- a/xwords4/common/knownplyr.c +++ b/xwords4/common/knownplyr.c @@ -64,6 +64,8 @@ static KPState* loadState( XW_DUtilCtxt* dutil, XWEnv xwe ) { LOG_FUNC(); + pthread_mutex_lock( &dutil->kpMutex ); + KPState* state = (KPState*)dutil->kpCtxt; if ( NULL == state ) { dutil->kpCtxt = state = XP_CALLOC( dutil->mpool, sizeof(*state) ); @@ -107,6 +109,8 @@ releaseState( XW_DUtilCtxt* dutil, XWEnv xwe, KPState* state ) XP_ASSERT( state->inUse ); saveState( dutil, xwe, state ); state->inUse = XP_FALSE; + + pthread_mutex_unlock( &dutil->kpMutex ); } static const XP_UCHAR* diff --git a/xwords4/linux/lindutil.c b/xwords4/linux/lindutil.c index b2876ae6c..125b112a0 100644 --- a/xwords4/linux/lindutil.c +++ b/xwords4/linux/lindutil.c @@ -133,6 +133,9 @@ XW_DUtilCtxt* dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure ) { XW_DUtilCtxt* result = XP_CALLOC( mpool, sizeof(*result) ); + + dutil_super_init( MPPARM(mpool) result ); + result->vtMgr = vtMgr; result->closure = closure; @@ -170,7 +173,6 @@ dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure ) assertTableFull( &result->vtable, sizeof(result->vtable), "lindutil" ); - MPASSIGN( result->mpool, mpool ); return result; }