2018-01-31 03:23:06 +00:00
|
|
|
//
|
|
|
|
// MultiCRTMachine.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 29/01/2018.
|
|
|
|
// Copyright © 2018 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "MultiCRTMachine.hpp"
|
|
|
|
|
2018-02-09 01:33:57 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
|
2018-01-31 03:23:06 +00:00
|
|
|
using namespace Analyser::Dynamic;
|
|
|
|
|
2018-02-11 00:38:26 +00:00
|
|
|
MultiCRTMachine::MultiCRTMachine(const std::vector<std::unique_ptr<::Machine::DynamicMachine>> &machines, std::mutex &machines_mutex) :
|
|
|
|
machines_(machines), machines_mutex_(machines_mutex), queues_(machines.size()) {}
|
2018-01-31 03:23:06 +00:00
|
|
|
|
2018-02-09 01:33:57 +00:00
|
|
|
void MultiCRTMachine::perform_parallel(const std::function<void(::CRTMachine::Machine *)> &function) {
|
|
|
|
// Apply a blunt force parallelisation of the machines; each run_for is dispatched
|
|
|
|
// to a separate queue and this queue will block until all are done.
|
2018-02-11 00:38:26 +00:00
|
|
|
volatile std::size_t outstanding_machines;
|
2018-02-09 01:33:57 +00:00
|
|
|
std::condition_variable condition;
|
|
|
|
std::mutex mutex;
|
2018-02-11 00:38:26 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> machines_lock(machines_mutex_);
|
2018-02-11 04:39:30 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2018-02-11 00:38:26 +00:00
|
|
|
outstanding_machines = machines_.size();
|
|
|
|
|
|
|
|
for(std::size_t index = 0; index < machines_.size(); ++index) {
|
2018-02-11 04:39:30 +00:00
|
|
|
CRTMachine::Machine *crt_machine = machines_[index]->crt_machine();
|
|
|
|
queues_[index].enqueue([&mutex, &condition, crt_machine, function, &outstanding_machines]() {
|
2018-02-11 00:38:26 +00:00
|
|
|
if(crt_machine) function(crt_machine);
|
|
|
|
|
2018-02-11 04:39:30 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
outstanding_machines--;
|
|
|
|
}
|
2018-02-11 00:38:26 +00:00
|
|
|
condition.notify_all();
|
|
|
|
});
|
|
|
|
}
|
2018-01-31 03:23:06 +00:00
|
|
|
}
|
2018-02-09 01:33:57 +00:00
|
|
|
|
2018-02-11 04:39:30 +00:00
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
|
|
condition.wait(lock, [&outstanding_machines] { return !outstanding_machines; });
|
2018-01-31 03:23:06 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 01:33:57 +00:00
|
|
|
void MultiCRTMachine::perform_serial(const std::function<void (::CRTMachine::Machine *)> &function) {
|
2018-02-11 00:38:26 +00:00
|
|
|
std::lock_guard<std::mutex> machines_lock(machines_mutex_);
|
2018-01-31 03:23:06 +00:00
|
|
|
for(const auto &machine: machines_) {
|
|
|
|
CRTMachine::Machine *crt_machine = machine->crt_machine();
|
2018-02-09 01:33:57 +00:00
|
|
|
if(crt_machine) function(crt_machine);
|
2018-01-31 03:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 01:33:57 +00:00
|
|
|
void MultiCRTMachine::setup_output(float aspect_ratio) {
|
|
|
|
perform_serial([=](::CRTMachine::Machine *machine) {
|
|
|
|
machine->setup_output(aspect_ratio);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiCRTMachine::close_output() {
|
|
|
|
perform_serial([=](::CRTMachine::Machine *machine) {
|
|
|
|
machine->close_output();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-31 03:23:06 +00:00
|
|
|
Outputs::CRT::CRT *MultiCRTMachine::get_crt() {
|
2018-02-11 00:38:26 +00:00
|
|
|
std::lock_guard<std::mutex> machines_lock(machines_mutex_);
|
2018-01-31 03:23:06 +00:00
|
|
|
CRTMachine::Machine *crt_machine = machines_.front()->crt_machine();
|
|
|
|
return crt_machine ? crt_machine->get_crt() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Outputs::Speaker::Speaker *MultiCRTMachine::get_speaker() {
|
2018-02-11 00:38:26 +00:00
|
|
|
std::lock_guard<std::mutex> machines_lock(machines_mutex_);
|
2018-01-31 03:23:06 +00:00
|
|
|
CRTMachine::Machine *crt_machine = machines_.front()->crt_machine();
|
|
|
|
return crt_machine ? crt_machine->get_speaker() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiCRTMachine::run_for(const Cycles cycles) {
|
2018-02-09 01:33:57 +00:00
|
|
|
perform_parallel([=](::CRTMachine::Machine *machine) {
|
2018-02-09 14:12:05 +00:00
|
|
|
if(machine->get_confidence() >= 0.01f) machine->run_for(cycles);
|
2018-02-09 01:33:57 +00:00
|
|
|
});
|
2018-01-31 03:23:06 +00:00
|
|
|
|
2018-02-01 12:53:52 +00:00
|
|
|
if(delegate_) delegate_->multi_crt_did_run_machines();
|
2018-01-31 03:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double MultiCRTMachine::get_clock_rate() {
|
|
|
|
// TODO: something smarter than this? Not all clock rates will necessarily be the same.
|
2018-02-11 00:38:26 +00:00
|
|
|
std::lock_guard<std::mutex> machines_lock(machines_mutex_);
|
2018-01-31 03:23:06 +00:00
|
|
|
CRTMachine::Machine *crt_machine = machines_.front()->crt_machine();
|
|
|
|
return crt_machine ? crt_machine->get_clock_rate() : 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MultiCRTMachine::get_clock_is_unlimited() {
|
2018-02-11 00:38:26 +00:00
|
|
|
std::lock_guard<std::mutex> machines_lock(machines_mutex_);
|
2018-01-31 03:23:06 +00:00
|
|
|
CRTMachine::Machine *crt_machine = machines_.front()->crt_machine();
|
|
|
|
return crt_machine ? crt_machine->get_clock_is_unlimited() : false;
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:53:52 +00:00
|
|
|
void MultiCRTMachine::did_change_machine_order() {
|
2018-02-10 22:11:16 +00:00
|
|
|
// TODO: shuffle delegates and announce potential output rate changes.
|
2018-02-01 12:53:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 03:23:06 +00:00
|
|
|
void MultiCRTMachine::set_delegate(::CRTMachine::Machine::Delegate *delegate) {
|
2018-02-10 22:11:16 +00:00
|
|
|
// TODO:
|
2018-01-31 03:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiCRTMachine::machine_did_change_clock_rate(Machine *machine) {
|
2018-02-10 22:11:16 +00:00
|
|
|
// TODO: consider passing along.
|
2018-01-31 03:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiCRTMachine::machine_did_change_clock_is_unlimited(Machine *machine) {
|
2018-02-10 22:11:16 +00:00
|
|
|
// TODO: consider passing along.
|
2018-01-31 03:23:06 +00:00
|
|
|
}
|