[U-Boot] [PATCH v3 01/10] arm: add atomic functions with return support

Antoine Tenart antoine.tenart at free-electrons.com
Wed Oct 26 14:10:24 CEST 2016


Implement three atomic functions to allow making an atomic operation
that returns the value. Adds: atomic_add_return(), atomic_sub_return(),
atomic_inc_return() and atomic_dec_return().

Signed-off-by: Antoine Tenart <antoine.tenart at free-electrons.com>
---
 arch/arm/include/asm/atomic.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/arch/arm/include/asm/atomic.h b/arch/arm/include/asm/atomic.h
index 171f4d979281..0d9a6e3901e2 100644
--- a/arch/arm/include/asm/atomic.h
+++ b/arch/arm/include/asm/atomic.h
@@ -46,6 +46,18 @@ static inline void atomic_add(int i, volatile atomic_t *v)
 	local_irq_restore(flags);
 }
 
+static inline int atomic_add_return(int i, volatile atomic_t *v)
+{
+	unsigned long flags = 0;
+	int ret;
+
+	local_irq_save(flags);
+	ret = (v->counter += i);
+	local_irq_restore(flags);
+
+	return ret;
+}
+
 static inline void atomic_sub(int i, volatile atomic_t *v)
 {
 	unsigned long flags = 0;
@@ -55,6 +67,18 @@ static inline void atomic_sub(int i, volatile atomic_t *v)
 	local_irq_restore(flags);
 }
 
+static inline int atomic_sub_return(int i, volatile atomic_t *v)
+{
+	unsigned long flags = 0;
+	int ret;
+
+	local_irq_save(flags);
+	ret = (v->counter -= i);
+	local_irq_restore(flags);
+
+	return ret;
+}
+
 static inline void atomic_inc(volatile atomic_t *v)
 {
 	unsigned long flags = 0;
@@ -64,6 +88,11 @@ static inline void atomic_inc(volatile atomic_t *v)
 	local_irq_restore(flags);
 }
 
+static inline int atomic_inc_return(volatile atomic_t *v)
+{
+	return atomic_add_return(1, v);
+}
+
 static inline void atomic_dec(volatile atomic_t *v)
 {
 	unsigned long flags = 0;
@@ -73,6 +102,11 @@ static inline void atomic_dec(volatile atomic_t *v)
 	local_irq_restore(flags);
 }
 
+static inline int atomic_dec_return(volatile atomic_t *v)
+{
+	return atomic_sub_return(1, v);
+}
+
 static inline int atomic_dec_and_test(volatile atomic_t *v)
 {
 	unsigned long flags = 0;
-- 
2.10.1



More information about the U-Boot mailing list