HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
thread.hpp
1// Copyright Take Vos 2019-2020.
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
5#pragma once
6
7#include "required.hpp"
8#include "architecture.hpp"
9#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
10#include <intrin.h>
11#endif
12#include <thread>
13#include <string_view>
14#include <functional>
15#include <atomic>
16#include <chrono>
17#include <bit>
18
19namespace hi::inline v1 {
20
21#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
22constexpr std::size_t maximum_num_cpus = 64;
23#elif HI_OPERATING_SYSTEM == HI_OS_LINUX || HI_OPERATING_SYSTEM == HI_OS_MACOS
24constexpr std::size_t maximum_num_cpus = CPU_SETSIZE;
25#endif
26
27#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
28using thread_id = uint32_t;
29#else
30using thread_id = uint64_t;
33inline thread_local thread_id current_thread_id_dummy = 0;
34#endif
35
39[[nodiscard]] inline thread_id current_thread_id() noexcept
40{
41#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
42 // Thread IDs on Win32 are guaranteed to be not zero.
43 constexpr uint64_t NT_TIB_CurrentThreadID = 0x48;
44 return __readgsdword(NT_TIB_CurrentThreadID);
45#else
46 // Addresses can not be zero.
47 return reinterpret_cast<uint64_t>(&current_thread_id_dummy);
48#endif
49}
50
57void set_thread_name(std::string_view name) noexcept;
58
68[[nodiscard]] std::string get_thread_name(thread_id id) noexcept;
69
75[[nodiscard]] std::vector<bool> process_affinity_mask() noexcept;
76
86std::vector<bool> set_thread_affinity_mask(std::vector<bool> const &mask);
87
97std::vector<bool> set_thread_affinity(std::size_t cpu_id);
98
108std::size_t advance_thread_affinity(std::size_t &cpu) noexcept;
109
114[[nodiscard]] std::size_t current_cpu_id() noexcept;
115
116} // namespace hi::inline v1
This file includes required definitions.
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
STL namespace.