openMSX
Schedulable.hh
Go to the documentation of this file.
1 #ifndef SCHEDULABLE_HH
2 #define SCHEDULABLE_HH
3 
4 #include "EmuTime.hh"
5 #include "serialize.hh"
6 #include "serialize_meta.hh"
7 #include "serialize_stl.hh"
8 #include <cassert>
9 #include <vector>
10 
11 namespace openmsx {
12 
13 class Scheduler;
14 
15 // For backwards-compatible savestates
17 {
18  template<typename Archive>
19  void serialize(Archive& ar, unsigned /*version*/) {
20  assert(Archive::IS_LOADER);
21  ar.serialize("time", time,
22  "type", userData);
23  }
24 
25  EmuTime time = EmuTime::zero();
26  int userData = 0;
27 };
28 
34 {
35 public:
36  Schedulable(const Schedulable&) = delete;
37  Schedulable& operator=(const Schedulable&) = delete;
38 
43  virtual void executeUntil(EmuTime::param time) = 0;
44 
56  virtual void schedulerDeleted();
57 
58  [[nodiscard]] Scheduler& getScheduler() const { return scheduler; }
59 
62  [[nodiscard]] EmuTime::param getCurrentTime() const;
63 
64  template<typename Archive>
65  void serialize(Archive& ar, unsigned version);
66 
67  template<typename Archive>
68  static std::vector<SyncPointBW> serializeBW(Archive& ar) {
69  assert(Archive::IS_LOADER);
70  ar.beginTag("Schedulable");
71  std::vector<SyncPointBW> result;
72  ar.serialize("syncPoints", result);
73  ar.endTag("Schedulable");
74  return result;
75  }
76  template<typename Archive>
77  static void restoreOld(Archive& ar, std::vector<Schedulable*> schedulables) {
78  assert(Archive::IS_LOADER);
79  for (auto* s : schedulables) {
80  s->removeSyncPoints();
81  }
82  for (auto& old : serializeBW(ar)) {
83  unsigned i = old.userData;
84  if (i < schedulables.size()) {
85  schedulables[i]->setSyncPoint(old.time);
86  }
87  }
88  }
89 
90 protected:
91  explicit Schedulable(Scheduler& scheduler);
92  ~Schedulable();
93 
94  void setSyncPoint(EmuTime::param timestamp);
95  bool removeSyncPoint();
96  void removeSyncPoints();
97  [[nodiscard]] bool pendingSyncPoint() const;
98  [[nodiscard]] bool pendingSyncPoint(EmuTime& result) const;
99 
100 private:
101  Scheduler& scheduler;
102 };
104 
105 } // namespace openmsx
106 
107 #endif
Every class that wants to get scheduled at some point must inherit from this class.
Definition: Schedulable.hh:34
virtual void executeUntil(EmuTime::param time)=0
When the previously registered syncPoint is reached, this method gets called.
void setSyncPoint(EmuTime::param timestamp)
Definition: Schedulable.cc:23
void serialize(Archive &ar, unsigned version)
Definition: Schedulable.cc:55
Schedulable(const Schedulable &)=delete
Scheduler & getScheduler() const
Definition: Schedulable.hh:58
virtual void schedulerDeleted()
Just before the the Scheduler is deleted, it calls this method of all the Schedulables that are still...
Definition: Schedulable.cc:17
bool pendingSyncPoint() const
Definition: Schedulable.cc:38
Schedulable & operator=(const Schedulable &)=delete
static void restoreOld(Archive &ar, std::vector< Schedulable * > schedulables)
Definition: Schedulable.hh:77
EmuTime::param getCurrentTime() const
Convenience method: This is the same as getScheduler().getCurrentTime().
Definition: Schedulable.cc:49
static std::vector< SyncPointBW > serializeBW(Archive &ar)
Definition: Schedulable.hh:68
This file implemented 3 utility functions:
Definition: Autofire.cc:9
REGISTER_BASE_CLASS(Connector, "Connector")
void serialize(Archive &ar, unsigned)
Definition: Schedulable.hh:19