diff options
author | Christian Gromm <christian.gromm@microchip.com> | 2016-06-21 14:14:04 +0200 |
---|---|---|
committer | Christian Gromm <christian.gromm@microchip.com> | 2016-06-21 14:14:04 +0200 |
commit | 860b3c0ccfb2756b8ed77523048952011b74a212 (patch) | |
tree | e0672fb5cf923dcf0a4a48a62a69011e1b72976a /K2LABI/Common/SystemUtilities/SLock.h | |
parent | 742bace60b9c0a0f583d8afe1097d029f468fa03 (diff) |
src: can-lin: initial source import
This patch adds the sources needed for the HVAC and iDrive applications.
Change-Id: I53148a5d11c34787dd11295939bbaf8702c64dcb
Signed-off-by: Christian Gromm <christian.gromm@microchip.com>
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 + |