summaryrefslogtreecommitdiff
path: root/zen/thread.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'zen/thread.cpp')
-rwxr-xr-xzen/thread.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/zen/thread.cpp b/zen/thread.cpp
new file mode 100755
index 00000000..8016d4a9
--- /dev/null
+++ b/zen/thread.cpp
@@ -0,0 +1,55 @@
+// *****************************************************************************
+// * This file is part of the FreeFileSync project. It is distributed under *
+// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0 *
+// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
+// *****************************************************************************
+
+#include "thread.h"
+ #include <sys/prctl.h>
+ #include <unistd.h>
+ #include <sys/syscall.h>
+
+using namespace zen;
+
+
+
+
+void zen::setCurrentThreadName(const char* threadName)
+{
+ ::prctl(PR_SET_NAME, threadName, 0, 0, 0);
+
+}
+
+
+namespace
+{
+uint64_t getThreadIdNative()
+{
+ const pid_t tid = ::syscall(SYS_gettid); //no-fail
+ //"Invalid thread and process IDs": https://blogs.msdn.microsoft.com/oldnewthing/20040223-00/?p=40503
+ //if (tid == 0) -> not sure this holds on Linux, too!
+ // throw std::runtime_error(std::string(__FILE__) + "[" + numberTo<std::string>(__LINE__) + "] Failed to get thread ID.");
+ static_assert(sizeof(uint64_t) >= sizeof(tid));
+ return tid;
+}
+
+
+struct InitMainThreadIdOnStartup
+{
+ InitMainThreadIdOnStartup() { getMainThreadId(); }
+} startupInitMainThreadId;
+}
+
+
+uint64_t zen::getThreadId()
+{
+ thread_local const uint64_t tid = getThreadIdNative(); //buffer to get predictable perf characteristics
+ return tid;
+}
+
+
+uint64_t zen::getMainThreadId()
+{
+ static const uint64_t mainThreadId = getThreadId();
+ return mainThreadId;
+}
bgstack15