From 8a27fa9c617533e76673ce61a65e2ba869b52208 Mon Sep 17 00:00:00 2001 From: B Stack Date: Mon, 31 Aug 2020 20:07:13 -0400 Subject: add upstream 11.1 --- zen/thread.cpp | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) (limited to 'zen/thread.cpp') 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 - #include - #include 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(__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; } -- cgit