Skip to content

Commit 8f7ae64

Browse files
[libc][cpp] add atomic_signal_fence (#82138)
Add `atomic_signal_fence`. This will be useful in https://gustedt.gitlabpages.inria.fr/c23-library/#memset_explicit.
1 parent 2f1e33d commit 8f7ae64

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

libc/src/__support/CPP/atomic.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,27 @@ template <typename T> struct Atomic {
150150
};
151151

152152
// Issue a thread fence with the given memory ordering.
153-
LIBC_INLINE void atomic_thread_fence(MemoryOrder mem_ord) {
153+
LIBC_INLINE void atomic_thread_fence([[maybe_unused]] MemoryOrder mem_ord) {
154154
// The NVPTX backend currently does not support atomic thread fences so we use a
155155
// full system fence instead.
156156
#ifdef LIBC_TARGET_ARCH_IS_NVPTX
157-
(void)mem_ord;
158157
__nvvm_membar_sys();
159158
#else
160-
__atomic_thread_fence(int(mem_ord));
159+
__atomic_thread_fence(static_cast<int>(mem_ord));
160+
#endif
161+
}
162+
163+
// Establishes memory synchronization ordering of non-atomic and relaxed atomic
164+
// accesses, as instructed by order, between a thread and a signal handler
165+
// executed on the same thread. This is equivalent to atomic_thread_fence,
166+
// except no instructions for memory ordering are issued. Only reordering of
167+
// the instructions by the compiler is suppressed as order instructs.
168+
LIBC_INLINE void atomic_signal_fence([[maybe_unused]] MemoryOrder mem_ord) {
169+
#if __has_builtin(__atomic_signal_fence)
170+
__atomic_signal_fence(static_cast<int>(mem_ord));
171+
#else
172+
// if the builtin is not ready, use asm as a full compiler barrier.
173+
asm volatile("" ::: "memory");
161174
#endif
162175
}
163176

0 commit comments

Comments
 (0)