From nobody Mon Apr 29 10:53:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517520789888399.9718494980318; Thu, 1 Feb 2018 13:33:09 -0800 (PST) Received: from localhost ([::1]:36533 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMTa-00081M-Tt for importer@patchew.org; Thu, 01 Feb 2018 16:32:58 -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) 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) 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) 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Fam Zheng --- 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); } =20 -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 =3D tmp; } =20 +static bool locked =3D false; +static int done; + +static void coroutine_fn mutex_fn(void *opaque) +{ + CoMutex *m =3D opaque; + qemu_co_mutex_lock(m); + assert(!locked); + locked =3D true; + qemu_coroutine_yield(); + locked =3D false; + qemu_co_mutex_unlock(m); + done++; +} + +static void do_test_co_mutex(CoroutineEntry *entry, void *opaque) +{ + Coroutine *c1 =3D qemu_coroutine_create(entry, opaque); + Coroutine *c2 =3D qemu_coroutine_create(entry, opaque); + + done =3D 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, =3D=3D, 1); + g_assert(locked); + + qemu_coroutine_enter(c2); + g_assert_cmpint(done, =3D=3D, 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_acce= ss); } =20 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); --=20 2.14.3 From nobody Mon Apr 29 10:53:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517521169685521.7335557585635; Thu, 1 Feb 2018 13:39:29 -0800 (PST) Received: from localhost ([::1]:36932 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMZl-00048b-Oh for importer@patchew.org; Thu, 01 Feb 2018 16:39:21 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59606) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMRA-00078d-P1 for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:34:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehMQ6-0003mY-Ob for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:30:28 -0500 Received: from mail-qt0-x22c.google.com ([2607:f8b0:400d:c0d::22c]:34261) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ehMQ6-0003lc-DI; Thu, 01 Feb 2018 16:29:22 -0500 Received: by mail-qt0-x22c.google.com with SMTP id a27so28408903qtd.1; Thu, 01 Feb 2018 13:29:22 -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.20 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); 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=K6wv/wNGooE2hU2/N/EgxFyxz+FA7JnhfewXDEIhwJY=; b=qQUzgLvLK24o3wklCJuVrf9AzJx9UCd++8PKd48/wT0a4xZ+LPHHKNLIjj/gttl8tA UTUXCOvfFXfdc+gk3N6OcAo/0m/42v2EeEuk5zTeCqSZuArmbhy7PtiEDeUKtBY6Ji2Q pRViAiAjAK1ABJou8g6UGwfr7B9H1DdIfJnUWlQKK+f72UTLMjNa1AOCOdWVc2PWFD6R dz7Wy/JF5ZXJzVUELu73MNtat2g2v7w/8P6ZpHMhJcnHI0kfzJTBfmplvfCIuOW8Wi4H rb2cAEcvgknl+PtBqzsUYxgi3hGuNEpV52QUpR4juWU1c2iXDWqhOTZa4+YH6wcx6TbI rAhQ== 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=K6wv/wNGooE2hU2/N/EgxFyxz+FA7JnhfewXDEIhwJY=; b=U8/NbghS9axbIdmWGH9N2niaND7bl+7caq5oN6oN3S5Rr3v+33xmeS5S0AjkWkVeKz +vRa2lX2U1Ja05I62Fe2z1EIMdYwLn375Firj14geDSjhsvlnRXTczTH3LZiPW+j6bFZ LXCj13/5bZ9YyVOtAYEOyU3UeOcYKlX4yI1O1DWYm8WhMXXNiZ/Ont6ZjXuRwy/PoOzd 3Tv270GBcO+AXjrI5GnNhcpxPX5QR0VHLYvW5D9CbYTOLTEhZRYd1bJ+e9n1Omcd2Ooq iCQHKRYGG12bQFNWuZM+Ql5v8CqEwNUcEuvLimm08hLqDhwCFQP91a3Qb92GXAvVx3Kr ppPA== X-Gm-Message-State: AKwxytdvpqZsyr6BX0cCWLfzHML3KC9Na78Z9UU1KQMpJc2eV1qip6T0 vlMJwuEhasQik7MVdBA89eKHY/2F X-Google-Smtp-Source: AH8x224AveoR0JbzWQ+7WylrPoO89A26TlgJYgBg9MfPIX3twP5ejrFyw1CBHLSV+nPW/2W6rFvL/g== X-Received: by 10.200.52.233 with SMTP id x38mr61625607qtb.267.1517520561365; Thu, 01 Feb 2018 13:29:21 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 16:29:14 -0500 Message-Id: <20180201212917.18131-3-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::22c Subject: [Qemu-devel] [PATCH 2/5] lockable: add QemuLockable 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" QemuLockable is a polymorphic lock type that takes an object and knows which function to use for locking and unlocking. The implementation could use C11 _Generic, but since the support is not very widespread I am instead using __builtin_choose_expr and __builtin_types_compatible_p, which are already used by include/qemu/atomic.h. QemuLockable can be used to implement lock guards, or to pass around a lock in such a way that a function can release it and re-acquire it. The next patch will do this for CoQueue. Signed-off-by: Paolo Bonzini Reviewed-by: Fam Zheng --- include/qemu/compiler.h | 40 ++++++++++++++++++++ include/qemu/coroutine.h | 4 +- include/qemu/lockable.h | 96 ++++++++++++++++++++++++++++++++++++++++++++= ++++ include/qemu/thread.h | 5 +-- include/qemu/typedefs.h | 4 ++ tests/test-coroutine.c | 25 +++++++++++++ 6 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 include/qemu/lockable.h diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h index 340e5fdc09..5179bedb1e 100644 --- a/include/qemu/compiler.h +++ b/include/qemu/compiler.h @@ -111,4 +111,44 @@ #define GCC_FMT_ATTR(n, m) #endif =20 +/* Implement C11 _Generic via GCC builtins. Example: + * + * QEMU_GENERIC(x, (float, sinf), (long double, sinl), sin) (x) + * + * The first argument is the discriminator. The last is the default value. + * The middle ones are tuples in "(type, expansion)" format. + */ + +/* First, find out the number of generic cases. */ +#define QEMU_GENERIC(x, ...) \ + QEMU_GENERIC_(typeof(x), __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) + +/* There will be extra arguments, but they are not used. */ +#define QEMU_GENERIC_(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, count, ..= .) \ + QEMU_GENERIC##count(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) + +/* Two more helper macros, this time to extract items from a parenthesized + * list. + */ +#define QEMU_FIRST_(a, b) a +#define QEMU_SECOND_(a, b) b + +/* ... and a final one for the common part of the "recursion". */ +#define QEMU_GENERIC_IF(x, type_then, else_) = \ + __builtin_choose_expr(__builtin_types_compatible_p(x, = \ + QEMU_FIRST_ type_th= en), \ + QEMU_SECOND_ type_then, else_) + +/* CPP poor man's "recursion". */ +#define QEMU_GENERIC1(x, a0, ...) (a0) +#define QEMU_GENERIC2(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC1(x, = __VA_ARGS__)) +#define QEMU_GENERIC3(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC2(x, = __VA_ARGS__)) +#define QEMU_GENERIC4(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC3(x, = __VA_ARGS__)) +#define QEMU_GENERIC5(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC4(x, = __VA_ARGS__)) +#define QEMU_GENERIC6(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC5(x, = __VA_ARGS__)) +#define QEMU_GENERIC7(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC6(x, = __VA_ARGS__)) +#define QEMU_GENERIC8(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC7(x, = __VA_ARGS__)) +#define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, = __VA_ARGS__)) +#define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x,= __VA_ARGS__)) + #endif /* COMPILER_H */ diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index ce2eb73670..8a5129741c 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -121,7 +121,7 @@ bool qemu_coroutine_entered(Coroutine *co); * Provides a mutex that can be used to synchronise coroutines */ struct CoWaitRecord; -typedef struct CoMutex { +struct CoMutex { /* Count of pending lockers; 0 for a free mutex, 1 for an * uncontended mutex. */ @@ -142,7 +142,7 @@ typedef struct CoMutex { unsigned handoff, sequence; =20 Coroutine *holder; -} CoMutex; +}; =20 /** * Initialises a CoMutex. This must be called before any other operation i= s used diff --git a/include/qemu/lockable.h b/include/qemu/lockable.h new file mode 100644 index 0000000000..826ac3f675 --- /dev/null +++ b/include/qemu/lockable.h @@ -0,0 +1,96 @@ +/* + * Polymorphic locking functions (aka poor man templates) + * + * Copyright Red Hat, Inc. 2017, 2018 + * + * Author: Paolo Bonzini + * + * This work is licensed under the terms of the GNU LGPL, version 2 or lat= er. + * See the COPYING.LIB file in the top-level directory. + * + */ + +#ifndef QEMU_LOCKABLE_H +#define QEMU_LOCKABLE_H + +#include "qemu/coroutine.h" +#include "qemu/thread.h" + +typedef void QemuLockUnlockFunc(void *); + +struct QemuLockable { + void *object; + QemuLockUnlockFunc *lock; + QemuLockUnlockFunc *unlock; +}; + +/* This function gives an error if an invalid, non-NULL pointer type is pa= ssed + * to QEMU_MAKE_LOCKABLE. For optimized builds, we can rely on dead-code = elimination + * from the compiler, and give the errors already at link time. + */ +#ifdef __OPTIMIZE__ +void unknown_lock_type(void *); +#else +static inline void unknown_lock_type(void *unused) +{ + abort(); +} +#endif + +static inline __attribute__((__always_inline__)) QemuLockable * +qemu_make_lockable(void *x, QemuLockable *lockable) +{ + /* We cannot test this in a macro, otherwise we get compiler + * warnings like "the address of 'm' will always evaluate as 'true'". + */ + return x ? lockable : NULL; +} + +/* Auxiliary macros to simplify QEMU_MAKE_LOCABLE. */ +#define QEMU_LOCK_FUNC(x) ((QemuLockUnlockFunc *) \ + QEMU_GENERIC(x, \ + (QemuMutex *, qemu_mutex_lock), \ + (CoMutex *, qemu_co_mutex_lock), \ + (QemuSpin *, qemu_spin_lock), \ + unknown_lock_type)) + +#define QEMU_UNLOCK_FUNC(x) ((QemuLockUnlockFunc *) \ + QEMU_GENERIC(x, \ + (QemuMutex *, qemu_mutex_unlock), \ + (CoMutex *, qemu_co_mutex_unlock), \ + (QemuSpin *, qemu_spin_unlock), \ + unknown_lock_type)) + +/* In C, compound literals have the lifetime of an automatic variable. + * In C++ it would be different, but then C++ wouldn't need QemuLockable + * either... + */ +#define QEMU_MAKE_LOCKABLE_(x) qemu_make_lockable((x), &(QemuLockable) { = \ + .object =3D (x), \ + .lock =3D QEMU_LOCK_FUNC(x), \ + .unlock =3D QEMU_UNLOCK_FUNC(x), \ + }) + +/* QEMU_MAKE_LOCKABLE - Make a polymorphic QemuLockable + * + * @x: a lock object (currently one of QemuMutex, CoMutex, QemuSpin). + * + * Returns a QemuLockable object that can be passed around + * to a function that can operate with locks of any kind. + */ +#define QEMU_MAKE_LOCKABLE(x) \ + QEMU_GENERIC(x, \ + (QemuLockable *, (x)), \ + QEMU_MAKE_LOCKABLE_(x)) + +static inline void qemu_lockable_lock(QemuLockable *x) +{ + x->lock(x->object); +} + +static inline void qemu_lockable_unlock(QemuLockable *x) +{ + x->unlock(x->object); +} + +#endif diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 9af4e945aa..ef7bd16123 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -4,7 +4,6 @@ #include "qemu/processor.h" #include "qemu/atomic.h" =20 -typedef struct QemuMutex QemuMutex; typedef struct QemuCond QemuCond; typedef struct QemuSemaphore QemuSemaphore; typedef struct QemuEvent QemuEvent; @@ -97,9 +96,9 @@ struct Notifier; void qemu_thread_atexit_add(struct Notifier *notifier); void qemu_thread_atexit_remove(struct Notifier *notifier); =20 -typedef struct QemuSpin { +struct QemuSpin { int value; -} QemuSpin; +}; =20 static inline void qemu_spin_init(QemuSpin *spin) { diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h index 9bd7a834ba..5923849cdd 100644 --- a/include/qemu/typedefs.h +++ b/include/qemu/typedefs.h @@ -19,6 +19,7 @@ typedef struct BusClass BusClass; typedef struct BusState BusState; typedef struct Chardev Chardev; typedef struct CompatProperty CompatProperty; +typedef struct CoMutex CoMutex; typedef struct CPUAddressSpace CPUAddressSpace; typedef struct CPUState CPUState; typedef struct DeviceListener DeviceListener; @@ -86,9 +87,12 @@ typedef struct QEMUBH QEMUBH; typedef struct QemuConsole QemuConsole; typedef struct QemuDmaBuf QemuDmaBuf; typedef struct QEMUFile QEMUFile; +typedef struct QemuLockable QemuLockable; +typedef struct QemuMutex QemuMutex; typedef struct QemuOpt QemuOpt; typedef struct QemuOpts QemuOpts; typedef struct QemuOptsList QemuOptsList; +typedef struct QemuSpin QemuSpin; typedef struct QEMUSGList QEMUSGList; typedef struct QEMUTimer QEMUTimer; typedef struct QEMUTimerListGroup QEMUTimerListGroup; diff --git a/tests/test-coroutine.c b/tests/test-coroutine.c index 010cb95ad6..6e4842bfda 100644 --- a/tests/test-coroutine.c +++ b/tests/test-coroutine.c @@ -14,6 +14,7 @@ #include "qemu/osdep.h" #include "qemu/coroutine.h" #include "qemu/coroutine_int.h" +#include "qemu/lockable.h" =20 /* * Check that qemu_in_coroutine() works @@ -210,6 +211,18 @@ static void coroutine_fn mutex_fn(void *opaque) done++; } =20 +static void coroutine_fn lockable_fn(void *opaque) +{ + QemuLockable *x =3D opaque; + qemu_lockable_lock(x); + assert(!locked); + locked =3D true; + qemu_coroutine_yield(); + locked =3D false; + qemu_lockable_unlock(x); + done++; +} + static void do_test_co_mutex(CoroutineEntry *entry, void *opaque) { Coroutine *c1 =3D qemu_coroutine_create(entry, opaque); @@ -240,6 +253,17 @@ static void test_co_mutex(void) do_test_co_mutex(mutex_fn, &m); } =20 +static void test_co_mutex_lockable(void) +{ + CoMutex m; + CoMutex *null_pointer =3D NULL; + + qemu_co_mutex_init(&m); + do_test_co_mutex(lockable_fn, QEMU_MAKE_LOCKABLE(&m)); + + g_assert(QEMU_MAKE_LOCKABLE(null_pointer) =3D=3D NULL); +} + /* * Check that creation, enter, and return work */ @@ -478,6 +502,7 @@ int main(int argc, char **argv) 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); + g_test_add_func("/locking/co-mutex/lockable", test_co_mutex_lockable); if (g_test_perf()) { g_test_add_func("/perf/lifecycle", perf_lifecycle); g_test_add_func("/perf/nesting", perf_nesting); --=20 2.14.3 From nobody Mon Apr 29 10:53:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517520900098831.6581929094345; Thu, 1 Feb 2018 13:35:00 -0800 (PST) Received: from localhost ([::1]:36654 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMVX-0000vl-9z for importer@patchew.org; Thu, 01 Feb 2018 16:34:59 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59644) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMRC-00079F-7A for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:31:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehMQ8-0003oC-2c for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:30:30 -0500 Received: from mail-qk0-x242.google.com ([2607:f8b0:400d:c09::242]:35654) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ehMQ7-0003nQ-Mg; Thu, 01 Feb 2018 16:29:23 -0500 Received: by mail-qk0-x242.google.com with SMTP id 69so8196298qkz.2; Thu, 01 Feb 2018 13:29:23 -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.21 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 01 Feb 2018 13:29:21 -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=LoUFZAcUcnaHqzUJBuntOgHXCp6yCt4oc9+g/EMR6lM=; b=VFS0QiiM/mKlilTqPmI1n5Yoos9VsV3OZIpFk5D6FV3gwmzoDpEO37QhDPnctlWfSj nOsfBeTj++ee5Z8sriF7AZavGisPBfBLfuHzEbEuObhhJ6iKqU/OCN4X+vHHYcCidyIa Q6UQLPFV7fxdkVKjXHJumRXT85ETK4HD6417PmXW//Z4P1XJS9RVEahL2q6Jgv2YaYOd cKyIhKY8GPfY9SM9/pZmexlZ7U5KfMo0U4ocuvUmMolr/BhLiOdMX7X37GuteKFLuVXB wxm0gYwmIT89BTPgIwWl6y5cej1ezdTU10ya+3gqhm3im74K7N61Z0NKuUa/wkfzMf1o EyIw== 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=LoUFZAcUcnaHqzUJBuntOgHXCp6yCt4oc9+g/EMR6lM=; b=kiGXbAUPXJD+rY38Q+1E0U/2szoCb2G5AVuE4C2jfBZogxBUb3BYJ4BXO7sahvvHTA aDv1W1sj+IE5yFN8X7w9lOdAm+T+S+3hxzZeXJQZyL+dsU/oTyI5OyDotbaS52G0gvdN hRl6V54AfDC1FFIGDb3yJg6WpqymjTNkZoPP5v2Ozd9BzeP5ZFbdmt8YATdhcXxCWPsN /aR3KNII6fd8hoql4gk8Xs79FBBcXEfKzDZgnZCUsHGYnGkYQQiwnp9lVKcjwexUPkiL LUq+4l5RFefAOOaIzdaIUixFrqBXvr2dnyHJnpbyuZcLI9cJXGcRdesgfD1ZMB/U6NQK Ts+A== X-Gm-Message-State: AKwxytcG/BE2swiKJ7l9+RP131MqhkHy4N8Aw6mLHQ1ORwbf7rRz0Mu9 hKV40dA4JnwdG9DgeM9nHhdThO23 X-Google-Smtp-Source: AH8x226e9xuQ9LxXfmAUEpk2WVvRXae/w+g34XswZXuviqIOuGklBlOLrGoOpOGOImpVOO5FczHB/w== X-Received: by 10.233.223.134 with SMTP id t128mr59263346qkf.121.1517520562656; Thu, 01 Feb 2018 13:29:22 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 16:29:15 -0500 Message-Id: <20180201212917.18131-4-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:c09::242 Subject: [Qemu-devel] [PATCH 3/5] coroutine-lock: convert CoQueue to use QemuLockable 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" There are cases in which a queued coroutine must be restarted from non-coroutine context (with qemu_co_enter_next). In this cases, qemu_co_enter_next also needs to be thread-safe, but it cannot use a CoMutex and so cannot qemu_co_queue_wait. Use QemuLockable so that the CoQueue can interchangeably use CoMutex or QemuMutex. Reviewed-by: Stefan Hajnoczi Signed-off-by: Paolo Bonzini Reviewed-by: Fam Zheng --- include/qemu/coroutine.h | 6 +++++- util/qemu-coroutine-lock.c | 12 +++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 8a5129741c..1e5f0957e6 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -183,7 +183,9 @@ void qemu_co_queue_init(CoQueue *queue); * caller of the coroutine. The mutex is unlocked during the wait and * locked again afterwards. */ -void coroutine_fn qemu_co_queue_wait(CoQueue *queue, CoMutex *mutex); +#define qemu_co_queue_wait(queue, lock) \ + qemu_co_queue_wait_impl(queue, QEMU_MAKE_LOCKABLE(lock)) +void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lo= ck); =20 /** * Restarts the next coroutine in the CoQueue and removes it from the queu= e. @@ -271,4 +273,6 @@ void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, = int64_t ns); */ void coroutine_fn yield_until_fd_readable(int fd); =20 +#include "qemu/lockable.h" + #endif /* QEMU_COROUTINE_H */ diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c index 846ff9167f..2a66fc1467 100644 --- a/util/qemu-coroutine-lock.c +++ b/util/qemu-coroutine-lock.c @@ -40,13 +40,13 @@ void qemu_co_queue_init(CoQueue *queue) QSIMPLEQ_INIT(&queue->entries); } =20 -void coroutine_fn qemu_co_queue_wait(CoQueue *queue, CoMutex *mutex) +void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lo= ck) { Coroutine *self =3D qemu_coroutine_self(); QSIMPLEQ_INSERT_TAIL(&queue->entries, self, co_queue_next); =20 - if (mutex) { - qemu_co_mutex_unlock(mutex); + if (lock) { + qemu_lockable_unlock(lock); } =20 /* There is no race condition here. Other threads will call @@ -60,9 +60,11 @@ void coroutine_fn qemu_co_queue_wait(CoQueue *queue, CoM= utex *mutex) /* TODO: OSv implements wait morphing here, where the wakeup * primitive automatically places the woken coroutine on the * mutex's queue. This avoids the thundering herd effect. + * This could be implemented for CoMutexes, but not really for + * other cases of QemuLockable. */ - if (mutex) { - qemu_co_mutex_lock(mutex); + if (lock) { + qemu_lockable_lock(lock); } } =20 --=20 2.14.3 From nobody Mon Apr 29 10:53:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517521086539446.1575628570806; Thu, 1 Feb 2018 13:38:06 -0800 (PST) Received: from localhost ([::1]:36876 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMYV-0003Ha-EJ for importer@patchew.org; Thu, 01 Feb 2018 16:38:03 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35156) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMU8-0000Ez-PI for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:34:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehMQ9-0003pG-5z for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:33:32 -0500 Received: from mail-qk0-x241.google.com ([2607:f8b0:400d:c09::241]:35855) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ehMQ8-0003of-Ow; Thu, 01 Feb 2018 16:29:24 -0500 Received: by mail-qk0-x241.google.com with SMTP id d21so21354974qkj.3; Thu, 01 Feb 2018 13:29:24 -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.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 01 Feb 2018 13:29:22 -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=TAyPcChUbYxGtepxj1z0oJi1t5ofsBGEuvVnnYjiqww=; b=b47TiDkxE93eteKAeaDqw6K8OhT4EIqI25OgrK7H9WELfqk9hXrZzdrVI/2YL2E7S6 eU5PmGbZ9WpDnVo+/TEbdqkmMkxT/Rz0ZtU8ODvlFJurgZ6OrEj84UyuQlrv/29scxFc e/RgzmjjSQ0bhfyPXx9kf1TFLmwMWlWVXD0Yt6d7QvKqlEtRO2fPetlHwWTPu9zeulAk 6Iq9NZjovBFhzW4cQvwia3x18TRo6YxOdycEYREfyWJz5Ur92H9zJ0823F5yAkbYMTSA 3DQIenOoOXJSN60fQjdas8dDM9YLvLmrN9ThWL4GrVoRmMwbh5yDQ+l2Lj1t+fasXWXR o/Ew== 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=TAyPcChUbYxGtepxj1z0oJi1t5ofsBGEuvVnnYjiqww=; b=hF/29mIlDaMdEzjgsa2rVARk9e1TUHhcqRlAU8QJYlp9Qwk4hMILnqNOBaQdbIewbi gJWDmJZFUGWawFgmaCWYMca/T8INX5mdB+Ko7DLTv/GBe+gEZwUoMRJQ3sy8ue5qKaGy xpecb43PaRWGx/Mn5Z/CX7gyeBMN7JZEa+spRxTGYLFlUAS+VXPLuukClhuEi+r3/CVm Y/N5HRSo8BmLAXccJ8nkO4zfWKDuEV2FkdxxDap7h1o8FPdhrxXrTojI8mdHeMSStBSK tQjCEAjdUwngsMJ7y8+mtwr9nKTVTPg8ZtgtTDy0XeAB1bI9SdTUI9YcdVe743HU85xl ed6g== X-Gm-Message-State: APf1xPAcNgY8yiF/eA+UijaZzZok2lYHIIKtv7S9Bjwbc0t43TMJfbvQ Kq/zWVfsb2ERzYm/BunpLV+n4VEs X-Google-Smtp-Source: AH8x227/JMWJLaXgoaNCRc9AG7e/MIbbcd4AAnjMkBRM+IUNF45pQAPaF0RwZYJq+SU+Rwp/mH55/g== X-Received: by 10.55.34.72 with SMTP id i69mr1022016qki.287.1517520563814; Thu, 01 Feb 2018 13:29:23 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 16:29:16 -0500 Message-Id: <20180201212917.18131-5-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:c09::241 Subject: [Qemu-devel] [PATCH 4/5] coroutine-lock: make qemu_co_enter_next thread-safe 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" qemu_co_queue_next does not need to release and re-acquire the mutex, because the queued coroutine does not run immediately. However, this does not hold for qemu_co_enter_next. Now that qemu_co_queue_wait can synchronize (via QemuLockable) with code that is not running in coroutine context, it's important that code using qemu_co_enter_next can easily use a standardized locking idiom. First of all, qemu_co_enter_next must use aio_co_wake to restart the coroutine. Second, the function gains a second argument, a QemuLockable*, and the comments of qemu_co_queue_next and qemu_co_queue_restart_all are adjusted to clarify the difference. Signed-off-by: Paolo Bonzini Reviewed-by: Fam Zheng --- fsdev/qemu-fsdev-throttle.c | 4 ++-- include/qemu/coroutine.h | 19 +++++++++++++------ util/qemu-coroutine-lock.c | 10 ++++++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/fsdev/qemu-fsdev-throttle.c b/fsdev/qemu-fsdev-throttle.c index 49eebb5412..1dc07fbc12 100644 --- a/fsdev/qemu-fsdev-throttle.c +++ b/fsdev/qemu-fsdev-throttle.c @@ -20,13 +20,13 @@ static void fsdev_throttle_read_timer_cb(void *opaque) { FsThrottle *fst =3D opaque; - qemu_co_enter_next(&fst->throttled_reqs[false]); + qemu_co_enter_next(&fst->throttled_reqs[false], NULL); } =20 static void fsdev_throttle_write_timer_cb(void *opaque) { FsThrottle *fst =3D opaque; - qemu_co_enter_next(&fst->throttled_reqs[true]); + qemu_co_enter_next(&fst->throttled_reqs[true], NULL); } =20 void fsdev_throttle_parse_opts(QemuOpts *opts, FsThrottle *fst, Error **er= rp) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 1e5f0957e6..3afee7169b 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -188,21 +188,28 @@ void qemu_co_queue_init(CoQueue *queue); void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lo= ck); =20 /** - * Restarts the next coroutine in the CoQueue and removes it from the queu= e. - * - * Returns true if a coroutine was restarted, false if the queue is empty. + * Removes the next coroutine from the CoQueue, and wake it up. + * Returns true if a coroutine was removed, false if the queue is empty. */ bool coroutine_fn qemu_co_queue_next(CoQueue *queue); =20 /** - * Restarts all coroutines in the CoQueue and leaves the queue empty. + * Empties the CoQueue; all coroutines are woken up. */ void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue); =20 /** - * Enter the next coroutine in the queue + * Removes the next coroutine from the CoQueue, and wake it up. Unlike + * qemu_co_queue_next, this function releases the lock during aio_co_wake + * because it is meant to be used outside coroutine context; in that case,= the + * coroutine is entered immediately, before qemu_co_enter_next returns. + * + * If used in coroutine context, qemu_co_enter_next is equivalent to + * qemu_co_queue_next. */ -bool qemu_co_enter_next(CoQueue *queue); +#define qemu_co_enter_next(queue, lock) \ + qemu_co_enter_next_impl(queue, QEMU_MAKE_LOCKABLE(lock)) +bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock); =20 /** * Checks if the CoQueue is empty. diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c index 2a66fc1467..78fb79acf8 100644 --- a/util/qemu-coroutine-lock.c +++ b/util/qemu-coroutine-lock.c @@ -132,7 +132,7 @@ void coroutine_fn qemu_co_queue_restart_all(CoQueue *qu= eue) qemu_co_queue_do_restart(queue, false); } =20 -bool qemu_co_enter_next(CoQueue *queue) +bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock) { Coroutine *next; =20 @@ -142,7 +142,13 @@ bool qemu_co_enter_next(CoQueue *queue) } =20 QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next); - qemu_coroutine_enter(next); + if (lock) { + qemu_lockable_unlock(lock); + } + aio_co_wake(next); + if (lock) { + qemu_lockable_lock(lock); + } return true; } =20 --=20 2.14.3 From nobody Mon Apr 29 10:53:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517520959792871.2131829967126; Thu, 1 Feb 2018 13:35:59 -0800 (PST) Received: from localhost ([::1]:36683 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMWV-0001kg-06 for importer@patchew.org; Thu, 01 Feb 2018 16:35:59 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59696) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMRE-00079f-Kx for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:34:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehMQA-0003r0-JI for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:30:32 -0500 Received: from mail-qk0-x244.google.com ([2607:f8b0:400d:c09::244]:44122) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ehMQA-0003qU-9f; Thu, 01 Feb 2018 16:29:26 -0500 Received: by mail-qk0-x244.google.com with SMTP id n188so9981371qkn.11; Thu, 01 Feb 2018 13:29:26 -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.23 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 01 Feb 2018 13:29:24 -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=2YvlCWabKdL+I8FjttoOo1yKaWZ0H0Kvr+485G3eEP8=; b=C3ZxuLzGChA/MmM/HA8jY3wk96YX7wlfPj0wORV18+01dahffZpVtfEUeWx8GkqnCx lloAQbMDgL3EivdanYK/pOToYynAs+tX1plOd30cctW+Y52PH22U6EYuQrkvOCTmbB8Q k3cr2NnSNB891prq6HC0o2eoD9lrFufW++e8WxJooPZ15txCFgqK9P1am9n6S6dH74va brOurfX1GoNpiYt6XfMCvWY/ej3joMcL5C5wblXRtKB9/bmsKmyCejAtn4rLGtSMSjqL 2W6H6jr6YdPC9kZrpJzSchbgm0z+2eNqrTbw9QaA3MhwyH59JGCcA7pE75Bg2Vckef9q GwRw== 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=2YvlCWabKdL+I8FjttoOo1yKaWZ0H0Kvr+485G3eEP8=; b=VZ+8p8+F56qMHmc6VnidORG+I7k3e2Nxlg5+v2OwhN2P25BN+u7xns2zkKCRsDsht3 zXR+Qo2ZWo3ByVfPhlQvE2O3refGxZD622ZoUCV27LK3IMEVtM/zlYvYm8ojBZIN3oaH liFNO5xYoTlNvcW16cGPhMMS2l2oh7tZInfGt66ld0xrenLgMSA2mNBbp+6J4GXKVzNX v2PTNqJS8zEMnLdi5Cy/DNN/PBA7pq6Kkfab3wk9dFbA0CImHrOt8sMSkYlc3vFk3aL3 3ww62MSkql0Wnhid5Dx+ssVWdx++cMnTZIDLV9lCf2MVLFp3LfaVxc0nlZ0oisBKMi43 2fTA== X-Gm-Message-State: AKwxytf9TKyDAP+URxvgeQBACGeja8ThZ0OFNZHVeSKt6q0g1hIgBQ7N Gm5veMX1W/4MZbH5+X+eWc7So1TB X-Google-Smtp-Source: AH8x2255iIB1y449dcvNGDukqxVLXoyIbDot7GIKP82PH/+a110U0llw4iidSywl8SLNITG2CgFdiA== X-Received: by 10.55.7.2 with SMTP id 2mr42309029qkh.244.1517520565480; Thu, 01 Feb 2018 13:29:25 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 16:29:17 -0500 Message-Id: <20180201212917.18131-6-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:c09::244 Subject: [Qemu-devel] [PATCH 5/5] curl: convert to CoQueue 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Now that CoQueues can use a QemuMutex for thread-safety, there is no need for curl to roll its own coroutine queue. Coroutines can be placed directly on the queue instead of using a list of CURLAIOCBs. Reviewed-by: Stefan Hajnoczi Signed-off-by: Paolo Bonzini Reviewed-by: Fam Zheng --- block/curl.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/block/curl.c b/block/curl.c index 35cf417f59..cd578d3d14 100644 --- a/block/curl.c +++ b/block/curl.c @@ -101,8 +101,6 @@ typedef struct CURLAIOCB { =20 size_t start; size_t end; - - QSIMPLEQ_ENTRY(CURLAIOCB) next; } CURLAIOCB; =20 typedef struct CURLSocket { @@ -138,7 +136,7 @@ typedef struct BDRVCURLState { bool accept_range; AioContext *aio_context; QemuMutex mutex; - QSIMPLEQ_HEAD(, CURLAIOCB) free_state_waitq; + CoQueue free_state_waitq; char *username; char *password; char *proxyusername; @@ -538,7 +536,6 @@ static int curl_init_state(BDRVCURLState *s, CURLState = *state) /* Called with s->mutex held. */ static void curl_clean_state(CURLState *s) { - CURLAIOCB *next; int j; for (j =3D 0; j < CURL_NUM_ACB; j++) { assert(!s->acb[j]); @@ -556,13 +553,7 @@ static void curl_clean_state(CURLState *s) =20 s->in_use =3D 0; =20 - next =3D QSIMPLEQ_FIRST(&s->s->free_state_waitq); - if (next) { - QSIMPLEQ_REMOVE_HEAD(&s->s->free_state_waitq, next); - qemu_mutex_unlock(&s->s->mutex); - aio_co_wake(next->co); - qemu_mutex_lock(&s->s->mutex); - } + qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex); } =20 static void curl_parse_filename(const char *filename, QDict *options, @@ -784,7 +775,7 @@ static int curl_open(BlockDriverState *bs, QDict *optio= ns, int flags, } =20 DPRINTF("CURL: Opening %s\n", file); - QSIMPLEQ_INIT(&s->free_state_waitq); + qemu_co_queue_init(&s->free_state_waitq); s->aio_context =3D bdrv_get_aio_context(bs); s->url =3D g_strdup(file); qemu_mutex_lock(&s->mutex); @@ -888,10 +879,7 @@ static void curl_setup_preadv(BlockDriverState *bs, CU= RLAIOCB *acb) if (state) { break; } - QSIMPLEQ_INSERT_TAIL(&s->free_state_waitq, acb, next); - qemu_mutex_unlock(&s->mutex); - qemu_coroutine_yield(); - qemu_mutex_lock(&s->mutex); + qemu_co_queue_wait(&s->free_state_waitq, &s->mutex); } =20 if (curl_init_state(s, state) < 0) { --=20 2.14.3