issue#11: tests/unit: changed .h file guard prefix to CDSUNIT_xxx
[libcds.git] / tests / unit / lock / win32_lock.h
1 //$$CDS-header$$
2
3 #ifndef CDSUNIT_LOCK_WIN32_LOCK_H
4 #define CDSUNIT_LOCK_WIN32_LOCK_H
5
6 #if defined(_WIN32) || defined(_WIN64)
7 #include <windows.h>
8
9 #define UNIT_LOCK_WIN_CS
10
11 namespace lock {
12     namespace win {
13         // Win32 critical section
14         class CS {
15             CRITICAL_SECTION    m_cs;
16
17         public:
18             CS()    { ::InitializeCriticalSection( &m_cs ) ; }
19             ~CS()   { ::DeleteCriticalSection( &m_cs ) ; }
20
21             void lock()     { ::EnterCriticalSection( &m_cs ) ; }
22             void unlock()   { ::LeaveCriticalSection( &m_cs)  ; }
23             bool try_lock()  { return ::TryEnterCriticalSection( &m_cs ) != 0 ; }
24         };
25
26         class Mutex {
27             HANDLE  m_hMutex;
28         public:
29
30             Mutex()     { m_hMutex = ::CreateMutex( nullptr, false, nullptr ); }
31             ~Mutex()    { ::CloseHandle( m_hMutex ) ; }
32
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; }
36         };
37
38     } // namespace win
39 }   // namespace lock
40
41 #endif  // defined(_WIN32) || defined(_WIN64)
42 #endif  // #ifndef CDSUNIT_LOCK_WIN32_LOCK_H