From 7c05b1788e73e5ffa59be70e1af6609f7baa2593 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Mon, 27 Jul 2020 20:25:52 -0400 Subject: [PATCH] Ensures proper thread confinement for `updateStatusBarText`. --- OSBindings/Qt/mainwindow.cpp | 11 ++++++----- OSBindings/Qt/mainwindow.h | 6 +++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/OSBindings/Qt/mainwindow.cpp b/OSBindings/Qt/mainwindow.cpp index 3afe5e70e..8286fe45f 100644 --- a/OSBindings/Qt/mainwindow.cpp +++ b/OSBindings/Qt/mainwindow.cpp @@ -1338,24 +1338,25 @@ void MainWindow::addActivityObserver() { } void MainWindow::register_led(const std::string &name) { + std::lock_guard guard(ledStatusesLock); ledStatuses[name] = false; - updateStatusBarText(); + QMetaObject::invokeMethod(this, "updateStatusBarText"); } void MainWindow::set_led_status(const std::string &name, bool isLit) { + std::lock_guard guard(ledStatusesLock); ledStatuses[name] = isLit; - updateStatusBarText(); // Assumption here: Qt's attempt at automatic thread confinement will work here. + QMetaObject::invokeMethod(this, "updateStatusBarText"); } void MainWindow::updateStatusBarText() { QString fullText; - bool isFirst = true; + std::lock_guard guard(ledStatusesLock); for(const auto &pair: ledStatuses) { - if(!isFirst) fullText += " | "; + if(!fullText.isEmpty()) fullText += " | "; fullText += QString::fromStdString(pair.first); fullText += " "; fullText += pair.second ? "■" : "□"; - isFirst = false; } statusBar()->showMessage(fullText); } diff --git a/OSBindings/Qt/mainwindow.h b/OSBindings/Qt/mainwindow.h index 4bbe4d108..91f3cab2d 100644 --- a/OSBindings/Qt/mainwindow.h +++ b/OSBindings/Qt/mainwindow.h @@ -5,6 +5,7 @@ #include #include +#include #include #include "audiobuffer.h" @@ -80,6 +81,7 @@ class MainWindow : public QMainWindow, public Outputs::Speaker::Speaker::Delegat private slots: void startMachine(); + void updateStatusBarText(); private: void start_appleII(); @@ -144,9 +146,11 @@ class MainWindow : public QMainWindow, public Outputs::Speaker::Speaker::Delegat void register_led(const std::string &) override; void set_led_status(const std::string &, bool) override; + + std::recursive_mutex ledStatusesLock; std::map ledStatuses; + void addActivityObserver(); - void updateStatusBarText(); }; #endif // MAINWINDOW_H