2018-01-31 03:23:06 +00:00
|
|
|
//
|
|
|
|
// MultiCRTMachine.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 29/01/2018.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2018 Thomas Harte. All rights reserved.
|
2018-01-31 03:23:06 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2019-02-28 03:39:33 +00:00
|
|
|
MultiCRTMachine::MultiCRTMachine(const std::vector<std::unique_ptr<::Machine::DynamicMachine>> &machines, std::recursive_mutex &machines_mutex) :
|
2018-02-18 20:23:15 +00:00
|
|
|
machines_(machines), machines_mutex_(machines_mutex), queues_(machines.size()) {
|
|
|
|
speaker_ = MultiSpeaker::create(machines);
|
|
|
|
}
|
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
|
|
|
{
|
2019-02-28 03:39:33 +00:00
|
|
|
std::lock_guard<decltype(machines_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-18 21:37:07 +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);
|
2018-02-18 21:37:07 +00:00
|
|
|
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) {
|
2019-02-28 03:39:33 +00:00
|
|
|
std::lock_guard<decltype(machines_mutex_)> machines_lock(machines_mutex_);
|
2018-01-31 03:23:06 +00:00
|
|
|
for(const auto &machine: machines_) {
|
2019-02-03 20:07:22 +00:00
|
|
|
CRTMachine::Machine *const 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-11-15 02:52:57 +00:00
|
|
|
void MultiCRTMachine::set_scan_target(Outputs::Display::ScanTarget *scan_target) {
|
2019-02-03 20:07:22 +00:00
|
|
|
scan_target_ = scan_target;
|
|
|
|
|
|
|
|
CRTMachine::Machine *const crt_machine = machines_.front()->crt_machine();
|
|
|
|
if(crt_machine) crt_machine->set_scan_target(scan_target);
|
2018-02-09 01:33:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 02:45:10 +00:00
|
|
|
Outputs::Display::ScanStatus MultiCRTMachine::get_scan_status() const {
|
|
|
|
CRTMachine::Machine *const crt_machine = machines_.front()->crt_machine();
|
|
|
|
if(crt_machine) crt_machine->get_scan_status();
|
|
|
|
|
|
|
|
return Outputs::Display::ScanStatus();
|
|
|
|
}
|
|
|
|
|
2018-01-31 03:23:06 +00:00
|
|
|
Outputs::Speaker::Speaker *MultiCRTMachine::get_speaker() {
|
2018-02-18 20:23:15 +00:00
|
|
|
return speaker_;
|
2018-01-31 03:23:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 02:18:13 +00:00
|
|
|
void MultiCRTMachine::run_for(Time::Seconds duration) {
|
2020-02-15 04:39:08 +00:00
|
|
|
perform_parallel([duration](::CRTMachine::Machine *machine) {
|
2018-03-22 02:18:13 +00:00
|
|
|
if(machine->get_confidence() >= 0.01f) machine->run_for(duration);
|
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
|
|
|
}
|
|
|
|
|
2018-02-01 12:53:52 +00:00
|
|
|
void MultiCRTMachine::did_change_machine_order() {
|
2019-02-03 20:07:22 +00:00
|
|
|
if(scan_target_) scan_target_->will_change_owner();
|
|
|
|
|
2020-02-15 04:39:08 +00:00
|
|
|
perform_serial([](::CRTMachine::Machine *machine) {
|
2019-02-03 20:07:22 +00:00
|
|
|
machine->set_scan_target(nullptr);
|
|
|
|
});
|
|
|
|
CRTMachine::Machine *const crt_machine = machines_.front()->crt_machine();
|
|
|
|
if(crt_machine) crt_machine->set_scan_target(scan_target_);
|
|
|
|
|
2018-02-18 20:23:15 +00:00
|
|
|
if(speaker_) {
|
|
|
|
speaker_->set_new_front_machine(machines_.front().get());
|
|
|
|
}
|
2018-02-01 12:53:52 +00:00
|
|
|
}
|