blob: 8016d4a992a781982439022bcfb0349aabd38927 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
}
|