openMSX
RTScheduler.cc
Go to the documentation of this file.
1#include "RTScheduler.hh"
2#include "RTSchedulable.hh"
3#include "ranges.hh"
4#include <limits>
5
6namespace openmsx {
7
9 explicit EqualRTSchedulable(const RTSchedulable& schedulable_)
10 : schedulable(schedulable_) {}
11 bool operator()(const RTSyncPoint& sp) const {
12 return sp.schedulable == &schedulable;
13 }
15};
16
17void RTScheduler::add(uint64_t delta, RTSchedulable& schedulable)
18{
19 queue.insert(RTSyncPoint{Timer::getTime() + delta, &schedulable},
20 [](RTSyncPoint& sp) {
21 sp.time = std::numeric_limits<uint64_t>::max(); },
22 [](const RTSyncPoint& x, const RTSyncPoint& y) {
23 return x.time < y.time; });
24}
25
26bool RTScheduler::remove(RTSchedulable& schedulable)
27{
28 return queue.remove(EqualRTSchedulable(schedulable));
29}
30
31bool RTScheduler::isPending(const RTSchedulable& schedulable) const
32{
33 return ranges::any_of(queue, EqualRTSchedulable(schedulable));
34}
35
36void RTScheduler::scheduleHelper(uint64_t limit)
37{
38 // Process at most this many events to prevent getting stuck in an
39 // infinite loop when a RTSchedulable keeps on rescheduling itself in
40 // the (too) near future.
41 auto count = queue.size();
42 while (true) {
43 auto* schedulable = queue.front().schedulable;
44 queue.remove_front();
45
46 schedulable->executeRT();
47
48 // It's possible RTSchedulables are canceled in the mean time,
49 // so we can't rely on 'count' to replace this empty check.
50 if (queue.empty()) break;
51 if (queue.front().time > limit) [[likely]] break;
52 if (--count == 0) break;
53 }
54}
55
56} // namespace openmsx
ALWAYS_INLINE unsigned count(const uint8_t *pIn, const uint8_t *pMatch, const uint8_t *pInLimit)
Definition lz4.cc:147
uint64_t getTime()
Get current (real) time in us.
Definition Timer.cc:7
This file implemented 3 utility functions:
Definition Autofire.cc:9
bool any_of(InputRange &&range, UnaryPredicate pred)
Definition ranges.hh:198
bool operator()(const RTSyncPoint &sp) const
EqualRTSchedulable(const RTSchedulable &schedulable_)
Definition RTScheduler.cc:9
const RTSchedulable & schedulable
RTSchedulable * schedulable