Revert "Replace cds::OS::ThreadId with std::thread::id, cds::OS::nullThreadId() with...
[libcds.git] / cds / os / posix / thread.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_OS_POSIX_THREAD_H
4 #define __CDS_OS_POSIX_THREAD_H
5
6 #include <pthread.h>
7
8 namespace cds { namespace OS {
9     /// posix-related wrappers
10     namespace posix {
11
12         /// Posix thread id type
13         typedef std::thread::native_thread_handle ThreadId;
14
15         /// Get current thread id
16         static inline ThreadId getCurrentThreadId()    { return pthread_self(); }
17
18         /// Checks if thread \p id is alive
19         static inline bool isThreadAlive( ThreadId id )
20         {
21             // if sig is zero, error checking is performed but no signal is actually sent.
22             // ESRCH - No thread could be found corresponding to that specified by the given thread ID
23             // Unresolved problem: Linux may crash on dead thread_id.  Workaround unknown (except signal handler...)
24             return pthread_kill( id, 0 ) != ESRCH;
25         }
26
27         /// Default back-off thread strategy (yield)
28         static inline void backoff()    
29         { 
30             std::this_thread::yield(); 
31         }
32
33     }    // namespace posix
34
35     using posix::ThreadId;
36     constexpr const ThreadId c_NullThreadId = 0;
37
38     using posix::getCurrentThreadId;
39     using posix::isThreadAlive;
40     using posix::backoff;
41
42 }} // namespace cds::OS
43
44
45 #endif // #ifndef __CDS_OS_POSIX_THREAD_H