From patchwork Mon Oct 17 13:54:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 683016 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 3syL9410p6z9snm for ; Tue, 18 Oct 2016 01:26:00 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=SyHFA+Ps; dkim-atps=neutral Received: from localhost ([::1]:33445 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bw8rU-0004EA-W2 for incoming@patchwork.ozlabs.org; Mon, 17 Oct 2016 10:25:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45740) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bw8OH-0003eH-IO for qemu-devel@nongnu.org; Mon, 17 Oct 2016 09:55:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bw8OG-0005TZ-K6 for qemu-devel@nongnu.org; Mon, 17 Oct 2016 09:55:45 -0400 Received: from mail-lf0-x241.google.com ([2a00:1450:4010:c07::241]:36432) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1bw8OG-0005TL-Bh; Mon, 17 Oct 2016 09:55:44 -0400 Received: by mail-lf0-x241.google.com with SMTP id b75so26673112lfg.3; Mon, 17 Oct 2016 06:55:44 -0700 (PDT) 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=vQqB2Iv0fyahnuI+VCW13Y/iDPddy1+WbbzGM9P8qic=; b=SyHFA+PstLyAyE0/RGC68i9waFaEjJFnwjCr3nnlHIojdtxV5ne3VWW4tmkuQ892B4 nNHu2+Ov+bAxY6fEK/8W8/3v2OZMQj1bSm7sj7f5/KA8XxgVbCryro8abh9+9vEGPC2A sm8TQ5VAkapZWfLuSa7kCUBUZIaTzTWQzG3eUapquMNsM/4EvKJSvdgOdEJ/tdyDBVP+ rfLK32JaUu8lEs+kQeY1Y47mEq94MTHIbar/MBj0W7P2VQ/4b1A2hQqcLgHrj1fqLYMR I7CGRswL8gvdINf4GN4oB3JQaFpDvElIvALfWwua3zW2G2AYo24hycCUwQuCjSNSzZ0f V0gQ== 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=vQqB2Iv0fyahnuI+VCW13Y/iDPddy1+WbbzGM9P8qic=; b=SK5Zuugw8PFDfxGxHPPseB6vHwMR/VzxFPFVmkfAkdBat+/1lOHjgiLlqiPvl1TLLV w/TLB6m9j7Rv2evvN+0J7CK6bnfddOov/YoaHY8Xh3NApAz1QiczlFiabT69WsTqiElp gEBbMLM7TePen+9KOuYy2XxsNVYKi/Pkt3+y5Kg3K/Y3NSwr+xuwpQFAipx67q1ZBIU1 vZoQy0IBtWHVMzig4tVeExVdVgi3afDN0JATAy9qYcAdEKUuNDQt5g0nXUNgdWNiRVtM B9A260yQgTibBW49pqQMj16MT9P8HebiOZzi5kfNE3lxogEIIjoCDxiz4KFS1v/c3sD4 i8eQ== X-Gm-Message-State: AA6/9RnIu3f2R5YjMoA245xPyprrBuYAwODDP2TkDPEbXMh5qZT8jjGiOhzyBuduPyT3uQ== X-Received: by 10.194.170.163 with SMTP id an3mr11851331wjc.73.1476712542722; Mon, 17 Oct 2016 06:55:42 -0700 (PDT) Received: from donizetti.lan (94-39-170-54.adsl-ull.clienti.tiscali.it. [94.39.170.54]) by smtp.gmail.com with ESMTPSA id af4sm43814062wjc.17.2016.10.17.06.55.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Oct 2016 06:55:42 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 17 Oct 2016 15:54:29 +0200 Message-Id: <1476712470-11660-20-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1476712470-11660-1-git-send-email-pbonzini@redhat.com> References: <1476712470-11660-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:4010:c07::241 Subject: [Qemu-devel] [PATCH 19/20] qemu-thread: introduce QemuRecMutex 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: kwolf@redhat.com, famz@redhat.com, stefanha@redhat.com, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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. Reviewed-by: Fam Zheng Reviewed-by: Stefan Hajnoczi 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 | 14 ++++++++++++++ util/qemu-thread-win32.c | 25 +++++++++++++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/include/qemu/thread-posix.h b/include/qemu/thread-posix.h index aa03567..09d1e15 100644 --- a/include/qemu/thread-posix.h +++ b/include/qemu/thread-posix.h @@ -4,6 +4,12 @@ #include #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 c7ce8dc..5fb6541 100644 --- a/include/qemu/thread-win32.h +++ b/include/qemu/thread-win32.h @@ -8,6 +8,16 @@ struct QemuMutex { LONG owner; }; +typedef struct QemuRecMutex QemuRecMutex; +struct QemuRecMutex { + CRITICAL_SECTION lock; +}; + +void qemu_rec_mutex_destroy(QemuRecMutex *mutex); +void qemu_rec_mutex_lock(QemuRecMutex *mutex); +int qemu_rec_mutex_trylock(QemuRecMutex *mutex); +void qemu_rec_mutex_unlock(QemuRecMutex *mutex); + struct QemuCond { LONG waiters, target; HANDLE sema; diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 31237e9..e8e665f 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..1bb00a8 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -80,6 +80,20 @@ 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); + pthread_mutexattr_destroy(&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));