7#include "function_fifo.hpp"
8#include "function_timer.hpp"
9#include "utility/module.hpp"
10#include "net/network_event.hpp"
17namespace hi::inline
v1 {
35 virtual void set_maximum_frame_rate(
double frame_rate)
noexcept = 0;
37 void wfree_post_function(
auto&& func)
noexcept
39 return _function_fifo.add_function(
hi_forward(func));
42 void post_function(
auto&& func)
noexcept
48 [[nodiscard]]
auto async_function(
auto&& func)
noexcept
50 auto future = _function_fifo.add_async_function(
hi_forward(func));
57 auto [token, first_to_call] = _function_timer.delay_function(time_point,
hi_forward(func));
67 auto [token, first_to_call] = _function_timer.repeat_function(period, time_point,
hi_forward(func));
77 auto [token, first_to_call] = _function_timer.repeat_function(period,
hi_forward(func));
87 virtual void remove_socket(
int fd) = 0;
88 virtual int resume(std::stop_token stop_token)
noexcept = 0;
89 virtual void resume_once(
bool block)
noexcept = 0;
91 [[nodiscard]]
bool on_thread()
const noexcept
95 return _thread_id == 0 or current_thread_id() == _thread_id;
101 virtual void notify_has_send()
noexcept = 0;
106 std::optional<int> _exit_code = {};
107 double _maximum_frame_rate = 30.0;
109 thread_id _thread_id = 0;
119 loop& operator=(
loop const&) = delete;
120 loop& operator=(
loop&&) noexcept = default;
124 [[nodiscard]] hi_no_inline static
loop& local() noexcept
127 _local = std::make_unique<loop>();
137 [[nodiscard]] hi_no_inline
static loop&
main() noexcept
139 if (
auto ptr = _main.load(std::memory_order::acquire)) {
144 ptr->_pimpl->is_main =
true;
145 _main.store(ptr, std::memory_order::release);
153 [[nodiscard]] hi_no_inline
static loop&
timer() noexcept
155 return *start_subsystem_or_terminate(_timer,
nullptr, timer_init, timer_deinit);
167 return _pimpl->set_maximum_frame_rate(frame_rate);
182 return _pimpl->wfree_post_function(
hi_forward(func));
193 return _pimpl->post_function(
hi_forward(func));
206 return _pimpl->async_function(
hi_forward(func));
217 return _pimpl->delay_function(time_point,
hi_forward(func));
226 [[nodiscard]] timer_callback_token
230 return _pimpl->repeat_function(period, time_point,
hi_forward(func));
241 return _pimpl->repeat_function(period,
hi_forward(func));
251 return _pimpl->add_window(
std::move(window));
269 return _pimpl->add_socket(fd, event_mask,
std::move(f));
279 return _pimpl->remove_socket(fd);
289 int resume(std::stop_token stop_token = {})
noexcept
292 return _pimpl->resume(stop_token);
311 return _pimpl->resume_once(block);
321 return _pimpl->on_thread();
325 static loop *timer_init() noexcept
329 _timer_thread = std::jthread{[](std::stop_token stop_token) {
330 _timer.store(
std::addressof(loop::local()), std::memory_order::release);
332 set_thread_name(
"timer");
333 loop::local().resume(stop_token);
337 if (
auto ptr = _timer.load(std::memory_order::relaxed)) {
344 static void timer_deinit() noexcept
346 if (
auto const *
const ptr = _timer.exchange(
nullptr, std::memory_order::acquire)) {
348 _timer_thread.request_stop();
349 _timer_thread.join();
363 inline static std::jthread _timer_thread;
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:184
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:223
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
DOXYGEN BUG.
Definition algorithm.hpp:13
@ local
Call the function asynchronously from the current thread's loop.
A fifo (First-in, Firts-out) for asynchronous calls.
Definition function_fifo.hpp:23
A time that calls functions.
Definition function_timer.hpp:22
void add_window(std::weak_ptr< gui_window > window) noexcept
Add a window to be redrawn from the event loop.
Definition loop.hpp:248
void resume_once(bool block=false) noexcept
Resume for a single iteration.
Definition loop.hpp:308
bool on_thread() const noexcept
Check if the current thread is the same as the loop's thread.
Definition loop.hpp:318
static hi_no_inline loop & timer() noexcept
Get or create the timer event-loop.
Definition loop.hpp:153
auto async_function(auto &&func) noexcept
Call a function from the loop.
Definition loop.hpp:203
timer_callback_token delay_function(utc_nanoseconds time_point, auto &&func) noexcept
Call a function at a certain time.
Definition loop.hpp:214
int resume(std::stop_token stop_token={}) noexcept
Resume the loop on the current thread.
Definition loop.hpp:289
void set_maximum_frame_rate(double frame_rate) noexcept
Set maximum frame rate.
Definition loop.hpp:164
void wfree_post_function(auto &&func) noexcept
Wait-free post a function to be called from the loop.
Definition loop.hpp:179
void add_socket(int fd, network_event event_mask, std::function< void(int, network_events const &)> f)
Add a callback that reacts on a socket.
Definition loop.hpp:266
void post_function(auto &&func) noexcept
Post a function to be called from the loop.
Definition loop.hpp:190
timer_callback_token repeat_function(std::chrono::nanoseconds period, auto &&func) noexcept
Call a function repeatedly.
Definition loop.hpp:238
static hi_no_inline loop & main() noexcept
Get or create the main-loop.
Definition loop.hpp:137
void remove_socket(int fd)
Remove the callback associated with a socket.
Definition loop.hpp:276
timer_callback_token repeat_function(std::chrono::nanoseconds period, utc_nanoseconds time_point, auto &&func) noexcept
Call a function repeatedly.
Definition loop.hpp:227
Definition network_event.hpp:71