summaryrefslogtreecommitdiff
path: root/zen/thread.cpp
diff options
context:
space:
mode:
authorB Stack <bgstack15@gmail.com>2020-08-31 20:07:13 -0400
committerB Stack <bgstack15@gmail.com>2020-08-31 20:07:13 -0400
commit8a27fa9c617533e76673ce61a65e2ba869b52208 (patch)
treeacfdfb3e1046db87040477033fda0df76d92916a /zen/thread.cpp
parentMerge branch '11.0' into 'master' (diff)
downloadFreeFileSync-8a27fa9c617533e76673ce61a65e2ba869b52208.tar.gz
FreeFileSync-8a27fa9c617533e76673ce61a65e2ba869b52208.tar.bz2
FreeFileSync-8a27fa9c617533e76673ce61a65e2ba869b52208.zip
add upstream 11.1
Diffstat (limited to 'zen/thread.cpp')
-rw-r--r--zen/thread.cpp36
1 files changed, 8 insertions, 28 deletions
diff --git a/zen/thread.cpp b/zen/thread.cpp
index 6b763f39..89fa0233 100644
--- a/zen/thread.cpp
+++ b/zen/thread.cpp
@@ -6,52 +6,32 @@
#include "thread.h"
#include <sys/prctl.h>
- #include <unistd.h>
- #include <sys/syscall.h>
using namespace zen;
-void zen::setCurrentThreadName(const char* threadName)
+void zen::setCurrentThreadName(const Zstring& threadName)
{
- ::prctl(PR_SET_NAME, threadName, 0, 0, 0);
+ ::prctl(PR_SET_NAME, threadName.c_str(), 0, 0, 0);
}
namespace
{
-uint64_t getThreadIdNative()
-{
- const pid_t tid = ::syscall(SYS_gettid); //no-fail
- //"Invalid thread and process IDs": https://devblogs.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;
-}
-
-
-const uint64_t globalMainThreadId = getThreadId(); //avoid code-gen for "magic static"!
-}
-
-
-uint64_t zen::getThreadId()
-{
- thread_local const uint64_t tid = getThreadIdNative(); //buffer to get predictable perf characteristics
- return tid;
+//don't make this a function-scope static (avoid code-gen for "magic static")
+const std::thread::id globalMainThreadId = std::this_thread::get_id();
}
-uint64_t zen::getMainThreadId()
+bool zen::runningOnMainThread()
{
- //don't make this a function-scope static (avoid code-gen for "magic static")
- if (globalMainThreadId == 0) //might be called during static initialization
- return getThreadId();
+ if (globalMainThreadId == std::thread::id()) //called during static initialization!
+ return true;
- return globalMainThreadId;
+ return std::this_thread::get_id() == globalMainThreadId;
}
bgstack15