Commit 62e2ce51 authored by Leander Schulten's avatar Leander Schulten

Modules: ProgramBlock + Controller: Run start methods of the Programs and the...

Modules: ProgramBlock + Controller: Run start methods of the Programs and the Consumers of a Programblock in the Controller thread instead of in the main thread. Add Method to run code in the controller thread
parent 09f429a7
Pipeline #272669 passed with stage
in 11 minutes and 4 seconds
......@@ -126,6 +126,12 @@ void Controller::run() noexcept {
lastElapsedMilliseconds = elapsedMilliseconds;
std::unique_lock<std::mutex> l(vectorLock);
for (auto &i : runnables)
i();
runnables.clear();
for (auto &i : mustBeStartedPrograms)
i->runStartMethods();
mustBeStartedPrograms.clear();
updateSpotifyState();
for (auto pb = runningProgramms.begin(); pb != runningProgramms.end();) {
try {
......@@ -217,7 +223,8 @@ void Controller::run() noexcept {
void Controller::runProgramm(std::shared_ptr<ProgramBlock> pb){
std::unique_lock<std::mutex> l(vectorLock);
runningProgramms.push_back(pb);
runningProgramms.push_back(pb);
mustBeStartedPrograms.push_back(pb);
}
void Controller::stopProgramm(std::shared_ptr<ProgramBlock> pb){
......@@ -225,11 +232,13 @@ void Controller::stopProgramm(std::shared_ptr<ProgramBlock> pb){
if(deletingProgramBlock == pb.get())
return;
std::unique_lock<std::mutex> l(vectorLock);
runningProgramms.erase(std::remove(runningProgramms.begin(),runningProgramms.end(),pb),runningProgramms.end());
runningProgramms.erase(std::remove(runningProgramms.begin(), runningProgramms.end(), pb), runningProgramms.end());
mustBeStartedPrograms.erase(std::remove(runningProgramms.begin(), runningProgramms.end(), pb), runningProgramms.end());
}
void Controller::stopProgramm(ProgramBlock* pb){
std::unique_lock<std::mutex> l(vectorLock);
runningProgramms.erase(std::remove_if(runningProgramms.begin(),runningProgramms.end(),[&](const auto &v){return v.get()==pb;}),runningProgramms.end());
runningProgramms.erase(std::remove_if(runningProgramms.begin(), runningProgramms.end(), [&](const auto &v) { return v.get() == pb; }), runningProgramms.end());
mustBeStartedPrograms.erase(std::remove_if(runningProgramms.begin(), runningProgramms.end(), [&](const auto &v) { return v.get() == pb; }), runningProgramms.end());
}
bool Controller::isProgramRunning(ProgramBlock * pb){
......
......@@ -25,6 +25,8 @@ class Controller : public QObject {
std::mutex mutex;
std::condition_variable wait;
std::vector<std::shared_ptr<ProgramBlock>> runningProgramms;
std::vector<std::shared_ptr<ProgramBlock>> mustBeStartedPrograms;
std::vector<std::function<void()>> runnables;
std::mutex vectorLock;
ProgramBlock * deletingProgramBlock = nullptr;
// Variables for Spotify:
......@@ -79,6 +81,13 @@ public:
thread.exit();
lastElapsedMilliseconds = -1;
}
/**
* @brief runInController executes the func in the controller thread, if the controller thread is stopped, the controller thread must be started to execute the function
*/
void runInController(std::function<void()> func) {
std::unique_lock<std::mutex> l(vectorLock);
runnables.push_back(func);
}
~Controller();
private:
......
......@@ -278,12 +278,6 @@ namespace Modules {
void ProgramBlock::start(Controller * c){
if(status!=Running && c){
for(auto & p : programs){
p->start();
}
for(auto i = consumer.cbegin(); i != consumer.cend();++i){
static_cast<Consumer*>(i->source.get())->start();
}
setStatus(Running);
c->runProgramm(shared_from_this());
controller = c;
......@@ -430,16 +424,25 @@ namespace Modules {
}
}
void ProgramBlockManager::writeToJsonObject(QJsonObject & o){
void ProgramBlock::runStartMethods() {
for (auto &p : programs) {
p->start();
}
for (auto i = consumer.cbegin(); i != consumer.cend(); ++i) {
static_cast<Consumer *>(i->source.get())->start();
}
}
void ProgramBlockManager::writeToJsonObject(QJsonObject &o) {
QJsonArray a;
for(const auto & m : model){
for (const auto &m : model) {
QJsonObject o1;
m->writeJsonObject(o1);
a.append(o1);
}
o.insert("model",a);
o.insert("model", a);
}
void ProgramBlockManager::readFromJsonObject(const QJsonObject & o){
void ProgramBlockManager::readFromJsonObject(const QJsonObject &o) {
auto a = o["model"].toArray();
for(auto val_:a){
QJsonObject ob = val_.toObject();
......@@ -450,6 +453,4 @@ namespace Modules {
}
}
}
}
......@@ -86,6 +86,7 @@ namespace Modules {
{
Q_OBJECT
friend class ModuleManager;
friend class Controller;
/* Input n programms
m filters
k consumer
......@@ -336,6 +337,10 @@ namespace Modules {
emit statusChanged();
}
}
/**
* @brief runStartMethods runs the start methods of the programs and consumer, must be called from the controller thread
*/
void runStartMethods();
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment