mirror of
https://github.com/vcmi/vcmi.git
synced 2025-01-14 02:33:51 +02:00
e4dc00abac
* all lock/unlock and unlock/lock pairs are done by RAII guards now * fixed two possible crashes at the end of battle when last stack was killed by spell. That should fix #749 and #752. * fixed a very nasty race condition, eliminating possible deadlock at the start of battle when human hero has tactics * fixed #422
84 lines
1.5 KiB
C++
84 lines
1.5 KiB
C++
#pragma once
|
|
|
|
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_)
|
|
{
|
|
unlock(*m);
|
|
}
|
|
|
|
unlock_guard(unlock_guard &&other)
|
|
: m(other.m)
|
|
{
|
|
other.m = NULL;
|
|
}
|
|
|
|
void release()
|
|
{
|
|
m = NULL;
|
|
}
|
|
|
|
~unlock_guard()
|
|
{
|
|
if(m)
|
|
lock(*m);
|
|
}
|
|
};
|
|
|
|
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>
|
|
unlock_guard<Mutex, detail::unlock_shared_policy<Mutex> > makeUnlockSharedGuard(Mutex &m_)
|
|
{
|
|
return unlock_guard<Mutex, detail::unlock_shared_policy<Mutex> >(m_);
|
|
}
|
|
|
|
typedef unlock_guard<boost::shared_mutex, detail::unlock_shared_policy<boost::shared_mutex> > unlock_shared_guard;
|
|
}
|