2018-09-29 17:36:42 +00:00
|
|
|
#ifndef OPENMW_COMPONENTS_MISC_GUARDED_H
|
|
|
|
#define OPENMW_COMPONENTS_MISC_GUARDED_H
|
|
|
|
|
2020-02-22 21:03:44 +00:00
|
|
|
#include <condition_variable>
|
2018-09-29 17:36:42 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2021-12-19 21:49:41 +00:00
|
|
|
#include <type_traits>
|
2018-09-29 17:36:42 +00:00
|
|
|
|
|
|
|
namespace Misc
|
|
|
|
{
|
|
|
|
template <class T>
|
|
|
|
class Locked
|
|
|
|
{
|
|
|
|
public:
|
2021-12-19 21:49:41 +00:00
|
|
|
Locked(std::mutex& mutex, std::remove_reference_t<T>& value)
|
2018-09-29 17:36:42 +00:00
|
|
|
: mLock(mutex)
|
|
|
|
, mValue(value)
|
|
|
|
{
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2021-12-19 21:49:41 +00:00
|
|
|
std::remove_reference_t<T>& get() const { return mValue.get(); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2021-12-19 21:49:41 +00:00
|
|
|
std::remove_reference_t<T>* operator->() const { return &get(); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2021-12-19 21:49:41 +00:00
|
|
|
std::remove_reference_t<T>& operator*() const { return get(); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-09-29 17:36:42 +00:00
|
|
|
private:
|
|
|
|
std::unique_lock<std::mutex> mLock;
|
2021-12-19 21:49:41 +00:00
|
|
|
std::reference_wrapper<std::remove_reference_t<T>> mValue;
|
2018-09-29 17:36:42 +00:00
|
|
|
};
|
|
|
|
|
2018-09-29 19:57:41 +00:00
|
|
|
template <class T>
|
|
|
|
class ScopeGuarded
|
|
|
|
{
|
|
|
|
public:
|
2018-11-01 22:59:27 +00:00
|
|
|
ScopeGuarded()
|
|
|
|
: mMutex()
|
|
|
|
, mValue()
|
|
|
|
{
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-09-29 19:57:41 +00:00
|
|
|
ScopeGuarded(const T& value)
|
2018-11-01 22:59:27 +00:00
|
|
|
: mMutex()
|
|
|
|
, mValue(value)
|
2018-09-29 19:57:41 +00:00
|
|
|
{
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-09-29 19:57:41 +00:00
|
|
|
ScopeGuarded(T&& value)
|
2018-11-01 22:59:27 +00:00
|
|
|
: mMutex()
|
|
|
|
, mValue(std::move(value))
|
2018-09-29 19:57:41 +00:00
|
|
|
{
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-09-30 22:33:25 +00:00
|
|
|
template <class... Args>
|
|
|
|
ScopeGuarded(Args&&... args)
|
2018-11-01 22:59:27 +00:00
|
|
|
: mMutex()
|
|
|
|
, mValue(std::forward<Args>(args)...)
|
2018-09-30 22:33:25 +00:00
|
|
|
{
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-09-30 22:33:25 +00:00
|
|
|
ScopeGuarded(const ScopeGuarded& other)
|
|
|
|
: mMutex()
|
|
|
|
, mValue(other.lock().get())
|
|
|
|
{
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-09-30 22:33:25 +00:00
|
|
|
ScopeGuarded(ScopeGuarded&& other)
|
|
|
|
: mMutex()
|
|
|
|
, mValue(std::move(other.lock().get()))
|
2018-09-29 19:57:41 +00:00
|
|
|
{
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-29 19:57:41 +00:00
|
|
|
Locked<T> lock() { return Locked<T>(mMutex, mValue); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-09-29 19:57:41 +00:00
|
|
|
Locked<const T> lockConst() const { return Locked<const T>(mMutex, mValue); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2020-02-22 21:03:44 +00:00
|
|
|
template <class Predicate>
|
|
|
|
void wait(std::condition_variable& cv, Predicate&& predicate)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(mMutex);
|
|
|
|
cv.wait(lock, [&] { return predicate(mValue); });
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-09-29 19:57:41 +00:00
|
|
|
private:
|
2021-05-05 16:13:17 +00:00
|
|
|
mutable std::mutex mMutex;
|
2018-09-29 19:57:41 +00:00
|
|
|
T mValue;
|
|
|
|
};
|
2018-09-29 17:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|