From d15de921581ff500a48d1a463d33902bd499fad7 Mon Sep 17 00:00:00 2001 From: mwilsnd <53413200+mwilsnd@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:11:13 -0400 Subject: [PATCH] Take locks before signaling thread condition variables (#2636) --- src/mbgl/tile/tile_cache.cpp | 2 ++ src/mbgl/tile/tile_cache.hpp | 3 +-- src/mbgl/util/thread_pool.cpp | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/mbgl/tile/tile_cache.cpp b/src/mbgl/tile/tile_cache.cpp index 8dc0bac35..2bbde03a8 100644 --- a/src/mbgl/tile/tile_cache.cpp +++ b/src/mbgl/tile/tile_cache.cpp @@ -53,6 +53,8 @@ void TileCache::deferredRelease(std::unique_ptr&& tile) { // last one and the destruction actually occurs here on this thread. std::function func{[tile_{CaptureWrapper{std::move(tile)}}, this]() mutable { tile_.item = {}; + + std::lock_guard counterLock(deferredSignalLock); deferredDeletionsPending--; deferredSignal.notify_all(); }}; diff --git a/src/mbgl/tile/tile_cache.hpp b/src/mbgl/tile/tile_cache.hpp index 72d430db3..db9f8658c 100644 --- a/src/mbgl/tile/tile_cache.hpp +++ b/src/mbgl/tile/tile_cache.hpp @@ -50,10 +50,9 @@ private: std::map> tiles; std::list orderedKeys; TaggedScheduler threadPool; - std::atomic deferredDeletionsPending{0}; + size_t deferredDeletionsPending{0}; std::mutex deferredSignalLock; std::condition_variable deferredSignal; - size_t size; }; diff --git a/src/mbgl/util/thread_pool.cpp b/src/mbgl/util/thread_pool.cpp index af3f2e6a3..a2e4cc09d 100644 --- a/src/mbgl/util/thread_pool.cpp +++ b/src/mbgl/util/thread_pool.cpp @@ -130,6 +130,8 @@ void ThreadedSchedulerBase::schedule(const util::SimpleIdentity tag, std::functi taskCount++; } + // Take the worker lock before notifying to prevent threads from waiting while we try to wake them + std::lock_guard workerLock(workerMutex); cvAvailable.notify_one(); }