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(EmuTime::param time, Schedulable* dev)
18  : timeStamp(time), device(dev) {}
20  : timeStamp(EmuTime::zero), device(nullptr) {}
21  EmuTime::param getTime() const { return timeStamp; }
22  void setTime(EmuTime::param time) { timeStamp = time; }
23  Schedulable* getDevice() const { return device; }
24 
25  template <typename Archive>
26  void serialize(Archive& ar, unsigned version);
27 
28 private:
29  EmuTime timeStamp;
30  Schedulable* device;
31 };
32 
33 
34 class Scheduler
35 {
36 public:
37  using SyncPoints = std::vector<SynchronizationPoint>;
38 
39  Scheduler();
40  ~Scheduler();
41 
42  void setCPU(MSXCPU* cpu_)
43  {
44  cpu = cpu_;
45  }
46 
50  EmuTime::param getCurrentTime() const;
51 
55  inline EmuTime::param getNext() const
56  {
57  return queue.front().getTime();
58  }
59 
63  inline void schedule(EmuTime::param limit)
64  {
65  EmuTime next = getNext();
66  if (unlikely(limit >= next)) {
67  scheduleHelper(limit, next); // slow path not inlined
68  }
69  scheduleTime = limit;
70  }
71 
72  template <typename Archive>
73  void serialize(Archive& ar, unsigned version);
74 
75 private: // -> intended for Schedulable
76  friend class Schedulable;
77 
87  void setSyncPoint(EmuTime::param timestamp, Schedulable& device);
88 
89  SyncPoints getSyncPoints(const Schedulable& device) const;
90 
98  bool removeSyncPoint(Schedulable& device);
99 
102  void removeSyncPoints(Schedulable& device);
103 
107  bool pendingSyncPoint(const Schedulable& device, EmuTime& result) const;
108 
109 private:
110  void scheduleHelper(EmuTime::param limit, EmuTime next);
111 
116  EmuTime scheduleTime;
117  MSXCPU* cpu;
118  bool scheduleInProgress;
119 };
120 
121 } // namespace openmsx
122 
123 #endif
#define unlikely(x)
Definition: likely.hh:15
SynchronizationPoint(EmuTime::param time, Schedulable *dev)
Definition: Scheduler.hh:17
Schedulable * getDevice() const
Definition: Scheduler.hh:23
void setTime(EmuTime::param time)
Definition: Scheduler.hh:22
Every class that wants to get scheduled at some point must inherit from this class.
Definition: Schedulable.hh:33
void setCPU(MSXCPU *cpu_)
Definition: Scheduler.hh:42
std::vector< SynchronizationPoint > SyncPoints
Definition: Scheduler.hh:37
EmuTime::param getNext() const
TODO.
Definition: Scheduler.hh:55
EmuTime::param getTime() const
Definition: Scheduler.hh:21
void serialize(Archive &ar, unsigned version)
Definition: Scheduler.cc:124
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:63