[Concept,10/19] asm-generic: atomic: Add cmpxchg() macro

Message ID 20260117011448.3007171-11-sjg@u-boot.org
State New
Headers
Series ext4l: Reduce ext4_uboot.h size by moving code to include/linux |

Commit Message

Simon Glass Jan. 17, 2026, 1:14 a.m. UTC
  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(-)
  

Patch

diff --git a/fs/ext4l/ext4_uboot.h b/fs/ext4l/ext4_uboot.h
index 3ce94750fcf..f26006f9291 100644
--- a/fs/ext4l/ext4_uboot.h
+++ b/fs/ext4l/ext4_uboot.h
@@ -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;
diff --git a/include/asm-generic/atomic.h b/include/asm-generic/atomic.h
index 7825258922a..3fa749bf012 100644
--- a/include/asm-generic/atomic.h
+++ b/include/asm-generic/atomic.h
@@ -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