AlertLayer threading fixes

(cherry picked from commit e6c395a657bbb6d3532eb0cf883c154a3b508deb)
This commit is contained in:
Dan Paulat 2025-09-04 21:34:21 -05:00
parent f34d11a7ea
commit 76a74922c5

View file

@ -6,8 +6,8 @@
#include <scwx/qt/util/color.hpp> #include <scwx/qt/util/color.hpp>
#include <scwx/qt/util/tooltip.hpp> #include <scwx/qt/util/tooltip.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
#include <scwx/util/time.hpp>
#include <atomic>
#include <chrono> #include <chrono>
#include <mutex> #include <mutex>
#include <ranges> #include <ranges>
@ -15,6 +15,7 @@
#include <shared_mutex> #include <shared_mutex>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <utility>
#include <boost/algorithm/string/join.hpp> #include <boost/algorithm/string/join.hpp>
#include <boost/asio/system_timer.hpp> #include <boost/asio/system_timer.hpp>
@ -22,7 +23,6 @@
#include <boost/container/stable_vector.hpp> #include <boost/container/stable_vector.hpp>
#include <boost/container_hash/hash.hpp> #include <boost/container_hash/hash.hpp>
#include <QEvent> #include <QEvent>
#include <utility>
namespace scwx::qt::map namespace scwx::qt::map
{ {
@ -153,9 +153,11 @@ public:
~Impl() ~Impl()
{ {
std::unique_lock refreshLock(refreshMutex_); std::unique_lock refreshLock(refreshMutex_);
refreshEnabled_ = false;
refreshTimer_.cancel(); refreshTimer_.cancel();
refreshLock.unlock(); refreshLock.unlock();
threadPool_.stop();
threadPool_.join(); threadPool_.join();
receiver_ = nullptr; receiver_ = nullptr;
@ -213,6 +215,7 @@ public:
AlertLayer* self_; AlertLayer* self_;
std::atomic<bool> refreshEnabled_ {true};
boost::asio::system_timer refreshTimer_ {threadPool_}; boost::asio::system_timer refreshTimer_ {threadPool_};
std::mutex refreshMutex_; std::mutex refreshMutex_;
@ -582,7 +585,8 @@ void AlertLayer::Impl::ScheduleRefresh()
// Expires at the top of the next minute // Expires at the top of the next minute
std::chrono::system_clock::time_point now = std::chrono::system_clock::time_point now =
std::chrono::floor<std::chrono::minutes>(scwx::util::time::now()); std::chrono::floor<std::chrono::minutes>(
std::chrono::system_clock::now());
refreshTimer_.expires_at(now + 1min); refreshTimer_.expires_at(now + 1min);
refreshTimer_.async_wait( refreshTimer_.async_wait(
@ -599,7 +603,11 @@ void AlertLayer::Impl::ScheduleRefresh()
else else
{ {
Q_EMIT self_->NeedsRendering(); Q_EMIT self_->NeedsRendering();
ScheduleRefresh();
if (refreshEnabled_)
{
ScheduleRefresh();
}
} }
}); });
} }