7#include "function_fifo.hpp"
8#include "function_timer.hpp"
9#include "subsystem.hpp"
11#include "net/network_event.hpp"
18namespace hi::inline v1 {
33 virtual void set_maximum_frame_rate(
double frame_rate)
noexcept = 0;
35 [[nodiscard]]
void wfree_post_function(
auto&& func)
noexcept
37 return _function_fifo.add_function(
hi_forward(func));
40 [[nodiscard]]
void post_function(
auto&& func)
noexcept
46 [[nodiscard]]
auto async_function(
auto&& func)
noexcept
48 auto future = _function_fifo.add_async_function(
hi_forward(func));
53 timer_token_type delay_function(utc_nanoseconds time_point,
auto&& func)
noexcept
55 auto [token, first_to_call] = _function_timer.delay_function(time_point,
hi_forward(func));
65 auto [token, first_to_call] = _function_timer.repeat_function(period, time_point,
hi_forward(func));
75 auto [token, first_to_call] = _function_timer.repeat_function(period,
hi_forward(func));
85 virtual void remove_socket(
int fd) = 0;
86 virtual int resume(std::stop_token stop_token)
noexcept = 0;
87 virtual void resume_once(
bool block)
noexcept = 0;
92 virtual void notify_has_send()
noexcept = 0;
94 [[nodiscard]]
bool is_same_thread()
const noexcept
96 return _thread_id == 0 or current_thread_id() == _thread_id;
102 std::optional<int> _exit_code;
103 double _maximum_frame_rate = 30.0;
105 thread_id _thread_id = 0;
115 loop& operator=(
loop const&) = delete;
116 loop& operator=(
loop&&) noexcept = default;
120 [[nodiscard]] hi_no_inline static
loop& local() noexcept
123 _local = std::make_unique<loop>();
133 [[nodiscard]] hi_no_inline
static loop&
main() noexcept
135 if (
auto ptr = _main.load(std::memory_order::acquire)) {
140 ptr->_pimpl->is_main =
true;
141 _main.store(ptr, std::memory_order::release);
149 [[nodiscard]] hi_no_inline
static loop&
timer() noexcept
151 return *start_subsystem_or_terminate(_timer,
nullptr, timer_init, timer_deinit);
163 return _pimpl->set_maximum_frame_rate(frame_rate);
178 return _pimpl->wfree_post_function(
hi_forward(func));
189 return _pimpl->post_function(
hi_forward(func));
202 return _pimpl->async_function(
hi_forward(func));
213 return _pimpl->delay_function(time_point,
hi_forward(func));
222 [[nodiscard]] timer_token_type
226 return _pimpl->repeat_function(period, time_point,
hi_forward(func));
237 return _pimpl->repeat_function(period,
hi_forward(func));
247 return _pimpl->add_window(
std::move(window));
265 return _pimpl->add_socket(fd, event_mask,
std::move(f));
275 return _pimpl->remove_socket(fd);
285 int resume(std::stop_token stop_token = {})
noexcept
288 return _pimpl->resume(stop_token);
307 return _pimpl->resume_once(block);
311 static loop *timer_init() noexcept
313 hi_axiom(not _timer_thread.joinable());
315 _timer_thread = std::jthread{[](std::stop_token stop_token) {
316 _timer.store(
std::addressof(loop::local()), std::memory_order::release);
318 set_thread_name(
"timer");
319 loop::local().resume(stop_token);
323 if (
auto ptr = _timer.load(std::memory_order::relaxed)) {
330 static void timer_deinit() noexcept
332 if (
auto ptr = _timer.exchange(
nullptr, std::memory_order::acquire)) {
333 hi_axiom(_timer_thread.joinable());
334 _timer_thread.request_stop();
335 _timer_thread.join();
349 inline static std::jthread _timer_thread;
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition required.hpp:29
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:244
timer_token_type repeat_function(std::chrono::nanoseconds period, utc_nanoseconds time_point, auto &&func) noexcept
Call a function repeatedly.
Definition loop.hpp:223
void resume_once(bool block=false) noexcept
Resume for a single iteration.
Definition loop.hpp:304
static hi_no_inline loop & timer() noexcept
Get or create the timer event-loop.
Definition loop.hpp:149
auto async_function(auto &&func) noexcept
Call a function from the loop.
Definition loop.hpp:199
int resume(std::stop_token stop_token={}) noexcept
Resume the loop on the current thread.
Definition loop.hpp:285
void set_maximum_frame_rate(double frame_rate) noexcept
Set maximum frame rate.
Definition loop.hpp:160
void wfree_post_function(auto &&func) noexcept
Wait-free post a function to be called from the loop.
Definition loop.hpp:175
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:262
void post_function(auto &&func) noexcept
Post a function to be called from the loop.
Definition loop.hpp:186
timer_token_type delay_function(utc_nanoseconds time_point, auto &&func) noexcept
Call a function at a certain time.
Definition loop.hpp:210
static hi_no_inline loop & main() noexcept
Get or create the main-loop.
Definition loop.hpp:133
void remove_socket(int fd)
Remove the callback associated with a socket.
Definition loop.hpp:272
timer_token_type repeat_function(std::chrono::nanoseconds period, auto &&func) noexcept
Call a function repeatedly.
Definition loop.hpp:234
Definition network_event.hpp:72