From 157e3750e304913e1277ab8ebdd09ab99c2db0a4 Mon Sep 17 00:00:00 2001 From: Johannes Schmidt Date: Wed, 11 Jun 2025 09:54:53 +0200 Subject: [PATCH] Add IsLockable method to WaitGroup --- lib/base/wait-group.cpp | 7 ++++++- lib/base/wait-group.hpp | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/base/wait-group.cpp b/lib/base/wait-group.cpp index 1e1ad00ee..6ad716605 100644 --- a/lib/base/wait-group.cpp +++ b/lib/base/wait-group.cpp @@ -26,6 +26,11 @@ void StoppableWaitGroup::unlock_shared() } } +bool StoppableWaitGroup::IsLockable() const +{ + return !m_Stopped.load(std::memory_order_relaxed); +} + /** * Disallow new shared locks, wait for all existing ones. */ @@ -33,6 +38,6 @@ void StoppableWaitGroup::Join() { std::unique_lock lock (m_Mutex); - m_Stopped = true; + m_Stopped.store(true, std::memory_order_relaxed); m_CV.wait(lock, [this] { return !m_SharedLocks; }); } diff --git a/lib/base/wait-group.hpp b/lib/base/wait-group.hpp index 5b4527011..618f07aec 100644 --- a/lib/base/wait-group.hpp +++ b/lib/base/wait-group.hpp @@ -3,6 +3,7 @@ #pragma once #include "base/object.hpp" +#include "base/atomic.hpp" #include #include #include @@ -22,6 +23,8 @@ public: virtual bool try_lock_shared() = 0; virtual void unlock_shared() = 0; + + virtual bool IsLockable() const = 0; }; /** @@ -42,13 +45,16 @@ public: bool try_lock_shared() override; void unlock_shared() override; + + bool IsLockable() const override; + void Join(); private: std::mutex m_Mutex; std::condition_variable m_CV; uint_fast32_t m_SharedLocks = 0; - bool m_Stopped = false; + Atomic m_Stopped = false; }; }