2012-12-19 17:54:10 +03:00
|
|
|
/*
|
|
|
|
* UnlockGuard.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2012-02-20 00:03:43 +03:00
|
|
|
#pragma once
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2012-02-20 00:03:43 +03:00
|
|
|
namespace vstd
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
template<typename Mutex>
|
|
|
|
class unlock_policy
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
void unlock(Mutex &m)
|
|
|
|
{
|
|
|
|
m.unlock();
|
|
|
|
}
|
|
|
|
void lock(Mutex &m)
|
|
|
|
{
|
|
|
|
m.lock();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Mutex>
|
|
|
|
class unlock_shared_policy
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
void unlock(Mutex &m)
|
|
|
|
{
|
|
|
|
m.unlock_shared();
|
|
|
|
}
|
|
|
|
void lock(Mutex &m)
|
|
|
|
{
|
|
|
|
m.lock_shared();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//similar to boost::lock_guard but UNlocks for the scope + assertions
|
|
|
|
template<typename Mutex, typename LockingPolicy = detail::unlock_policy<Mutex> >
|
|
|
|
class unlock_guard : LockingPolicy
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
Mutex* m;
|
|
|
|
|
|
|
|
explicit unlock_guard(unlock_guard&);
|
|
|
|
unlock_guard& operator=(unlock_guard&);
|
|
|
|
public:
|
|
|
|
explicit unlock_guard(Mutex& m_):
|
|
|
|
m(&m_)
|
|
|
|
{
|
2012-04-08 13:34:23 +03:00
|
|
|
this->unlock(*m);
|
2012-02-20 00:03:43 +03:00
|
|
|
}
|
|
|
|
|
2012-04-06 18:02:15 +03:00
|
|
|
unlock_guard()
|
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
m = nullptr;
|
2012-04-06 18:02:15 +03:00
|
|
|
}
|
|
|
|
|
2012-02-20 00:03:43 +03:00
|
|
|
unlock_guard(unlock_guard &&other)
|
|
|
|
: m(other.m)
|
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
other.m = nullptr;
|
2012-02-20 00:03:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void release()
|
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
m = nullptr;
|
2012-02-20 00:03:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~unlock_guard()
|
|
|
|
{
|
|
|
|
if(m)
|
2012-04-08 13:34:23 +03:00
|
|
|
this->lock(*m);
|
2012-02-20 00:03:43 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Mutex>
|
|
|
|
unlock_guard<Mutex, detail::unlock_policy<Mutex> > makeUnlockGuard(Mutex &m_)
|
|
|
|
{
|
|
|
|
return unlock_guard<Mutex, detail::unlock_policy<Mutex> >(m_);
|
|
|
|
}
|
|
|
|
template<typename Mutex>
|
2012-04-06 18:02:15 +03:00
|
|
|
unlock_guard<Mutex, detail::unlock_policy<Mutex> > makeEmptyGuard(Mutex &)
|
|
|
|
{
|
|
|
|
return unlock_guard<Mutex, detail::unlock_policy<Mutex> >();
|
|
|
|
}
|
|
|
|
template<typename Mutex>
|
|
|
|
unlock_guard<Mutex, detail::unlock_policy<Mutex> > makeUnlockGuardIf(Mutex &m_, bool shallUnlock)
|
|
|
|
{
|
|
|
|
return shallUnlock
|
|
|
|
? makeUnlockGuard(m_)
|
|
|
|
: unlock_guard<Mutex, detail::unlock_policy<Mutex> >();
|
|
|
|
}
|
|
|
|
template<typename Mutex>
|
2012-02-20 00:03:43 +03:00
|
|
|
unlock_guard<Mutex, detail::unlock_shared_policy<Mutex> > makeUnlockSharedGuard(Mutex &m_)
|
|
|
|
{
|
|
|
|
return unlock_guard<Mutex, detail::unlock_shared_policy<Mutex> >(m_);
|
|
|
|
}
|
2012-04-06 18:02:15 +03:00
|
|
|
template<typename Mutex>
|
|
|
|
unlock_guard<Mutex, detail::unlock_shared_policy<Mutex> > makeUnlockSharedGuardIf(Mutex &m_, bool shallUnlock)
|
|
|
|
{
|
|
|
|
return shallUnlock
|
|
|
|
? makeUnlockSharedGuard(m_)
|
|
|
|
: unlock_guard<Mutex, detail::unlock_shared_policy<Mutex> >();
|
|
|
|
}
|
2012-02-20 00:03:43 +03:00
|
|
|
|
2023-04-17 23:11:16 +02:00
|
|
|
using unlock_shared_guard = unlock_guard<boost::shared_mutex, detail::unlock_shared_policy<boost::shared_mutex>>;
|
2012-02-20 00:03:43 +03:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|