Skip to content

Commit a8112d0

Browse files
committed
use scoped atomic
1 parent 689bef2 commit a8112d0

File tree

1 file changed

+10
-4
lines changed
  • libc/src/__support/RPC

1 file changed

+10
-4
lines changed

libc/src/__support/RPC/rpc.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ namespace rpc {
3737
#define __scoped_atomic_fetch_and(src, val, ord, scp) \
3838
__atomic_fetch_and(src, val, ord)
3939
#endif
40+
#if !__has_builtin(__scoped_atomic_thread_fence)
41+
#define __scoped_atomic_thread_fence(ord, scp) __atomic_thread_fence(ord)
42+
#endif
4043

4144
/// A fixed size channel used to communicate between the RPC client and server.
4245
struct Buffer {
@@ -127,7 +130,7 @@ template <bool Invert> struct Process {
127130
/// cheaper than calling load_outbox to get the value to store.
128131
LIBC_INLINE uint32_t invert_outbox(uint32_t index, uint32_t current_outbox) {
129132
uint32_t inverted_outbox = !current_outbox;
130-
__atomic_thread_fence(__ATOMIC_RELEASE);
133+
__scoped_atomic_thread_fence(__ATOMIC_RELEASE, __MEMORY_SCOPE_SYSTEM);
131134
__scoped_atomic_store_n(&outbox[index], inverted_outbox, __ATOMIC_RELAXED,
132135
__MEMORY_SCOPE_SYSTEM);
133136
return inverted_outbox;
@@ -141,7 +144,7 @@ template <bool Invert> struct Process {
141144
sleep_briefly();
142145
in = load_inbox(lane_mask, index);
143146
}
144-
__atomic_thread_fence(__ATOMIC_ACQUIRE);
147+
__scoped_atomic_thread_fence(__ATOMIC_ACQUIRE, __MEMORY_SCOPE_SYSTEM);
145148
}
146149

147150
/// The packet is a linearly allocated array of buffers used to communicate
@@ -198,7 +201,7 @@ template <bool Invert> struct Process {
198201
// inlining the current function.
199202
bool holding_lock = lane_mask != packed;
200203
if (holding_lock)
201-
__atomic_thread_fence(__ATOMIC_ACQUIRE);
204+
__scoped_atomic_thread_fence(__ATOMIC_ACQUIRE, __MEMORY_SCOPE_DEVICE);
202205
return holding_lock;
203206
}
204207

@@ -207,7 +210,7 @@ template <bool Invert> struct Process {
207210
[[clang::convergent]] LIBC_INLINE void unlock(uint64_t lane_mask,
208211
uint32_t index) {
209212
// Do not move any writes past the unlock.
210-
__atomic_thread_fence(__ATOMIC_RELEASE);
213+
__scoped_atomic_thread_fence(__ATOMIC_RELEASE, __MEMORY_SCOPE_DEVICE);
211214

212215
// Use exactly one thread to clear the nth bit in the lock array Must
213216
// restrict to a single thread to avoid one thread dropping the lock, then
@@ -597,6 +600,9 @@ LIBC_INLINE Server::Port Server::open(uint32_t lane_size) {
597600
#undef __scoped_atomic_fetch_or
598601
#undef __scoped_atomic_fetch_and
599602
#endif
603+
#if !__has_builtin(__scoped_atomic_thread_fence)
604+
#undef __scoped_atomic_thread_fence
605+
#endif
600606

601607
} // namespace rpc
602608
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)