Skip to content

Display and control your Android device

Expose mutex assertions

Romain Vimont21d206f36053
diff --git a/app/src/util/thread.c b/app/src/util/thread.c
index 1fc6bb19..fa774cfe 100644
--- a/app/src/util/thread.c
+++ b/app/src/util/thread.c
@@ -30,6 +30,9 @@ sc_mutex_init(sc_mutex *mutex) {
}
mutex->mutex = sdl_mutex;
+#ifndef NDEBUG
+ mutex->locker = 0;
+#endif
return true;
}
@@ -46,6 +49,8 @@ sc_mutex_lock(sc_mutex *mutex) {
LOGC("Could not lock mutex: %s", SDL_GetError());
abort();
}
+
+ mutex->locker = sc_thread_get_id();
#else
(void) r;
#endif
@@ -53,6 +58,9 @@ sc_mutex_lock(sc_mutex *mutex) {
void
sc_mutex_unlock(sc_mutex *mutex) {
+#ifndef NDEBUG
+ mutex->locker = 0;
+#endif
int r = SDL_UnlockMutex(mutex->mutex);
#ifndef NDEBUG
if (r) {
@@ -69,6 +77,13 @@ sc_thread_get_id(void) {
return SDL_ThreadID();
}
+#ifndef NDEBUG
+bool
+sc_mutex_held(struct sc_mutex *mutex) {
+ return mutex->locker == sc_thread_get_id();
+}
+#endif
+
bool
sc_cond_init(sc_cond *cond) {
SDL_cond *sdl_cond = SDL_CreateCond();
@@ -93,6 +108,8 @@ sc_cond_wait(sc_cond *cond, sc_mutex *mutex) {
LOGC("Could not wait on condition: %s", SDL_GetError());
abort();
}
+
+ mutex->locker = sc_thread_get_id();
#else
(void) r;
#endif
@@ -106,6 +123,8 @@ sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, uint32_t ms) {
LOGC("Could not wait on condition with timeout: %s", SDL_GetError());
abort();
}
+
+ mutex->locker = sc_thread_get_id();
#endif
assert(r == 0 || r == SDL_MUTEX_TIMEDOUT);
return r == 0;
diff --git a/app/src/util/thread.h b/app/src/util/thread.h
index 85a2aca0..d23e1432 100644
--- a/app/src/util/thread.h
+++ b/app/src/util/thread.h
@@ -20,6 +20,9 @@ typedef struct sc_thread {
typedef struct sc_mutex {
SDL_mutex *mutex;
+#ifndef NDEBUG
+ sc_thread_id locker;
+#endif
} sc_mutex;
typedef struct sc_cond {
@@ -48,6 +51,14 @@ sc_mutex_unlock(sc_mutex *mutex);
sc_thread_id
sc_thread_get_id(void);
+#ifndef NDEBUG
+bool
+sc_mutex_held(struct sc_mutex *mutex);
+# define sc_mutex_assert(mutex) assert(sc_mutex_held(mutex))
+#else
+# define sc_mutex_assert(mutex)
+#endif
+
bool
sc_cond_init(sc_cond *cond);