[llvm] [llvm][Support] Add ExponentialBackoff helper (PR #81206)

Michael Spencer via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 10 00:20:06 PST 2024


================
@@ -295,29 +295,15 @@ LockFileManager::waitForUnlock(const unsigned MaxSeconds) {
     return Res_Success;
 
   // Since we don't yet have an event-based method to wait for the lock file,
-  // implement randomized exponential backoff, similar to Ethernet collision
+  // use randomized exponential backoff, similar to Ethernet collision
   // algorithm. This improves performance on machines with high core counts
   // when the file lock is heavily contended by multiple clang processes
-  const unsigned long MinWaitDurationMS = 10;
-  const unsigned long MaxWaitMultiplier = 50; // 500ms max wait
-  unsigned long WaitMultiplier = 1;
-  unsigned long ElapsedTimeSeconds = 0;
+  using namespace std::chrono_literals;
+  ExponentialBackoff Backoff(std::chrono::seconds(MaxSeconds), 10ms, 500ms);
 
-  std::random_device Device;
-  std::default_random_engine Engine(Device());
-
-  auto StartTime = std::chrono::steady_clock::now();
-
-  do {
+  // Wait first as this is only called when the lock is known to be held.
+  while (Backoff.waitForNextAttempt()) {
     // FIXME: implement event-based waiting
----------------
Bigcheese wrote:

The difficult part of adding event based waiting is doing so portably while handling crashed processes correctly. I'm also looking at adding global named synchronization (`sem_open`/ `CreateSemaphore`) to LLVM, but it's not clear yet if that will be able to replace all usages of exponential backoff, we may still need it as a fallback.

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


More information about the llvm-commits mailing list