util/simple_mtx: add assert_locked()
authorRob Clark <robdclark@chromium.org>
Tue, 28 Apr 2020 19:39:32 +0000 (12:39 -0700)
committerMarge Bot <eric+marge@anholt.net>
Wed, 29 Apr 2020 20:37:00 +0000 (20:37 +0000)
Signed-off-by: Rob Clark <robdclark@chromium.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4810>

src/util/simple_mtx.h

index 4fc8a0d4df49edb659dc7ae3457a351f61cd6ef8..ec3889497f3afbf703a0114aaa462a9ebd5e1901 100644 (file)
@@ -113,6 +113,12 @@ simple_mtx_unlock(simple_mtx_t *mtx)
    }
 }
 
+static inline void
+simple_mtx_assert_locked(simple_mtx_t *mtx)
+{
+   assert(mtx->val);
+}
+
 #else
 
 typedef mtx_t simple_mtx_t;
@@ -143,6 +149,22 @@ simple_mtx_unlock(simple_mtx_t *mtx)
    mtx_unlock(mtx);
 }
 
+static inline void
+simple_mtx_assert_locked(simple_mtx_t *mtx)
+{
+#ifdef DEBUG
+   /* NOTE: this would not work for recursive mutexes, but
+    * mtx_t doesn't support those
+    */
+   int ret = mtx_trylock(mtx);
+   assert(ret == thrd_busy);
+   if (ret == thrd_success)
+      mtx_unlock(mtx);
+#else
+   (void)mtx;
+#endif
+}
+
 #endif
 
 #endif