From nobody Tue Oct 28 01:56:36 2025 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 Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 151605431184938.250304213609866; Mon, 15 Jan 2018 14:11:51 -0800 (PST) Received: from localhost ([::1]:38667 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebCyn-0001BK-HQ for importer@patchew.org; Mon, 15 Jan 2018 17:11:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56421) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebCve-0007Mn-RI for qemu-devel@nongnu.org; Mon, 15 Jan 2018 17:08:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ebCvd-0006AZ-4I for qemu-devel@nongnu.org; Mon, 15 Jan 2018 17:08:30 -0500 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]:34134) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ebCvc-0006A9-T1; Mon, 15 Jan 2018 17:08:29 -0500 Received: by mail-wr0-x242.google.com with SMTP id 36so13273686wrh.1; Mon, 15 Jan 2018 14:08:28 -0800 (PST) Received: from donizetti.lan (dynamic-adsl-78-12-229-84.clienti.tiscali.it. [78.12.229.84]) by smtp.gmail.com with ESMTPSA id n74sm17351039wmi.1.2018.01.15.14.08.26 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 15 Jan 2018 14:08:26 -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=jWMFEBjmM/jy5p7aYeNaR6KVul/E5BuMckwLvXga4Do=; b=sChj33oLPRTAt3Qb9qfkhdOUcpEfiL924gJBxRRCojNDgcL8ZYfHoZ85gK8ZGHH1Li CY0P26m6SQz/9Ikp7PRYHq8TiNir7H232N1xqJ9KaxcVj17M145h1TzIAyfyGFB+LiWg q+d2ISFsYQpC7lmp5rgL4VbrzU7UoFysvJmCETeE7s0kU1rO0RGLFOp/aDxJdnXLAO5T cSEWNfS+y3Rht7P/mm4Dbf41q6kjp8KJsk9EyleLB+3GV16yzzx20o2y3IyjvOOUiMeR 6h4VIL6JTBwlW3MQuPVfdk8mXTb6FrMgWuOiv8nqSw1o7iO7dlRY1pOBsUUkrlqXyaAO /ELA== 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=jWMFEBjmM/jy5p7aYeNaR6KVul/E5BuMckwLvXga4Do=; b=N44M4gW8jJaDBYSfZFC1piDC/9lhv/FQlKSM9C55yJCXAa5lmGOOT381/DnnrvCh6v gXyN952mjKnnbddke8sYAihtuVyXgOdfkEBV1ET6lj63SmmBf5qAAym6W0Q4fgIrxqw1 74ksSXgaYDUTaQ/N5agwvq4BJbsxVy0VacohLJdMrBTV6IheJSZ314HpOuZHpX/I0CEx NW4oxPQBUZ/5QdWwRwhOED8HgAHgwKgkfGwnFfOEJxlNbkr2uxkRq0GITCsosxO0Ig9p zghy+mETfWghipX8uPjuwuUT+wpSsYbsdfp7PYcASoFKhBWsv4qxNjdEfEbKvOwYIaEZ ypUQ== X-Gm-Message-State: AKwxytfh0hS9sl/gokAdNOK9tyieiXDx+Yt0oFXRZC3wsX0/Gu9lzSyy IEYq5GLYzwLhn8c9MNQHhWb6M5Fl X-Google-Smtp-Source: ACJfBovbae9SsXUGB2VaOHP00PkToYDApkfHZaAfVwFj+oqJLv/ehvK4XfJO20wxjTFJA7VEs/ZLwA== X-Received: by 10.223.165.30 with SMTP id i30mr11663752wrb.84.1516054107674; Mon, 15 Jan 2018 14:08:27 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 15 Jan 2018 23:08:20 +0100 Message-Id: <20180115220822.10156-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180115220822.10156-1-pbonzini@redhat.com> References: <20180115220822.10156-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::242 Subject: [Qemu-devel] [PATCH 2/4] 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. Signed-off-by: Paolo Bonzini --- 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