From 0a992c5a4b7613ee6785714f3bec6ba05fb8927b Mon Sep 17 00:00:00 2001 From: Eric House Date: Sun, 11 Aug 2024 19:59:38 -0700 Subject: [PATCH] move mutex into a struct (for future dev advantage) --- xwords4/common/comms.c | 2 +- xwords4/common/stats.c | 2 +- xwords4/common/xwmutex.c | 4 ++-- xwords4/common/xwmutex.h | 27 ++++++++++++++++----------- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/xwords4/common/comms.c b/xwords4/common/comms.c index b822d845a..aaa118631 100644 --- a/xwords4/common/comms.c +++ b/xwords4/common/comms.c @@ -116,8 +116,8 @@ typedef struct AddressRecord { struct CommsCtxt { XW_UtilCtxt* util; XW_DUtilCtxt* dutil; + MutexState mutex; - pthread_mutex_t mutex; #ifdef DEBUG pthread_t lockHolder; #endif diff --git a/xwords4/common/stats.c b/xwords4/common/stats.c index 85b1b705f..d063f5bf0 100644 --- a/xwords4/common/stats.c +++ b/xwords4/common/stats.c @@ -25,7 +25,7 @@ typedef struct StatsState { XP_U32* statsVals; - pthread_mutex_t mutex; + MutexState mutex; } StatsState; static const XP_UCHAR* STATtoStr(STAT stat); diff --git a/xwords4/common/xwmutex.c b/xwords4/common/xwmutex.c index d04fda19a..e1c95120b 100644 --- a/xwords4/common/xwmutex.c +++ b/xwords4/common/xwmutex.c @@ -20,7 +20,7 @@ #include "xwmutex.h" void -initMutex( pthread_mutex_t* mutex, XP_Bool recursive ) +initMutex( MutexState* mutex, XP_Bool recursive ) { pthread_mutexattr_t attr; int ret = pthread_mutexattr_init(&attr); @@ -30,7 +30,7 @@ initMutex( pthread_mutex_t* mutex, XP_Bool recursive ) PTHREAD_MUTEX_RECURSIVE); XP_ASSERT(0 == ret); } - pthread_mutex_init( mutex, &attr ); + pthread_mutex_init( &mutex->mutex, &attr ); ret = pthread_mutexattr_destroy(&attr); XP_ASSERT(0 == ret); } diff --git a/xwords4/common/xwmutex.h b/xwords4/common/xwmutex.h index 0d8c91ff9..fcb6802f9 100644 --- a/xwords4/common/xwmutex.h +++ b/xwords4/common/xwmutex.h @@ -23,15 +23,20 @@ #include #include "xptypes.h" -#define WITH_MUTEX_LOCK_DEBUG(MUTEX) { \ - pthread_mutex_t* _mutex = (MUTEX); \ - time_t startTime = time(NULL); \ - pthread_mutex_lock(_mutex); \ - time_t gotItTime = time(NULL); \ - time_t _elapsed = gotItTime-startTime; \ - if ( 0 < _elapsed ) { \ - XP_LOGFF("took %lds to get mutex", _elapsed); \ - } \ +/* Making this a struct in case I want to add e.g. a chain of holders */ +typedef struct _MutexState { + pthread_mutex_t mutex; +} MutexState; + +#define WITH_MUTEX_LOCK_DEBUG(STATEP) { \ + MutexState* _state = (STATEP); \ + time_t startTime = time(NULL); \ + pthread_mutex_lock(&_state->mutex); \ + time_t gotItTime = time(NULL); \ + time_t _elapsed = gotItTime-startTime; \ + if ( 0 < _elapsed ) { \ + XP_LOGFF("took %lds to get mutex", _elapsed); \ + } \ #define WITH_MUTEX_UNLOCK_DEBUG() \ time_t unlockTime = time(NULL); \ @@ -39,7 +44,7 @@ if ( 0 < _elapsed ) { \ XP_LOGFF("held mutex for %lds", _elapsed); \ } \ - pthread_mutex_unlock(_mutex); \ + pthread_mutex_unlock(&_state->mutex); \ } \ #define WITH_MUTEX_LOCK_RELEASE(COMMS) { \ @@ -58,6 +63,6 @@ #define END_WITH_MUTEX WITH_MUTEX_UNLOCK_RELEASE #endif -void initMutex( pthread_mutex_t* mutex, XP_Bool recursive ); +void initMutex( MutexState* mutex, XP_Bool recursive ); #endif