1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-01-27 06:35:04 +00:00

Adds a forward route for event flags. Doesn't yet account for extra time expended.

This commit is contained in:
Thomas Harte 2020-01-20 17:09:01 -05:00
parent 4de121142b
commit 290db67f09
7 changed files with 12 additions and 16 deletions

View File

@ -29,12 +29,13 @@ BestEffortUpdater::~BestEffortUpdater() {
update_thread_.join();
}
void BestEffortUpdater::update() {
void BestEffortUpdater::update(int flags) {
// Bump the requested target time and set the update requested flag.
{
std::lock_guard<decltype(update_mutex_)> lock(update_mutex_);
has_skipped_ = update_requested_;
update_requested_ = true;
flags_ |= flags;
target_time_ = std::chrono::high_resolution_clock::now();
}
update_condition_.notify_one();
@ -66,6 +67,8 @@ void BestEffortUpdater::update_loop() {
// Release the lock on requesting new updates.
is_updating_ = true;
const int flags = flags_;
flags_ = 0;
lock.unlock();
// Calculate period from previous time to now.
@ -80,7 +83,7 @@ void BestEffortUpdater::update_loop() {
// Cap running at 1/5th of a second, to avoid doing a huge amount of work after any
// brief system interruption.
const double duration = std::min(double(integer_duration) / 1e9, 0.2);
delegate->update(this, duration, has_skipped_, 0);
delegate->update(this, duration, has_skipped_, flags);
has_skipped_ = false;
}
}

View File

@ -43,7 +43,7 @@ class BestEffortUpdater {
If the delegate is not currently in the process of an `update` call, calls it now to catch up to the current time.
The call is asynchronous; this method will return immediately.
*/
void update();
void update(int flags = 0);
/// Blocks until any ongoing update is complete; may spin.
void flush();
@ -53,6 +53,7 @@ class BestEffortUpdater {
std::atomic<bool> is_updating_;
std::chrono::time_point<std::chrono::high_resolution_clock> target_time_;
int flags_ = 0;
bool update_requested_;
std::mutex update_mutex_;
std::condition_variable update_condition_;

View File

@ -269,14 +269,6 @@ class MachineDocument:
}
}
/// Responds to CSBestEffortUpdaterDelegate update message by running the machine.
final func bestEffortUpdater(_ bestEffortUpdater: CSBestEffortUpdater!, runForInterval duration: TimeInterval, didSkipPreviousUpdate: Bool) {
if let machine = self.machine, actionLock.try() {
machine.run(forInterval: duration)
actionLock.unlock()
}
}
// MARK: - Pasteboard Forwarding.
/// Forwards any text currently on the pasteboard into the active machine.

View File

@ -57,7 +57,7 @@ typedef NS_ENUM(NSInteger, CSMachineKeyboardInputMode) {
*/
- (nullable instancetype)initWithAnalyser:(nonnull CSStaticAnalyser *)result missingROMs:(nullable inout NSMutableArray<CSMissingROM *> *)missingROMs NS_DESIGNATED_INITIALIZER;
- (void)runForInterval:(NSTimeInterval)interval;
- (void)runForInterval:(NSTimeInterval)interval untilEvent:(int)events;
- (float)idealSamplingRateFromRange:(NSRange)range;
- (void)setAudioSamplingRate:(float)samplingRate bufferSize:(NSUInteger)bufferSize;

View File

@ -259,7 +259,7 @@ struct ActivityObserver: public Activity::Observer {
}
}
- (void)runForInterval:(NSTimeInterval)interval {
- (void)runForInterval:(NSTimeInterval)interval untilEvent:(int)events {
@synchronized(self) {
if(_joystickMachine && _joystickManager) {
[_joystickManager update];
@ -309,7 +309,7 @@ struct ActivityObserver: public Activity::Observer {
}
}
}
_machine->crt_machine()->run_for(interval);
_machine->crt_machine()->run_until(interval, events);
}
}

View File

@ -14,7 +14,7 @@ struct UpdaterDelegate: public Concurrency::BestEffortUpdater::Delegate {
__weak CSMachine *machine;
void update(Concurrency::BestEffortUpdater *updater, Time::Seconds seconds, bool did_skip_previous_update, int flags) {
[machine runForInterval:seconds];
[machine runForInterval:seconds untilEvent:flags];
}
};

View File

@ -35,7 +35,7 @@ namespace {
struct BestEffortUpdaterDelegate: public Concurrency::BestEffortUpdater::Delegate {
void update(Concurrency::BestEffortUpdater *updater, Time::Seconds duration, bool did_skip_previous_update, int flags) override {
machine->crt_machine()->run_until(duration);
machine->crt_machine()->run_until(duration, flags);
}
Machine::DynamicMachine *machine;