aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/futex_like.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 /library/cpp/messagebus/futex_like.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/messagebus/futex_like.cpp')
-rw-r--r--library/cpp/messagebus/futex_like.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/library/cpp/messagebus/futex_like.cpp b/library/cpp/messagebus/futex_like.cpp
new file mode 100644
index 0000000000..7f965126db
--- /dev/null
+++ b/library/cpp/messagebus/futex_like.cpp
@@ -0,0 +1,55 @@
+#include <util/system/platform.h>
+
+#ifdef _linux_
+#include <sys/syscall.h>
+#include <linux/futex.h>
+
+#if !defined(SYS_futex)
+#define SYS_futex __NR_futex
+#endif
+#endif
+
+#include <errno.h>
+
+#include <util/system/yassert.h>
+
+#include "futex_like.h"
+
+#ifdef _linux_
+namespace {
+ int futex(int* uaddr, int op, int val, const struct timespec* timeout,
+ int* uaddr2, int val3) {
+ return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3);
+ }
+}
+#endif
+
+void TFutexLike::Wake(size_t count) {
+ Y_ASSERT(count > 0);
+#ifdef _linux_
+ if (count > unsigned(Max<int>())) {
+ count = Max<int>();
+ }
+ int r = futex(&Value, FUTEX_WAKE, count, nullptr, nullptr, 0);
+ Y_VERIFY(r >= 0, "futex_wake failed: %s", strerror(errno));
+#else
+ TGuard<TMutex> guard(Mutex);
+ if (count == 1) {
+ CondVar.Signal();
+ } else {
+ CondVar.BroadCast();
+ }
+#endif
+}
+
+void TFutexLike::Wait(int expected) {
+#ifdef _linux_
+ int r = futex(&Value, FUTEX_WAIT, expected, nullptr, nullptr, 0);
+ Y_VERIFY(r >= 0 || errno == EWOULDBLOCK, "futex_wait failed: %s", strerror(errno));
+#else
+ TGuard<TMutex> guard(Mutex);
+ if (expected == Get()) {
+ CondVar.WaitI(Mutex);
+ }
+#endif
+}