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 TT_OPERATING_SYSTEM == TT_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 tt {
20
27void set_thread_name(std::string_view name);
28
29#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
30constexpr size_t maximum_num_cpus = 64;
31#elif TT_OPERATING_SYSTEM == TT_OS_LINUX || TT_OPERATING_SYSTEM == TT_OS_MACOS
32constexpr size_t maximum_num_cpus = CPU_SETSIZE;
33#endif
34
35#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
36using thread_id = uint32_t;
37#else
38using thread_id = uint64_t;
41inline thread_local thread_id current_thread_id_dummy = 0;
42#endif
43
47[[nodiscard]] inline thread_id current_thread_id() noexcept
48{
49#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
50 // Thread IDs on Win32 are guaranteed to be not zero.
51 constexpr uint64_t NT_TIB_CurrentThreadID = 0x48;
52 return __readgsdword(NT_TIB_CurrentThreadID);
53#else
54 // Addresses can not be zero.
55 return reinterpret_cast<uint64_t>(&current_thread_id_dummy);
56#endif
57}
58
61[[nodiscard]] bool is_gui_thread() noexcept;
62
67void run_on_gui_thread(std::function<void()> function) noexcept;
68
74[[nodiscard]] std::vector<bool> process_affinity_mask() noexcept;
75
85std::vector<bool> set_thread_affinity_mask(std::vector<bool> const &mask);
86
96std::vector<bool> set_thread_affinity(size_t cpu_id);
97
107size_t advance_thread_affinity(size_t &cpu) noexcept;
108
113[[nodiscard]] size_t current_cpu_id() noexcept;
114
115
116}
117
STL namespace.