From 15a206b7fcc9ecab2a1d0ffa782bb3d4c845a25a Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 27 Nov 2019 21:32:52 +0100 Subject: [PATCH] Assert return value of mutex functions Mutex functions may only fail due to a programming error. Use assertions in debug builds, and ignore the value in release builds. --- app/src/util/lock.h | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/app/src/util/lock.h b/app/src/util/lock.h index 8ebee241..cb7c318c 100644 --- a/app/src/util/lock.h +++ b/app/src/util/lock.h @@ -9,44 +9,66 @@ static inline void mutex_lock(SDL_mutex *mutex) { - if (SDL_LockMutex(mutex)) { - LOGC("Could not lock mutex"); + int r = SDL_LockMutex(mutex); +#ifndef NDEBUG + if (r) { + LOGC("Could not lock mutex: %s", SDL_GetError()); abort(); } +#else + (void) r; +#endif } static inline void mutex_unlock(SDL_mutex *mutex) { - if (SDL_UnlockMutex(mutex)) { - LOGC("Could not unlock mutex"); + int r = SDL_UnlockMutex(mutex); +#ifndef NDEBUG + if (r) { + LOGC("Could not unlock mutex: %s", SDL_GetError()); abort(); } +#else + (void) r; +#endif } static inline void cond_wait(SDL_cond *cond, SDL_mutex *mutex) { - if (SDL_CondWait(cond, mutex)) { - LOGC("Could not wait on condition"); + int r = SDL_CondWait(cond, mutex); +#ifndef NDEBUG + if (r) { + LOGC("Could not wait on condition: %s", SDL_GetError()); abort(); } +#else + (void) r; +#endif } static inline int cond_wait_timeout(SDL_cond *cond, SDL_mutex *mutex, uint32_t ms) { int r = SDL_CondWaitTimeout(cond, mutex, ms); +#ifndef NDEBUG if (r < 0) { - LOGC("Could not wait on condition with timeout"); + LOGC("Could not wait on condition with timeout: %s", SDL_GetError()); abort(); } +#endif return r; } static inline void cond_signal(SDL_cond *cond) { - if (SDL_CondSignal(cond)) { - LOGC("Could not signal a condition"); + int r = SDL_CondSignal(cond); +#ifndef NDEBUG + if (r) { + LOGC("Could not signal a condition: %s", SDL_GetError()); abort(); } +#else + (void) r; +#endif } #endif