summary refs log tree commit diff stats
path: root/include/qemu/coroutine.h
diff options
context:
space:
mode:
authorKevin Wolf <kwolf@redhat.com>2019-10-24 16:26:57 +0200
committerKevin Wolf <kwolf@redhat.com>2019-10-25 15:18:38 +0200
commit944f3d5dd216fcd8cb007eddd4f82dced0a15b3d (patch)
treed8befdc71c3b232d96476c60dd9ec2468dbc2b9d /include/qemu/coroutine.h
parentc9b749d7bcaf035de4dc32cd6225a2ff51200d7b (diff)
downloadfocaccia-qemu-944f3d5dd216fcd8cb007eddd4f82dced0a15b3d.tar.gz
focaccia-qemu-944f3d5dd216fcd8cb007eddd4f82dced0a15b3d.zip
coroutine: Add qemu_co_mutex_assert_locked()
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: qemu-stable@nongnu.org
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Tested-by: Michael Weiser <michael.weiser@gmx.de>
Reviewed-by: Michael Weiser <michael.weiser@gmx.de>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Denis V. Lunev <den@openvz.org>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Diffstat (limited to 'include/qemu/coroutine.h')
-rw-r--r--include/qemu/coroutine.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
index 8d55663062..dfd261c5b1 100644
--- a/include/qemu/coroutine.h
+++ b/include/qemu/coroutine.h
@@ -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