From a98326eb2954ac1e79f5eac28dbeab3ec15e047f Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Sat, 30 Jun 2018 12:43:08 +0200 Subject: 10.1 --- zen/thread.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 zen/thread.cpp (limited to 'zen/thread.cpp') 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 + #include + #include + +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(__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; +} -- cgit