[clang][bytecode][NFC] Only get expr when checking for UB (#125397)
[llvm-project.git] / llvm / lib / Support / ExponentialBackoff.cpp
blob7e68cf67ad3857e8b3cb5eedd4bb49c3b9949684
1 //===- llvm/Support/ExponentialBackoff.h ------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/ExponentialBackoff.h"
10 #include <thread>
12 using namespace llvm;
14 bool ExponentialBackoff::waitForNextAttempt() {
15 auto Now = std::chrono::steady_clock::now();
16 if (Now >= EndTime)
17 return false;
19 duration CurMaxWait = std::min(MinWait * CurrentMultiplier, MaxWait);
20 std::uniform_int_distribution<uint64_t> Dist(MinWait.count(),
21 CurMaxWait.count());
22 // Use random_device directly instead of a PRNG as uniform_int_distribution
23 // often only takes a few samples anyway.
24 duration WaitDuration = std::min(duration(Dist(RandDev)), EndTime - Now);
25 if (CurMaxWait < MaxWait)
26 CurrentMultiplier *= 2;
27 std::this_thread::sleep_for(WaitDuration);
28 return true;