Move libcds 1.6.0 from SVN
[libcds.git] / cds / os / win / thread.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_OS_WIN_THREAD_H
4 #define __CDS_OS_WIN_THREAD_H
5
6 #include <windows.h>
7
8 namespace cds { namespace OS {
9     /// Windows-specific functions
10     namespace Win32 {
11
12         /// OS-specific type of thread identifier
13         typedef DWORD           ThreadId;
14
15         /// OS-specific type of thread handle
16         typedef HANDLE            ThreadHandle;
17
18         /// Get null thread id
19         CDS_CONSTEXPR static inline ThreadId nullThreadId()
20         {
21             return 0;
22         }
23
24         /// Get current thread id
25         static inline ThreadId getCurrentThreadId()
26         {
27             return ::GetCurrentThreadId();
28         }
29
30         /// Tests whether the thread is alive
31         static inline bool isThreadAlive( ThreadId id )
32         {
33             HANDLE h = ::OpenThread( SYNCHRONIZE, FALSE, id );
34             if ( h == NULL )
35                 return false;
36             ::CloseHandle( h );
37             return true;
38         }
39
40         /// Yield current thread, switch to another
41         static inline void yield()
42         {
43     #    if _WIN32_WINNT >= 0x0400
44             ::SwitchToThread();
45     #   else
46             ::Sleep(0);
47     #   endif
48         }
49
50         /// Default backoff::yield implementation
51         static inline void    backoff()
52         {
53             yield();
54         }
55     }    // namespace Win32
56
57     using Win32::ThreadId;
58     using Win32::ThreadHandle;
59
60     using Win32::nullThreadId;
61     using Win32::getCurrentThreadId;
62     using Win32::isThreadAlive;
63     using Win32::yield;
64     using Win32::backoff;
65
66 }} // namespace cds::OS
67
68 #endif // #ifndef __CDS_OS_WIN_THREAD_H