From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: david.edmondson@oracle.com, kwolf@redhat.com, qemu-block@nongnu.org
Subject: [PATCH 5/6] test-coroutine: add rwlock upgrade test
Date: Wed, 17 Mar 2021 19:00:12 +0100 [thread overview]
Message-ID: <20210317180013.235231-6-pbonzini@redhat.com> (raw)
In-Reply-To: <20210317180013.235231-1-pbonzini@redhat.com>
Test that rwlock upgrade is fair, and readers go back to sleep if a writer
is in line.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
tests/unit/test-coroutine.c | 62 +++++++++++++++++++++++++++++++++++++
1 file changed, 62 insertions(+)
diff --git a/tests/unit/test-coroutine.c b/tests/unit/test-coroutine.c
index e946d93a65..6e6f51d480 100644
--- a/tests/unit/test-coroutine.c
+++ b/tests/unit/test-coroutine.c
@@ -264,6 +264,67 @@ static void test_co_mutex_lockable(void)
g_assert(QEMU_MAKE_LOCKABLE(null_pointer) == NULL);
}
+static CoRwlock rwlock;
+
+/* Test that readers are properly sent back to the queue when upgrading,
+ * even if they are the sole readers. The test scenario is as follows:
+ *
+ *
+ * | c1 | c2 |
+ * |--------------+------------+
+ * | rdlock | |
+ * | yield | |
+ * | | wrlock |
+ * | | <queued> |
+ * | upgrade | |
+ * | <queued> | <dequeued> |
+ * | | unlock |
+ * | <dequeued> | |
+ * | unlock | |
+ */
+
+static void coroutine_fn rwlock_yield_upgrade(void *opaque)
+{
+ qemu_co_rwlock_rdlock(&rwlock);
+ qemu_coroutine_yield();
+
+ qemu_co_rwlock_upgrade(&rwlock);
+ qemu_co_rwlock_unlock(&rwlock);
+
+ *(bool *)opaque = true;
+}
+
+static void coroutine_fn rwlock_wrlock_yield(void *opaque)
+{
+ qemu_co_rwlock_wrlock(&rwlock);
+ qemu_coroutine_yield();
+
+ qemu_co_rwlock_unlock(&rwlock);
+ *(bool *)opaque = true;
+}
+
+static void test_co_rwlock_upgrade(void)
+{
+ bool c1_done = false;
+ bool c2_done = false;
+ Coroutine *c1, *c2;
+
+ qemu_co_rwlock_init(&rwlock);
+ c1 = qemu_coroutine_create(rwlock_yield_upgrade, &c1_done);
+ c2 = qemu_coroutine_create(rwlock_wrlock_yield, &c2_done);
+
+ qemu_coroutine_enter(c1);
+ qemu_coroutine_enter(c2);
+
+ /* c1 now should go to sleep. */
+ qemu_coroutine_enter(c1);
+ g_assert(!c1_done);
+
+ qemu_coroutine_enter(c2);
+ g_assert(c1_done);
+ g_assert(c2_done);
+}
+
/*
* Check that creation, enter, and return work
*/
@@ -501,6 +562,7 @@ int main(int argc, char **argv)
g_test_add_func("/basic/order", test_order);
g_test_add_func("/locking/co-mutex", test_co_mutex);
g_test_add_func("/locking/co-mutex/lockable", test_co_mutex_lockable);
+ g_test_add_func("/locking/co-rwlock/upgrade", test_co_rwlock_upgrade);
if (g_test_perf()) {
g_test_add_func("/perf/lifecycle", perf_lifecycle);
g_test_add_func("/perf/nesting", perf_nesting);
--
2.29.2
next prev parent reply other threads:[~2021-03-17 18:03 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-17 18:00 [PATCH v5 0/6] coroutine rwlock downgrade fix, minor VDI changes Paolo Bonzini
2021-03-17 18:00 ` [PATCH 1/6] block/vdi: When writing new bmap entry fails, don't leak the buffer Paolo Bonzini
2021-03-24 14:25 ` Max Reitz
2021-03-17 18:00 ` [PATCH 2/6] block/vdi: Don't assume that blocks are larger than VdiHeader Paolo Bonzini
2021-03-24 14:25 ` Max Reitz
2021-03-17 18:00 ` [PATCH 3/6] coroutine/mutex: Store the coroutine in the CoWaitRecord only once Paolo Bonzini
2021-03-17 18:00 ` [PATCH 4/6] coroutine-lock: reimplement CoRwlock to fix downgrade bug Paolo Bonzini
2021-03-24 16:15 ` Stefan Hajnoczi
2021-03-24 16:40 ` Paolo Bonzini
2021-03-17 18:00 ` Paolo Bonzini [this message]
2021-03-17 18:19 ` [PATCH 5/6] test-coroutine: add rwlock upgrade test David Edmondson
2021-03-17 18:00 ` [PATCH 6/6] test-coroutine: Add rwlock downgrade test Paolo Bonzini
2021-03-24 14:26 ` [PATCH v5 0/6] coroutine rwlock downgrade fix, minor VDI changes Max Reitz
2021-03-24 16:23 ` Stefan Hajnoczi
2021-03-24 16:43 ` Paolo Bonzini
-- strict thread matches above, loose matches on Subject: below --
2021-03-17 12:16 [PATCH v4 " Paolo Bonzini
2021-03-17 12:16 ` [PATCH 5/6] test-coroutine: add rwlock upgrade test Paolo Bonzini
2021-03-17 15:19 ` David Edmondson
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=20210317180013.235231-6-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=david.edmondson@oracle.com \
--cc=kwolf@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/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).