From patchwork Fri Dec 8 10:55:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 846151 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="JJgWLMqP"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3ytTn64QvYz9s74 for ; Fri, 8 Dec 2017 21:56:42 +1100 (AEDT) Received: from localhost ([::1]:36545 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGKe-00012C-O2 for incoming@patchwork.ozlabs.org; Fri, 08 Dec 2017 05:56:40 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47548) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGK6-00010H-8V for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eNGK4-0001ls-UR for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:06 -0500 Received: from mail-wr0-x243.google.com ([2a00:1450:400c:c0c::243]:39706) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eNGK4-0001lk-NI for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:04 -0500 Received: by mail-wr0-x243.google.com with SMTP id a41so10423483wra.6 for ; Fri, 08 Dec 2017 02:56:04 -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=H6vQfnB5wgV1UdUKKYRr1ZjGAWxY87aTymaSXFGAvJY=; b=JJgWLMqPVF+KgKcRKsnMKlqempjtpfRTJI1VKhrE6s9mUKtlPZ1QDc3GZDvS5TAzHi Y4C2bhI5BT82r2jzXDIRkNzqrX0lJ0mc8wURL4Vo9YKrnnp7/MZ4yYEXlTvlaXsJnwiL WnO82qWQgtVx9lBkxYCe+0i8VLoARKE4+8uXZd2UY1/inHaSjDTAQ7BKCA5w8rgmFmuU gg1HvwwZL3NOYj+LYQ6Qm3me4v3oEzJEDoZg233PDb8hNT6128tocgp92iekzLSV6jrM Myja29QDDlCcd/1hrLN0scgA+04aETflh1IgL90Jitp+8EJTQUn/9bGXanxTvtEpBUza t6Kg== 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=H6vQfnB5wgV1UdUKKYRr1ZjGAWxY87aTymaSXFGAvJY=; b=H5iWtYI86jApNIVwepADy7sTSLtDURoEExE5H/KPRYOTLe7LnUaPtqoa5riNzoB93I m5SKGtmgPEIfgq0AyeO22K6Jc+fZC8yiLc7sRW1eryBeFIAEvb0WNx5oxlKOaB8dM5UC 0+1q4Q7Q9nkWRFrDjQ3FxABTHh4tlWUrqpruQ0S5HzQGgEkmilb1dDJv4YPvoY3kRVHc xjgUdyMgAUf8cQFNDbar9L0kX1A0cnfn6wDtdhsGoVGf9TQA3nis41kEnpkP48PKzWTB lyIzaox9g/FsDC1pxBx8bDGmJ46+Rqh6HiycgCTeWNGNVMJCxPvFYBH0vH38HHOCm81X +Yrw== X-Gm-Message-State: AJaThX7cG6zSpGJstlZFwGp6fgxxcs8S71I4GSntZgBn6yX1HOONfI4T vHU67tbo0BNh6VLT54mY6ehSu9Bz X-Google-Smtp-Source: AGs4zMYkwuJ+dwdOBQTuje18dN6S634ZzLkT1z1KmWlBno1MdzcMx9mtYHDPEL36zW+1bJfhIJ5POw== X-Received: by 10.223.186.67 with SMTP id t3mr24211203wrg.276.1512730563189; Fri, 08 Dec 2017 02:56:03 -0800 (PST) Received: from donizetti.lan (dynamic-adsl-78-12-251-125.clienti.tiscali.it. [78.12.251.125]) by smtp.gmail.com with ESMTPSA id v195sm1325461wmf.25.2017.12.08.02.56.00 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Dec 2017 02:56:00 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 8 Dec 2017 11:55:49 +0100 Message-Id: <20171208105553.12249-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171208105553.12249-1-pbonzini@redhat.com> References: <20171208105553.12249-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::243 Subject: [Qemu-devel] [PATCH 1/5] compiler: add a helper for C99 inline functions 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: "Emilio G . Cota" , Fam Zheng , Stefan Hajnoczi Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" C99 inline functions are useful whenever a function provide an abstraction that is generally expected to be inlined away, but yet a function pointer might be needed. One such case is function passed to GCC's cleanup attribute. Unfortunately, C99 inline functions clash a bit with -Wredundant-decls. Provide a helper macro to hide the ugliness. Signed-off-by: Paolo Bonzini --- include/qemu/compiler.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h index 340e5fdc09..e02b73b9e2 100644 --- a/include/qemu/compiler.h +++ b/include/qemu/compiler.h @@ -94,6 +94,21 @@ #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \ sizeof(QEMU_BUILD_BUG_ON_STRUCT(x))) +/* Provide an extern declaration for a C99 inline function. This can + * be simply placed in a C file but, unfortunately, this means the + * declaration comes after the inline definition, and GCC thus + * stubbornly raises a -Wredundant-decls warning. + * + * Putting the declaration before the header would be uglier (it couldn't + * just use typeof) and not always possible if the function requires types + * that are defined in the header. Therefore, we just shut up the warning. + */ +#define QEMU_EXTERN_INLINE(func) \ + _Pragma("GCC diagnostic push"); \ + _Pragma("GCC diagnostic ignored \"-Wredundant-decls\""); \ + extern typeof(func) func; \ + _Pragma("GCC diagnostic pop") \ + #if defined __GNUC__ # if !QEMU_GNUC_PREREQ(4, 4) /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */ From patchwork Fri Dec 8 10:55:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 846157 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="de/z6SuH"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3ytTqW4gL0z9s74 for ; Fri, 8 Dec 2017 21:58:47 +1100 (AEDT) Received: from localhost ([::1]:36554 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGMf-0002xx-Og for incoming@patchwork.ozlabs.org; Fri, 08 Dec 2017 05:58:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47567) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGK8-00011H-08 for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eNGK6-0001mA-KT for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:08 -0500 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:39291) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eNGK6-0001lx-BA for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:06 -0500 Received: by mail-wm0-x243.google.com with SMTP id i11so2546206wmf.4 for ; Fri, 08 Dec 2017 02:56:06 -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=rarMQ9gYFtK7kuq7jsxlAKo847BemotRT/G5+dGpzoU=; b=de/z6SuH4qysrrOLMYOE0hqfJdVy/esBbWOVB7U+lajoEdvGQuzitHmvEqS1SBK4RH U7uOXJcgl+FReMwIVXY5eRXJe2aImzkvVYae4oa1LgHuYxeKq1OScBHmw9kPxQlaxhwg yEL99ECMNpeKiJ0YeTCRkjkXfv+dRs8AywP+QfJYAjgpO02S9IcQBbdA2sOGgWZP4fz3 rHpBLQK9KKzoQgH115mBqJT9B74gJE7WAgSHS+xbnmr76opHjAfsQXw57rSuRquiKiCR oXIwMhMIqXU2yIvtsaDhC6s1NAg+O8BYdxKeUK0HcMufsy1YoCJ8U5xesuMOtZyIxd3P hteg== 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=rarMQ9gYFtK7kuq7jsxlAKo847BemotRT/G5+dGpzoU=; b=gOiEocJ4bkwK///KfewXrG0NvwRy99ywKs45eWrL8UeJ3o/5YoYoKnoQxNDYuyHT70 5BrZysWO63HVe1K4hiRmzLng18UNQMfHVpBui0RcL+oiE1HBtBUbEnGrA811MHvPfDsM XWx3Dhg5Rc5qXUiqWUnB30LTMsiKGuSX11UHM0Yh4zcAg18XclRSr6jkr9AMlG1TZe2f d3oTorLT37/SHGlPkqaF5E+AsrrD0J6GTbh2c40xYzG/ySc691IVLKLWo9CHFmxs6pAN wHG+YoQsOYxGreJudTPkQmdV339v3cw36vmg8Iuj8pipE05jNrm4zv8ryU2ZZUjKa3cO 5OeQ== X-Gm-Message-State: AKGB3mKH1Z9L9dTmKaGtW4NckBRD8QkrjqcuC6d84a8ZrxF1YrQa+Vii h9u1txIIrAIX4ageGcB3o0qK9bhD X-Google-Smtp-Source: AGs4zMZwlmC+S3qKJ4kRL8ISeKkwS2ttIgpG2lKUhPx2d/F+GPKECo8otcZlv+VZD7VfV+cEXOo89g== X-Received: by 10.28.27.206 with SMTP id b197mr3845565wmb.96.1512730564794; Fri, 08 Dec 2017 02:56:04 -0800 (PST) Received: from donizetti.lan (dynamic-adsl-78-12-251-125.clienti.tiscali.it. [78.12.251.125]) by smtp.gmail.com with ESMTPSA id v195sm1325461wmf.25.2017.12.08.02.56.03 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Dec 2017 02:56:04 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 8 Dec 2017 11:55:50 +0100 Message-Id: <20171208105553.12249-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171208105553.12249-1-pbonzini@redhat.com> References: <20171208105553.12249-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [PATCH 2/5] lock-guard: add scoped lock implementation 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: "Emilio G . Cota" , Fam Zheng , Stefan Hajnoczi Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Paolo Bonzini --- include/qemu/coroutine.h | 4 ++ include/qemu/lock-guard.h | 99 +++++++++++++++++++++++++++++++++++++++++++++++ include/qemu/thread.h | 7 ++++ util/Makefile.objs | 1 + util/qemu-thread.c | 17 ++++++++ 5 files changed, 128 insertions(+) create mode 100644 include/qemu/lock-guard.h create mode 100644 util/qemu-thread.c diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 9aff9a735e..8b48803fa8 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -17,6 +17,7 @@ #include "qemu/queue.h" #include "qemu/timer.h" +#include "qemu/lock-guard.h" /** * Coroutines are a mechanism for stack switching and can be used for @@ -162,6 +163,9 @@ void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex); */ void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex); +#define QEMU_LOCK_GUARD_FUNCS_CoMutex \ + QEMU_INIT_LOCK_GUARD(CoMutex, qemu_co_mutex_lock, qemu_co_mutex_unlock) + /** * CoQueues are a mechanism to queue coroutines in order to continue executing diff --git a/include/qemu/lock-guard.h b/include/qemu/lock-guard.h new file mode 100644 index 0000000000..e6a83bf9ee --- /dev/null +++ b/include/qemu/lock-guard.h @@ -0,0 +1,103 @@ +#ifndef QEMU_LOCK_GUARD_H +#define QEMU_LOCK_GUARD_H 1 + +typedef void QemuLockGuardFunc(void *); +typedef struct QemuLockGuard { + QemuLockGuardFunc *p_lock_fn, *p_unlock_fn; + void *lock; + int locked; +} QemuLockGuard; + +static inline void qemu_lock_guard_lock(QemuLockGuard *lock_guard) +{ + assert(!lock_guard->locked); + lock_guard->p_lock_fn(lock_guard->lock); + lock_guard->locked = true; +} + +static inline void qemu_lock_guard_unlock(QemuLockGuard *lock_guard) +{ + assert(lock_guard->locked); + lock_guard->locked = false; + lock_guard->p_unlock_fn(lock_guard->lock); +} + +static inline bool qemu_lock_guard_is_taken(QemuLockGuard *lock_guard) +{ + return lock_guard->locked; +} + +static inline void qemu_lock_guard_release(QemuLockGuard *lock_guard) +{ + lock_guard->lock = NULL; + lock_guard->locked = false; +} + +inline void qemu_lock_guard_pass(void *ptr) +{ + QemuLockGuard *lock_guard = ptr; + assert(lock_guard->locked || !lock_guard->lock); +} + +inline void qemu_lock_guard_cleanup(void *ptr) +{ + QemuLockGuard *lock_guard = ptr; + if (likely(lock_guard->locked)) { + lock_guard->p_unlock_fn(lock_guard->lock); + } +} + +static inline QemuLockGuard qemu_lock_guard_init(QemuLockGuard lock_guard) +{ + qemu_lock_guard_lock(&lock_guard); + return lock_guard; +} + +#define QEMU_INIT_LOCK_GUARD(type, lock_fn, unlock_fn) \ + .p_lock_fn = (QemuLockGuardFunc *) (void (*) (type *)) lock_fn, \ + .p_unlock_fn = (QemuLockGuardFunc *) (void (*) (type *)) unlock_fn + +#define QEMU_LOCK_GUARD_(type, lock, locked) \ + (QemuLockGuard) { \ + QEMU_LOCK_GUARD_FUNCS_##type, \ + lock + type_check(typeof(*lock), type), \ + locked \ + } + +/* Take a lock that will be unlocked on returning */ +#define QEMU_LOCK_GUARD(type, name, lock) \ + QemuLockGuard __attribute__((cleanup(qemu_lock_guard_cleanup))) name = \ + qemu_lock_guard_init(QEMU_LOCK_GUARD_(type, lock, false)) + +#define QEMU_WITH_LOCK(type, name, lock) \ + for (QEMU_LOCK_GUARD(type, name, lock); \ + qemu_lock_guard_is_taken(&name); \ + qemu_lock_guard_unlock(&name)) + +/* Create a QemuLockGuard for a lock that is taken and will be unlocked on + * returning + */ +#define QEMU_ADOPT_LOCK(type, name, lock) \ + QemuLockGuard __attribute__((cleanup(qemu_lock_guard_cleanup))) name = \ + QEMU_LOCK_GUARD_(type, lock, true) + +#define QEMU_WITH_ADOPTED_LOCK(type, name, lock) \ + for (QEMU_ADOPT_LOCK(type, name, lock); \ + qemu_lock_guard_is_taken(&name); \ + qemu_lock_guard_unlock(&name)) + +/* Take a lock and create a QemuLockGuard for it, asserting that it will + * be locked when returning. + */ +#define QEMU_RETURN_LOCK(type, name, lock) \ + QemuLockGuard __attribute__((cleanup(qemu_lock_guard_pass))) name = \ + qemu_lock_guard_init(QEMU_LOCK_GUARD_(type, lock, false)) + +/* Create a QemuLockGuard for a lock that is taken and must be locked + * when returning + */ +#define QEMU_TAKEN_LOCK(type, name, lock) \ + QemuLockGuard __attribute__((cleanup(qemu_lock_guard_pass))) name = \ + QEMU_LOCK_GUARD_(type, lock, true) + +#endif diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 9910f49b3a..4066702d0c 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -3,6 +3,7 @@ #include "qemu/processor.h" #include "qemu/atomic.h" +#include "qemu/lock-guard.h" typedef struct QemuMutex QemuMutex; typedef struct QemuCond QemuCond; @@ -26,6 +27,9 @@ void qemu_mutex_lock(QemuMutex *mutex); int qemu_mutex_trylock(QemuMutex *mutex); void qemu_mutex_unlock(QemuMutex *mutex); +#define QEMU_LOCK_GUARD_FUNCS_QemuMutex \ + QEMU_INIT_LOCK_GUARD(QemuMutex, qemu_mutex_lock, qemu_mutex_unlock) + /* Prototypes for other functions are in thread-posix.h/thread-win32.h. */ void qemu_rec_mutex_init(QemuRecMutex *mutex); @@ -99,6 +103,9 @@ static inline void qemu_spin_unlock(QemuSpin *spin) __sync_lock_release(&spin->value); } +#define QEMU_LOCK_GUARD_FUNCS_QemuSpin \ + QEMU_INIT_LOCK_GUARD(QemuSpin, qemu_spin_lock, qemu_spin_lock) + struct QemuLockCnt { #ifndef CONFIG_LINUX QemuMutex mutex; diff --git a/util/Makefile.objs b/util/Makefile.objs index 2973b0a323..cb0591f750 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -3,6 +3,7 @@ util-obj-y += bufferiszero.o util-obj-y += lockcnt.o util-obj-y += aiocb.o async.o thread-pool.o qemu-timer.o util-obj-y += main-loop.o iohandler.o +util-obj-y += qemu-thread.o util-obj-$(CONFIG_POSIX) += aio-posix.o util-obj-$(CONFIG_POSIX) += compatfd.o util-obj-$(CONFIG_POSIX) += event_notifier-posix.o diff --git a/util/qemu-thread.c b/util/qemu-thread.c new file mode 100644 index 0000000000..bb06b5f58b --- /dev/null +++ b/util/qemu-thread.c @@ -0,0 +1,17 @@ +/* + * Extern inline functions for QemuLockGuard. + * + * Copyright Red Hat, Inc. 2017 + * + * Author: + * Paolo Bonzini + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ +#include "qemu/osdep.h" +#include "qemu/thread.h" + +QEMU_EXTERN_INLINE(qemu_lock_guard_cleanup) +QEMU_EXTERN_INLINE(qemu_lock_guard_pass) From patchwork Fri Dec 8 10:55:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 846158 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Se4j+kEJ"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3ytTqW5Hywz9s83 for ; Fri, 8 Dec 2017 21:58:47 +1100 (AEDT) Received: from localhost ([::1]:36553 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGMf-0002xb-Sx for incoming@patchwork.ozlabs.org; Fri, 08 Dec 2017 05:58:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47577) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGK8-00011d-Mx for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eNGK7-0001mU-PY for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:08 -0500 Received: from mail-wr0-x244.google.com ([2a00:1450:400c:c0c::244]:42112) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eNGK7-0001mK-FS for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:07 -0500 Received: by mail-wr0-x244.google.com with SMTP id s66so10421640wrc.9 for ; Fri, 08 Dec 2017 02:56:07 -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=3IouiZhdTWBaQFl9Fga5R94i8paWQals15TPfKCUiaA=; b=Se4j+kEJuGEIrtH5QIRn3FaCh0KB1/A13SVhfaAfnzZ62S/EaNMeU2EOsKYZug/qmz HgoW7jybxCxgw5eZ7PbBuox5+jT4spfeaONFJhrK+72eMmLrBUR+qfgdDDuKCwHOVR3N 0JkuV6yJy0HGrp9AXPB9QUcIxn3YARRJsYaazu4S2p9RBR/zegIx8cNNoKP8Z5RW3aXD KopuKh/ZAGy8ZF8ynyfM3+FAyUCBz8EI8kpeYnDCn2i4uG6TCeflyCm7VIL3hzFvphq2 jTQzpCKme9x7BSBSaTa++3MlusABNgt+9TX7mxhF2C++KU2Gq0W8g/B02FGM/cxvtfG8 UiDw== 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=3IouiZhdTWBaQFl9Fga5R94i8paWQals15TPfKCUiaA=; b=Ga9puPZ+O2tfsSHQl1hPXJ0UGYkdZtGaRMmD2Qru1uZ0n/+Eyhv4Tbm8IiR2P0e3Z/ +ZvF9Uqbs2d5Z1XVo/kFjKP5rDuAHwfQUdZ5bWmvA2AY8QtXO5z8Q93wuZ9bWWAzN9q8 8nip19GhDFN/j/rtWASqJDIFNqk0MExEkh4XbIsSXA/AwpyEkOTJhqQHV4/m796Q8IUB e3jlhT1/SYZmYRPN4qKtI6H8JwfZK5HOCnfglEhkAq4GAVUlP6SLTXNpLYx/nyrgfzlW eUb98O8Gs7Pm5GxYvMtuUzm2yX9zTpNDOqE6ttELPUUb1eEo5V1abtEfGEAZPDaCFwyW 8rRw== X-Gm-Message-State: AJaThX48Rvv6ELgWZRPeIB8lOXE8PnvPdeQqmvLcf24JB8dyqOqVIDEN kIvRRpz8R66O04Hmao8waNXN3JmQ X-Google-Smtp-Source: AGs4zMY64vyYNsTAVQiTgQqtWhdvLHrlnRGkJR4J4ws9CxGQQLZvGFEdIvGZqD2qLTtGykEWjz2cUg== X-Received: by 10.223.135.169 with SMTP id b38mr25751209wrb.278.1512730565958; Fri, 08 Dec 2017 02:56:05 -0800 (PST) Received: from donizetti.lan (dynamic-adsl-78-12-251-125.clienti.tiscali.it. [78.12.251.125]) by smtp.gmail.com with ESMTPSA id v195sm1325461wmf.25.2017.12.08.02.56.04 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Dec 2017 02:56:05 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 8 Dec 2017 11:55:51 +0100 Message-Id: <20171208105553.12249-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171208105553.12249-1-pbonzini@redhat.com> References: <20171208105553.12249-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::244 Subject: [Qemu-devel] [PATCH 3/5] qemu-timer: convert to use lock guards 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: "Emilio G . Cota" , Fam Zheng , Stefan Hajnoczi Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Paolo Bonzini --- util/qemu-timer.c | 84 +++++++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/util/qemu-timer.c b/util/qemu-timer.c index 82d56507a2..7a99e0e336 100644 --- a/util/qemu-timer.c +++ b/util/qemu-timer.c @@ -192,14 +192,11 @@ bool timerlist_expired(QEMUTimerList *timer_list) return false; } - qemu_mutex_lock(&timer_list->active_timers_lock); + QEMU_LOCK_GUARD(QemuMutex, timers_guard, &timer_list->active_timers_lock); if (!timer_list->active_timers) { - qemu_mutex_unlock(&timer_list->active_timers_lock); return false; } expire_time = timer_list->active_timers->expire_time; - qemu_mutex_unlock(&timer_list->active_timers_lock); - return expire_time <= qemu_clock_get_ns(timer_list->clock->type); } @@ -231,13 +228,13 @@ int64_t timerlist_deadline_ns(QEMUTimerList *timer_list) * value but ->notify_cb() is called when the deadline changes. Therefore * the caller should notice the change and there is no race condition. */ - qemu_mutex_lock(&timer_list->active_timers_lock); - if (!timer_list->active_timers) { - qemu_mutex_unlock(&timer_list->active_timers_lock); - return -1; + QEMU_WITH_LOCK(QemuMutex, timers_guard, + &timer_list->active_timers_lock) { + if (!timer_list->active_timers) { + return -1; + } + expire_time = timer_list->active_timers->expire_time; } - expire_time = timer_list->active_timers->expire_time; - qemu_mutex_unlock(&timer_list->active_timers_lock); delta = expire_time - qemu_clock_get_ns(timer_list->clock->type); @@ -410,9 +407,8 @@ void timer_del(QEMUTimer *ts) QEMUTimerList *timer_list = ts->timer_list; if (timer_list) { - qemu_mutex_lock(&timer_list->active_timers_lock); + QEMU_LOCK_GUARD(QemuMutex, timers_guard, &timer_list->active_timers_lock); timer_del_locked(timer_list, ts); - qemu_mutex_unlock(&timer_list->active_timers_lock); } } @@ -423,10 +419,11 @@ void timer_mod_ns(QEMUTimer *ts, int64_t expire_time) QEMUTimerList *timer_list = ts->timer_list; bool rearm; - qemu_mutex_lock(&timer_list->active_timers_lock); - timer_del_locked(timer_list, ts); - rearm = timer_mod_ns_locked(timer_list, ts, expire_time); - qemu_mutex_unlock(&timer_list->active_timers_lock); + QEMU_WITH_LOCK(QemuMutex, timers_guard, + &timer_list->active_timers_lock) { + timer_del_locked(timer_list, ts); + rearm = timer_mod_ns_locked(timer_list, ts, expire_time); + } if (rearm) { timerlist_rearm(timer_list); @@ -441,16 +438,17 @@ void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time) QEMUTimerList *timer_list = ts->timer_list; bool rearm; - qemu_mutex_lock(&timer_list->active_timers_lock); - if (ts->expire_time == -1 || ts->expire_time > expire_time) { - if (ts->expire_time != -1) { - timer_del_locked(timer_list, ts); + QEMU_WITH_LOCK(QemuMutex, timers_guard, + &timer_list->active_timers_lock) { + if (ts->expire_time == -1 || ts->expire_time > expire_time) { + if (ts->expire_time != -1) { + timer_del_locked(timer_list, ts); + } + rearm = timer_mod_ns_locked(timer_list, ts, expire_time); + } else { + rearm = false; } - rearm = timer_mod_ns_locked(timer_list, ts, expire_time); - } else { - rearm = false; } - qemu_mutex_unlock(&timer_list->active_timers_lock); if (rearm) { timerlist_rearm(timer_list); @@ -516,25 +514,27 @@ bool timerlist_run_timers(QEMUTimerList *timer_list) } current_time = qemu_clock_get_ns(timer_list->clock->type); - for(;;) { - qemu_mutex_lock(&timer_list->active_timers_lock); - ts = timer_list->active_timers; - if (!timer_expired_ns(ts, current_time)) { - qemu_mutex_unlock(&timer_list->active_timers_lock); - break; - } + QEMU_WITH_LOCK(QemuMutex, timers_guard, + &timer_list->active_timers_lock) { + for(;;) { + ts = timer_list->active_timers; + if (!timer_expired_ns(ts, current_time)) { + break; + } - /* remove timer from the list before calling the callback */ - timer_list->active_timers = ts->next; - ts->next = NULL; - ts->expire_time = -1; - cb = ts->cb; - opaque = ts->opaque; - qemu_mutex_unlock(&timer_list->active_timers_lock); - - /* run the callback (the timer list can be modified) */ - cb(opaque); - progress = true; + /* remove timer from the list before calling the callback */ + timer_list->active_timers = ts->next; + ts->next = NULL; + ts->expire_time = -1; + cb = ts->cb; + opaque = ts->opaque; + + /* run the callback (the timer list can be modified) */ + qemu_lock_guard_unlock(&timers_guard); + cb(opaque); + progress = true; + qemu_lock_guard_lock(&timers_guard); + } } out: From patchwork Fri Dec 8 10:55:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 846160 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="phvwiXlX"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3ytTsQ1592z9s74 for ; Fri, 8 Dec 2017 22:00:23 +1100 (AEDT) Received: from localhost ([::1]:36561 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGOB-0004GE-Iy for incoming@patchwork.ozlabs.org; Fri, 08 Dec 2017 06:00:19 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47596) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGKB-00013e-3S for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eNGKA-0001mz-7s for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:11 -0500 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]:42111) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eNGKA-0001mp-13 for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:10 -0500 Received: by mail-wr0-x242.google.com with SMTP id s66so10421769wrc.9 for ; Fri, 08 Dec 2017 02:56:09 -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=46WCvDhoc/xPyF4BVZJI0K+Iu2c7T1UvQgq6JTDQsq8=; b=phvwiXlX8FFyR8HBgvdkoauqU91gI4u1VyB3NL0w9weT1ycB1fgxSRhNag7TAgj293 na3flCYsGp0keOLqWouFNC+SRU5NHN1n7bJChWgN6yKl/2v8AhGY2bmnSy+0Gi/U33r5 t3kC8+QIxY+gIu+CJIEAHtG8I0EROc4ofDxdVqNOoT1LMYdD7hww4K9Uy9NKes+YoWpX xrMLrxIQPU+UrX1JL0CRk5eiJqm4jj+e9RR+k6tkGorvgRSxBtac92wMKPBaZaUqxKYI sKADZ1Kj8JEvKbgcR6+gWV1YcTsD6Z51mjp+Gxf5Tv9VuV8dKIpEBULhs/FL8aDhfo2U rTWg== 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=46WCvDhoc/xPyF4BVZJI0K+Iu2c7T1UvQgq6JTDQsq8=; b=DkBsGdiwdhJLCiaS/UMuLUsUtM4swBy8JP6xRXfJtHsy4G0lohHDX0DqLfkQAw+SfK 11EYc88lgoQR8g4vXnshlpeGOQgMxO2Jypg1Sv3mDEkdA1pkdyQOafo8l5OiBzzmqnqx L3339mvhR2HPV41BTPifwsDGnUxTmfK7PUyFkmVMaqxscWoLZl7Ch43P390u1DJmHH/P /xGitGJ1jgNC3qWsTDXwtpHVT726lJgxFFFr1u1dpKszjp+tZanrxPXxzM3i4ZvLWRN3 Yhe1ZzfBt5Eu1AZlG+vsKoSoW3zbRrlnh+Py1zBkZQiwzgz25w+67XJUCt8aP01YYJzj lTVw== X-Gm-Message-State: AJaThX6w1mkZhH/SZfUK4B6b9/FWj1XqVBXAFu3I9Z7kbr+QTFL5Hfrs Tx0yU8IMN8dlyiyGejKL+T1ru/5x X-Google-Smtp-Source: AGs4zMZ8rY+pm+ei4ALPV+m4JOqBwaFejmqfBKljLN3Ld181s5SiKue060kO/yMqvYrALcd9nzqsdQ== X-Received: by 10.223.155.131 with SMTP id d3mr28657408wrc.29.1512730568577; Fri, 08 Dec 2017 02:56:08 -0800 (PST) Received: from donizetti.lan (dynamic-adsl-78-12-251-125.clienti.tiscali.it. [78.12.251.125]) by smtp.gmail.com with ESMTPSA id v195sm1325461wmf.25.2017.12.08.02.56.05 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Dec 2017 02:56:06 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 8 Dec 2017 11:55:52 +0100 Message-Id: <20171208105553.12249-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171208105553.12249-1-pbonzini@redhat.com> References: <20171208105553.12249-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 4/5] qht: convert to use lock guards 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: "Emilio G . Cota" , Fam Zheng , Stefan Hajnoczi Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Paolo Bonzini Reviewed-by: Stefan Hajnoczi --- util/qht.c | 59 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/util/qht.c b/util/qht.c index ff4d2e6974..95ac32f990 100644 --- a/util/qht.c +++ b/util/qht.c @@ -248,18 +248,18 @@ void qht_map_lock_buckets__no_stale(struct qht *ht, struct qht_map **pmap) map = atomic_rcu_read(&ht->map); qht_map_lock_buckets(map); if (likely(!qht_map_is_stale__locked(ht, map))) { - *pmap = map; - return; + goto out; } qht_map_unlock_buckets(map); /* we raced with a resize; acquire ht->lock to see the updated ht->map */ - qemu_mutex_lock(&ht->lock); - map = ht->map; - qht_map_lock_buckets(map); - qemu_mutex_unlock(&ht->lock); + QEMU_WITH_LOCK(QemuMutex, ht_guard, &ht->lock) { + map = ht->map; + qht_map_lock_buckets(map); + } + +out: *pmap = map; - return; } /* @@ -282,17 +282,18 @@ struct qht_bucket *qht_bucket_lock__no_stale(struct qht *ht, uint32_t hash, qemu_spin_lock(&b->lock); if (likely(!qht_map_is_stale__locked(ht, map))) { - *pmap = map; - return b; + goto out; } qemu_spin_unlock(&b->lock); /* we raced with a resize; acquire ht->lock to see the updated ht->map */ - qemu_mutex_lock(&ht->lock); - map = ht->map; - b = qht_map_to_bucket(map, hash); - qemu_spin_lock(&b->lock); - qemu_mutex_unlock(&ht->lock); + QEMU_WITH_LOCK(QemuMutex, ht_guard, &ht->lock) { + map = ht->map; + b = qht_map_to_bucket(map, hash); + qemu_spin_lock(&b->lock); + } + +out: *pmap = map; return b; } @@ -427,13 +428,13 @@ bool qht_reset_size(struct qht *ht, size_t n_elems) n_buckets = qht_elems_to_buckets(n_elems); - qemu_mutex_lock(&ht->lock); - map = ht->map; - if (n_buckets != map->n_buckets) { - new = qht_map_create(n_buckets); + QEMU_WITH_LOCK(QemuMutex, ht_guard, &ht->lock) { + map = ht->map; + if (n_buckets != map->n_buckets) { + new = qht_map_create(n_buckets); + } + qht_do_resize_and_reset(ht, new); } - qht_do_resize_and_reset(ht, new); - qemu_mutex_unlock(&ht->lock); return !!new; } @@ -771,19 +772,17 @@ static void qht_do_resize_reset(struct qht *ht, struct qht_map *new, bool reset) bool qht_resize(struct qht *ht, size_t n_elems) { size_t n_buckets = qht_elems_to_buckets(n_elems); - size_t ret = false; - qemu_mutex_lock(&ht->lock); - if (n_buckets != ht->map->n_buckets) { - struct qht_map *new; + QEMU_WITH_LOCK(QemuMutex, ht_guard, &ht->lock) { + if (n_buckets != ht->map->n_buckets) { + struct qht_map *new; - new = qht_map_create(n_buckets); - qht_do_resize(ht, new); - ret = true; + new = qht_map_create(n_buckets); + qht_do_resize(ht, new); + return true; + } } - qemu_mutex_unlock(&ht->lock); - - return ret; + return false; } /* pass @stats to qht_statistics_destroy() when done */ From patchwork Fri Dec 8 10:55:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 846154 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Izdm7dR+"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3ytTnL73l8z9s83 for ; Fri, 8 Dec 2017 21:56:54 +1100 (AEDT) Received: from localhost ([::1]:36547 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGKr-000190-4D for incoming@patchwork.ozlabs.org; Fri, 08 Dec 2017 05:56:53 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47610) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGKC-000153-KT for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eNGKB-0001nM-NI for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:12 -0500 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:41262) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eNGKB-0001n9-Gg for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:11 -0500 Received: by mail-wm0-x241.google.com with SMTP id g75so2682490wme.0 for ; Fri, 08 Dec 2017 02:56:11 -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=xj4fH80eRXkRf4FC9SRWLTcUIDogFFhHVxxWPTBKkYk=; b=Izdm7dR+AIVCHLEAFqps6aXZZxRZ/5gfkigzKwIeKE23/eFD+h1gCKxG7r70I5zoyk ++xBUJw0Rp60fUaNLrmdLxjwHYEGwlL0Xqq5GoA1f3rHyPaO3h89EZKoJN842+IfrDbs IHmUaDne/2WpTIe8JQ/DccAVZ1Tmn253prOtHPRXbkQ+kCYs47zpqAS4lrbgSI9i4+6r Uq0tekJrPwgagumgY6zUmjiBo+OoXCzpvfiGcWY7m6UZtMhd7VbIngG/WdlqejNPCr4G AjERWkl8eDUKZOqApVsk53AVDbaXiqzDEI+WUoTJg+QGi8h0XXv7Iuk3SjYe0MdDBp52 S2IA== 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=xj4fH80eRXkRf4FC9SRWLTcUIDogFFhHVxxWPTBKkYk=; b=rQEMnY1LqdfJ6N9z5AE4HaQl3qYjT9AeMDvP5s7ZYoO+/B5Sbu8VFp/VEtAKfogxZY RzBf+94yLPkrYADvL+ZUB+cnaLx0XzXf5yCcVJH/3Ed/LT0rSkFbjAIY7ksWrU6hWjI6 pDga7Xq53bcLoFtYg6M5wEJg0Pl2d+IHSiSRinaaplmhZ8iB70XbxbVBQFqVOwGraV06 p6fQOXPojKAR1UpYV/35bObhK0DGwsnMvzb9zzc/T7mKmcNfYZPliVSwKM+zO4uGx2LF TGtdFJNpFlc+AwBDXm/EhbzPkJejUWX4FxAPRERt4oeIofZ1SoB3AtxyoxAeuX1lvSze Mf0A== X-Gm-Message-State: AKGB3mI9iCXV+CdJ+dUB/vMHR6L3BaRZ/C1EXtGusTDa6t/biUslldBf j38mdLXM/Hu8CLYzgj2PL3k/9HSB X-Google-Smtp-Source: AGs4zMbGdP4vrti780Scast84S9YqjHLeUfVleyn7QsPKnY6VO6QdAmqrROh7jJ20Ri9PaKCjmQc9g== X-Received: by 10.28.174.20 with SMTP id x20mr3822415wme.27.1512730570032; Fri, 08 Dec 2017 02:56:10 -0800 (PST) Received: from donizetti.lan (dynamic-adsl-78-12-251-125.clienti.tiscali.it. [78.12.251.125]) by smtp.gmail.com with ESMTPSA id v195sm1325461wmf.25.2017.12.08.02.56.08 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Dec 2017 02:56:09 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 8 Dec 2017 11:55:53 +0100 Message-Id: <20171208105553.12249-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171208105553.12249-1-pbonzini@redhat.com> References: <20171208105553.12249-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::241 Subject: [Qemu-devel] [PATCH 5/5] thread-pool: convert to use lock guards 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: "Emilio G . Cota" , Fam Zheng , Stefan Hajnoczi Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Paolo Bonzini --- util/thread-pool.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/util/thread-pool.c b/util/thread-pool.c index 610646d131..06ada38376 100644 --- a/util/thread-pool.c +++ b/util/thread-pool.c @@ -78,7 +78,7 @@ static void *worker_thread(void *opaque) { ThreadPool *pool = opaque; - qemu_mutex_lock(&pool->lock); + QEMU_LOCK_GUARD(QemuMutex, pool_guard, &pool->lock); pool->pending_threads--; do_spawn_thread(pool); @@ -88,9 +88,9 @@ static void *worker_thread(void *opaque) do { pool->idle_threads++; - qemu_mutex_unlock(&pool->lock); + qemu_lock_guard_unlock(&pool_guard); ret = qemu_sem_timedwait(&pool->sem, 10000); - qemu_mutex_lock(&pool->lock); + qemu_lock_guard_lock(&pool_guard); pool->idle_threads--; } while (ret == -1 && !QTAILQ_EMPTY(&pool->request_list)); if (ret == -1 || pool->stopping) { @@ -100,7 +100,7 @@ static void *worker_thread(void *opaque) req = QTAILQ_FIRST(&pool->request_list); QTAILQ_REMOVE(&pool->request_list, req, reqs); req->state = THREAD_ACTIVE; - qemu_mutex_unlock(&pool->lock); + qemu_lock_guard_unlock(&pool_guard); ret = req->func(req->arg); @@ -109,14 +109,13 @@ static void *worker_thread(void *opaque) smp_wmb(); req->state = THREAD_DONE; - qemu_mutex_lock(&pool->lock); + qemu_lock_guard_lock(&pool_guard); qemu_bh_schedule(pool->completion_bh); } pool->cur_threads--; qemu_cond_signal(&pool->worker_stopped); - qemu_mutex_unlock(&pool->lock); return NULL; } @@ -139,9 +138,8 @@ static void spawn_thread_bh_fn(void *opaque) { ThreadPool *pool = opaque; - qemu_mutex_lock(&pool->lock); + QEMU_LOCK_GUARD(QemuMutex, pool_guard, &pool->lock); do_spawn_thread(pool); - qemu_mutex_unlock(&pool->lock); } static void spawn_thread(ThreadPool *pool) @@ -208,10 +206,10 @@ static void thread_pool_cancel(BlockAIOCB *acb) { ThreadPoolElement *elem = (ThreadPoolElement *)acb; ThreadPool *pool = elem->pool; + QEMU_LOCK_GUARD(QemuMutex, pool_guard, &pool->lock); trace_thread_pool_cancel(elem, elem->common.opaque); - qemu_mutex_lock(&pool->lock); if (elem->state == THREAD_QUEUED && /* No thread has yet started working on elem. we can try to "steal" * the item from the worker if we can get a signal from the @@ -225,8 +223,6 @@ static void thread_pool_cancel(BlockAIOCB *acb) elem->state = THREAD_DONE; elem->ret = -ECANCELED; } - - qemu_mutex_unlock(&pool->lock); } static AioContext *thread_pool_get_aio_context(BlockAIOCB *acb) @@ -258,12 +254,12 @@ BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool, trace_thread_pool_submit(pool, req, arg); - qemu_mutex_lock(&pool->lock); + QEMU_LOCK_GUARD(QemuMutex, pool_guard, &pool->lock); if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) { spawn_thread(pool); } QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs); - qemu_mutex_unlock(&pool->lock); + qemu_lock_guard_unlock(&pool_guard); qemu_sem_post(&pool->sem); return &req->common; } @@ -330,7 +326,7 @@ void thread_pool_free(ThreadPool *pool) assert(QLIST_EMPTY(&pool->head)); - qemu_mutex_lock(&pool->lock); + QEMU_LOCK_GUARD(QemuMutex, pool_guard, &pool->lock); /* Stop new threads from spawning */ qemu_bh_delete(pool->new_thread_bh); @@ -344,7 +340,7 @@ void thread_pool_free(ThreadPool *pool) qemu_cond_wait(&pool->worker_stopped, &pool->lock); } - qemu_mutex_unlock(&pool->lock); + qemu_lock_guard_unlock(&pool_guard); qemu_bh_delete(pool->completion_bh); qemu_sem_destroy(&pool->sem);