3#include <ossia/detail/config.hpp>
5#include <ossia/detail/flat_map.hpp>
15#if !defined(_WIN32) && !defined(__EMSCRIPTEN__) && !defined(OSSIA_FREESTANDING)
16#define OSSIA_THREAD_PTHREAD 1
27class OSSIA_EXPORT thread
30 static constexpr std::size_t stack_size = 16 * 1024 * 1024;
32#if defined(OSSIA_THREAD_PTHREAD)
33 using native_handle_type = pthread_t;
35 using native_handle_type = std::thread::native_handle_type;
38 thread()
noexcept =
default;
41 template <
typename F,
typename... Args>
42 requires(!std::is_same_v<std::remove_cvref_t<F>, thread>)
43 explicit thread(F&& f, Args&&... args)
45 using call_t = std::tuple<std::decay_t<F>, std::decay_t<Args>...>;
48 auto* call =
static_cast<call_t*
>(p);
50 [](
auto& fun,
auto&... a) { std::invoke(std::move(fun), std::move(a)...); },
54 [](
void* p) {
delete static_cast<call_t*
>(p); },
55 new call_t(std::forward<F>(f), std::forward<Args>(args)...));
64 bool joinable() const noexcept;
67 native_handle_type native_handle() noexcept;
71 void start(
void (*fun)(
void*),
void (*destroy)(
void*),
void* arg);
73#if defined(OSSIA_THREAD_PTHREAD)
115void set_thread_name(std::thread& t, std::string_view name);
117void set_thread_name(ossia::thread& t, std::string_view name);
119void set_thread_name(std::string_view name);
121void set_thread_pinned(
int cpu);
123enum class thread_type :
unsigned char
143using thread_specs = ossia::flat_map<thread_type, thread_spec>;
146const thread_specs& get_thread_specs() noexcept;
149thread_type get_current_thread_type();
153void set_thread_pinned(thread_type kind,
int thread_index);
157void ensure_current_thread_kind(thread_type kind);
161void ensure_current_thread(thread_type kind);
164#define OSSIA_ENSURE_CURRENT_THREAD_KIND(a) \
168#define OSSIA_ENSURE_CURRENT_THREAD(a) \
173#define OSSIA_ENSURE_CURRENT_THREAD_KIND(a) ::ossia::ensure_current_thread_kind(a)
174#define OSSIA_ENSURE_CURRENT_THREAD(a) ::ossia::ensure_current_thread(a)
178std::string get_exe_path();
181std::string get_exe_folder();
184std::string get_module_path();
thread(F &&f, Args &&... args)
As std::thread: f and args are copied (or moved) to the new thread.
Definition thread.hpp:43
bool unlock_memory(const void *ptr, std::size_t size)
Lets the kernel evict this range again: once lock_process_memory() is on, every page read from a mapp...
Definition thread.cpp:213
bool lock_process_memory(bool lock)
Keeps the process's memory resident (mlockall), so that a real-time thread never waits for a page to ...
Definition thread.cpp:243
bool set_current_thread_realtime(int prio, bool algo_fifo, bool log)
For threads created by a library, e.g.
Definition thread.cpp:197
bool set_thread_realtime(std::thread &t, int prio, bool algo_fifo)
Requests a real-time policy for the thread.
Definition thread.cpp:188
void preload_memory(const void *ptr, std::size_t size)
Reads a mapped range in from its file now, so that a real-time thread reading it later does not wait ...
Definition thread.cpp:222