resonance 1.0.1
Loading...
Searching...
No Matches
scheduler.h
Go to the documentation of this file.
1#pragma once
2
3#include <set>
4
5#include "app/systemtime.h"
6#include "sch/cmdid.h"
7#include "sch/timedcommand.h"
8
9class IBStream;
10class OBStream;
11
12namespace Sch {
13
14class Playbacker;
15class Recorder;
16
45class Scheduler {
46public:
53 struct QueueOrder {
61 bool operator()(const Sch::TimedCommand *pLeft, const Sch::TimedCommand *pRight) const {
62 if (pLeft->mDueTick.mValue == pRight->mDueTick.mValue) {
63 return static_cast<unsigned>(pLeft->mOrder) < static_cast<unsigned>(pRight->mOrder);
64 }
65 return pLeft->mDueTick.mValue < pRight->mDueTick.mValue;
66 }
67 };
68
76
84
95 void BeginRecording(OBStream &stream);
96
114 Sch::TimedCommand *pCommand, long long nTick, CmdID &id, int bRecordable, int nOrder);
115
133 Sch::TimedCommand *pCommand, long long nDelta, CmdID &id, int bRecordable, int nOrder);
134
147
160
171 void WithdrawByCmdID(const CmdID &id);
172
186
196
206
217 void StartPlayback(IBStream &stream);
218
236 void Service();
237
244 void Flush();
245
255 void Snapshot();
256
259
260private:
261 // TimeClock::Now() reads mNowNs.
262 friend class TimeClock;
263
264 // The tail both queueing paths expand: allocate the handle while it still reads -2, copy it
265 // into the wrapper, and queue the wrapper with a reference of the queue's own.
266 void Enqueue(Sch::TimedCommand *pCommand, CmdID &id);
267
268 // Every queued wrapper, each holding one reference the queue gives back when it runs or is
269 // withdrawn.
270 std::multiset<Sch::TimedCommand *, QueueOrder> mQueue; // +0x00
271 // +0x0c 1 while recording, 2 while playing back; released by StopRecOrPlayback().
272 int mStreamMode;
273 // The recording BeginRecording() installs, released by StopRecOrPlayback().
274 Recorder *mRecorder; // +0x10
275 // The replay StartPlayback() installs, released by StopRecOrPlayback().
276 Playbacker *mPlayback; // +0x14
277 long long mNowNs; // +0x18 the due tick of the command most recently run
278 int mBlocked; // +0x48 blocks every queueing path while set
279 int mReserved4c; // +0x4c never accessed
280};
281
282} // namespace Sch
283
293extern long long g_llWatchdogSecondNs;
Input half of the byte stream interface.
Definition ibstream.h:36
Output half of the byte stream interface.
Definition obstream.h:25
Handle that identifies one batch of commands the scheduler has queued.
Definition cmdid.h:35
Base of every action the scheduler performs on behalf of another subsystem.
Definition command.h:60
Replay of a recorded command stream that Sch::Scheduler::StartPlayback() installs.
Definition playbacker.h:21
Recording of every command the scheduler queues, installed by Sch::Scheduler::BeginRecording().
Definition recorder.h:18
SystemTime mClock
Clock every due time is measured against.
Definition scheduler.h:258
void Snapshot()
Empty the command queue, releasing every wrapper it held.
void StartPlayback(IBStream &stream)
Start replaying a recorded command stream.
void Withdraw(Sch::TimedCommand *pCommand)
Withdraw the entry at a wrapper's position in the queue.
void WithdrawByCmdID(const CmdID &id)
Withdraw the first queued wrapper whose handle matches.
void QueueReplayed(Sch::TimedCommand *pCommand)
Queue a command the playback reader supplies.
void QueueDelta(Sch::TimedCommand *pCommand, long long nDelta, CmdID &id, int bRecordable, int nOrder)
Queue a wrapper at a distance from the current time.
friend class TimeClock
Definition scheduler.h:262
void StopRecOrPlayback()
Release the recorder and the playback and leave both stream modes.
Scheduler()
Construct an idle scheduler with an empty queue and a fresh clock.
~Scheduler()
Block queueing, release every queued wrapper, and release the recorder and the playback.
void Service()
Run every queued command whose due time has arrived.
void Flush()
Mark the clock at the current time and record the reading in g_llWatchdogSecondNs.
void PostUnreferenced(Sch::Command *pCommand)
Build a wrapper for a command while recording, and discard it.
void ResetTimes()
Reset the clock to zero and the current time with it.
void QueueAbsolute(Sch::TimedCommand *pCommand, long long nTick, CmdID &id, int bRecordable, int nOrder)
Queue a wrapper at an absolute scheduler time.
void BeginRecording(OBStream &stream)
Start recording every recordable command queued from now on.
Clock the scheduler reads its due times from, in nanoseconds.
Definition systemtime.h:21
long long mValue
Definition time.h:83
One queued command, together with the time the scheduler is to run it at.
Definition timedcommand.h:46
int mOrder
Order among the wrappers that share one due tick.
Definition timedcommand.h:153
Time mDueTick
Scheduler time the command is to run at.
Definition timedcommand.h:165
Scheduling of timed commands.
Definition activefilter.h:9
long long g_llWatchdogSecondNs
Clock reading Sch::Scheduler::Service() refreshes at most once a second, and Sch::Scheduler::Flush() ...
Order of the command queue: by due tick, then by order as an unsigned quantity.
Definition scheduler.h:53
bool operator()(const Sch::TimedCommand *pLeft, const Sch::TimedCommand *pRight) const
Report whether one wrapper runs before another.
Definition scheduler.h:61