From patchwork Thu Feb 1 21:29:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 10196043 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A9FD360600 for ; Thu, 1 Feb 2018 21:50:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A057D28604 for ; Thu, 1 Feb 2018 21:50:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8DB9E28CED; Thu, 1 Feb 2018 21:50:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5073328935 for ; Thu, 1 Feb 2018 21:33:14 +0000 (UTC) Received: from localhost ([::1]:36533 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMTF-00081M-2X for patchwork-qemu-devel@patchwork.kernel.org; Thu, 01 Feb 2018 16:32:37 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59583) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMR9-00078X-Nl for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:31:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehMQ5-0003kx-Ed for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:30:27 -0500 Received: from mail-qt0-x242.google.com ([2607:f8b0:400d:c0d::242]:36433) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ehMQ5-0003kX-1p; Thu, 01 Feb 2018 16:29:21 -0500 Received: by mail-qt0-x242.google.com with SMTP id z11so28394350qtm.3; Thu, 01 Feb 2018 13:29:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=94sjX6i+WpYuId2an5RqK0nB9ez56AuhnFrDx4gI5LI=; b=Gqju6DywSNLLdSsgT5iPdTHYI1hK0IKrfWwG5hfy8mY1bDsDzVybDuHFE9Bcwhs3Md 8IZVWRJaS4U/MdL7OHj3GEnEk0dNBBFli02DLz9C4O874u5SzCOljiLToQi7rcAI9C8w W0W7mmGj2LjmJdzCJTLn6XwXntvqBIur2/ctLmH6HGNBLUdX9zzsNivikbCPihvIK+X3 A8s1c40DyJYmVMnNGbVRNWHsCaHJbat1QgdxaVx6mQ5U894bpgLTDkgJQXo3Bfqfu6aB 4e6zIs4iE7zXp/mCvvD9g75bD3z6mFKD70Vq8soCMaVLckY+wxLWS2sEJ0fwzo9gmznh xANA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=94sjX6i+WpYuId2an5RqK0nB9ez56AuhnFrDx4gI5LI=; b=EMZvl77GTADyqFyvWondTnZKJ5fgygJA2+oT3rFREp5BV7nIMkb0J9jA0R5Ei3ctz+ g566ykDXz9+9+k3VVtX5pVwrYkDkcmxACgTYnUmmHrRRBZAQ498r7OAk6ygxJYDyp8Vl mioWmMwjmHJpxwJeZUZp0yxQAZ9bab3ndFla+MzAmkZFitSwjkcZuDYG6IOLz+GewDRW lGDesyOdrYbf+YBSO9GOWu7G6ifJPmnwyQHKBwI1362VtFk2jXbq2TEglxDqxGkQUJH0 GORAnH7mqd67hVhSnpGC5RZnf99HWk/yIKs6xNE7wZtjAz9vG8DWEbOoKkGtP+DHgXL9 2LbA== X-Gm-Message-State: AKwxytcq3BKvSfqzwp6OQ5/Vy/cgz3/u+vxCy5s/kgFxueh4zG/xNrRx Or3pvPTtv+54xbGMPpwFB0JpC5V1 X-Google-Smtp-Source: AH8x227spjLieiNH7CATA7MbtXFM/bqSKGMnme5uG12d3H8xWoIsrRc+x3CtCwAaRx8UozBziddyJw== X-Received: by 10.200.18.131 with SMTP id y3mr50056943qti.330.1517520560260; Thu, 01 Feb 2018 13:29:20 -0800 (PST) Received: from donizetti.redhat.com ([144.121.20.163]) by smtp.gmail.com with ESMTPSA id q26sm312558qtl.68.2018.02.01.13.29.19 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 01 Feb 2018 13:29:19 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 16:29:13 -0500 Message-Id: <20180201212917.18131-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180201212917.18131-1-pbonzini@redhat.com> References: <20180201212917.18131-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c0d::242 Subject: [Qemu-devel] [PATCH 1/5] test-coroutine: add simple CoMutex test X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: famz@redhat.com, qemu-block@nongnu.org, stefanha@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 Signed-off-by: Paolo Bonzini --- 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..010cb95ad6 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 = false; +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);