From 0505dc06d425b604fbe0977a11588755c507767b Mon Sep 17 00:00:00 2001 From: arighi Date: Mon, 1 Nov 2010 16:26:07 +0000 Subject: [PATCH 1/1] CPU: introduce cpu_atomic_xchg() Add an architecture-optimized function to atomically exchange two different cpu_atomic_t values. More exactly, the function replaces the value of a cpu_atomic_t variable and returns the old value, atomically. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4485 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cpu/byteorder.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/bertos/cpu/byteorder.h b/bertos/cpu/byteorder.h index ff1237ad..721f81e5 100644 --- a/bertos/cpu/byteorder.h +++ b/bertos/cpu/byteorder.h @@ -41,6 +41,7 @@ #include #include #include +#include #include /** @@ -168,6 +169,37 @@ INLINE float net_to_host_float(float x) return be_float_to_cpu(x); } +#if CPU_ARM +INLINE cpu_atomic_t +cpu_atomic_xchg(volatile cpu_atomic_t *ptr, cpu_atomic_t val) +{ + cpu_atomic_t ret; + + asm volatile( + "swp %0, %1, [%2]" + + : "=&r" (ret) + : "r" (val), "r" (ptr) + : "memory", "cc"); + + return ret; +} +#else /* CPU_ARM */ +#include + +INLINE cpu_atomic_t +cpu_atomic_xchg(volatile cpu_atomic_t *ptr, cpu_atomic_t val) +{ + cpu_atomic_t ret; + + ATOMIC( + ret = *ptr; + *ptr = val; + ); + return ret; +} +#endif /* CPU_ARM */ + #ifdef __cplusplus /// Type generic byte swapping. -- 2.25.1