This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit ded3bcf6feba3c0a91ec5a9f277de626639b93e3
Author: Bowen Wang <[email protected]>
AuthorDate: Tue Apr 7 11:41:19 2026 +0800

    libc/atomic: add ARCH_SYNC_DEFINE for __sync_* inline support
    
    Move __sync_* macro definitions from arch_atomic.c to
    arch_atomic.h using ARCH_SYNC_DEFINE, consistent with
    ARCH_ATOMIC_DEFINE pattern. This provides both inline
    version (via header) and exported symbol version (via .c)
    for __sync_* builtins, matching the dual-version approach
    already used for __atomic_* operations.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 include/nuttx/lib/arch_atomic.h |  97 ++++++++++++
 libs/libc/machine/arch_atomic.c | 342 ++--------------------------------------
 2 files changed, 106 insertions(+), 333 deletions(-)

diff --git a/include/nuttx/lib/arch_atomic.h b/include/nuttx/lib/arch_atomic.h
index bdea1a3a8d7..a578999d3f2 100644
--- a/include/nuttx/lib/arch_atomic.h
+++ b/include/nuttx/lib/arch_atomic.h
@@ -183,9 +183,106 @@ static inline void atomic_unlock(irqstate_t flags)
   ARCH_ATOMIC_FETCH_OP(prefix ## _fetch_or_ ## n, t, |)                  \
   ARCH_ATOMIC_FETCH_OP(prefix ## _fetch_xor_ ## n, t, ^)
 
+#define ARCH_SYNC_OP_FETCH(func, t, op)                                  \
+  ARCH_ATOMIC_SPECIFIER                                                  \
+  t func(FAR volatile void *ptr, t value)                                \
+  {                                                                      \
+    irqstate_t irqstate = atomic_lock();                                 \
+    FAR t *tmp = (FAR t *)ptr;                                           \
+    t ret;                                                               \
+                                                                         \
+    *tmp = *tmp op value;                                                \
+    ret = *tmp;                                                          \
+                                                                         \
+    atomic_unlock(irqstate);                                             \
+    return ret;                                                          \
+  }
+
+#define ARCH_SYNC_NAND_FETCH(func, t)                                    \
+  ARCH_ATOMIC_SPECIFIER                                                  \
+  t func(FAR volatile void *ptr, t value)                                \
+  {                                                                      \
+    irqstate_t irqstate = atomic_lock();                                 \
+    FAR t *tmp = (FAR t *)ptr;                                           \
+    t ret;                                                               \
+                                                                         \
+    *tmp = ~(*tmp & value);                                              \
+    ret = *tmp;                                                          \
+                                                                         \
+    atomic_unlock(irqstate);                                             \
+    return ret;                                                          \
+  }
+
+#define ARCH_SYNC_BOOL_CMP_SWAP(func, t)                                 \
+  ARCH_ATOMIC_SPECIFIER                                                  \
+  bool func(FAR volatile void *ptr, t oldvalue, t newvalue)              \
+  {                                                                      \
+    bool ret = false;                                                    \
+    irqstate_t irqstate = atomic_lock();                                 \
+    FAR t *tmp = (FAR t *)ptr;                                           \
+                                                                         \
+    if (*tmp == oldvalue)                                                \
+      {                                                                  \
+        ret = true;                                                      \
+        *tmp = newvalue;                                                 \
+      }                                                                  \
+                                                                         \
+    atomic_unlock(irqstate);                                             \
+    return ret;                                                          \
+  }
+
+#define ARCH_SYNC_VAL_CMP_SWAP(func, t)                                  \
+  ARCH_ATOMIC_SPECIFIER                                                  \
+  t func(FAR volatile void *ptr, t oldvalue, t newvalue)                 \
+  {                                                                      \
+    irqstate_t irqstate = atomic_lock();                                 \
+    FAR t *tmp = (FAR t *)ptr;                                           \
+    t ret = *tmp;                                                        \
+                                                                         \
+    if (*tmp == oldvalue)                                                \
+      {                                                                  \
+        *tmp = newvalue;                                                 \
+      }                                                                  \
+                                                                         \
+    atomic_unlock(irqstate);                                             \
+    return ret;                                                          \
+  }
+
+#ifdef UP_DMB
+#  define ARCH_SYNC_BARRIER() UP_DMB()
+#else
+#  define ARCH_SYNC_BARRIER()
+#endif
+
+#define ARCH_SYNC_SYNCHRONIZE(prefix)                                    \
+  ARCH_ATOMIC_SPECIFIER                                                  \
+  void prefix ## _synchronize(void)                                      \
+  {                                                                      \
+    ARCH_SYNC_BARRIER();                                                 \
+  }
+
+#define ARCH_SYNC_DEFINE(prefix, t, n)                                   \
+  ARCH_SYNC_OP_FETCH(prefix ## _add_and_fetch_ ## n, t, +)               \
+  ARCH_SYNC_OP_FETCH(prefix ## _sub_and_fetch_ ## n, t, -)               \
+  ARCH_SYNC_OP_FETCH(prefix ## _or_and_fetch_ ## n, t, |)                \
+  ARCH_SYNC_OP_FETCH(prefix ## _and_and_fetch_ ## n, t, &)               \
+  ARCH_SYNC_OP_FETCH(prefix ## _xor_and_fetch_ ## n, t, ^)               \
+  ARCH_SYNC_NAND_FETCH(prefix ## _nand_and_fetch_ ## n, t)               \
+  ARCH_SYNC_BOOL_CMP_SWAP(prefix ## _bool_compare_and_swap_ ## n, t)     \
+  ARCH_SYNC_VAL_CMP_SWAP(prefix ## _val_compare_and_swap_ ## n, t)
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
+
 ARCH_ATOMIC_DEFINE(atomic, uint8_t, 1)
 ARCH_ATOMIC_DEFINE(atomic, uint16_t, 2)
 ARCH_ATOMIC_DEFINE(atomic, int32_t, 4)
 ARCH_ATOMIC_DEFINE(atomic, int64_t, 8)
 
+ARCH_SYNC_DEFINE(sync, uint8_t, 1)
+ARCH_SYNC_DEFINE(sync, uint16_t, 2)
+ARCH_SYNC_DEFINE(sync, uint32_t, 4)
+ARCH_SYNC_DEFINE(sync, uint64_t, 8)
+
 #endif /* __INCLUDE_NUTTX_LIB_ARCH_ATOMIC_H */
diff --git a/libs/libc/machine/arch_atomic.c b/libs/libc/machine/arch_atomic.c
index 4b811e4f565..db63a72899b 100644
--- a/libs/libc/machine/arch_atomic.c
+++ b/libs/libc/machine/arch_atomic.c
@@ -29,146 +29,6 @@
 #define ARCH_ATOMIC_SPECIFIER
 
 #include <nuttx/atomic.h>
-#include <nuttx/macro.h>
-
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
-
-#define SYNC_ADD_FETCH(fn, n, type)                                   \
-                                                                      \
-  type weak_function CONCATENATE(fn, n)(FAR volatile void *ptr,       \
-                                        type value)                   \
-  {                                                                   \
-    irqstate_t irqstate = atomic_lock();                              \
-    FAR type *tmp = (FAR type *)ptr;                                  \
-    type ret;                                                         \
-                                                                      \
-    *tmp = *tmp + value;                                              \
-    ret = *tmp;                                                       \
-                                                                      \
-    atomic_unlock(irqstate);                                          \
-    return ret;                                                       \
-  }
-
-#define SYNC_SUB_FETCH(fn, n, type)                                   \
-                                                                      \
-  type weak_function CONCATENATE(fn, n)(FAR volatile void *ptr,       \
-                                        type value)                   \
-  {                                                                   \
-    irqstate_t irqstate = atomic_lock();                              \
-    FAR type *tmp = (FAR type *)ptr;                                  \
-    type ret;                                                         \
-                                                                      \
-    *tmp = *tmp - value;                                              \
-    ret = *tmp;                                                       \
-                                                                      \
-    atomic_unlock(irqstate);                                          \
-    return ret;                                                       \
-  }
-
-#define SYNC_OR_FETCH(fn, n, type)                                    \
-                                                                      \
-  type weak_function CONCATENATE(fn, n)(FAR volatile void *ptr,       \
-                                        type value)                   \
-  {                                                                   \
-    irqstate_t irqstate = atomic_lock();                              \
-    FAR type *tmp = (FAR type *)ptr;                                  \
-    type ret;                                                         \
-                                                                      \
-    *tmp = *tmp | value;                                              \
-    ret = *tmp;                                                       \
-                                                                      \
-    atomic_unlock(irqstate);                                          \
-    return ret;                                                       \
-  }
-
-#define SYNC_AND_FETCH(fn, n, type)                                   \
-                                                                      \
-  type weak_function CONCATENATE(fn, n)(FAR volatile void *ptr,       \
-                                        type value)                   \
-  {                                                                   \
-    irqstate_t irqstate = atomic_lock();                              \
-    FAR type *tmp = (FAR type *)ptr;                                  \
-    type ret;                                                         \
-                                                                      \
-    *tmp = *tmp & value;                                              \
-    ret = *tmp;                                                       \
-                                                                      \
-    atomic_unlock(irqstate);                                          \
-    return ret;                                                       \
-  }
-
-#define SYNC_XOR_FETCH(fn, n, type)                                   \
-                                                                      \
-  type weak_function CONCATENATE(fn, n)(FAR volatile void *ptr,       \
-                                        type value)                   \
-  {                                                                   \
-    irqstate_t irqstate = atomic_lock();                              \
-    FAR type *tmp = (FAR type *)ptr;                                  \
-    type ret;                                                         \
-                                                                      \
-    *tmp = *tmp ^ value;                                              \
-    ret = *tmp;                                                       \
-                                                                      \
-    atomic_unlock(irqstate);                                          \
-    return ret;                                                       \
-  }
-
-#define SYNC_NAND_FETCH(fn, n, type)                                  \
-                                                                      \
-  type weak_function CONCATENATE(fn, n)(FAR volatile void *ptr,       \
-                                        type value)                   \
-  {                                                                   \
-    irqstate_t irqstate = atomic_lock();                              \
-    FAR type *tmp = (FAR type *)ptr;                                  \
-    type ret;                                                         \
-                                                                      \
-    *tmp = ~(*tmp & value);                                           \
-    ret = *tmp;                                                       \
-                                                                      \
-    atomic_unlock(irqstate);                                          \
-    return ret;                                                       \
-  }
-
-#define SYNC_BOOL_CMP_SWAP(fn, n, type)                               \
-                                                                      \
-  bool weak_function CONCATENATE(fn, n)(FAR volatile void *ptr,       \
-                                        type oldvalue,                \
-                                        type newvalue)                \
-  {                                                                   \
-    bool ret = false;                                                 \
-    irqstate_t irqstate = atomic_lock();                              \
-    FAR type *tmp = (FAR type *)ptr;                                  \
-                                                                      \
-    if (*tmp == oldvalue)                                             \
-      {                                                               \
-        ret = true;                                                   \
-        *tmp = newvalue;                                              \
-      }                                                               \
-                                                                      \
-    atomic_unlock(irqstate);                                          \
-    return ret;                                                       \
-  }
-
-#define SYNC_VAL_CMP_SWAP(fn, n, type)                                \
-                                                                      \
-  type weak_function CONCATENATE(fn, n)(FAR volatile void *ptr,       \
-                                        type oldvalue,                \
-                                        type newvalue)                \
-  {                                                                   \
-    irqstate_t irqstate = atomic_lock();                              \
-    FAR type *tmp = (FAR type *)ptr;                                  \
-    type ret = *tmp;                                                  \
-                                                                      \
-    if (*tmp == oldvalue)                                             \
-      {                                                               \
-        *tmp = newvalue;                                              \
-      }                                                               \
-                                                                      \
-    atomic_unlock(irqstate);                                          \
-    return ret;                                                       \
-  }
 
 /****************************************************************************
  * Public Functions
@@ -190,206 +50,22 @@ ARCH_ATOMIC_DEFINE(__atomic, uint64_t, 8)
 #ifndef __clang__
 
 /****************************************************************************
- * Name: __sync_add_and_fetch_1
- ****************************************************************************/
-
-SYNC_ADD_FETCH(__sync_add_and_fetch_, 1, uint8_t)
-
-/****************************************************************************
- * Name: __sync_add_and_fetch_2
- ****************************************************************************/
-
-SYNC_ADD_FETCH(__sync_add_and_fetch_, 2, uint16_t)
-
-/****************************************************************************
- * Name: __sync_add_and_fetch_4
- ****************************************************************************/
-
-SYNC_ADD_FETCH(__sync_add_and_fetch_, 4, uint32_t)
-
-/****************************************************************************
- * Name: __sync_add_and_fetch_8
- ****************************************************************************/
-
-SYNC_ADD_FETCH(__sync_add_and_fetch_, 8, uint64_t)
-
-/****************************************************************************
- * Name: __sync_sub_and_fetch_1
- ****************************************************************************/
-
-SYNC_SUB_FETCH(__sync_sub_and_fetch_, 1, uint8_t)
-
-/****************************************************************************
- * Name: __sync_sub_and_fetch_2
- ****************************************************************************/
-
-SYNC_SUB_FETCH(__sync_sub_and_fetch_, 2, uint16_t)
-
-/****************************************************************************
- * Name: __sync_sub_and_fetch_4
- ****************************************************************************/
-
-SYNC_SUB_FETCH(__sync_sub_and_fetch_, 4, uint32_t)
-
-/****************************************************************************
- * Name: __sync_sub_and_fetch_8
- ****************************************************************************/
-
-SYNC_SUB_FETCH(__sync_sub_and_fetch_, 8, uint64_t)
-
-/****************************************************************************
- * Name: __sync_or_and_fetch_1
- ****************************************************************************/
-
-SYNC_OR_FETCH(__sync_or_and_fetch_, 1, uint8_t)
-
-/****************************************************************************
- * Name: __sync_or_and_fetch_2
- ****************************************************************************/
-
-SYNC_OR_FETCH(__sync_or_and_fetch_, 2, uint16_t)
-
-/****************************************************************************
- * Name: __sync_or_and_fetch_4
- ****************************************************************************/
-
-SYNC_OR_FETCH(__sync_or_and_fetch_, 4, uint32_t)
-
-/****************************************************************************
- * Name: __sync_or_and_fetch_8
- ****************************************************************************/
-
-SYNC_OR_FETCH(__sync_or_and_fetch_, 8, uint64_t)
-
-/****************************************************************************
- * Name: __sync_and_and_fetch_1
- ****************************************************************************/
-
-SYNC_AND_FETCH(__sync_and_and_fetch_, 1, uint8_t)
-
-/****************************************************************************
- * Name: __sync_and_and_fetch_2
- ****************************************************************************/
-
-SYNC_AND_FETCH(__sync_and_and_fetch_, 2, uint16_t)
-
-/****************************************************************************
- * Name: __sync_and_and_fetch_4
- ****************************************************************************/
-
-SYNC_AND_FETCH(__sync_and_and_fetch_, 4, uint32_t)
-
-/****************************************************************************
- * Name: __sync_and_and_fetch_8
- ****************************************************************************/
-
-SYNC_AND_FETCH(__sync_and_and_fetch_, 8, uint64_t)
-
-/****************************************************************************
- * Name: __sync_xor_and_fetch_1
- ****************************************************************************/
-
-SYNC_XOR_FETCH(__sync_xor_and_fetch_, 1, uint8_t)
-
-/****************************************************************************
- * Name: __sync_xor_and_fetch_2
- ****************************************************************************/
-
-SYNC_XOR_FETCH(__sync_xor_and_fetch_, 2, uint16_t)
-
-/****************************************************************************
- * Name: __sync_xor_and_fetch_4
- ****************************************************************************/
-
-SYNC_XOR_FETCH(__sync_xor_and_fetch_, 4, uint32_t)
-
-/****************************************************************************
- * Name: __sync_xor_and_fetch_8
- ****************************************************************************/
-
-SYNC_XOR_FETCH(__sync_xor_and_fetch_, 8, uint64_t)
-
-/****************************************************************************
- * Name: __sync_nand_and_fetch_1
- ****************************************************************************/
-
-SYNC_NAND_FETCH(__sync_nand_and_fetch_, 1, uint8_t)
-
-/****************************************************************************
- * Name: __sync_nand_and_fetch_2
- ****************************************************************************/
-
-SYNC_NAND_FETCH(__sync_nand_and_fetch_, 2, uint16_t)
-
-/****************************************************************************
- * Name: __sync_nand_and_fetch_4
- ****************************************************************************/
-
-SYNC_NAND_FETCH(__sync_nand_and_fetch_, 4, uint32_t)
-
-/****************************************************************************
- * Name: __sync_nand_and_fetch_8
- ****************************************************************************/
-
-SYNC_NAND_FETCH(__sync_nand_and_fetch_, 8, uint64_t)
-
-/****************************************************************************
- * Name: __sync_bool_compare_and_swap_1
- ****************************************************************************/
-
-SYNC_BOOL_CMP_SWAP(__sync_bool_compare_and_swap_, 1, uint8_t)
-
-/****************************************************************************
- * Name: __sync_bool_compare_and_swap_2
- ****************************************************************************/
-
-SYNC_BOOL_CMP_SWAP(__sync_bool_compare_and_swap_, 2, uint16_t)
-
-/****************************************************************************
- * Name: __sync_bool_compare_and_swap_4
- ****************************************************************************/
-
-SYNC_BOOL_CMP_SWAP(__sync_bool_compare_and_swap_, 4, uint32_t)
-
-/****************************************************************************
- * Name: __sync_bool_compare_and_swap_8
+ * Name: __sync_*_{1,2,4,8}
  ****************************************************************************/
 
-SYNC_BOOL_CMP_SWAP(__sync_bool_compare_and_swap_, 8, uint64_t)
-
-/****************************************************************************
- * Name: __sync_val_compare_and_swap_1
- ****************************************************************************/
-
-SYNC_VAL_CMP_SWAP(__sync_val_compare_and_swap_, 1, uint8_t)
-
-/****************************************************************************
- * Name: __sync_val_compare_and_swap_2
- ****************************************************************************/
-
-SYNC_VAL_CMP_SWAP(__sync_val_compare_and_swap_, 2, uint16_t)
-
-/****************************************************************************
- * Name: __sync_val_compare_and_swap_4
- ****************************************************************************/
-
-SYNC_VAL_CMP_SWAP(__sync_val_compare_and_swap_, 4, uint32_t)
-
-/****************************************************************************
- * Name: __sync_val_compare_and_swap_8
- ****************************************************************************/
-
-SYNC_VAL_CMP_SWAP(__sync_val_compare_and_swap_, 8, uint64_t)
+#ifdef ARCH_SYNC_DEFINE
+ARCH_SYNC_DEFINE(__sync, uint8_t, 1)
+ARCH_SYNC_DEFINE(__sync, uint16_t, 2)
+ARCH_SYNC_DEFINE(__sync, uint32_t, 4)
+ARCH_SYNC_DEFINE(__sync, uint64_t, 8)
+#endif
 
 /****************************************************************************
  * Name: __sync_synchronize
  ****************************************************************************/
 
-void weak_function __sync_synchronize(void)
-{
-#ifdef UP_DMB
-  UP_DMB();
+#ifdef ARCH_SYNC_SYNCHRONIZE
+ARCH_SYNC_SYNCHRONIZE(__sync)
 #endif
-}
 
 #endif /* __clang__ */

Reply via email to