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 {
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 };
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 = {};
81 const Timing *timing = nullptr;
82};
83
84}
85
86#endif
Executor of deferred and recurrent tasks.
Definition schedule.h:21
std::size_t n_atexit(void) const
Definition schedule.h:48
bool cancel_atexit(std::size_t i)
Definition schedule.cpp:110
u32 cur_gen
Definition schedule.h:80
Flag
Definition schedule.h:29
@ NONE
Definition schedule.h:30
@ HEARTBEAT
After it is triggered, execute again at every frame.
Definition schedule.h:34
@ IGNORE_FAILURES
Whether a failure in the task is critical or can be tolerated.
Definition schedule.h:32
bool update(void)
Definition schedule.cpp:114
void init(const Timing *t)
Definition schedule.h:45
std::vector< BaseEntry > atexit_v
Definition schedule.h:79
std::size_t frame(u64 f, Entry e)
Definition schedule.cpp:95
std::size_t at(Timing::time_point t, Entry e)
Definition schedule.cpp:88
std::size_t add(std::vector< T > *v, T t)
Definition schedule.cpp:56
std::size_t n(void) const
Definition schedule.h:47
bool exit(void)
Definition schedule.cpp:133
const Timing * timing
Definition schedule.h:81
std::size_t atexit(Entry e)
Definition schedule.cpp:102
bool(*)(void *) Fn
Signature for task functions.
Definition schedule.h:28
std::vector< TimeEntry > v
Definition schedule.h:78
std::size_t in(std::chrono::milliseconds t, Entry e)
Definition schedule.cpp:84
bool cancel_common(std::vector< T > *v, std::size_t i)
Definition schedule.cpp:67
map next
Definition demo1.lua:80
for i
Definition font.lua:5
#define T(f0, f1, f2)
local function bool(title, init, text)
Definition audio.cpp:7
static auto now(const Timing &t)
Definition timing.cpp:14
std::uint32_t u32
Definition def.h:14
std::uint8_t u8
Definition def.h:12
std::uint64_t u64
Definition def.h:15
local function cancel()
local function f()) end
Definition schedule.h:62
bool is_heartbeat() const
Definition schedule.h:64
bool active() const
Definition schedule.h:63
bool call(void)
Definition schedule.cpp:34
bool destroy(void)
Definition schedule.cpp:38
Definition schedule.h:36
std::vector< std::byte > data
Opaque byte array associated with the task.
Definition schedule.h:42
Fn dest
Destructor for the associated data.
Definition schedule.h:40
Fn f
The task's main function.
Definition schedule.h:38
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
Definition timing.h:20
clock::time_point time_point
Definition timing.h:22
local e
Definition math.lua:4