From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, psyhomb@gmail.com, michael@weiser.dinsnail.net,
vsementsov@virtuozzo.com, den@virtuozzo.com,
qemu-devel@nongnu.org, qemu-stable@nongnu.org,
dgilbert@redhat.com, mreitz@redhat.com, lersek@redhat.com
Subject: [PATCH v2 1/2] coroutine: Add qemu_co_mutex_assert_locked()
Date: Thu, 24 Oct 2019 16:26:57 +0200 [thread overview]
Message-ID: <20191024142658.22306-2-kwolf@redhat.com> (raw)
In-Reply-To: <20191024142658.22306-1-kwolf@redhat.com>
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>
---
include/qemu/coroutine.h | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
index 9801e7f5a4..f4843b5f59 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
--
2.20.1
next prev parent reply other threads:[~2019-10-24 16:10 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-24 14:26 [PATCH v2 0/2] qcow2: Fix image corruption bug in 4.1 Kevin Wolf
2019-10-24 14:26 ` Kevin Wolf [this message]
2019-10-24 15:35 ` [PATCH v2 1/2] coroutine: Add qemu_co_mutex_assert_locked() Vladimir Sementsov-Ogievskiy
2019-10-24 15:50 ` Denis Lunev
2019-10-24 14:26 ` [PATCH v2 2/2] qcow2: Fix corruption bug in qcow2_detect_metadata_preallocation() Kevin Wolf
2019-10-25 10:44 ` [PATCH v2 0/2] qcow2: Fix image corruption bug in 4.1 Max Reitz
2019-10-25 14:42 ` Michael Weiser
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191024142658.22306-2-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=den@virtuozzo.com \
--cc=dgilbert@redhat.com \
--cc=lersek@redhat.com \
--cc=michael@weiser.dinsnail.net \
--cc=mreitz@redhat.com \
--cc=psyhomb@gmail.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-stable@nongnu.org \
--cc=vsementsov@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).