mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-03-17 10:21:11 +00:00
Use variant and optional to implement Maybe*Locks
To avoid calling lock and unlock on the mutex. User-defined destructor is no more needed.
This commit is contained in:
parent
a1e7622a30
commit
8c3c65fe9f
@ -1,4 +1,8 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <variant>
|
||||||
|
#include <optional>
|
||||||
|
#include <shared_mutex>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#include <BulletCollision/BroadphaseCollision/btDbvtBroadphase.h>
|
#include <BulletCollision/BroadphaseCollision/btDbvtBroadphase.h>
|
||||||
#include <BulletCollision/CollisionShapes/btCollisionShape.h>
|
#include <BulletCollision/CollisionShapes/btCollisionShape.h>
|
||||||
@ -28,6 +32,15 @@
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
template <class Mutex>
|
||||||
|
std::optional<std::unique_lock<Mutex>> makeExclusiveLock(Mutex& mutex, int threadCount)
|
||||||
|
{
|
||||||
|
assert(threadCount >= 0);
|
||||||
|
if (threadCount > 0)
|
||||||
|
return std::unique_lock(mutex);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
/// @brief A scoped lock that is either exclusive or inexistent depending on configuration
|
/// @brief A scoped lock that is either exclusive or inexistent depending on configuration
|
||||||
template<class Mutex>
|
template<class Mutex>
|
||||||
class MaybeExclusiveLock
|
class MaybeExclusiveLock
|
||||||
@ -35,24 +48,23 @@ namespace
|
|||||||
public:
|
public:
|
||||||
/// @param mutex a mutex
|
/// @param mutex a mutex
|
||||||
/// @param threadCount decide wether the excluse lock will be taken
|
/// @param threadCount decide wether the excluse lock will be taken
|
||||||
MaybeExclusiveLock(Mutex& mutex, int threadCount) : mMutex(mutex), mThreadCount(threadCount)
|
explicit MaybeExclusiveLock(Mutex& mutex, int threadCount)
|
||||||
{
|
: mImpl(makeExclusiveLock(mutex, threadCount))
|
||||||
assert(threadCount >= 0);
|
{}
|
||||||
if (mThreadCount > 0)
|
|
||||||
mMutex.lock();
|
|
||||||
}
|
|
||||||
|
|
||||||
~MaybeExclusiveLock()
|
|
||||||
{
|
|
||||||
if (mThreadCount > 0)
|
|
||||||
mMutex.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex& mMutex;
|
std::optional<std::unique_lock<Mutex>> mImpl;
|
||||||
unsigned int mThreadCount;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <class Mutex>
|
||||||
|
std::optional<std::shared_lock<Mutex>> makeSharedLock(Mutex& mutex, int threadCount)
|
||||||
|
{
|
||||||
|
assert(threadCount >= 0);
|
||||||
|
if (threadCount > 0)
|
||||||
|
return std::shared_lock(mutex);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
/// @brief A scoped lock that is either shared or inexistent depending on configuration
|
/// @brief A scoped lock that is either shared or inexistent depending on configuration
|
||||||
template<class Mutex>
|
template<class Mutex>
|
||||||
class MaybeSharedLock
|
class MaybeSharedLock
|
||||||
@ -60,24 +72,25 @@ namespace
|
|||||||
public:
|
public:
|
||||||
/// @param mutex a shared mutex
|
/// @param mutex a shared mutex
|
||||||
/// @param threadCount decide wether the shared lock will be taken
|
/// @param threadCount decide wether the shared lock will be taken
|
||||||
MaybeSharedLock(Mutex& mutex, int threadCount) : mMutex(mutex), mThreadCount(threadCount)
|
explicit MaybeSharedLock(Mutex& mutex, int threadCount)
|
||||||
{
|
: mImpl(makeSharedLock(mutex, threadCount))
|
||||||
assert(threadCount >= 0);
|
{}
|
||||||
if (mThreadCount > 0)
|
|
||||||
mMutex.lock_shared();
|
|
||||||
}
|
|
||||||
|
|
||||||
~MaybeSharedLock()
|
|
||||||
{
|
|
||||||
if (mThreadCount > 0)
|
|
||||||
mMutex.unlock_shared();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex& mMutex;
|
std::optional<std::shared_lock<Mutex>> mImpl;
|
||||||
unsigned int mThreadCount;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <class Mutex>
|
||||||
|
std::variant<std::monostate, std::unique_lock<Mutex>, std::shared_lock<Mutex>> makeLock(Mutex& mutex, int threadCount)
|
||||||
|
{
|
||||||
|
assert(threadCount >= 0);
|
||||||
|
if (threadCount > 1)
|
||||||
|
return std::shared_lock(mutex);
|
||||||
|
if (threadCount == 1)
|
||||||
|
return std::unique_lock(mutex);
|
||||||
|
return std::monostate {};
|
||||||
|
}
|
||||||
|
|
||||||
/// @brief A scoped lock that is either shared, exclusive or inexistent depending on configuration
|
/// @brief A scoped lock that is either shared, exclusive or inexistent depending on configuration
|
||||||
template<class Mutex>
|
template<class Mutex>
|
||||||
class MaybeLock
|
class MaybeLock
|
||||||
@ -85,25 +98,11 @@ namespace
|
|||||||
public:
|
public:
|
||||||
/// @param mutex a shared mutex
|
/// @param mutex a shared mutex
|
||||||
/// @param threadCount decide wether the lock will be shared, exclusive or inexistent
|
/// @param threadCount decide wether the lock will be shared, exclusive or inexistent
|
||||||
MaybeLock(Mutex& mutex, int threadCount) : mMutex(mutex), mThreadCount(threadCount)
|
explicit MaybeLock(Mutex& mutex, int threadCount)
|
||||||
{
|
: mImpl(makeLock(mutex, threadCount)) {}
|
||||||
assert(threadCount >= 0);
|
|
||||||
if (mThreadCount > 1)
|
|
||||||
mMutex.lock_shared();
|
|
||||||
else if(mThreadCount == 1)
|
|
||||||
mMutex.lock();
|
|
||||||
}
|
|
||||||
|
|
||||||
~MaybeLock()
|
|
||||||
{
|
|
||||||
if (mThreadCount > 1)
|
|
||||||
mMutex.unlock_shared();
|
|
||||||
else if(mThreadCount == 1)
|
|
||||||
mMutex.unlock();
|
|
||||||
}
|
|
||||||
private:
|
private:
|
||||||
Mutex& mMutex;
|
std::variant<std::monostate, std::unique_lock<Mutex>, std::shared_lock<Mutex>> mImpl;
|
||||||
unsigned int mThreadCount;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bool isUnderWater(const MWPhysics::ActorFrameData& actorData)
|
bool isUnderWater(const MWPhysics::ActorFrameData& actorData)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user