HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
thread_intf.hpp
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 "unfair_mutex.hpp"
12#include "../utility/utility.hpp"
13#include "../macros.hpp"
14#include <thread>
15#include <string>
16#include <string_view>
17#include <format>
18#include <functional>
19#include <atomic>
20#include <chrono>
21#include <unordered_map>
22#include <mutex>
23#include <bit>
24
25hi_export_module(hikogui.concurrency.thread : intf);
26
27hi_export namespace hi { inline namespace v1 {
28namespace detail {
29
30inline std::unordered_map<thread_id, std::string> thread_names = {};
31inline unfair_mutex thread_names_mutex = {};
32
33} // namespace detail
34
40[[nodiscard]] thread_id current_thread_id() noexcept;
41
50void set_thread_name(std::string_view name) noexcept;
51
62[[nodiscard]] inline std::string get_thread_name(thread_id id) noexcept
63{
64 auto const lock = std::scoped_lock(detail::thread_names_mutex);
65 auto const it = detail::thread_names.find(id);
66 if (it != detail::thread_names.end()) {
67 return it->second;
68 } else {
69 return std::format("{}", id);
70 }
71}
72
80
92
104{
105 auto new_mask = std::vector<bool>{};
106 new_mask.resize(cpu_id + 1);
107 new_mask[cpu_id] = true;
108 return set_thread_affinity_mask(new_mask);
109}
110
121[[nodiscard]] inline std::size_t advance_thread_affinity(std::size_t &cpu) noexcept
122{
123 auto available_cpus = process_affinity_mask();
124 hi_assert_bounds(cpu, available_cpus);
125
126 ssize_t selected_cpu = -1;
127 do {
128 if (available_cpus[cpu]) {
129 try {
131 selected_cpu = narrow_cast<ssize_t>(cpu);
132 } catch (os_error const &) {
133 }
134 }
135
136 // Advance to the next available cpu.
137 // We do this, so that the caller of this function can detect a wrap around.
138 do {
139 if (++cpu >= available_cpus.size()) {
140 cpu = 0;
141 }
142 } while (!available_cpus[cpu]);
143 } while (selected_cpu < 0);
144
145 return narrow_cast<std::size_t>(selected_cpu);
146}
147
153[[nodiscard]] std::size_t current_cpu_id() noexcept;
154
155}} // namespace hi::v1
Definition of the unfair_mutex.
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.
Definition thread_intf.hpp:62
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.
Definition thread_intf.hpp:121
std::vector< bool > set_thread_affinity(std::size_t cpu_id)
Set the current thread CPU affinity to a single CPU.
Definition thread_intf.hpp:103
thread_id current_thread_id() noexcept
Get the current thread id.
@ end
Start from the end of the file.
STL namespace.
The HikoGUI namespace.
Definition array_generic.hpp:20
cpu_id_result cpu_id(uint32_t leaf_id, uint32_t index=0) noexcept
A generic x86 cpu-id instruction.
Definition cpu_id_x86.hpp:347
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Exception thrown during an operating system call.
Definition exception_intf.hpp:184
T resize(T... args)