aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/spin_wait.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/system/spin_wait.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/spin_wait.cpp')
-rw-r--r--util/system/spin_wait.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/util/system/spin_wait.cpp b/util/system/spin_wait.cpp
new file mode 100644
index 0000000000..e27045e74f
--- /dev/null
+++ b/util/system/spin_wait.cpp
@@ -0,0 +1,40 @@
+#include "spin_wait.h"
+#include "yield.h"
+#include "compat.h"
+#include "thread.h"
+#include "spinlock.h"
+
+#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));
+
+ return (t * (T)4 + (rndNum % t) * (T)2) / (T)5;
+}
+
+//arbitrary values
+#define MIN_SLEEP_TIME 500
+#define MAX_SPIN_COUNT 0x7FF
+
+TSpinWait::TSpinWait() noexcept
+ : T(MIN_SLEEP_TIME)
+ , C(0)
+{
+}
+
+void TSpinWait::Sleep() noexcept {
+ ++C;
+
+ if (C == MAX_SPIN_COUNT) {
+ ThreadYield();
+ } else if ((C & MAX_SPIN_COUNT) == 0) {
+ usleep(RandomizeSleepTime(T));
+
+ T = Min<unsigned>((T * 3) / 2, 20000);
+ } else {
+ SpinLockPause();
+ }
+}