HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
timer.hpp
1// Copyright 2020 Pokitec
2// All rights reserved.
3
4
5#pragma once
6
7#include "hires_utc_clock.hpp"
8#include <mutex>
9#include <vector>
10#include <functional>
11#include <tuple>
12#include <thread>
13
14namespace tt {
15
19class timer {
20public:
23
29
30private:
31 struct callback_entry {
32 size_t id;
33 duration interval;
34 time_point next_wakeup;
35 callback_type callback;
36
37 callback_entry(size_t id, duration interval, time_point next_wakeup, callback_type callback) noexcept :
38 id(id), interval(interval), next_wakeup(next_wakeup), callback(std::move(callback)) {}
39 };
40
43 std::string name;
44
45 mutable std::mutex mutex;
46 std::thread thread;
47 std::vector<callback_entry> callback_list;
48 size_t callback_count = 0;
49
52 bool stop_thread;
53
59 [[nodiscard]] std::pair<std::vector<callback_type>,timer::time_point> find_triggered_callbacks(
60 timer::time_point current_time
61 ) noexcept;
62
65 void loop() noexcept;
66
71 void start_with_lock_held() noexcept;
72
77 void stop_with_lock_held() noexcept;
78
79public:
80 timer(std::string name) noexcept;
81 ~timer();
82
87 void start() noexcept;
88
93 void stop() noexcept;
94
105 [[nodiscard]] size_t add_callback(duration interval, callback_type callback) noexcept;
106
109 void remove_callback(size_t callback_id) noexcept;
110
111};
112
115inline timer maintenance_timer = {"MaintenanceThread"};
116
117}
STL namespace.
The maintence thread.
Definition timer.hpp:19
void stop() noexcept
Stop the timer thread.
void remove_callback(size_t callback_id) noexcept
Remove the callback function.
void start() noexcept
Start the timer thread.
size_t add_callback(duration interval, callback_type callback) noexcept
Add a callback function to be executed each interval.
T move(T... args)