aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/thread.i
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/thread.i
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/thread.i')
-rw-r--r--util/system/thread.i52
1 files changed, 52 insertions, 0 deletions
diff --git a/util/system/thread.i b/util/system/thread.i
new file mode 100644
index 0000000000..8cba505473
--- /dev/null
+++ b/util/system/thread.i
@@ -0,0 +1,52 @@
+//do not use directly
+#pragma once
+#include "platform.h"
+
+#if defined(_win_)
+ #include "winint.h"
+ #include <process.h>
+
+ typedef HANDLE THREADHANDLE;
+#else
+ #include <pthread.h>
+ #include <sched.h>
+ #include <errno.h>
+ #include <string.h>
+
+ typedef pthread_t THREADHANDLE;
+#endif
+
+#if defined(_freebsd_)
+ #include <pthread_np.h>
+#elif defined(_linux_)
+ #include <sys/prctl.h>
+#endif
+
+#include <util/digest/numeric.h>
+
+static inline size_t SystemCurrentThreadIdImpl() noexcept {
+ #if defined(_unix_)
+ return (size_t)pthread_self();
+ #elif defined(_win_)
+ return (size_t)GetCurrentThreadId();
+ #else
+ #error todo
+ #endif
+}
+
+template <class T>
+static inline T ThreadIdHashFunction(T t) noexcept {
+ /*
+ * we must permute threadid bits, because some strange platforms(such Linux)
+ * have strange threadid numeric properties
+ *
+ * Because they are alligned pointers to pthread_t rather that tid.
+ * Currently there is no way to get tid without syscall (slightly slower)
+ * (pthread_getthreadid_np is not implemeted in glibc/musl for some reason).
+ */
+ return IntHash(t);
+}
+
+static inline size_t SystemCurrentThreadId() noexcept {
+ return ThreadIdHashFunction(SystemCurrentThreadIdImpl());
+}