JD2022-TU1/main/extern/dtest/dtcore/inc/dtmutex.h

87 lines
1.7 KiB
C++

#pragma once
#include "dtcore/inc/dtdefinitions.h"
namespace dt
{
class MutexImpl;
class Mutex
{
public:
// no copy
Mutex(const Mutex&) = delete;
Mutex& operator=(const Mutex&) = delete;
DT_CORE_DLL_ENTRY Mutex();
DT_CORE_DLL_ENTRY ~Mutex();
DT_CORE_DLL_ENTRY void Lock();
DT_CORE_DLL_ENTRY void Unlock();
DT_CORE_DLL_ENTRY bool TryLock();
private:
enum { DT_SHADOW_SIZE_MUTEX = 88 };
U8 m_Shadow[DT_SHADOW_SIZE_MUTEX];
MutexImpl* m_Impl;
};
//////////////////////////////////////////////////////////////////////////
template <typename LOCK> class AutoLock
{
public:
AutoLock();
AutoLock(LOCK& lock);
AutoLock(const AutoLock& lock);
AutoLock& operator=(const AutoLock& rhs);
~AutoLock();
private:
LOCK* m_Lock;
};
//////////////////////////////////////////////////////////////////////////
template <typename LOCK> AutoLock<LOCK>::AutoLock()
: m_Lock(nullptr)
{
}
template <typename LOCK> AutoLock<LOCK>::AutoLock(LOCK& lock)
: m_Lock(&lock)
{
m_Lock->Lock();
}
template <typename LOCK> AutoLock<LOCK>::AutoLock(const AutoLock& lock)
: m_Lock(nullptr)
{
*this = lock;
}
template <typename LOCK> AutoLock<LOCK>& AutoLock<LOCK>::operator=(const AutoLock& rhs)
{
if (m_Lock)
{
m_Lock->Unlock();
}
m_Lock = rhs.m_Lock;
if (m_Lock)
{
m_Lock->Lock();
}
return *this;
}
template <typename LOCK> AutoLock<LOCK>::~AutoLock()
{
if (m_Lock)
{
m_Lock->Unlock();
}
}
}