3 #ifndef CDSUNIT_LOCK_WIN32_LOCK_H
4 #define CDSUNIT_LOCK_WIN32_LOCK_H
6 #if defined(_WIN32) || defined(_WIN64)
10 #define UNIT_LOCK_WIN_CS
14 // Win32 critical section
16 CRITICAL_SECTION m_cs;
19 CS() { ::InitializeCriticalSection( &m_cs ) ; }
20 ~CS() { ::DeleteCriticalSection( &m_cs ) ; }
22 void lock() { ::EnterCriticalSection( &m_cs ) ; }
23 void unlock() { ::LeaveCriticalSection( &m_cs) ; }
24 bool try_lock() { return ::TryEnterCriticalSection( &m_cs ) != 0 ; }
31 Mutex() { m_hMutex = ::CreateMutex( nullptr, false, nullptr ); }
32 ~Mutex() { ::CloseHandle( m_hMutex ) ; }
34 void lock() { ::WaitForSingleObject( m_hMutex, INFINITE ); }
35 void unlock() { ::ReleaseMutex( m_hMutex ); }
36 bool try_lock() { return ::WaitForSingleObject( m_hMutex, 0) == WAIT_OBJECT_0; }
42 #endif // defined(_WIN32) || defined(_WIN64)
43 #endif // #ifndef CDSUNIT_LOCK_WIN32_LOCK_H