3 #ifndef CDSUNIT_LOCK_WIN32_LOCK_H
4 #define CDSUNIT_LOCK_WIN32_LOCK_H
6 #if defined(_WIN32) || defined(_WIN64)
9 #define UNIT_LOCK_WIN_CS
13 // Win32 critical section
15 CRITICAL_SECTION m_cs;
18 CS() { ::InitializeCriticalSection( &m_cs ) ; }
19 ~CS() { ::DeleteCriticalSection( &m_cs ) ; }
21 void lock() { ::EnterCriticalSection( &m_cs ) ; }
22 void unlock() { ::LeaveCriticalSection( &m_cs) ; }
23 bool try_lock() { return ::TryEnterCriticalSection( &m_cs ) != 0 ; }
30 Mutex() { m_hMutex = ::CreateMutex( nullptr, false, nullptr ); }
31 ~Mutex() { ::CloseHandle( m_hMutex ) ; }
33 void lock() { ::WaitForSingleObject( m_hMutex, INFINITE ); }
34 void unlock() { ::ReleaseMutex( m_hMutex ); }
35 bool try_lock() { return ::WaitForSingleObject( m_hMutex, 0) == WAIT_OBJECT_0; }
41 #endif // defined(_WIN32) || defined(_WIN64)
42 #endif // #ifndef CDSUNIT_LOCK_WIN32_LOCK_H