Add IsLockable method to WaitGroup

This commit is contained in:
Johannes Schmidt 2025-06-11 09:54:53 +02:00
parent b27310fb6c
commit 157e3750e3
2 changed files with 13 additions and 2 deletions

View File

@ -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; });
}

View File

@ -3,6 +3,7 @@
#pragma once
#include "base/object.hpp"
#include "base/atomic.hpp"
#include <condition_variable>
#include <cstdint>
#include <mutex>
@ -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<bool> m_Stopped = false;
};
}