openMSX
Scheduler.hh
Go to the documentation of this file.
1 #ifndef SCHEDULER_HH
2 #define SCHEDULER_HH
3 
4 #include "EmuTime.hh"
5 #include "SchedulerQueue.hh"
6 #include "likely.hh"
7 #include <vector>
8 
9 namespace openmsx {
10 
11 class Schedulable;
12 class MSXCPU;
13 
15 {
16 public:
17  SynchronizationPoint() = default;
18  SynchronizationPoint(EmuTime::param time, Schedulable* dev)
19  : timeStamp(time), device(dev) {}
20  EmuTime::param getTime() const { return timeStamp; }
21  void setTime(EmuTime::param time) { timeStamp = time; }
22  Schedulable* getDevice() const { return device; }
23 
24  template <typename Archive>
25  void serialize(Archive& ar, unsigned version);
26 
27 private:
28  EmuTime timeStamp = EmuTime::zero;
29  Schedulable* device = nullptr;
30 };
31 
32 
33 class Scheduler
34 {
35 public:
36  using SyncPoints = std::vector<SynchronizationPoint>;
37 
38  Scheduler() = default;
39  ~Scheduler();
40 
41  void setCPU(MSXCPU* cpu_)
42  {
43  cpu = cpu_;
44  }
45 
49  EmuTime::param getCurrentTime() const;
50 
54  inline EmuTime::param getNext() const
55  {
56  return queue.front().getTime();
57  }
58 
62  inline void schedule(EmuTime::param limit)
63  {
64  EmuTime next = getNext();
65  if (unlikely(limit >= next)) {
66  scheduleHelper(limit, next); // slow path not inlined
67  }
68  scheduleTime = limit;
69  }
70 
71  template <typename Archive>
72  void serialize(Archive& ar, unsigned version);
73 
74 private: // -> intended for Schedulable
75  friend class Schedulable;
76 
86  void setSyncPoint(EmuTime::param timestamp, Schedulable& device);
87 
88  SyncPoints getSyncPoints(const Schedulable& device) const;
89 
97  bool removeSyncPoint(Schedulable& device);
98 
101  void removeSyncPoints(Schedulable& device);
102 
106  bool pendingSyncPoint(const Schedulable& device, EmuTime& result) const;
107 
108 private:
109  void scheduleHelper(EmuTime::param limit, EmuTime next);
110 
115  EmuTime scheduleTime = EmuTime::zero;
116  MSXCPU* cpu = nullptr;
117  bool scheduleInProgress = false;
118 };
119 
120 } // namespace openmsx
121 
122 #endif
#define unlikely(x)
Definition: likely.hh:15
SynchronizationPoint(EmuTime::param time, Schedulable *dev)
Definition: Scheduler.hh:18
Schedulable * getDevice() const
Definition: Scheduler.hh:22
void setTime(EmuTime::param time)
Definition: Scheduler.hh:21
Every class that wants to get scheduled at some point must inherit from this class.
Definition: Schedulable.hh:31
void setCPU(MSXCPU *cpu_)
Definition: Scheduler.hh:41
std::vector< SynchronizationPoint > SyncPoints
Definition: Scheduler.hh:36
EmuTime::param getNext() const
TODO.
Definition: Scheduler.hh:54
EmuTime::param getTime() const
Definition: Scheduler.hh:20
void serialize(Archive &ar, unsigned version)
Definition: Scheduler.cc:117
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void schedule(EmuTime::param limit)
Schedule till a certain moment in time.
Definition: Scheduler.hh:62