OXYGENHUB
◆ main openmsx / src / RTScheduler.hh
1.2 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef RTSCHEDULER_HH
2
#define RTSCHEDULER_HH
3
4
#include "SchedulerQueue.hh"
5
#include "Timer.hh"
6
7
#include <cstdint>
8
#include <optional>
9
10
namespace openmsx {
11
12
class RTSchedulable;
13
14
struct RTSyncPoint
15
{
16
	uint64_t time;
17
	RTSchedulable* schedulable;
18
};
19
20
class RTScheduler
21
{
22
public:
23
	/** Execute all expired RTSchedulables. */
24
	void execute() {
25
		if (!queue.empty()) {
26
			auto limit = Timer::getTime();
27
			if (limit >= queue.front().time) [[unlikely]] {
28
				scheduleHelper(limit); // slow path not inlined
29
			}
30
		}
31
	}
32
33
	/** Get the next scheduled time point (absolute, in microseconds).
34
	  * @return The earliest scheduled time, or nullopt if queue is empty.
35
	  */
36
	[[nodiscard]] std::optional<uint64_t> getNextTime() const {
37
		return queue.empty() ? std::nullopt
38
		                     : std::optional(queue.front().time);
39
	}
40
41
private:
42
	// These are called by RTSchedulable
43
	friend class RTSchedulable;
44
	void add(uint64_t delta, RTSchedulable& schedulable);
45
	bool remove(RTSchedulable& schedulable);
46
	[[nodiscard]] bool isPending(const RTSchedulable& schedulable) const;
47
48
	void scheduleHelper(uint64_t limit);
49
50
private:
51
	SchedulerQueue<RTSyncPoint> queue;
52
};
53
54
} // namespace openmsx
55
56
#endif