From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752316AbcFZLV4 (ORCPT ); Sun, 26 Jun 2016 07:21:56 -0400 Received: from szxga04-in.huawei.com ([58.251.152.52]:19795 "EHLO szxga04-in.huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752071AbcFZLVy (ORCPT ); Sun, 26 Jun 2016 07:21:54 -0400 From: He Kuang To: , , , , , , , , CC: Subject: [RFC PATCH v2 06/26] tools include: Add (atomic|atomic64)_add implementation from the kernel sources Date: Sun, 26 Jun 2016 11:20:58 +0000 Message-ID: <1466940078-65581-7-git-send-email-hekuang@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1466940078-65581-1-git-send-email-hekuang@huawei.com> References: <1466940078-65581-1-git-send-email-hekuang@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.193.250] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090202.576FBAC4.0060,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: ba7fdf86d14ec3587defb6476caefab9 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Uses the arch/x86/ kernel code for x86_64/i386, fallbacking to a gcc intrinsics implementation. Signed-off-by: He Kuang Signed-off-by: Wang Nan --- tools/arch/x86/include/asm/atomic.h | 28 ++++++++++++++++++++++++++++ tools/include/asm-generic/atomic-gcc.h | 10 ++++++++++ tools/include/linux/types.h | 4 ++++ 3 files changed, 42 insertions(+) diff --git a/tools/arch/x86/include/asm/atomic.h b/tools/arch/x86/include/asm/atomic.h index 059e33e..41f814e 100644 --- a/tools/arch/x86/include/asm/atomic.h +++ b/tools/arch/x86/include/asm/atomic.h @@ -62,4 +62,32 @@ static inline int atomic_dec_and_test(atomic_t *v) GEN_UNARY_RMWcc(LOCK_PREFIX "decl", v->counter, "%0", "e"); } +/** + * atomic_add - add integer to atomic variable + * @i: integer value to add + * @v: pointer of type atomic_t + * + * Atomically adds @i to @v. + */ +static __always_inline void atomic_add(int i, atomic_t *v) +{ + asm volatile(LOCK_PREFIX "addl %1,%0" + : "+m" (v->counter) + : "ir" (i)); +} + +/** + * atomic64_add - add integer to atomic64 variable + * @i: integer value to add + * @v: pointer to type atomic64_t + * + * Atomically adds @i to @v. + */ +static __always_inline void atomic64_add(long i, atomic64_t *v) +{ + asm volatile(LOCK_PREFIX "addq %1,%0" + : "=m" (v->counter) + : "er" (i), "m" (v->counter)); +} + #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */ diff --git a/tools/include/asm-generic/atomic-gcc.h b/tools/include/asm-generic/atomic-gcc.h index 2ba78c9..b615907 100644 --- a/tools/include/asm-generic/atomic-gcc.h +++ b/tools/include/asm-generic/atomic-gcc.h @@ -60,4 +60,14 @@ static inline int atomic_dec_and_test(atomic_t *v) return __sync_sub_and_fetch(&v->counter, 1) == 0; } +static inline void atomic_add(int i, atomic_t *v) +{ + __sync_add_and_fetch(&v->counter, i); +} + +static inline void atomic64_add(long i, atomic64_t *v) +{ + __sync_add_and_fetch(&v->counter, i); +} + #endif /* __TOOLS_ASM_GENERIC_ATOMIC_H */ diff --git a/tools/include/linux/types.h b/tools/include/linux/types.h index 8ebf627..09b325f 100644 --- a/tools/include/linux/types.h +++ b/tools/include/linux/types.h @@ -64,6 +64,10 @@ typedef struct { int counter; } atomic_t; +typedef struct { + long counter; +} atomic64_t; + #ifndef __aligned_u64 # define __aligned_u64 __u64 __attribute__((aligned(8))) #endif -- 1.8.5.2