nngn
Loading...
Searching...
No Matches
schedule.h
Go to the documentation of this file.
1#ifndef NNGN_TIMING_SCHEDULE_H
2#define NNGN_TIMING_SCHEDULE_H
3
4#include <chrono>
5#include <vector>
6
7#include "utils/def.h"
8
9#include "timing.h"
10
11namespace nngn {
12
21class Schedule {
22public:
28 using Fn = bool(*)(void*);
29 enum Flag : u8 {
30 NONE,
32 IGNORE_FAILURES = 1u << 0,
34 HEARTBEAT = 1u << 1,
35 };
36 struct Entry {
38 Fn f = {};
40 Fn dest = {};
42 std::vector<std::byte> data = {};
43 Flag flags = {};
44 };
45 void init(const Timing *t) { this->timing = t; }
46 // Information
47 std::size_t n(void) const { return this->v.size(); }
48 std::size_t n_atexit(void) const { return this->atexit_v.size(); }
49 // Scheduling functions
50 std::size_t next(Entry e);
51 std::size_t frame(u64 f, Entry e);
52 std::size_t in(std::chrono::milliseconds t, Entry e);
53 std::size_t at(Timing::time_point t, Entry e);
54 std::size_t atexit(Entry e);
55 // Cancelling
56 bool cancel(std::size_t i);
57 bool cancel_atexit(std::size_t i);
58 // Update
59 bool update(void);
60 bool exit(void);
61private:
62 struct BaseEntry : Entry {
63 bool active() const { return this->f; }
64 bool is_heartbeat() const { return this->flags & Flag::HEARTBEAT; }
65 bool call(void);
66 bool destroy(void);
67 };
68 struct TimeEntry : BaseEntry {
70 u64 frame = 0;
71 u32 gen = {};
72 bool active(u32 cur_gen, u64 cur_frame, Timing::time_point now) const;
73 };
74 template<typename T>
75 std::size_t add(std::vector<T> *v, T t);
76 template<typename T>
77 bool cancel_common(std::vector<T> *v, std::size_t i);
78 std::vector<TimeEntry> v = {};
79 std::vector<BaseEntry> atexit_v = {};
80 u32 cur_gen = 0;
81 const Timing *timing = nullptr;
82};
83
84}
85
86#endif
std::size_t n_atexit(void) const
Definition: schedule.h:48
bool cancel_atexit(std::size_t i)
u32 cur_gen
Definition: schedule.h:80
Flag
Definition: schedule.h:29
@ IGNORE_FAILURES
Whether a failure in the task is critical or can be tolerated.
Definition: schedule.h:32
@ NONE
Definition: schedule.h:30
@ HEARTBEAT
After it is triggered, execute again at every frame.
Definition: schedule.h:34
bool update(void)
void init(const Timing *t)
Definition: schedule.h:45
std::size_t frame(u64 f, Entry e)
std::size_t at(Timing::time_point t, Entry e)
std::size_t add(std::vector< T > *v, T t)
std::size_t n(void) const
Definition: schedule.h:47
std::size_t next(Entry e)
bool exit(void)
std::vector< BaseEntry > atexit_v
Definition: schedule.h:79
std::size_t atexit(Entry e)
bool(*)(void *) Fn
Signature for task functions.
Definition: schedule.h:28
std::vector< TimeEntry > v
Definition: schedule.h:78
bool cancel(std::size_t i)
const Timing * timing
Definition: schedule.h:81
std::size_t in(std::chrono::milliseconds t, Entry e)
bool cancel_common(std::vector< T > *v, std::size_t i)
for i
Definition: font.lua:5
function bool(title, init, text)
function f()) end
#define T(f0, f1, f2)
e
Definition: math.lua:4
Definition: audio.cpp:7
std::uint32_t u32
Definition: def.h:14
static auto now(const Timing &t)
Definition: timing.cpp:14
std::uint64_t u64
Definition: def.h:15
std::uint8_t u8
Definition: def.h:12
TerminalFlag
Definition: graphics.h:156
Definition: schedule.h:62
bool is_heartbeat() const
Definition: schedule.h:64
bool active() const
Definition: schedule.h:63
Definition: schedule.h:36
Fn dest
Destructor for the associated data.
Definition: schedule.h:40
Fn f
The task's main function.
Definition: schedule.h:38
std::vector< std::byte > data
Opaque byte array associated with the task.
Definition: schedule.h:42
Flag flags
Definition: schedule.h:43
Definition: schedule.h:68
u32 gen
Definition: schedule.h:71
u64 frame
Definition: schedule.h:70
Timing::time_point time
Definition: schedule.h:69
bool active(u32 cur_gen, u64 cur_frame, Timing::time_point now) const
Definition: timing.h:20
clock::time_point time_point
Definition: timing.h:22