]> Git Repo - qemu.git/commitdiff
coroutine: Add qemu_co_mutex_assert_locked()
authorKevin Wolf <[email protected]>
Thu, 24 Oct 2019 14:26:57 +0000 (16:26 +0200)
committerKevin Wolf <[email protected]>
Fri, 25 Oct 2019 13:18:38 +0000 (15:18 +0200)
Some functions require that the caller holds a certain CoMutex for them
to operate correctly. Add a function so that they can assert the lock is
really held.

Cc: [email protected]
Signed-off-by: Kevin Wolf <[email protected]>
Tested-by: Michael Weiser <[email protected]>
Reviewed-by: Michael Weiser <[email protected]>
Reviewed-by: Vladimir Sementsov-Ogievskiy <[email protected]>
Reviewed-by: Denis V. Lunev <[email protected]>
Reviewed-by: Max Reitz <[email protected]>
include/qemu/coroutine.h

index 8d55663062ad05c7775bd844e72ae560768b3f85..dfd261c5b1f14683154ce8b0e5b937b320feaa49 100644 (file)
@@ -167,6 +167,21 @@ void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex);
  */
 void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex);
 
+/**
+ * Assert that the current coroutine holds @mutex.
+ */
+static inline coroutine_fn void qemu_co_mutex_assert_locked(CoMutex *mutex)
+{
+    /*
+     * mutex->holder doesn't need any synchronisation if the assertion holds
+     * true because the mutex protects it. If it doesn't hold true, we still
+     * don't mind if another thread takes or releases mutex behind our back,
+     * because the condition will be false no matter whether we read NULL or
+     * the pointer for any other coroutine.
+     */
+    assert(atomic_read(&mutex->locked) &&
+           mutex->holder == qemu_coroutine_self());
+}
 
 /**
  * CoQueues are a mechanism to queue coroutines in order to continue executing
This page took 0.02538 seconds and 4 git commands to generate.