qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Fam Zheng <famz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>
Subject: [Qemu-devel] [PULL 02/16] test-coroutine: add simple CoMutex test
Date: Thu,  8 Feb 2018 10:19:39 +0800	[thread overview]
Message-ID: <20180208021953.7354-3-famz@redhat.com> (raw)
In-Reply-To: <20180208021953.7354-1-famz@redhat.com>

From: Paolo Bonzini <pbonzini@redhat.com>

In preparation for adding a similar test using QemuLockable, add a very
simple testcase that has two interleaved calls to lock and unlock.

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20180203153935.8056-2-pbonzini@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Fam Zheng <famz@redhat.com>
---
 tests/test-coroutine.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/tests/test-coroutine.c b/tests/test-coroutine.c
index 76c646107e..ab8fdf701e 100644
--- a/tests/test-coroutine.c
+++ b/tests/test-coroutine.c
@@ -175,7 +175,7 @@ static void coroutine_fn c1_fn(void *opaque)
     qemu_coroutine_enter(c2);
 }
 
-static void test_co_queue(void)
+static void test_no_dangling_access(void)
 {
     Coroutine *c1;
     Coroutine *c2;
@@ -195,6 +195,51 @@ static void test_co_queue(void)
     *c1 = tmp;
 }
 
+static bool locked;
+static int done;
+
+static void coroutine_fn mutex_fn(void *opaque)
+{
+    CoMutex *m = opaque;
+    qemu_co_mutex_lock(m);
+    assert(!locked);
+    locked = true;
+    qemu_coroutine_yield();
+    locked = false;
+    qemu_co_mutex_unlock(m);
+    done++;
+}
+
+static void do_test_co_mutex(CoroutineEntry *entry, void *opaque)
+{
+    Coroutine *c1 = qemu_coroutine_create(entry, opaque);
+    Coroutine *c2 = qemu_coroutine_create(entry, opaque);
+
+    done = 0;
+    qemu_coroutine_enter(c1);
+    g_assert(locked);
+    qemu_coroutine_enter(c2);
+
+    /* Unlock queues c2.  It is then started automatically when c1 yields or
+     * terminates.
+     */
+    qemu_coroutine_enter(c1);
+    g_assert_cmpint(done, ==, 1);
+    g_assert(locked);
+
+    qemu_coroutine_enter(c2);
+    g_assert_cmpint(done, ==, 2);
+    g_assert(!locked);
+}
+
+static void test_co_mutex(void)
+{
+    CoMutex m;
+
+    qemu_co_mutex_init(&m);
+    do_test_co_mutex(mutex_fn, &m);
+}
+
 /*
  * Check that creation, enter, and return work
  */
@@ -422,7 +467,7 @@ int main(int argc, char **argv)
      * crash, so skip it.
      */
     if (CONFIG_COROUTINE_POOL) {
-        g_test_add_func("/basic/co_queue", test_co_queue);
+        g_test_add_func("/basic/no-dangling-access", test_no_dangling_access);
     }
 
     g_test_add_func("/basic/lifecycle", test_lifecycle);
@@ -432,6 +477,7 @@ int main(int argc, char **argv)
     g_test_add_func("/basic/entered", test_entered);
     g_test_add_func("/basic/in_coroutine", test_in_coroutine);
     g_test_add_func("/basic/order", test_order);
+    g_test_add_func("/locking/co-mutex", test_co_mutex);
     if (g_test_perf()) {
         g_test_add_func("/perf/lifecycle", perf_lifecycle);
         g_test_add_func("/perf/nesting", perf_nesting);
-- 
2.14.3

  parent reply	other threads:[~2018-02-08  2:20 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-08  2:19 [Qemu-devel] [PULL 00/16] Docker and block patches Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 01/16] docker: change Fedora base image to fedora:27 Fam Zheng
2018-02-08  2:19 ` Fam Zheng [this message]
2018-02-08  2:19 ` [Qemu-devel] [PULL 03/16] lockable: add QemuLockable Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 04/16] coroutine-lock: convert CoQueue to use QemuLockable Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 05/16] coroutine-lock: make qemu_co_enter_next thread-safe Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 06/16] curl: convert to CoQueue Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 07/16] stubs: Add stubs for ram block API Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 08/16] util: Introduce vfio helpers Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 09/16] block: Add VFIO based NVMe driver Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 10/16] block: Introduce buf register API Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 11/16] block/nvme: Implement .bdrv_(un)register_buf Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 12/16] qemu-img: Map bench buffer Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 13/16] block: Move NVMe constants to a separate header Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 14/16] docs: Add section for NVMe VFIO driver Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 15/16] qapi: Add NVMe driver options to the schema Fam Zheng
2018-02-08  2:19 ` [Qemu-devel] [PULL 16/16] docs: Add docs/devel/testing.rst Fam Zheng
2018-02-08 16:17 ` [Qemu-devel] [PULL 00/16] Docker and block patches Peter Maydell

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=20180208021953.7354-3-famz@redhat.com \
    --to=famz@redhat.com \
    --cc=peter.maydell@linaro.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).