From patchwork Sun Oct 28 23:48:40 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfank@linux.vnet.ibm.com X-Patchwork-Id: 194757 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 50E1B2C0086 for ; Mon, 29 Oct 2012 10:49:23 +1100 (EST) Received: from localhost ([::1]:34278 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TScbZ-0005Ws-8v for incoming@patchwork.ozlabs.org; Sun, 28 Oct 2012 19:49:21 -0400 Received: from eggs.gnu.org ([208.118.235.92]:52646) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TScbI-0005Wb-Fo for qemu-devel@nongnu.org; Sun, 28 Oct 2012 19:49:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TScbG-0003mZ-PB for qemu-devel@nongnu.org; Sun, 28 Oct 2012 19:49:04 -0400 Received: from e28smtp05.in.ibm.com ([122.248.162.5]:36866) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TScbG-0003fl-3O for qemu-devel@nongnu.org; Sun, 28 Oct 2012 19:49:02 -0400 Received: from /spool/local by e28smtp05.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 29 Oct 2012 05:18:56 +0530 Received: from d28relay04.in.ibm.com (9.184.220.61) by e28smtp05.in.ibm.com (192.168.1.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 29 Oct 2012 05:18:53 +0530 Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay04.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q9SNmrmC7471506 for ; Mon, 29 Oct 2012 05:18:53 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q9T5IixZ008210 for ; Mon, 29 Oct 2012 05:18:45 GMT Received: from oc8440477808.ibm.com ([9.125.25.249]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q9T5IebI008036; Mon, 29 Oct 2012 05:18:43 GMT From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Mon, 29 Oct 2012 07:48:40 +0800 Message-Id: <1351468127-15025-2-git-send-email-pingfank@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1351468127-15025-1-git-send-email-pingfank@linux.vnet.ibm.com> References: <1351468127-15025-1-git-send-email-pingfank@linux.vnet.ibm.com> x-cbid: 12102823-8256-0000-0000-000004C3485B X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 122.248.162.5 Cc: Peter Maydell , Stefan Hajnoczi , Marcelo Tosatti , Avi Kivity , Anthony Liguori , Jan Kiszka , Paolo Bonzini Subject: [Qemu-devel] [patch v5 1/8] atomic: introduce atomic operations 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org If out of global lock, we will be challenged by SMP in low level, so need atomic ops. This file is a wrapper of GCC atomic builtin. Signed-off-by: Liu Ping Fan --- include/qemu/atomic.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 63 insertions(+), 0 deletions(-) create mode 100644 include/qemu/atomic.h diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h new file mode 100644 index 0000000..a9e6d35 --- /dev/null +++ b/include/qemu/atomic.h @@ -0,0 +1,63 @@ +/* + * Simple wrapper of gcc Atomic-Builtins + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ +#ifndef __QEMU_ATOMIC_H +#define __QEMU_ATOMIC_H + +typedef struct Atomic { + volatile int counter; +} Atomic; + +static inline void atomic_set(Atomic *v, int i) +{ + v->counter = i; +} + +static inline int atomic_read(Atomic *v) +{ + return v->counter; +} + +static inline int atomic_return_and_add(int i, Atomic *v) +{ + int ret; + + ret = __sync_fetch_and_add(&v->counter, i); + return ret; +} + +static inline int atomic_return_and_sub(int i, Atomic *v) +{ + int ret; + + ret = __sync_fetch_and_sub(&v->counter, i); + return ret; +} + +/** + * * atomic_inc - increment atomic variable + * * @v: pointer of type Atomic + * * + * * Atomically increments @v by 1. + * */ +static inline void atomic_inc(Atomic *v) +{ + __sync_fetch_and_add(&v->counter, 1); +} + +/** + * * atomic_dec - decrement atomic variable + * * @v: pointer of type Atomic + * * + * * Atomically decrements @v by 1. + * */ +static inline void atomic_dec(Atomic *v) +{ + __sync_fetch_and_sub(&v->counter, 1); +} + +#endif