Free Electron
threads.h
1 #ifndef AL_THREADS_H
2 #define AL_THREADS_H
3 
4 #include <time.h>
5 
6 #if defined(__GNUC__) && defined(__i386__)
7 /* force_align_arg_pointer is required for proper function arguments aligning
8  * when SSE code is used. Some systems (Windows, QNX) do not guarantee our
9  * thread functions will be properly aligned on the stack, even though GCC may
10  * generate code with the assumption that it is. */
11 #define FORCE_ALIGN __attribute__((force_align_arg_pointer))
12 #else
13 #define FORCE_ALIGN
14 #endif
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 enum {
21  althrd_success = 0,
22  althrd_error,
23  althrd_nomem,
24  althrd_timedout,
25  althrd_busy
26 };
27 
28 enum {
29  almtx_plain = 0,
30  almtx_recursive = 1,
31 };
32 
33 typedef int (*althrd_start_t)(void*);
34 typedef void (*altss_dtor_t)(void*);
35 
36 
37 #define AL_TIME_UTC 1
38 
39 
40 #ifdef _WIN32
41 #define WIN32_LEAN_AND_MEAN
42 #include <windows.h>
43 
44 
45 #ifndef HAVE_STRUCT_TIMESPEC
46 struct timespec {
47  time_t tv_sec;
48  long tv_nsec;
49 };
50 #endif
51 
52 typedef DWORD althrd_t;
53 typedef CRITICAL_SECTION almtx_t;
54 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
55 typedef CONDITION_VARIABLE alcnd_t;
56 #else
57 typedef struct { void *Ptr; } alcnd_t;
58 #endif
59 typedef HANDLE alsem_t;
60 typedef DWORD altss_t;
61 typedef LONG alonce_flag;
62 
63 #define AL_ONCE_FLAG_INIT 0
64 
65 int althrd_sleep(const struct timespec *ts, struct timespec *rem);
66 void alcall_once(alonce_flag *once, void (*callback)(void));
67 
68 void althrd_deinit(void);
69 void althrd_thread_detach(void);
70 
71 
72 inline althrd_t althrd_current(void)
73 {
74  return GetCurrentThreadId();
75 }
76 
77 inline int althrd_equal(althrd_t thr0, althrd_t thr1)
78 {
79  return thr0 == thr1;
80 }
81 
82 inline void althrd_exit(int res)
83 {
84  ExitThread(res);
85 }
86 
87 inline void althrd_yield(void)
88 {
89  SwitchToThread();
90 }
91 
92 
93 inline int almtx_lock(almtx_t *mtx)
94 {
95  if(!mtx) return althrd_error;
96  EnterCriticalSection(mtx);
97  return althrd_success;
98 }
99 
100 inline int almtx_unlock(almtx_t *mtx)
101 {
102  if(!mtx) return althrd_error;
103  LeaveCriticalSection(mtx);
104  return althrd_success;
105 }
106 
107 inline int almtx_trylock(almtx_t *mtx)
108 {
109  if(!mtx) return althrd_error;
110  if(!TryEnterCriticalSection(mtx))
111  return althrd_busy;
112  return althrd_success;
113 }
114 
115 
116 inline void *altss_get(altss_t tss_id)
117 {
118  return TlsGetValue(tss_id);
119 }
120 
121 inline int altss_set(altss_t tss_id, void *val)
122 {
123  if(TlsSetValue(tss_id, val) == 0)
124  return althrd_error;
125  return althrd_success;
126 }
127 
128 #else
129 
130 #include <stdint.h>
131 #include <errno.h>
132 #include <pthread.h>
133 #include <semaphore.h>
134 
135 
136 typedef pthread_t althrd_t;
137 typedef pthread_mutex_t almtx_t;
138 typedef pthread_cond_t alcnd_t;
139 typedef sem_t alsem_t;
140 typedef pthread_key_t altss_t;
141 typedef pthread_once_t alonce_flag;
142 
143 #define AL_ONCE_FLAG_INIT PTHREAD_ONCE_INIT
144 
145 
146 inline althrd_t althrd_current(void)
147 {
148  return pthread_self();
149 }
150 
151 inline int althrd_equal(althrd_t thr0, althrd_t thr1)
152 {
153  return pthread_equal(thr0, thr1);
154 }
155 
156 inline void althrd_exit(int res)
157 {
158  pthread_exit((void*)(intptr_t)res);
159 }
160 
161 inline void althrd_yield(void)
162 {
163  sched_yield();
164 }
165 
166 inline int althrd_sleep(const struct timespec *ts, struct timespec *rem)
167 {
168  int ret = nanosleep(ts, rem);
169  if(ret != 0)
170  {
171  ret = ((errno==EINTR) ? -1 : -2);
172  errno = 0;
173  }
174  return ret;
175 }
176 
177 
178 inline int almtx_lock(almtx_t *mtx)
179 {
180  if(pthread_mutex_lock(mtx) != 0)
181  return althrd_error;
182  return althrd_success;
183 }
184 
185 inline int almtx_unlock(almtx_t *mtx)
186 {
187  if(pthread_mutex_unlock(mtx) != 0)
188  return althrd_error;
189  return althrd_success;
190 }
191 
192 inline int almtx_trylock(almtx_t *mtx)
193 {
194  int ret = pthread_mutex_trylock(mtx);
195  switch(ret)
196  {
197  case 0: return althrd_success;
198  case EBUSY: return althrd_busy;
199  }
200  return althrd_error;
201 }
202 
203 
204 inline void *altss_get(altss_t tss_id)
205 {
206  return pthread_getspecific(tss_id);
207 }
208 
209 inline int altss_set(altss_t tss_id, void *val)
210 {
211  if(pthread_setspecific(tss_id, val) != 0)
212  return althrd_error;
213  return althrd_success;
214 }
215 
216 
217 inline void alcall_once(alonce_flag *once, void (*callback)(void))
218 {
219  pthread_once(once, callback);
220 }
221 
222 
223 inline void althrd_deinit(void) { }
224 inline void althrd_thread_detach(void) { }
225 
226 #endif
227 
228 
229 int althrd_create(althrd_t *thr, althrd_start_t func, void *arg);
230 int althrd_detach(althrd_t thr);
231 int althrd_join(althrd_t thr, int *res);
232 void althrd_setname(althrd_t thr, const char *name);
233 
234 int almtx_init(almtx_t *mtx, int type);
235 void almtx_destroy(almtx_t *mtx);
236 
237 int alcnd_init(alcnd_t *cond);
238 int alcnd_signal(alcnd_t *cond);
239 int alcnd_broadcast(alcnd_t *cond);
240 int alcnd_wait(alcnd_t *cond, almtx_t *mtx);
241 void alcnd_destroy(alcnd_t *cond);
242 
243 int alsem_init(alsem_t *sem, unsigned int initial);
244 void alsem_destroy(alsem_t *sem);
245 int alsem_post(alsem_t *sem);
246 int alsem_wait(alsem_t *sem);
247 int alsem_trywait(alsem_t *sem);
248 
249 int altss_create(altss_t *tss_id, altss_dtor_t callback);
250 void altss_delete(altss_t tss_id);
251 
252 int altimespec_get(struct timespec *ts, int base);
253 
254 void al_nssleep(unsigned long nsec);
255 
256 #ifdef __cplusplus
257 }
258 #endif
259 
260 #endif /* AL_THREADS_H */