Skip to content

[libc][cpp] add atomic_signal_fence #82138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2024

Conversation

SchrodingerZhu
Copy link
Contributor

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

@llvmbot llvmbot added the libc label Feb 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 17, 2024

@llvm/pr-subscribers-libc

Author: Schrodinger ZHU Yifan (SchrodingerZhu)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/82138.diff

1 Files Affected:

  • (modified) libc/src/__support/CPP/atomic.h (+15)
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
 

@SchrodingerZhu
Copy link
Contributor Author

updated.

@SchrodingerZhu SchrodingerZhu merged commit 8f7ae64 into llvm:main Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants