Commit 837bd79b authored by Max Kellermann's avatar Max Kellermann

db_lock: add assertions

parent 3edd4a24
...@@ -21,3 +21,7 @@ ...@@ -21,3 +21,7 @@
#include "db_lock.h" #include "db_lock.h"
GStaticMutex db_mutex = G_STATIC_MUTEX_INIT; GStaticMutex db_mutex = G_STATIC_MUTEX_INIT;
#ifndef NDEBUG
GThread *db_mutex_holder;
#endif
...@@ -30,9 +30,26 @@ ...@@ -30,9 +30,26 @@
#include <glib.h> #include <glib.h>
#include <assert.h> #include <assert.h>
#include <stdbool.h>
extern GStaticMutex db_mutex; extern GStaticMutex db_mutex;
#ifndef NDEBUG
extern GThread *db_mutex_holder;
/**
* Does the current thread hold the database lock?
*/
G_GNUC_PURE
static inline bool
holding_db_lock(void)
{
return db_mutex_holder == g_thread_self();
}
#endif
/** /**
* Obtain the global database lock. This is needed before * Obtain the global database lock. This is needed before
* dereferencing a #song or #directory. It is not recursive. * dereferencing a #song or #directory. It is not recursive.
...@@ -40,7 +57,14 @@ extern GStaticMutex db_mutex; ...@@ -40,7 +57,14 @@ extern GStaticMutex db_mutex;
static inline void static inline void
db_lock(void) db_lock(void)
{ {
assert(!holding_db_lock());
g_static_mutex_lock(&db_mutex); g_static_mutex_lock(&db_mutex);
assert(db_mutex_holder == NULL);
#ifndef NDEBUG
db_mutex_holder = g_thread_self();
#endif
} }
/** /**
...@@ -49,6 +73,11 @@ db_lock(void) ...@@ -49,6 +73,11 @@ db_lock(void)
static inline void static inline void
db_unlock(void) db_unlock(void)
{ {
assert(holding_db_lock());
#ifndef NDEBUG
db_mutex_holder = NULL;
#endif
g_static_mutex_unlock(&db_mutex); g_static_mutex_unlock(&db_mutex);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment