diff options
Diffstat (limited to 'K2LABI/Common/SystemUtilities/SLock.h')
-rwxr-xr-x | K2LABI/Common/SystemUtilities/SLock.h | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/K2LABI/Common/SystemUtilities/SLock.h b/K2LABI/Common/SystemUtilities/SLock.h new file mode 100755 index 0000000..d08ddb3 --- /dev/null +++ b/K2LABI/Common/SystemUtilities/SLock.h @@ -0,0 +1,79 @@ +#ifndef S_LOCK_H +#define S_LOCK_H + +#include "windows-adapter.h" + +//----------------------------------------------------------------------------- +class CLock +{ +public: + //----------------------------------------------------------------------------- + CLock(bool bCreate = true) + { + m_bCreated = false; + if (bCreate) + { + create(); + } + } + //----------------------------------------------------------------------------- + ~CLock() + { + destroy(); + } + //----------------------------------------------------------------------------- + virtual int create() + { + ::InitializeCriticalSection(&m_CS); + m_bCreated = true; + return 0; + } + //----------------------------------------------------------------------------- + virtual int destroy() + { + if (m_bCreated) + { + ::DeleteCriticalSection(&m_CS); + m_bCreated = false; + } + return 0; + } + //----------------------------------------------------------------------------- + virtual int lock() + { + ::EnterCriticalSection(&m_CS); + return 0; + } + //----------------------------------------------------------------------------- + virtual int unlock() + { + ::LeaveCriticalSection(&m_CS); + return 0; + } + //----------------------------------------------------------------------------- +private: + CRITICAL_SECTION m_CS; + bool m_bCreated; +}; +//----------------------------------------------------------------------------- +class CSafeLock +{ +public: + CSafeLock(CLock* p) : m_pLock(p) + { + m_pLock->lock(); + } + + ~CSafeLock() + { + m_pLock->unlock(); + m_pLock = 0; + } + +private: + CLock* m_pLock; +}; +//----------------------------------------------------------------------------- + +#endif // S_LOCK_H + |