From edca516ecd5ce729f50789769963cd0abe742386 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 22 Nov 2024 09:37:10 -0800 Subject: [PATCH] turn on mutex deadlock checking --- xwords4/android/jni/xwjni.c | 18 +++++++++--------- xwords4/common/xwmutex.c | 4 ++-- xwords4/common/xwmutex.h | 5 ++++- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/xwords4/android/jni/xwjni.c b/xwords4/android/jni/xwjni.c index 7fe6733a8..9e3c0f699 100644 --- a/xwords4/android/jni/xwjni.c +++ b/xwords4/android/jni/xwjni.c @@ -67,7 +67,7 @@ typedef struct _EnvThreadEntry { } EnvThreadEntry; struct _EnvThreadInfo { - pthread_mutex_t mtxThreads; + MutexState mtxThreads; int nEntries; EnvThreadEntry* entries; MPSLOT @@ -160,7 +160,7 @@ map_thread_prv( EnvThreadInfo* ti, JNIEnv* env, const char* caller ) { pthread_t self = pthread_self(); - pthread_mutex_lock( &ti->mtxThreads ); + WITH_MUTEX( &ti->mtxThreads ); XP_Bool found = false; int nEntries = ti->nEntries; @@ -215,13 +215,13 @@ map_thread_prv( EnvThreadInfo* ti, JNIEnv* env, const char* caller ) #endif } - pthread_mutex_unlock( &ti->mtxThreads ); + END_WITH_MUTEX(); } /* map_thread_prv */ static void map_init( MPFORMAL EnvThreadInfo* ti, JNIEnv* env ) { - pthread_mutex_init( &ti->mtxThreads, NULL ); + MUTEX_INIT( &ti->mtxThreads, XP_TRUE ); MPASSIGN( ti->mpool, mpool ); MAP_THREAD( ti, env ); } @@ -232,7 +232,7 @@ map_remove_prv( EnvThreadInfo* ti, JNIEnv* env, const char* caller ) { XP_Bool found = false; - pthread_mutex_lock( &ti->mtxThreads ); + WITH_MUTEX( &ti->mtxThreads ); for ( int ii = 0; !found && ii < ti->nEntries; ++ii ) { EnvThreadEntry* entry = &ti->entries[ii]; found = env == entry->env; @@ -258,7 +258,7 @@ map_remove_prv( EnvThreadInfo* ti, JNIEnv* env, const char* caller ) entry->owner = 0; } } - pthread_mutex_unlock( &ti->mtxThreads ); + END_WITH_MUTEX(); if ( !found ) { RAW_LOG( "ERROR: mapping for env %p not found when called from %s()", @@ -270,7 +270,7 @@ map_remove_prv( EnvThreadInfo* ti, JNIEnv* env, const char* caller ) static void map_destroy( EnvThreadInfo* ti ) { - pthread_mutex_destroy( &ti->mtxThreads ); + MUTEX_DESTROY( &ti->mtxThreads ); } static JNIEnv* @@ -278,14 +278,14 @@ prvEnvForMe( EnvThreadInfo* ti ) { JNIEnv* result = NULL; pthread_t self = pthread_self(); - pthread_mutex_lock( &ti->mtxThreads ); + WITH_MUTEX( &ti->mtxThreads ); for ( int ii = 0; !result && ii < ti->nEntries; ++ii ) { EnvThreadEntry* entry = &ti->entries[ii]; if ( self == entry->owner ) { result = entry->env; } } - pthread_mutex_unlock( &ti->mtxThreads ); + END_WITH_MUTEX(); if ( !result ) { // JNI_VERSION_1_6 works, whether right or not diff --git a/xwords4/common/xwmutex.c b/xwords4/common/xwmutex.c index b328a8964..cffc8b3b0 100644 --- a/xwords4/common/xwmutex.c +++ b/xwords4/common/xwmutex.c @@ -57,7 +57,7 @@ checkListProcLocked( const DLHead* elem, void* closure ) if ( cld->currentTime > ctd->expiryTime ) { XP_LOGFF( "FAIL: %s() on line %d in %s unable to lock mutex", ctd->caller, ctd->lineNo, ctd->file ); - XP_ASSERT(0); + // XP_ASSERT(0); } ++cld->count; return FEA_OK; @@ -160,7 +160,7 @@ mtx_init_prv( MutexState* mutex, XP_Bool recursive } # endif mutex->waitSecs = waitSecs; - XP_LOGFF( "set waitSecs: %d (called by %s())", mutex->waitSecs, caller ); + // XP_LOGFF( "set waitSecs: %d (called by %s())", mutex->waitSecs, caller ); #endif pthread_mutex_init( &mutex->mutex, &attr ); #ifdef DEBUG diff --git a/xwords4/common/xwmutex.h b/xwords4/common/xwmutex.h index 79a0595c1..eb293546f 100644 --- a/xwords4/common/xwmutex.h +++ b/xwords4/common/xwmutex.h @@ -56,7 +56,10 @@ void mtx_crashToTest(); # define MUTEX_INIT_CHECKED(STATE, RECURSIVE, WS) \ mtx_init_prv((STATE), (RECURSIVE), (WS), __func__) -# define MUTEX_INIT(STATE, RECURSIVE) MUTEX_INIT_CHECKED(STATE, RECURSIVE, 0) +# ifndef MUTEX_CHECK_SECS +# define MUTEX_CHECK_SECS 3 +# endif +# define MUTEX_INIT(STATE, RECURSIVE) MUTEX_INIT_CHECKED(STATE, RECURSIVE, MUTEX_CHECK_SECS) #else # define mtx_crashToTest() # define MUTEX_INIT(STATE, RECURSIVE) mtx_init_prv((STATE), (RECURSIVE))