HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
thread.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "../utility/module.hpp"
12#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
13#include <intrin.h>
14#endif
15#include <thread>
16#include <string_view>
17#include <functional>
18#include <atomic>
19#include <chrono>
20#include <bit>
21
22namespace hi { inline namespace v1 {
23
24#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
25constexpr std::size_t maximum_num_cpus = 64;
26#elif HI_OPERATING_SYSTEM == HI_OS_LINUX || HI_OPERATING_SYSTEM == HI_OS_MACOS
27constexpr std::size_t maximum_num_cpus = CPU_SETSIZE;
28#endif
29
30#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
31using thread_id = uint32_t;
32#else
33using thread_id = uint64_t;
36inline thread_local thread_id current_thread_id_dummy = 0;
37#endif
38
44[[nodiscard]] inline thread_id current_thread_id() noexcept
45{
46#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
47 // Thread IDs on Win32 are guaranteed to be not zero.
48 constexpr uint64_t NT_TIB_CurrentThreadID = 0x48;
49 return __readgsdword(NT_TIB_CurrentThreadID);
50#else
51 // Addresses can not be zero.
52 return reinterpret_cast<uint64_t>(&current_thread_id_dummy);
53#endif
54}
55
64void set_thread_name(std::string_view name) noexcept;
65
76[[nodiscard]] std::string get_thread_name(thread_id id) noexcept;
77
85
97
109
121
127[[nodiscard]] std::size_t current_cpu_id() noexcept;
128
129}} // namespace hi::v1
std::vector< bool > process_affinity_mask()
Get the current process CPU affinity mask.
std::string get_thread_name(thread_id id) noexcept
Get the thread name of a thread id.
void set_thread_name(std::string_view name) noexcept
Set the name of the current thread.
std::vector< bool > set_thread_affinity_mask(std::vector< bool > const &mask)
Set the current thread CPU affinity mask.
std::size_t current_cpu_id() noexcept
Get the current CPU id.
std::size_t advance_thread_affinity(std::size_t &cpu) noexcept
Advance thread affinity to the next CPU.
std::vector< bool > set_thread_affinity(std::size_t cpu_id)
Set the current thread CPU affinity to a single CPU.
thread_id current_thread_id() noexcept
Get the current thread id.
Definition thread.hpp:44
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Definition cpu_id.hpp:21