[Concept,10/19] asm-generic: atomic: Add cmpxchg() macro
Commit Message
From: Simon Glass <simon.glass@canonical.com>
Move the cmpxchg (compare and exchange) macro to the asm-generic
atomic header where it belongs alongside other atomic operations.
This is a single-threaded implementation for U-Boot that atomically
compares *ptr with old and if equal, stores new. Returns the original
value of *ptr.
Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---
fs/ext4l/ext4_uboot.h | 10 +---------
include/asm-generic/atomic.h | 18 ++++++++++++++++++
2 files changed, 19 insertions(+), 9 deletions(-)
@@ -94,15 +94,7 @@
/* SMP stubs - U-Boot is single-threaded */
#define raw_smp_processor_id() 0
-/* cmpxchg - compare and exchange, single-threaded version */
-#define cmpxchg(ptr, old, new) ({ \
- typeof(*(ptr)) __cmpxchg_old = (old); \
- typeof(*(ptr)) __cmpxchg_new = (new); \
- typeof(*(ptr)) __cmpxchg_ret = *(ptr); \
- if (__cmpxchg_ret == __cmpxchg_old) \
- *(ptr) = __cmpxchg_new; \
- __cmpxchg_ret; \
-})
+/* cmpxchg is now in asm-generic/atomic.h */
/* Reference count type */
typedef struct { atomic_t refs; } refcount_t;
@@ -200,4 +200,22 @@ static inline void atomic64_dec(volatile atomic64_t *v)
local_irq_restore(flags);
}
+/**
+ * cmpxchg - compare and exchange
+ * @ptr: pointer to the value
+ * @old: expected old value
+ * @new: new value to store if current equals old
+ *
+ * Single-threaded version for U-Boot. Atomically compares *ptr with old
+ * and if equal, stores new. Returns the original value of *ptr.
+ */
+#define cmpxchg(ptr, old, new) ({ \
+ typeof(*(ptr)) __cmpxchg_old = (old); \
+ typeof(*(ptr)) __cmpxchg_new = (new); \
+ typeof(*(ptr)) __cmpxchg_ret = *(ptr); \
+ if (__cmpxchg_ret == __cmpxchg_old) \
+ *(ptr) = __cmpxchg_new; \
+ __cmpxchg_ret; \
+})
+
#endif