[libc-commits] [libc] [libc][cpp] add `atomic_signal_fence` (PR #82138)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Sat Feb 17 15:33:32 PST 2024


https://github.com/SchrodingerZhu created https://github.com/llvm/llvm-project/pull/82138

Add `atomic_signal_fence`. This will be useful in https://gustedt.gitlabpages.inria.fr/c23-library/#memset_explicit.

>From 5c995fd00a1c1fbdbdde99ec9db145b81e02e99f Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Sat, 17 Feb 2024 18:30:57 -0500
Subject: [PATCH] [libc][cpp] add atomic_signal_fence

---
 libc/src/__support/CPP/atomic.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/libc/src/__support/CPP/atomic.h b/libc/src/__support/CPP/atomic.h
index 1c4478dfeab6de..61e2742661f21a 100644
--- a/libc/src/__support/CPP/atomic.h
+++ b/libc/src/__support/CPP/atomic.h
@@ -161,6 +161,21 @@ LIBC_INLINE void atomic_thread_fence(MemoryOrder mem_ord) {
 #endif
 }
 
+// Establishes memory synchronization ordering of non-atomic and relaxed atomic
+// accesses, as instructed by order, between a thread and a signal handler
+// executed on the same thread. This is equivalent to atomic_thread_fence,
+// except no instructions for memory ordering are issued. Only reordering of
+// the instructions by the compiler is suppressed as order instructs.
+LIBC_INLINE void atomic_signal_fence(MemoryOrder mem_ord) {
+#if __has_builtin(__atomic_signal_fence)
+  __atomic_signal_fence(int(mem_ord));
+#else
+  // if the builtin is not ready, use asm as a full compiler barrier.
+  (void)mem_ord;
+  asm volatile("" ::: "memory");
+#endif
+}
+
 } // namespace cpp
 } // namespace LIBC_NAMESPACE
 



More information about the libc-commits mailing list