os: add pipe_mutex_assert_locked()
authorRob Clark <robdclark@gmail.com>
Thu, 21 Jul 2016 17:51:36 +0000 (13:51 -0400)
committerRob Clark <robdclark@gmail.com>
Sat, 30 Jul 2016 13:23:42 +0000 (09:23 -0400)
Would be nice if we could also have lockdep, like in the linux kernel.
But this is better than nothing.

Signed-off-by: Rob Clark <robdclark@gmail.com>
src/gallium/auxiliary/os/os_thread.h

index be8adcc6cf2929a1d7e5f37d02ed0416058512f2..ec8adbc75bb38657fd4fc73ffbea39ff403be81c 100644 (file)
@@ -116,6 +116,22 @@ typedef mtx_t pipe_mutex;
 #define pipe_mutex_unlock(mutex) \
    (void) mtx_unlock(&(mutex))
 
+#define pipe_mutex_assert_locked(mutex) \
+   __pipe_mutex_assert_locked(&(mutex))
+
+static inline void
+__pipe_mutex_assert_locked(pipe_mutex *mutex)
+{
+#ifdef DEBUG
+   /* NOTE: this would not work for recursive mutexes, but
+    * pipe_mutex doesn't support those
+    */
+   int ret = mtx_trylock(mutex);
+   assert(ret == thrd_busy);
+   if (ret == thrd_success)
+      mtx_unlock(mutex);
+#endif
+}
 
 /* pipe_condvar
  */