OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
thread.hpp
1#pragma once
2#include <cstddef>
3#include <ossia/detail/config.hpp>
4
5#include <ossia/detail/flat_map.hpp>
6
7#include <exception>
8#include <functional>
9#include <string>
10#include <thread>
11#include <tuple>
12#include <type_traits>
13#include <utility>
14
15#if !defined(_WIN32) && !defined(__EMSCRIPTEN__) && !defined(OSSIA_FREESTANDING)
16#define OSSIA_THREAD_PTHREAD 1
17#include <pthread.h>
18#endif
19
20namespace ossia
21{
27class OSSIA_EXPORT thread
28{
29public:
30 static constexpr std::size_t stack_size = 16 * 1024 * 1024;
31
32#if defined(OSSIA_THREAD_PTHREAD)
33 using native_handle_type = pthread_t;
34#else
35 using native_handle_type = std::thread::native_handle_type;
36#endif
37
38 thread() noexcept = default;
39
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)
44 {
45 using call_t = std::tuple<std::decay_t<F>, std::decay_t<Args>...>;
46 start(
47 [](void* p) {
48 auto* call = static_cast<call_t*>(p);
49 std::apply(
50 [](auto& fun, auto&... a) { std::invoke(std::move(fun), std::move(a)...); },
51 *call);
52 delete call;
53 },
54 [](void* p) { delete static_cast<call_t*>(p); },
55 new call_t(std::forward<F>(f), std::forward<Args>(args)...));
56 }
57
58 thread(thread&& other) noexcept;
59 thread& operator=(thread&& other) noexcept;
60 thread(const thread&) = delete;
61 thread& operator=(const thread&) = delete;
62 ~thread();
63
64 bool joinable() const noexcept;
65 void join();
66 void detach();
67 native_handle_type native_handle() noexcept;
68
69private:
71 void start(void (*fun)(void*), void (*destroy)(void*), void* arg);
72
73#if defined(OSSIA_THREAD_PTHREAD)
74 pthread_t m_handle{};
75 bool m_joinable{};
76#else
77 std::thread m_impl;
78#endif
79};
80
84OSSIA_EXPORT
85bool set_thread_realtime(std::thread& t, int prio = 95, bool algo_fifo = true);
86OSSIA_EXPORT
87bool set_thread_realtime(ossia::thread& t, int prio = 95, bool algo_fifo = true);
91OSSIA_EXPORT
92bool set_current_thread_realtime(int prio = 95, bool algo_fifo = true, bool log = true);
93
99OSSIA_EXPORT
100bool lock_process_memory(bool lock);
101
105OSSIA_EXPORT
106bool unlock_memory(const void* ptr, std::size_t size);
107
112OSSIA_EXPORT
113void preload_memory(const void* ptr, std::size_t size);
114OSSIA_EXPORT
115void set_thread_name(std::thread& t, std::string_view name);
116OSSIA_EXPORT
117void set_thread_name(ossia::thread& t, std::string_view name);
118OSSIA_EXPORT
119void set_thread_name(std::string_view name);
120OSSIA_EXPORT
121void set_thread_pinned(int cpu);
122
123enum class thread_type : unsigned char
124{
125 Net = 'N',
126 Midi = 'M',
127 Gpu = 'G',
128 GpuTask = 'g',
129 Audio = 'A',
130 AudioTask = 'a',
131 Ui = 'U',
132 UiTask = 'u',
133 Render = 'R',
134 RenderTask = 'r',
135};
136
137struct thread_spec
138{
139 int num_threads{};
140 int spin_interval{};
141};
142
143using thread_specs = ossia::flat_map<thread_type, thread_spec>;
144
145OSSIA_EXPORT
146const thread_specs& get_thread_specs() noexcept;
147
148OSSIA_EXPORT
149thread_type get_current_thread_type();
150
151// Schedule e.g. the third net thread on the appropriate CPU
152OSSIA_EXPORT
153void set_thread_pinned(thread_type kind, int thread_index);
154
155// Allows e.g. Audio and AudioTask
156OSSIA_EXPORT
157void ensure_current_thread_kind(thread_type kind);
158
159// Request explicitly Audio
160OSSIA_EXPORT
161void ensure_current_thread(thread_type kind);
162
163#if defined(NDEBUG)
164#define OSSIA_ENSURE_CURRENT_THREAD_KIND(a) \
165 do \
166 { \
167 } while(0)
168#define OSSIA_ENSURE_CURRENT_THREAD(a) \
169 do \
170 { \
171 } while(0)
172#else
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)
175#endif
176
177OSSIA_EXPORT
178std::string get_exe_path();
179
180OSSIA_EXPORT
181std::string get_exe_folder();
182
183OSSIA_EXPORT
184std::string get_module_path();
185
186OSSIA_EXPORT
187int get_pid();
188}
Definition thread.hpp:28
thread(F &&f, Args &&... args)
As std::thread: f and args are copied (or moved) to the new thread.
Definition thread.hpp:43
Definition git_info.h:7
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