From patchwork Tue Apr 5 05:30:48 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emilio Cota X-Patchwork-Id: 8747711 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 973E8C0553 for ; Tue, 5 Apr 2016 05:34:31 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DEC67202EC for ; Tue, 5 Apr 2016 05:34:30 +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 265AD2022A for ; Tue, 5 Apr 2016 05:34:30 +0000 (UTC) Received: from localhost ([::1]:34497 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1anJdF-00086d-Jc for patchwork-qemu-devel@patchwork.kernel.org; Tue, 05 Apr 2016 01:34:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51879) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1anJa1-0001eF-6o for qemu-devel@nongnu.org; Tue, 05 Apr 2016 01:31:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1anJZy-0005TM-3n for qemu-devel@nongnu.org; Tue, 05 Apr 2016 01:31:09 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:38031) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1anJZx-0005St-WA for qemu-devel@nongnu.org; Tue, 05 Apr 2016 01:31:06 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 18C7D20DA8 for ; Tue, 5 Apr 2016 01:31:05 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute1.internal (MEProxy); Tue, 05 Apr 2016 01:31:05 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=braap.org; h=cc :date:from:in-reply-to:message-id:references:subject:to :x-sasl-enc:x-sasl-enc; s=mesmtp; bh=Nmq1MPPAZtxhfH7wFtXgy+rjgzY =; b=XDIXGpiUmDFkg9JW3ugydZjGKoh9NCw8TbIxyHRVmUBa1csyGc7nGdQ0ckV t4GPWXX8/U3xFuS5vj8E78IivvqregjOwzvEILLfksSvtU0igZH/iOTN6d8QGL01 /07XkC6Q8aDnBhz/lPxHx2RztE3OZB59iMZd9uUwuf4F/LGc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=Nmq1 MPPAZtxhfH7wFtXgy+rjgzY=; b=J5966wp6ZEL+b1gnBT2Lidhg769rcFqemkLO bONTdgLwUMiaIZ11puJbb4YohKKGNmZ8EETdUg5vtGjQtl2wDNoTTBxFwN3KxVbX 1k/reDOsOf7LGhRvjlw1ai+xEOOHaXS4A/g43OZRr36ZAKfATKC2Fz1DWgjt+4wF f44jD+I= X-Sasl-enc: V8Jn1btMltgEgFKkOdOAJczvQhl0u6n02rirE37wApLQ 1459834264 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id D1BDD6801A1; Tue, 5 Apr 2016 01:31:04 -0400 (EDT) From: "Emilio G. Cota" To: QEMU Developers , MTTCG Devel Date: Tue, 5 Apr 2016 01:30:48 -0400 Message-Id: <1459834253-8291-6-git-send-email-cota@braap.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1459834253-8291-1-git-send-email-cota@braap.org> References: <1459834253-8291-1-git-send-email-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.29 Cc: Peter Maydell , Peter Crosthwaite , Sergey Fedorov , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Richard Henderson Subject: [Qemu-devel] [PATCH 05/10] include: add spinlock wrapper 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 Wrap pthread_spin on POSIX, or QemuMutex on Windows. AFAIK there are is no off-the-shelf spinlock implementation for Windows, so we'll just use QemuMutex. Signed-off-by: Emilio G. Cota --- include/qemu/spinlock-posix.h | 60 +++++++++++++++++++++++++++++++++++++++++++ include/qemu/spinlock-win32.h | 33 ++++++++++++++++++++++++ include/qemu/spinlock.h | 10 ++++++++ 3 files changed, 103 insertions(+) create mode 100644 include/qemu/spinlock-posix.h create mode 100644 include/qemu/spinlock-win32.h create mode 100644 include/qemu/spinlock.h diff --git a/include/qemu/spinlock-posix.h b/include/qemu/spinlock-posix.h new file mode 100644 index 0000000..51c2c08 --- /dev/null +++ b/include/qemu/spinlock-posix.h @@ -0,0 +1,60 @@ +#ifndef QEMU_SPINLOCK_POSIX_H +#define QEMU_SPINLOCK_POSIX_H + +#include +#include + +typedef pthread_spinlock_t QemuSpinLock; + +static inline void qemu_spinlock_error_exit(int err, const char *msg) +{ + fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err)); + abort(); +} + +static inline void qemu_spinlock_init(QemuSpinLock *lock) +{ + int rc; + + rc = pthread_spin_init(lock, PTHREAD_PROCESS_SHARED); + if (unlikely(rc)) { + qemu_spinlock_error_exit(rc, __func__); + } +} + +static inline void qemu_spinlock_destroy(QemuSpinLock *lock) +{ + int rc; + + rc = pthread_spin_destroy(lock); + if (unlikely(rc)) { + qemu_spinlock_error_exit(rc, __func__); + } +} + +static inline void qemu_spinlock_lock(QemuSpinLock *lock) +{ + int rc; + + rc = pthread_spin_lock(lock); + if (unlikely(rc)) { + qemu_spinlock_error_exit(rc, __func__); + } +} + +static inline int qemu_spinlock_trylock(QemuSpinLock *lock) +{ + return pthread_spin_trylock(lock); +} + +static inline void qemu_spinlock_unlock(QemuSpinLock *lock) +{ + int rc; + + rc = pthread_spin_unlock(lock); + if (unlikely(rc)) { + qemu_spinlock_error_exit(rc, __func__); + } +} + +#endif /* QEMU_SPINLOCK_POSIX_H */ diff --git a/include/qemu/spinlock-win32.h b/include/qemu/spinlock-win32.h new file mode 100644 index 0000000..5a105fb --- /dev/null +++ b/include/qemu/spinlock-win32.h @@ -0,0 +1,33 @@ +#ifndef QEMU_SPINLOCK_WIN32_H +#define QEMU_SPINLOCK_WIN32_H + +#include + +typedef QemuMutex QemuSpinLock; + +static inline void qemu_spinlock_init(QemuSpinLock *lock) +{ + qemu_mutex_init(lock); +} + +static inline void qemu_spinlock_destroy(QemuSpinLock *lock) +{ + qemu_mutex_destroy(lock); +} + +static inline void qemu_spinlock_lock(QemuSpinLock *lock) +{ + qemu_mutex_lock(lock); +} + +static inline int qemu_spinlock_trylock(QemuSpinLock *lock) +{ + return qemu_mutex_trylock(lock); +} + +static inline void qemu_spinlock_unlock(QemuSpinLock *lock) +{ + qemu_mutex_unlock(lock); +} + +#endif /* QEMU_SPINLOCK_WIN32_H */ diff --git a/include/qemu/spinlock.h b/include/qemu/spinlock.h new file mode 100644 index 0000000..001b55e --- /dev/null +++ b/include/qemu/spinlock.h @@ -0,0 +1,10 @@ +#ifndef QEMU_SPINLOCK_H +#define QEMU_SPINLOCK_H + +#ifdef _WIN32 +#include "spinlock-win32.h" +#else +#include "spinlock-posix.h" +#endif + +#endif /* QEMU_SPINLOCK_H */