1 krisbash 1.1 #ifndef _pal_thread_h
2 #define _pal_thread_h
3
4 #include <string.h>
5 #include <nits/base/nits.h>
6
7 #if defined(PAL_HAVE_PTHREADS)
8 # include <pthread.h>
9 #endif
10
11 #if defined(linux)
12 # include <unistd.h>
13 # include <syscall.h>
14 #endif
15
16 PAL_BEGIN_EXTERNC
17
18 #if defined(_MSC_VER)
19 # define THREAD_API WINAPI
20 #else
21 # define THREAD_API /* empty */
22 krisbash 1.1 #endif
23
24 typedef PAL_Uint32 (THREAD_API *ThreadProc)(void* param);
25
26 typedef struct _Thread
27 {
28 #if defined(_MSC_VER)
29 ptrdiff_t __impl;
30 #elif defined(PAL_HAVE_PTHREADS)
31 pthread_t __impl;
32 #endif
33 }
34 Thread;
35
36 typedef struct _ThreadID
37 {
38 #if defined(_MSC_VER)
39 DWORD __impl;
40 #elif defined(PAL_HAVE_PTHREADS)
41 pthread_t __impl;
42 #endif
43 krisbash 1.1 }
44 ThreadID;
45
46 _Return_type_success_(return == 0) int Thread_CreateDetached_Injected(
47 _In_ ThreadProc threadProcCallback,
48 _In_opt_ ThreadProc threadProcDestructor,
49 _In_ void* threadProcParam,
50 _In_ NitsCallSite cs);
51
52 #define Thread_CreateDetached(threadProcCallback, threadProcDestructor, threadProcParam) \
53 Thread_CreateDetached_Injected(threadProcCallback, threadProcDestructor, threadProcParam, NitsHere())
54
55 _Return_type_success_(return == 0) int Thread_CreateJoinable_Injected(
56 _Out_ Thread* self,
57 _In_ ThreadProc threadProcCallback,
58 _In_opt_ ThreadProc threadProcDestructor,
59 _In_ void* threadProcParam,
60 NitsCallSite cs);
61
62 #define Thread_CreateJoinable(self, threadProcCallback, threadProcDestructor, threadProcParam) Thread_CreateJoinable_Injected(self, threadProcCallback, threadProcDestructor, threadProcParam, NitsHere())
63
64 krisbash 1.1 #if defined(_MSC_VER)
65 void Thread_Destroy(
66 _Inout_ Thread* self);
67 #else
68 PAL_INLINE void Thread_Destroy(
69 _Inout_ Thread* self)
70 {
71 /* Zero out so it will fail if called before join */
72 memset(self, 0, sizeof(*self));
73 }
74 #endif
75
76 _Return_type_success_(return == 0) int Thread_Join(
77 _In_ Thread* self,
78 _Out_ PAL_Uint32* returnValue);
79
80 #if defined(_MSC_VER)
81 PAL_INLINE
82 int Thread_Equal(
83 _In_ ThreadID* thread1,
84 _In_ ThreadID* thread2)
85 krisbash 1.1 {
86 return thread1->__impl == thread2->__impl;
87 }
88 #else
89 int Thread_Equal(
90 _In_ ThreadID* thread1,
91 _In_ ThreadID* thread2);
92 #endif
93
94 #if defined(_MSC_VER)
95 PAL_INLINE
96 ThreadID Thread_ID()
97 {
98 ThreadID self;
99 self.__impl = GetCurrentThreadId();
100 return self;
101 }
102 #else
103 ThreadID Thread_ID();
104 #endif
105
106 krisbash 1.1 PAL_INLINE
107 PAL_Uint64 Thread_TID()
108 {
109 #if defined(_MSC_VER)
110 return (PAL_Uint64)GetCurrentThreadId();
111 #elif defined(CONFIG_OS_LINUX)
112 return (PAL_Uint64)syscall(SYS_gettid);
113 #else
114 // Avoid using a cast here since pthread_self() may return a structure.
115 // If so, we need to discover that and provide an alternative for that
116 // platform.
117 return pthread_self();
118 #endif
119 }
120
121 void Thread_Yield();
122
123 PAL_INLINE ThreadID Thread_ID_Null()
124 {
125 ThreadID returnValue = {0};
126 return returnValue;
127 krisbash 1.1 }
128
129 PAL_END_EXTERNC
130
131 #endif /* _pal_thread_h */
|