From 0bcdca6e7b7311cec5b70e6ef71b4ea1b8727bca Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 20 Feb 2024 01:30:47 +0000 Subject: [PATCH] core: more thread safety --- src/core/hyprlock.cpp | 14 +++++++------- src/renderer/AsyncResourceGatherer.cpp | 8 ++++++-- src/renderer/AsyncResourceGatherer.hpp | 1 + 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/core/hyprlock.cpp b/src/core/hyprlock.cpp index c4ffd85..8fc9e31 100644 --- a/src/core/hyprlock.cpp +++ b/src/core/hyprlock.cpp @@ -124,6 +124,10 @@ void CHyprlock::run() { std::thread pollThr([this, &pollfds]() { while (1) { int ret = poll(pollfds, 1, 5000 /* 5 seconds, reasonable. It's because we might need to terminate */); + + if (m_bTerminate) + break; + if (ret < 0) { Debug::log(CRIT, "[core] Polling fds failed with {}", errno); m_bTerminate = true; @@ -138,9 +142,6 @@ void CHyprlock::run() { } } - if (m_bTerminate) - break; - if (ret != 0) { Debug::log(TRACE, "[core] got poll event"); std::lock_guard lg2(m_sLoopState.eventLoopMutex); @@ -236,6 +237,9 @@ void CHyprlock::run() { std::lock_guard lg2(m_sLoopState.timerRequestMutex); m_sLoopState.timerCV.notify_all(); + g_pRenderer->asyncResourceGatherer->notify(); + + wl_display_disconnect(m_sWaylandState.display); Debug::log(LOG, "Reached the end, exiting"); } @@ -464,10 +468,6 @@ void CHyprlock::unlockSession() { Debug::log(LOG, "Unlocked, exiting!"); m_bTerminate = true; - - wl_display_roundtrip(m_sWaylandState.display); - - exit(0); } void CHyprlock::onLockLocked() { diff --git a/src/renderer/AsyncResourceGatherer.cpp b/src/renderer/AsyncResourceGatherer.cpp index e02cabd..faf63cc 100644 --- a/src/renderer/AsyncResourceGatherer.cpp +++ b/src/renderer/AsyncResourceGatherer.cpp @@ -231,7 +231,6 @@ void CAsyncResourceGatherer::asyncAssetSpinLock() { void CAsyncResourceGatherer::requestAsyncAssetPreload(const SPreloadRequest& request) { std::lock_guard lg(asyncLoopState.requestMutex); asyncLoopState.requests.push_back(request); - std::unique_lock lk(cvmtx); asyncLoopState.pending = true; asyncLoopState.loopGuard.notify_all(); } @@ -240,4 +239,9 @@ void CAsyncResourceGatherer::unloadAsset(SPreloadedAsset* asset) { std::lock_guard lg(asyncLoopState.assetsMutex); std::erase_if(assets, [asset](const auto& a) { return &a.second == asset; }); -} \ No newline at end of file +} + +void CAsyncResourceGatherer::notify() { + asyncLoopState.pending = true; + asyncLoopState.loopGuard.notify_all(); +} diff --git a/src/renderer/AsyncResourceGatherer.hpp b/src/renderer/AsyncResourceGatherer.hpp index af8fd98..de7cf22 100644 --- a/src/renderer/AsyncResourceGatherer.hpp +++ b/src/renderer/AsyncResourceGatherer.hpp @@ -43,6 +43,7 @@ class CAsyncResourceGatherer { void requestAsyncAssetPreload(const SPreloadRequest& request); void unloadAsset(SPreloadedAsset* asset); + void notify(); private: std::thread initThread;