[libc-commits] [PATCH] D153573: [libc] Move fences into outbox/wait-for-ownership test

Jon Chesterfield via Phabricator via libc-commits libc-commits at lists.llvm.org
Thu Jun 22 10:00:35 PDT 2023


JonChesterfield created this revision.
JonChesterfield added a reviewer: jhuber6.
Herald added subscribers: libc-commits, yaxunl.
Herald added projects: libc-project, All.
JonChesterfield requested review of this revision.

Also moves the wait-until-inbox-changes test into a shared method.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153573

Files:
  libc/src/__support/RPC/rpc.h


Index: libc/src/__support/RPC/rpc.h
===================================================================
--- libc/src/__support/RPC/rpc.h
+++ libc/src/__support/RPC/rpc.h
@@ -137,10 +137,22 @@
   /// cheaper than calling load_outbox to get the value to store.
   LIBC_INLINE uint32_t invert_outbox(uint64_t index, uint32_t current_outbox) {
     uint32_t inverted_outbox = !current_outbox;
+    atomic_thread_fence(cpp::MemoryOrder::RELEASE);
     outbox[index].store(inverted_outbox, cpp::MemoryOrder::RELAXED);
     return inverted_outbox;
   }
 
+  // Given current outbox and inbox values, wait until the inbox changes
+  // to indicate that this thread owns the buffer element.
+  LIBC_INLINE uint32_t wait_for_ownership(uint64_t index, uint32_t outbox,
+                                          uint32_t in) {
+    while (buffer_unavailable(in, outbox)) {
+      sleep_briefly();
+      in = process.load_inbox(index);
+    }
+    atomic_thread_fence(cpp::MemoryOrder::ACQUIRE);
+  }
+
   /// Determines if this process needs to wait for ownership of the buffer. We
   /// invert the condition on one of the processes to indicate that if one
   /// process owns the buffer then the other does not.
@@ -346,15 +358,11 @@
 LIBC_INLINE void Port<T, S>::send(F fill) {
   uint32_t in = owns_buffer ? out ^ T : process.load_inbox(index);
 
-  // We need to wait until we own the buffer before sending.
-  while (Process<T, S>::buffer_unavailable(in, out)) {
-    sleep_briefly();
-    in = process.load_inbox(index);
-  }
+  // We need to wait until we own the buffer before receiving.
+  Process<T, S>::wait_for_ownership(index, in, out);
 
   // Apply the \p fill function to initialize the buffer and release the memory.
   process.invoke_rpc(fill, process.packet[index]);
-  atomic_thread_fence(cpp::MemoryOrder::RELEASE);
   out = process.invert_outbox(index, out);
   owns_buffer = false;
   receive = false;
@@ -374,11 +382,7 @@
   uint32_t in = owns_buffer ? out ^ T : process.load_inbox(index);
 
   // We need to wait until we own the buffer before receiving.
-  while (Process<T, S>::buffer_unavailable(in, out)) {
-    sleep_briefly();
-    in = process.load_inbox(index);
-  }
-  atomic_thread_fence(cpp::MemoryOrder::ACQUIRE);
+  Process<T, S>::wait_for_ownership(index, in, out);
 
   // Apply the \p use function to read the memory out of the buffer.
   process.invoke_rpc(use, process.packet[index]);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153573.533671.patch
Type: text/x-patch
Size: 2419 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230622/234a12e1/attachment.bin>


More information about the libc-commits mailing list