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 "thread.hpp"
12#include "unfair_mutex.hpp"
13#include "../utility/utility.hpp"
14#include "../macros.hpp"
15#include <thread>
16#include <string>
17#include <string_view>
18#include <format>
19#include <functional>
20#include <atomic>
21#include <chrono>
22#include <unordered_map>
23#include <mutex>
24#include <bit>
25
26hi_export_module(hikogui.conurrency.thread : intf);
27
28hi_export namespace hi { inline namespace v1 {
29namespace detail {
30
31inline std::unordered_map<thread_id, std::string> thread_names = {};
32inline unfair_mutex thread_names_mutex = {};
33
34} // namespace detail
35
42
51void set_thread_name(std::string_view name) noexcept;
52
63[[nodiscard]] inline std::string get_thread_name(thread_id id) noexcept
64{
65 hilet lock = std::scoped_lock(detail::thread_names_mutex);
66 hilet it = detail::thread_names.find(id);
67 if (it != detail::thread_names.end()) {
68 return it->second;
69 } else {
70 return std::format("{}", id);
71 }
72}
73
81
93
111
123{
125 hi_assert_bounds(cpu, available_cpus);
126
128 do {
129 if (available_cpus[cpu]) {
130 try {
133 } catch (os_error const &) {
134 }
135 }
136
137 // Advance to the next available cpu.
138 // We do this, so that the caller of this function can detect a wrap around.
139 do {
140 if (++cpu >= available_cpus.size()) {
141 cpu = 0;
142 }
143 } while (!available_cpus[cpu]);
144 } while (selected_cpu < 0);
145
147}
148
155
156}} // namespace hi::v1
Functions and types for accessing operating system threads.
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:63
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:122
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:104
thread_id current_thread_id() noexcept
Get the current thread id.
@ end
Start from the end of the file.
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Definition cpu_id.hpp:24
Exception thrown during an operating system call.
Definition exception_intf.hpp:183
T resize(T... args)