From 12d4feb0ef21d7ceb20a2af3c7970fabea015e61 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 24 Jul 2018 08:52:49 +0200 Subject: Protect announcement switching variables with mutex --- src/MuxElements.cpp | 35 +++++++++++++++++++++++------------ src/MuxElements.h | 5 ++--- 2 files changed, 25 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/MuxElements.cpp b/src/MuxElements.cpp index 0e12515..e19c93d 100644 --- a/src/MuxElements.cpp +++ b/src/MuxElements.cpp @@ -55,16 +55,20 @@ std::string AnnouncementCluster::tostring() const ss << "cluster id(" << (int)cluster_id; ss << ", flags 0x" << boost::format("%04x") % flags; ss << ", subchannel " << subchanneluid; - if (m_active) { - ss << " active "; - } - if (m_deferred_start_time) { - ss << " start pending"; - } + { + lock_guard lock(m_active_mutex); + if (m_active) { + ss << " active "; + } - if (m_deferred_stop_time) { - ss << " stop pending"; + if (m_deferred_start_time) { + ss << " start pending"; + } + + if (m_deferred_stop_time) { + ss << " stop pending"; + } } ss << " )"; @@ -74,8 +78,9 @@ std::string AnnouncementCluster::tostring() const bool AnnouncementCluster::is_active() { - if (m_deferred_start_time) - { + lock_guard lock(m_active_mutex); + + if (m_deferred_start_time) { const auto now = std::chrono::steady_clock::now(); if (*m_deferred_start_time <= now) { @@ -85,8 +90,7 @@ bool AnnouncementCluster::is_active() } } - if (m_deferred_stop_time) - { + if (m_deferred_stop_time) { const auto now = std::chrono::steady_clock::now(); if (*m_deferred_stop_time <= now) { @@ -105,6 +109,8 @@ void AnnouncementCluster::set_parameter(const string& parameter, if (parameter == "active") { stringstream ss; ss << value; + + lock_guard lock(m_active_mutex); ss >> m_active; } else if (parameter == "start_in") { @@ -114,6 +120,7 @@ void AnnouncementCluster::set_parameter(const string& parameter, int start_in_ms; ss >> start_in_ms; + lock_guard lock(m_active_mutex); using namespace std::chrono; m_deferred_start_time = steady_clock::now() + milliseconds(start_in_ms); } @@ -124,6 +131,7 @@ void AnnouncementCluster::set_parameter(const string& parameter, int stop_in_ms; ss >> stop_in_ms; + lock_guard lock(m_active_mutex); using namespace std::chrono; m_deferred_stop_time = steady_clock::now() + milliseconds(stop_in_ms); } @@ -141,9 +149,11 @@ const string AnnouncementCluster::get_parameter(const string& parameter) const stringstream ss; if (parameter == "active") { + lock_guard lock(m_active_mutex); ss << m_active; } else if (parameter == "start_in") { + lock_guard lock(m_active_mutex); if (m_deferred_start_time) { const auto diff = *m_deferred_start_time - steady_clock::now(); ss << duration_cast(diff).count(); @@ -153,6 +163,7 @@ const string AnnouncementCluster::get_parameter(const string& parameter) const } } else if (parameter == "stop_in") { + lock_guard lock(m_active_mutex); if (m_deferred_stop_time) { const auto diff = *m_deferred_stop_time - steady_clock::now(); ss << duration_cast(diff).count(); diff --git a/src/MuxElements.h b/src/MuxElements.h index 73637ac..259d5d9 100644 --- a/src/MuxElements.h +++ b/src/MuxElements.h @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -110,17 +111,15 @@ class AnnouncementCluster : public RemoteControllable { bool is_active(void); private: + mutable std::mutex m_active_mutex; bool m_active = false; - boost::optional< std::chrono::time_point< std::chrono::steady_clock> > m_deferred_start_time; - boost::optional< std::chrono::time_point< std::chrono::steady_clock> > m_deferred_stop_time; - /* Remote control */ virtual void set_parameter(const std::string& parameter, const std::string& value); -- cgit v1.2.3