-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy paththread.cpp
More file actions
87 lines (62 loc) · 2.21 KB
/
thread.cpp
File metadata and controls
87 lines (62 loc) · 2.21 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "concurrencpp/threads/thread.h"
#include "concurrencpp/platform_defs.h"
#include <atomic>
#include "concurrencpp/runtime/constants.h"
using concurrencpp::details::thread;
namespace concurrencpp::details {
namespace {
std::uintptr_t generate_thread_id() noexcept {
static std::atomic_uintptr_t s_id_seed = 1;
return s_id_seed.fetch_add(1, std::memory_order_relaxed);
}
struct thread_per_thread_data {
const std::uintptr_t id = generate_thread_id();
};
thread_local thread_per_thread_data s_tl_thread_per_data;
} // namespace
} // namespace concurrencpp::details
std::thread::id thread::get_id() const noexcept {
return m_thread.get_id();
}
std::uintptr_t thread::get_current_virtual_id() noexcept {
return s_tl_thread_per_data.id;
}
bool thread::joinable() const noexcept {
return m_thread.joinable();
}
void thread::join() {
m_thread.join();
}
size_t thread::hardware_concurrency() noexcept {
const auto hc = std::thread::hardware_concurrency();
return (hc != 0) ? hc : consts::k_default_number_of_cores;
}
#ifdef CRCPP_WIN_OS
# include <Windows.h>
# include <VersionHelpers.h>
void thread::set_name(std::string_view name) noexcept {
if (IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN10), LOBYTE(_WIN32_WINNT_WIN10), 14393)) {
// Windows 10 1607 or greater, use SetThreadDescription
const std::wstring utf16_name(name.begin(),
name.end()); // concurrencpp strings are always ASCII (english only)
::SetThreadDescription(::GetCurrentThread(), utf16_name.c_str());
} else {
// Windows version is less than Windows 10, do nothing
}
}
#elif defined(CRCPP_MINGW_OS)
# include <pthread.h>
void thread::set_name(std::string_view name) noexcept {
::pthread_setname_np(::pthread_self(), name.data());
}
#elif defined(CRCPP_UNIX_OS)
# include <pthread.h>
void thread::set_name(std::string_view name) noexcept {
::pthread_setname_np(::pthread_self(), name.data());
}
#elif defined(CRCPP_MAC_OS)
# include <pthread.h>
void thread::set_name(std::string_view name) noexcept {
::pthread_setname_np(name.data());
}
#endif