--- src/dune	2024-05-08 02:46:09.000000000 +0800
+++ src/dune	2026-08-03 04:51:48.000000000 +0800
@@ -9,6 +9,7 @@
  (name ocaml_intrinsics)
  (public_name ocaml_intrinsics)
  (libraries ocaml_intrinsics_kernel)
+ (c_library_flags (-latomic))
  (preprocess no_preprocessing))
 
 (rule

--- src/atomic_stubs.c	2025-04-15 09:19:16.000000000 +0800
+++ src/atomic_stubs.c	2026-02-12 06:35:04.000000000 +0800
@@ -2,6 +2,8 @@
 #include "caml/bigarray.h"
 #include "ext_pointer.h"
 #include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
 
 static char *bigstring_element_at_pos(value v_bstr, intnat pos)
 {
@@ -11,7 +13,7 @@
 #define IMPL_INT(name)                                                                                   \
     intnat caml_native_pointer_##name##_int_untagged(intnat ptr, intnat n)                               \
     {                                                                                                    \
-        return __sync_##name((intnat*)ptr, n);                                                           \
+        return __atomic_##name((intnat*)ptr, n, __ATOMIC_SEQ_CST);                                       \
     }                                                                                                    \
     CAMLprim value caml_native_pointer_##name##_int_bytecode(value ptr, value n)                         \
     {                                                                                                    \
@@ -21,7 +23,7 @@
     intnat caml_ext_pointer_##name##_int_untagged(value ptr, intnat n)                                   \
     {                                                                                                    \
         intnat* decode = (intnat*)caml_ext_pointer_decode(ptr);                                          \
-        return __sync_##name(decode, n);                                                                 \
+        return __atomic_##name(decode, n, __ATOMIC_SEQ_CST);                                             \
     }                                                                                                    \
     CAMLprim value caml_ext_pointer_##name##_int_bytecode(value ptr, value n)                            \
     {                                                                                                    \
@@ -31,7 +33,7 @@
     intnat caml_bigstring_##name##_int_untagged(value v_bstr, intnat pos, intnat n)                      \
     {                                                                                                    \
         intnat* decode = (intnat*)bigstring_element_at_pos(v_bstr, pos);                                 \
-        return __sync_##name(decode, n);                                                                 \
+        return __atomic_##name(decode, n, __ATOMIC_SEQ_CST);                                             \
     }                                                                                                    \
     CAMLprim value caml_bigstring_##name##_int_bytecode(value v_bstr, value pos, value n)                \
     {                                                                                                    \
@@ -42,7 +44,7 @@
 #define IMPL_INT64(name)                                                                                 \
     int64_t caml_native_pointer_##name##_int64_unboxed(intnat ptr, int64_t n)                            \
     {                                                                                                    \
-        return __sync_##name((int64_t*)ptr, n);                                                          \
+        return __atomic_##name((int64_t*)ptr, n, __ATOMIC_SEQ_CST);                                      \
     }                                                                                                    \
     CAMLprim value caml_native_pointer_##name##_int64_bytecode(value ptr, value n)                       \
     {                                                                                                    \
@@ -52,7 +54,7 @@
     int64_t caml_ext_pointer_##name##_int64_unboxed(value ptr, int64_t n)                                \
     {                                                                                                    \
         int64_t* decode = (int64_t*)caml_ext_pointer_decode(ptr);                                        \
-        return __sync_##name(decode, n);                                                                 \
+        return __atomic_##name(decode, n, __ATOMIC_SEQ_CST);                                             \
     }                                                                                                    \
     CAMLprim value caml_ext_pointer_##name##_int64_bytecode(value ptr, value n)                          \
     {                                                                                                    \
@@ -62,7 +64,7 @@
     int64_t caml_bigstring_##name##_int64_unboxed(value v_bstr, intnat pos, int64_t n)                   \
     {                                                                                                    \
         int64_t* decode = (int64_t*)bigstring_element_at_pos(v_bstr, pos);                               \
-        return __sync_##name(decode, n);                                                                 \
+        return __atomic_##name(decode, n, __ATOMIC_SEQ_CST);                                             \
     }                                                                                                    \
     CAMLprim value caml_bigstring_##name##_int64_bytecode(value v_bstr, value pos, value n)              \
     {                                                                                                    \
@@ -73,7 +75,7 @@
 #define IMPL_INT32(name)                                                                                 \
     int32_t caml_native_pointer_##name##_int32_unboxed(intnat ptr, int32_t n)                            \
     {                                                                                                    \
-        return __sync_##name((int32_t*)ptr, n);                                                          \
+        return __atomic_##name((int32_t*)ptr, n, __ATOMIC_SEQ_CST);                                      \
     }                                                                                                    \
     CAMLprim value caml_native_pointer_##name##_int32_bytecode(value ptr, value n)                       \
     {                                                                                                    \
@@ -83,7 +85,7 @@
     int32_t caml_ext_pointer_##name##_int32_unboxed(value ptr, int32_t n)                                \
     {                                                                                                    \
         int32_t* decode = (int32_t*)caml_ext_pointer_decode(ptr);                                        \
-        return __sync_##name(decode, n);                                                                 \
+        return __atomic_##name(decode, n, __ATOMIC_SEQ_CST);                                             \
     }                                                                                                    \
     CAMLprim value caml_ext_pointer_##name##_int32_bytecode(value ptr, value n)                          \
     {                                                                                                    \
@@ -93,7 +95,7 @@
     int32_t caml_bigstring_##name##_int32_unboxed(value v_bstr, intnat pos, int32_t n)                   \
     {                                                                                                    \
         int32_t* decode = (int32_t*)bigstring_element_at_pos(v_bstr, pos);                               \
-        return __sync_##name(decode, n);                                                                 \
+        return __atomic_##name(decode, n, __ATOMIC_SEQ_CST);                                             \
     }                                                                                                    \
     CAMLprim value caml_bigstring_##name##_int32_bytecode(value v_bstr, value pos, value n)              \
     {                                                                                                    \
@@ -104,7 +106,7 @@
 #define IMPL_NATIVEINT(name)                                                                             \
     intnat caml_native_pointer_##name##_nativeint_unboxed(intnat ptr, intnat n)                          \
     {                                                                                                    \
-        return __sync_##name((intnat*)ptr, n);                                                           \
+        return __atomic_##name((intnat*)ptr, n, __ATOMIC_SEQ_CST);                                       \
     }                                                                                                    \
     CAMLprim value caml_native_pointer_##name##_nativeint_bytecode(value ptr, value n)                   \
     {                                                                                                    \
@@ -114,7 +116,7 @@
     intnat caml_ext_pointer_##name##_nativeint_unboxed(value ptr, intnat n)                              \
     {                                                                                                    \
         intnat* decode = (intnat*)caml_ext_pointer_decode(ptr);                                          \
-        return __sync_##name(decode, n);                                                                 \
+        return __atomic_##name(decode, n, __ATOMIC_SEQ_CST);                                             \
     }                                                                                                    \
     CAMLprim value caml_ext_pointer_##name##_nativeint_bytecode(value ptr, value n)                      \
     {                                                                                                    \
@@ -124,7 +126,7 @@
     intnat caml_bigstring_##name##_nativeint_unboxed(value v_bstr, intnat pos, intnat n)                 \
     {                                                                                                    \
         intnat* decode = (intnat*)bigstring_element_at_pos(v_bstr, pos);                                 \
-        return __sync_##name(decode, n);                                                                 \
+        return __atomic_##name(decode, n, __ATOMIC_SEQ_CST);                                             \
     }                                                                                                    \
     CAMLprim value caml_bigstring_##name##_nativeint_bytecode(value v_bstr, value pos, value n)          \
     {                                                                                                    \
@@ -134,22 +136,23 @@
 
 /* Arithmetic */
 
-IMPL_INT(fetch_and_add)
-IMPL_INT(fetch_and_sub)
+IMPL_INT(fetch_add)
+IMPL_INT(fetch_sub)
 
-IMPL_INT64(fetch_and_add)
-IMPL_INT64(fetch_and_sub)
+IMPL_INT64(fetch_add)
+IMPL_INT64(fetch_sub)
 
-IMPL_INT32(fetch_and_add)
-IMPL_INT32(fetch_and_sub)
+IMPL_INT32(fetch_add)
+IMPL_INT32(fetch_sub)
 
-IMPL_NATIVEINT(fetch_and_add)
-IMPL_NATIVEINT(fetch_and_sub)
+IMPL_NATIVEINT(fetch_add)
+IMPL_NATIVEINT(fetch_sub)
 
 /* Compare and Swap */
 
 CAMLprim value caml_native_pointer_compare_and_swap_int_untagged(intnat ptr, intnat compare, intnat swap) {
-    return Val_bool(__sync_bool_compare_and_swap((intnat*)ptr, compare, swap));
+    intnat expected = compare;
+    return Val_bool(__atomic_compare_exchange_n((intnat*)ptr, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_native_pointer_compare_and_swap_int_bytecode(value ptr, value compare, value swap) {
     return caml_native_pointer_compare_and_swap_int_untagged(
@@ -157,7 +160,8 @@
 }
 
 CAMLprim value caml_native_pointer_compare_and_swap_int64_unboxed(intnat ptr, int64_t compare, int64_t swap) {
-    return Val_bool(__sync_bool_compare_and_swap((int64_t*)ptr, compare, swap));
+    int64_t expected = compare;
+    return Val_bool(__atomic_compare_exchange_n((int64_t*)ptr, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_native_pointer_compare_and_swap_int64_bytecode(value ptr, value compare, value swap) {
     return caml_native_pointer_compare_and_swap_int64_unboxed(
@@ -165,7 +169,8 @@
 }
 
 CAMLprim value caml_native_pointer_compare_and_swap_int32_unboxed(intnat ptr, int32_t compare, int32_t swap) {
-    return Val_bool(__sync_bool_compare_and_swap((int32_t*)ptr, compare, swap));
+    int32_t expected = compare;
+    return Val_bool(__atomic_compare_exchange_n((int32_t*)ptr, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_native_pointer_compare_and_swap_int32_bytecode(value ptr, value compare, value swap) {
     return caml_native_pointer_compare_and_swap_int32_unboxed(
@@ -173,7 +178,8 @@
 }
 
 CAMLprim value caml_native_pointer_compare_and_swap_nativeint_unboxed(intnat ptr, intnat compare, intnat swap) {
-    return Val_bool(__sync_bool_compare_and_swap((intnat*)ptr, compare, swap));
+    intnat expected = compare;
+    return Val_bool(__atomic_compare_exchange_n((intnat*)ptr, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_native_pointer_compare_and_swap_nativeint_bytecode(value ptr, value compare, value swap) {
     return caml_native_pointer_compare_and_swap_nativeint_unboxed(
@@ -183,7 +189,8 @@
 
 CAMLprim value caml_ext_pointer_compare_and_swap_int_untagged(value ptr, intnat compare, intnat swap) {
     intnat* decode = (intnat*)caml_ext_pointer_decode(ptr);
-    return Val_bool(__sync_bool_compare_and_swap(decode, compare, swap));
+    intnat expected = compare;
+    return Val_bool(__atomic_compare_exchange_n(decode, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_ext_pointer_compare_and_swap_int_bytecode(value ptr, value compare, value swap) {
     return caml_ext_pointer_compare_and_swap_int_untagged(
@@ -192,7 +199,8 @@
 
 CAMLprim value caml_ext_pointer_compare_and_swap_int64_unboxed(value ptr, int64_t compare, int64_t swap) {
     int64_t* decode = (int64_t*)caml_ext_pointer_decode(ptr);
-    return Val_bool(__sync_bool_compare_and_swap(decode, compare, swap));
+    int64_t expected = compare;
+    return Val_bool(__atomic_compare_exchange_n(decode, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_ext_pointer_compare_and_swap_int64_bytecode(value ptr, value compare, value swap) {
     return caml_ext_pointer_compare_and_swap_int64_unboxed(
@@ -201,7 +209,8 @@
 
 CAMLprim value caml_ext_pointer_compare_and_swap_int32_unboxed(value ptr, int32_t compare, int32_t swap) {
     int32_t* decode = (int32_t*)caml_ext_pointer_decode(ptr);
-    return Val_bool(__sync_bool_compare_and_swap(decode, compare, swap));
+    int32_t expected = compare;
+    return Val_bool(__atomic_compare_exchange_n(decode, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_ext_pointer_compare_and_swap_int32_bytecode(value ptr, value compare, value swap) {
     return caml_ext_pointer_compare_and_swap_int32_unboxed(
@@ -210,7 +219,8 @@
 
 CAMLprim value caml_ext_pointer_compare_and_swap_nativeint_unboxed(value ptr, intnat compare, intnat swap) {
     intnat* decode = (intnat*)caml_ext_pointer_decode(ptr);
-    return Val_bool(__sync_bool_compare_and_swap(decode, compare, swap));
+    intnat expected = compare;
+    return Val_bool(__atomic_compare_exchange_n(decode, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_ext_pointer_compare_and_swap_nativeint_bytecode(value ptr, value compare, value swap) {
     return caml_ext_pointer_compare_and_swap_nativeint_unboxed(
@@ -220,7 +230,8 @@
 
 CAMLprim value caml_bigstring_compare_and_swap_int_untagged(value v_bstr, intnat pos, intnat compare, intnat swap) {
     intnat* decode = (intnat*)bigstring_element_at_pos(v_bstr, pos);
-    return Val_bool(__sync_bool_compare_and_swap(decode, compare, swap));
+    intnat expected = compare;
+    return Val_bool(__atomic_compare_exchange_n(decode, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_bigstring_compare_and_swap_int_bytecode(value v_bstr, value pos, value compare, value swap) {
     return caml_bigstring_compare_and_swap_int_untagged(
@@ -229,7 +240,8 @@
 
 CAMLprim value caml_bigstring_compare_and_swap_int64_unboxed(value v_bstr, intnat pos, int64_t compare, int64_t swap) {
     int64_t* decode = (int64_t*)bigstring_element_at_pos(v_bstr, pos);
-    return Val_bool(__sync_bool_compare_and_swap(decode, compare, swap));
+    int64_t expected = compare;
+    return Val_bool(__atomic_compare_exchange_n(decode, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_bigstring_compare_and_swap_int64_bytecode(value v_bstr, value pos, value compare, value swap) {
     return caml_bigstring_compare_and_swap_int64_unboxed(
@@ -238,7 +250,8 @@
 
 CAMLprim value caml_bigstring_compare_and_swap_int32_unboxed(value v_bstr, intnat pos, int32_t compare, int32_t swap) {
     int32_t* decode = (int32_t*)bigstring_element_at_pos(v_bstr, pos);
-    return Val_bool(__sync_bool_compare_and_swap(decode, compare, swap));
+    int32_t expected = compare;
+    return Val_bool(__atomic_compare_exchange_n(decode, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_bigstring_compare_and_swap_int32_bytecode(value v_bstr, value pos, value compare, value swap) {
     return caml_bigstring_compare_and_swap_int32_unboxed(
@@ -247,7 +260,8 @@
 
 CAMLprim value caml_bigstring_compare_and_swap_nativeint_unboxed(value v_bstr, intnat pos, intnat compare, intnat swap) {
     intnat* decode = (intnat*)bigstring_element_at_pos(v_bstr, pos);
-    return Val_bool(__sync_bool_compare_and_swap(decode, compare, swap));
+    intnat expected = compare;
+    return Val_bool(__atomic_compare_exchange_n(decode, &expected, swap, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
 }
 CAMLprim value caml_bigstring_compare_and_swap_nativeint_bytecode(value v_bstr, value pos, value compare, value swap) {
     return caml_bigstring_compare_and_swap_nativeint_unboxed(
