From ba08cda99e0127fabc21a864883e63fec1b88df4 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 12 Mar 2019 12:36:31 +0300 Subject: CRITICAL_SECTION isolated inside mir_cs --- include/m_system_cpp.h | 64 ++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 33 deletions(-) (limited to 'include') diff --git a/include/m_system_cpp.h b/include/m_system_cpp.h index 1cd2163f10..a592311f37 100644 --- a/include/m_system_cpp.h +++ b/include/m_system_cpp.h @@ -36,6 +36,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #if defined(__cplusplus) +/////////////////////////////////////////////////////////////////////////////// +// general lists' templates + +struct MNonCopyable +{ + MNonCopyable() {} + + MNonCopyable(const MNonCopyable&) = delete; + MNonCopyable& operator=(const MNonCopyable&) = delete; +}; + /////////////////////////////////////////////////////////////////////////////// // mir_ptr - automatic pointer for buffers, allocated using mir_alloc/mir_calloc @@ -70,20 +81,35 @@ public: __inline mir_cs() { ::InitializeCriticalSection(&m_cs); } __inline ~mir_cs() { ::DeleteCriticalSection(&m_cs); } + __inline void Lock() { ::EnterCriticalSection(&m_cs); } + __inline void Unlock() { ::LeaveCriticalSection(&m_cs); } + __inline operator CRITICAL_SECTION&() { return m_cs; } }; /////////////////////////////////////////////////////////////////////////////// // mir_cslock - simple locker for the critical sections -class mir_cslock +class mir_cslock : public MNonCopyable +{ + mir_cs &cs; + +public: + __inline mir_cslock(mir_cs &_cs) : cs(_cs) { cs.Lock(); } + __inline ~mir_cslock() { cs.Unlock(); } +}; + +class mir_cslockfull : public MNonCopyable { - CRITICAL_SECTION &cs; - __inline mir_cslock& operator=(const mir_cslock&) { return *this; } + mir_cs &cs; + bool bIsLocked = false; public: - __inline mir_cslock(CRITICAL_SECTION &_cs) : cs(_cs) { ::EnterCriticalSection(&cs); } - __inline ~mir_cslock() { ::LeaveCriticalSection(&cs); } + __inline void lock() { bIsLocked = true; cs.Lock(); } + __inline void unlock() { bIsLocked = false; cs.Unlock(); } + + __inline mir_cslockfull(mir_cs &_cs) : cs(_cs) { lock(); } + __inline ~mir_cslockfull() { if (bIsLocked) unlock(); } }; ////////////////////////////////////////////////////////////////////////////// @@ -113,23 +139,6 @@ public: } }; -/////////////////////////////////////////////////////////////////////////////// -// mir_cslockfull - controllable locker for the critical sections - -class mir_cslockfull -{ - CRITICAL_SECTION &cs; - bool bIsLocked; - __inline mir_cslockfull& operator= (const mir_cslockfull&) { return *this; } - -public: - __inline void lock() { bIsLocked = true; EnterCriticalSection(&cs); } - __inline void unlock() { bIsLocked = false; LeaveCriticalSection(&cs); } - - __inline mir_cslockfull(CRITICAL_SECTION &_cs) : cs(_cs) { lock(); } - __inline ~mir_cslockfull() { if (bIsLocked) unlock(); } -}; - /////////////////////////////////////////////////////////////////////////////// // basic class for classes that should be cleared inside new() @@ -148,17 +157,6 @@ public: /////////////////////////////////////////////////////////////////////////////// // general lists' templates -struct MNonCopyable -{ - MNonCopyable() {} - - MNonCopyable(const MNonCopyable&) = delete; - MNonCopyable& operator=(const MNonCopyable&) = delete; -}; - -/////////////////////////////////////////////////////////////////////////////// -// general lists' templates - #define NumericKeySortT -1 #define HandleKeySortT -2 #define PtrKeySortT -3 -- cgit v1.2.3