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