HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
thread_win32_impl.hpp
1// Copyright Take Vos 2021-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
6
7#include "thread_intf.hpp"
8#include "unfair_mutex.hpp"
9#include "../utility/utility.hpp"
10#include "../char_maps/module.hpp"
11#include "../macros.hpp"
12#include <intrin.h>
13#include <mutex>
14#include <string>
15#include <unordered_map>
16
17hi_export_module(hikogui.conurrency.thread);
18
19namespace hi::inline v1 {
20
21[[nodiscard]] inline thread_id current_thread_id() noexcept
22{
23 // Thread IDs on Win32 are guaranteed to be not zero.
24 constexpr uint64_t NT_TIB_CurrentThreadID = 0x48;
26}
27
28inline void set_thread_name(std::string_view name) noexcept
29{
30 hilet wname = hi::to_wstring(name);
32
33 hilet lock = std::scoped_lock(detail::thread_names_mutex);
34 detail::thread_names.emplace(current_thread_id(), std::string{name});
35}
36
37inline std::vector<bool> mask_int_to_vec(DWORD_PTR rhs) noexcept
38{
39 auto r = std::vector<bool>{};
40
41 r.resize(64);
42 for (std::size_t i = 0; i != r.size(); ++i) {
43 r[i] = to_bool(rhs & (DWORD_PTR{1} << i));
44 }
45
46 return r;
47}
48
49inline DWORD_PTR mask_vec_to_int(std::vector<bool> const &rhs) noexcept
50{
51 DWORD r = 0;
52 for (std::size_t i = 0; i != rhs.size(); ++i) {
53 r |= rhs[i] ? (DWORD{1} << i) : 0;
54 }
55 return r;
56}
57
59{
61 DWORD_PTR system_mask;
62
64
66 throw os_error(std::format("Could not get process affinity mask.", get_last_error_message()));
67 }
68
69 return mask_int_to_vec(process_mask);
70}
71
73{
74 hilet mask_ = mask_vec_to_int(mask);
75
77
79 if (old_mask == 0) {
80 throw os_error(std::format("Could not set the thread affinity. '{}'", get_last_error_message()));
81 }
82
83 return mask_int_to_vec(old_mask);
84}
85
87{
88 hilet index = GetCurrentProcessorNumber();
89 hi_assert(index < 64);
90 return index;
91}
92
93} // namespace hi::inline v1
Definition of the unfair_mutex.
Rules for working with win32 headers.
constexpr std::wstring to_wstring(std::u32string_view rhs) noexcept
Conversion from UTF-32 to wide-string (UTF-16/32).
Definition to_string.hpp:156
std::vector< bool > process_affinity_mask()
Get the current process CPU affinity mask.
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.
thread_id current_thread_id() noexcept
Get the current thread id.
DOXYGEN BUG.
Definition algorithm.hpp:16
std::string get_last_error_message()
Get the OS error message from the last error received on this thread.
Definition exception_win32_impl.hpp:31
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
T lock(T... args)
T resize(T... args)