[compiler-rt] [scudo] Only init RingBuffer when needed. (PR #85994)

Christopher Ferris via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 26 20:10:30 PDT 2024


================
@@ -1594,14 +1598,19 @@ class Allocator {
     RB->StackDepotSize = StackDepotSize;
     RB->RawStackDepotMap = DepotMap;
 
-    atomic_store(&RingBufferAddress, reinterpret_cast<uptr>(RB),
-                 memory_order_release);
+    // If multiple threads try to initialize at the same time, let one thread
+    // win and throw away the work done in the other threads. Since this
+    // path is only meant for debugging, a race that results in work being
+    // discarded should not matter.
+    uptr EmptyPtr = 0;
+    if (!atomic_compare_exchange_strong(&RingBufferAddress, &EmptyPtr,
+                                        reinterpret_cast<uptr>(RB),
+                                        memory_order_acquire)) {
+      unmapRingBuffer(RB);
+    }
----------------
cferris1000 wrote:

I added a lock, but just in the init function. I think that should be sufficient to prevent multiple threads trying to init at the same time.

https://github.com/llvm/llvm-project/pull/85994


More information about the llvm-commits mailing list