[libc-commits] [libc] bf02c84 - [libc] Use file lock to join newline on RPC puts call (#73373)

via libc-commits libc-commits at lists.llvm.org
Mon Nov 27 06:41:20 PST 2023


Author: Joseph Huber
Date: 2023-11-27T08:41:15-06:00
New Revision: bf02c84cb8e7f60dfc12f822cd2bb71890b03101

URL: https://github.com/llvm/llvm-project/commit/bf02c84cb8e7f60dfc12f822cd2bb71890b03101
DIFF: https://github.com/llvm/llvm-project/commit/bf02c84cb8e7f60dfc12f822cd2bb71890b03101.diff

LOG: [libc] Use file lock to join newline on RPC puts call (#73373)

Summary:
The puts call appends a newline. With multiple threads, this can be done
out of order such that another thread puts something before we finish
appending the newline. Add a flockfile and funlockfile to ensure that
the whole string is printed before another string can appear.

Added: 
    

Modified: 
    libc/utils/gpu/server/rpc_server.cpp

Removed: 
    


################################################################################
diff  --git a/libc/utils/gpu/server/rpc_server.cpp b/libc/utils/gpu/server/rpc_server.cpp
index 05e900edc6993bb..a2e5d0fd5a833f6 100644
--- a/libc/utils/gpu/server/rpc_server.cpp
+++ b/libc/utils/gpu/server/rpc_server.cpp
@@ -78,10 +78,12 @@ struct Server {
 
       port->recv_n(strs, sizes, [&](uint64_t size) { return new char[size]; });
       port->send([&](rpc::Buffer *buffer, uint32_t id) {
-        buffer->data[0] = fwrite(strs[id], 1, sizes[id], files[id]);
+        flockfile(files[id]);
+        buffer->data[0] = fwrite_unlocked(strs[id], 1, sizes[id], files[id]);
         if (port->get_opcode() == RPC_WRITE_TO_STDOUT_NEWLINE &&
             buffer->data[0] == sizes[id])
-          buffer->data[0] += fwrite("\n", 1, 1, files[id]);
+          buffer->data[0] += fwrite_unlocked("\n", 1, 1, files[id]);
+        funlockfile(files[id]);
         delete[] reinterpret_cast<uint8_t *>(strs[id]);
       });
       break;


        


More information about the libc-commits mailing list