diff options
author | Daniel Wilhelm <shieldwed@outlook.com> | 2018-06-30 12:43:08 +0200 |
---|---|---|
committer | Daniel Wilhelm <shieldwed@outlook.com> | 2018-06-30 12:43:08 +0200 |
commit | a98326eb2954ac1e79f5eac28dbeab3ec15e047f (patch) | |
tree | bb16257a1894b488e365851273735ec13a9442ef /zen/thread.cpp | |
parent | 10.0 (diff) | |
download | FreeFileSync-a98326eb2954ac1e79f5eac28dbeab3ec15e047f.tar.gz FreeFileSync-a98326eb2954ac1e79f5eac28dbeab3ec15e047f.tar.bz2 FreeFileSync-a98326eb2954ac1e79f5eac28dbeab3ec15e047f.zip |
10.1
Diffstat (limited to 'zen/thread.cpp')
-rwxr-xr-x | zen/thread.cpp | 55 |
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; +} |