| 1 |
#ifndef SCHEDULER_HH |
| 2 |
#define SCHEDULER_HH |
| 3 |
|
| 4 |
#include "EmuTime.hh" |
| 5 |
#include "SchedulerQueue.hh" |
| 6 |
|
| 7 |
#include <optional> |
| 8 |
#include <vector> |
| 9 |
|
| 10 |
namespace openmsx { |
| 11 |
|
| 12 |
class Schedulable; |
| 13 |
class MSXCPU; |
| 14 |
|
| 15 |
class SynchronizationPoint |
| 16 |
{ |
| 17 |
public: |
| 18 |
SynchronizationPoint() = default; |
| 19 |
SynchronizationPoint(EmuTime time, Schedulable* dev) |
| 20 |
: timeStamp(time), device(dev) {} |
| 21 |
[[nodiscard]] EmuTime getTime() const { return timeStamp; } |
| 22 |
void setTime(EmuTime time) { timeStamp = time; } |
| 23 |
[[nodiscard]] Schedulable* getDevice() const { return device; } |
| 24 |
|
| 25 |
template<typename Archive> |
| 26 |
void serialize(Archive& ar, unsigned version); |
| 27 |
|
| 28 |
private: |
| 29 |
EmuTime timeStamp = EmuTime::zero(); |
| 30 |
Schedulable* device = nullptr; |
| 31 |
}; |
| 32 |
|
| 33 |
|
| 34 |
class Scheduler |
| 35 |
{ |
| 36 |
public: |
| 37 |
using SyncPoints = std::vector<SynchronizationPoint>; |
| 38 |
|
| 39 |
Scheduler() = default; |
| 40 |
~Scheduler(); |
| 41 |
|
| 42 |
void setCPU(MSXCPU* cpu_) |
| 43 |
{ |
| 44 |
cpu = cpu_; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
[[nodiscard]] EmuTime getCurrentTime() const; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
[[nodiscard]] EmuTime getNext() const |
| 56 |
{ |
| 57 |
return queue.front().getTime(); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
void schedule(EmuTime limit) |
| 64 |
{ |
| 65 |
if (EmuTime next = getNext(); limit >= next) [[unlikely]] { |
| 66 |
scheduleHelper(limit, next); |
| 67 |
} |
| 68 |
scheduleTime = limit; |
| 69 |
} |
| 70 |
|
| 71 |
template<typename Archive> |
| 72 |
void serialize(Archive& ar, unsigned version); |
| 73 |
|
| 74 |
private: |
| 75 |
friend class Schedulable; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
void setSyncPoint(EmuTime timestamp, Schedulable& device); |
| 87 |
|
| 88 |
[[nodiscard]] SyncPoints getSyncPoints(const Schedulable& device) const; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
bool removeSyncPoint(const Schedulable& device); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
void removeSyncPoints(const Schedulable& device); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
[[nodiscard]] std::optional<EmuTime> isPending(const Schedulable& device) const; |
| 107 |
|
| 108 |
private: |
| 109 |
void scheduleHelper(EmuTime limit, EmuTime next); |
| 110 |
|
| 111 |
private: |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
SchedulerQueue<SynchronizationPoint> queue; |
| 116 |
EmuTime scheduleTime = EmuTime::zero(); |
| 117 |
MSXCPU* cpu = nullptr; |
| 118 |
bool scheduleInProgress = false; |
| 119 |
}; |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
#endif |