diff options
author | eeight <eeight@yandex-team.ru> | 2022-05-23 23:54:56 +0300 |
---|---|---|
committer | eeight <eeight@yandex-team.ru> | 2022-05-23 23:54:56 +0300 |
commit | cc9b48149be0bca6e6c350983ace4fc660902b80 (patch) | |
tree | dc0cad9a1f9f243b99e637d9718314ef8226e463 /util/system/spin_wait.cpp | |
parent | e8aa5160e09378c2bb8f8ed7c806a52336a34810 (diff) | |
download | ydb-cc9b48149be0bca6e6c350983ace4fc660902b80.tar.gz |
IGNIETFERRO-1105 Get rid of TAtomic in spin_wait
ref:788357fd9a851f54e97a8e0f133dcb3c3e275f92
Diffstat (limited to 'util/system/spin_wait.cpp')
-rw-r--r-- | util/system/spin_wait.cpp | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/util/system/spin_wait.cpp b/util/system/spin_wait.cpp index caac8d5f1f..ba12529111 100644 --- a/util/system/spin_wait.cpp +++ b/util/system/spin_wait.cpp @@ -6,17 +6,20 @@ #include <util/digest/numeric.h> #include <util/generic/utility.h> -template <class T> -static inline T RandomizeSleepTime(T t) noexcept { - static TAtomic counter = 0; - const T rndNum = IntHash((T)AtomicIncrement(counter)); +#include <atomic> - return (t * (T)4 + (rndNum % t) * (T)2) / (T)5; -} +namespace { + unsigned RandomizeSleepTime(unsigned t) noexcept { + static std::atomic<unsigned> counter = 0; + const unsigned rndNum = IntHash(++counter); + + return (t * 4 + (rndNum % t) * 2) / 5; + } -//arbitrary values -#define MIN_SLEEP_TIME 500 -#define MAX_SPIN_COUNT 0x7FF + //arbitrary values + constexpr unsigned MIN_SLEEP_TIME = 500; + constexpr unsigned MAX_SPIN_COUNT = 0x7FF; +} TSpinWait::TSpinWait() noexcept : T(MIN_SLEEP_TIME) @@ -32,7 +35,7 @@ void TSpinWait::Sleep() noexcept { } else if ((C & MAX_SPIN_COUNT) == 0) { usleep(RandomizeSleepTime(T)); - T = Min<unsigned>((T * 3) / 2, 20000); + T = Min<unsigned>(T * 3 / 2, 20000); } else { SpinLockPause(); } |