From patchwork Mon Feb 8 16:14:57 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 8251761 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id DB234BEEE5 for ; Mon, 8 Feb 2016 16:15:59 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2A204203B7 for ; Mon, 8 Feb 2016 16:15:59 +0000 (UTC) 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.kernel.org (Postfix) with ESMTPS id 6701020148 for ; Mon, 8 Feb 2016 16:15:58 +0000 (UTC) Received: from localhost ([::1]:46194 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSoTl-0006D6-Oz for patchwork-qemu-devel@patchwork.kernel.org; Mon, 08 Feb 2016 11:15:57 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57187) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSoTF-00067u-Qx for qemu-devel@nongnu.org; Mon, 08 Feb 2016 11:15:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aSoTE-0002QK-Qe for qemu-devel@nongnu.org; Mon, 08 Feb 2016 11:15:25 -0500 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]:34755) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSoTE-0002QB-8r for qemu-devel@nongnu.org; Mon, 08 Feb 2016 11:15:24 -0500 Received: by mail-wm0-x244.google.com with SMTP id p63so16174541wmp.1 for ; Mon, 08 Feb 2016 08:15:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=rRAAd1jlc2AYRBOOcAHeVRBn645lPGJrL48167ppJFI=; b=R5fGp/rUQ/wPQSlgu6mqlLCzSaZKqFBqH6zbh+CDsZPYli+hTMjON55i2iSuXeiJBY 08tZsszh0T3Kze+62xSwwz9ahy73icw6WL9yn0N+8oDvBimybC4ZohzzNfhuHqXw2Vx4 sGBdWM97sIavD40Mqm0Xw7kZb1keR+hRo2Tq+8iHhgy7wnSYc5aSRWAGwr2Y6SIP2GJV gg+JlTPsHT+T/zTZ81jfQcs676OsD/iBQmmbYtAEl8hzbNdkFOmAMQOh2IX104uStLme BTScsifPYQNkzQTSeY5iTkHb/6rRtkQZB8SWUfeQz6GWWQugOjVRRDH527tYzOBofi+v qraw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=rRAAd1jlc2AYRBOOcAHeVRBn645lPGJrL48167ppJFI=; b=lxe+Z8Shitt2aOo4ULQZukxnmnQNUGFr4D14rCnBstsU9I1MLfqTe4LE5wx3kPRP4t akCjHTM5yksxs9FRC+fXbZ4fkdEbjikGKusM8oUyZA3tndf/8p+GPPGrumb0ucSVJign 7cjGy+rRQojOLM6MYnLVUnTPWCbhxxcFTJujQkAs+YvQyvM55WBqJpMu8p7W79IaVaIp GcOl1EIbiGhl5UWM/JRgo5lUPlu1dOal8ln+ksTuhhfGSyl7NmqRA+QzkBrAtWoe6VGU LHuJBhk5X1bV0PT3U4OyGKAdxL9bzfzfatovarlc7AP8usIC6swHcAHPkor4RKPKePu5 MCbA== X-Gm-Message-State: AG10YORC1HPSSPkhlUoqCor9pF+1yzW/pGLqBHyKWm2sKbIi8wQhudgH91OQuNoSugYIow== X-Received: by 10.28.173.208 with SMTP id w199mr29601149wme.45.1454948123810; Mon, 08 Feb 2016 08:15:23 -0800 (PST) Received: from donizetti.lan (94-39-141-130.adsl-ull.clienti.tiscali.it. [94.39.141.130]) by smtp.gmail.com with ESMTPSA id v2sm13305429wmd.24.2016.02.08.08.15.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Feb 2016 08:15:23 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 8 Feb 2016 17:14:57 +0100 Message-Id: <1454948107-11844-7-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1454948107-11844-1-git-send-email-pbonzini@redhat.com> References: <1454948107-11844-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::244 Cc: stefanha@redhat.com Subject: [Qemu-devel] [PATCH 06/16] qemu-thread: introduce QemuRecMutex X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP GRecMutex is new in glib 2.32, so we cannot use it. Introduce a recursive mutex in qemu-thread instead, which will be used instead of RFifoLock. Signed-off-by: Paolo Bonzini --- include/qemu/thread-posix.h | 6 ++++++ include/qemu/thread-win32.h | 10 ++++++++++ include/qemu/thread.h | 3 +++ util/qemu-thread-posix.c | 13 +++++++++++++ util/qemu-thread-win32.c | 25 +++++++++++++++++++++++++ 5 files changed, 57 insertions(+) diff --git a/include/qemu/thread-posix.h b/include/qemu/thread-posix.h index eb5c7a1..2fb6b90 100644 --- a/include/qemu/thread-posix.h +++ b/include/qemu/thread-posix.h @@ -3,6 +3,12 @@ #include "pthread.h" #include +typedef QemuMutex QemuRecMutex; +#define qemu_rec_mutex_destroy qemu_mutex_destroy +#define qemu_rec_mutex_lock qemu_mutex_lock +#define qemu_rec_mutex_try_lock qemu_mutex_try_lock +#define qemu_rec_mutex_unlock qemu_mutex_unlock + struct QemuMutex { pthread_mutex_t lock; }; diff --git a/include/qemu/thread-win32.h b/include/qemu/thread-win32.h index 385ff5f..50999c5 100644 --- a/include/qemu/thread-win32.h +++ b/include/qemu/thread-win32.h @@ -7,6 +7,16 @@ struct QemuMutex { LONG owner; }; +typedef struct QemuRecMutex QemuRecMutex; +struct QemuRecMutex { + CRITICAL_SECTION lock; +}; + +void qemu_rec_mutex_destroy(QemuMutex *mutex); +void qemu_rec_mutex_lock(QemuMutex *mutex); +int qemu_rec_mutex_trylock(QemuMutex *mutex); +void qemu_rec_mutex_unlock(QemuMutex *mutex); + struct QemuCond { LONG waiters, target; HANDLE sema; diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 5114ec8..981f3dc 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -25,6 +25,9 @@ void qemu_mutex_lock(QemuMutex *mutex); int qemu_mutex_trylock(QemuMutex *mutex); void qemu_mutex_unlock(QemuMutex *mutex); +/* Prototypes for other functions are in thread-posix.h/thread-win32.h. */ +void qemu_rec_mutex_init(QemuRecMutex *mutex); + void qemu_cond_init(QemuCond *cond); void qemu_cond_destroy(QemuCond *cond); diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 74a3023..1aec83f 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -80,6 +80,19 @@ void qemu_mutex_unlock(QemuMutex *mutex) error_exit(err, __func__); } +void qemu_rec_mutex_init(QemuRecMutex *mutex) +{ + int err; + pthread_mutexattr_t attr; + + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + err = pthread_mutex_init(&mutex->lock, &attr); + if (err) { + error_exit(err, __func__); + } +} + void qemu_cond_init(QemuCond *cond) { int err; diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c index 98a5ddf..171d83c 100644 --- a/util/qemu-thread-win32.c +++ b/util/qemu-thread-win32.c @@ -79,6 +79,31 @@ void qemu_mutex_unlock(QemuMutex *mutex) LeaveCriticalSection(&mutex->lock); } +void qemu_rec_mutex_init(QemuRecMutex *mutex) +{ + InitializeCriticalSection(&mutex->lock); +} + +void qemu_rec_mutex_destroy(QemuRecMutex *mutex) +{ + DeleteCriticalSection(&mutex->lock); +} + +void qemu_rec_mutex_lock(QemuRecMutex *mutex) +{ + EnterCriticalSection(&mutex->lock); +} + +int qemu_rec_mutex_trylock(QemuRecMutex *mutex) +{ + return !TryEnterCriticalSection(&mutex->lock); +} + +void qemu_rec_mutex_unlock(QemuRecMutex *mutex) +{ + LeaveCriticalSection(&mutex->lock); +} + void qemu_cond_init(QemuCond *cond) { memset(cond, 0, sizeof(*cond));