Add a comment about waiting to kill the event thread
[openal-soft.git] / common / threads.h
blobb0bebd8def16e2cdbb7e9f87646a9f41557e88ed
1 #ifndef AL_THREADS_H
2 #define AL_THREADS_H
4 #include <time.h>
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
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
20 enum {
21 althrd_success = 0,
22 althrd_error,
23 althrd_nomem,
24 althrd_timedout,
25 althrd_busy
28 enum {
29 almtx_plain = 0,
30 almtx_recursive = 1,
33 typedef int (*althrd_start_t)(void*);
34 typedef void (*altss_dtor_t)(void*);
37 #define AL_TIME_UTC 1
40 #ifdef _WIN32
41 #define WIN32_LEAN_AND_MEAN
42 #include <windows.h>
45 #ifndef HAVE_STRUCT_TIMESPEC
46 struct timespec {
47 time_t tv_sec;
48 long tv_nsec;
50 #endif
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;
63 #define AL_ONCE_FLAG_INIT 0
65 int althrd_sleep(const struct timespec *ts, struct timespec *rem);
66 void alcall_once(alonce_flag *once, void (*callback)(void));
68 void althrd_deinit(void);
69 void althrd_thread_detach(void);
72 inline althrd_t althrd_current(void)
74 return GetCurrentThreadId();
77 inline int althrd_equal(althrd_t thr0, althrd_t thr1)
79 return thr0 == thr1;
82 inline void althrd_exit(int res)
84 ExitThread(res);
87 inline void althrd_yield(void)
89 SwitchToThread();
93 inline int almtx_lock(almtx_t *mtx)
95 if(!mtx) return althrd_error;
96 EnterCriticalSection(mtx);
97 return althrd_success;
100 inline int almtx_unlock(almtx_t *mtx)
102 if(!mtx) return althrd_error;
103 LeaveCriticalSection(mtx);
104 return althrd_success;
107 inline int almtx_trylock(almtx_t *mtx)
109 if(!mtx) return althrd_error;
110 if(!TryEnterCriticalSection(mtx))
111 return althrd_busy;
112 return althrd_success;
116 inline void *altss_get(altss_t tss_id)
118 return TlsGetValue(tss_id);
121 inline int altss_set(altss_t tss_id, void *val)
123 if(TlsSetValue(tss_id, val) == 0)
124 return althrd_error;
125 return althrd_success;
128 #else
130 #include <stdint.h>
131 #include <errno.h>
132 #include <pthread.h>
133 #include <semaphore.h>
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;
143 #define AL_ONCE_FLAG_INIT PTHREAD_ONCE_INIT
146 inline althrd_t althrd_current(void)
148 return pthread_self();
151 inline int althrd_equal(althrd_t thr0, althrd_t thr1)
153 return pthread_equal(thr0, thr1);
156 inline void althrd_exit(int res)
158 pthread_exit((void*)(intptr_t)res);
161 inline void althrd_yield(void)
163 sched_yield();
166 inline int althrd_sleep(const struct timespec *ts, struct timespec *rem)
168 int ret = nanosleep(ts, rem);
169 if(ret != 0)
171 ret = ((errno==EINTR) ? -1 : -2);
172 errno = 0;
174 return ret;
178 inline int almtx_lock(almtx_t *mtx)
180 if(pthread_mutex_lock(mtx) != 0)
181 return althrd_error;
182 return althrd_success;
185 inline int almtx_unlock(almtx_t *mtx)
187 if(pthread_mutex_unlock(mtx) != 0)
188 return althrd_error;
189 return althrd_success;
192 inline int almtx_trylock(almtx_t *mtx)
194 int ret = pthread_mutex_trylock(mtx);
195 switch(ret)
197 case 0: return althrd_success;
198 case EBUSY: return althrd_busy;
200 return althrd_error;
204 inline void *altss_get(altss_t tss_id)
206 return pthread_getspecific(tss_id);
209 inline int altss_set(altss_t tss_id, void *val)
211 if(pthread_setspecific(tss_id, val) != 0)
212 return althrd_error;
213 return althrd_success;
217 inline void alcall_once(alonce_flag *once, void (*callback)(void))
219 pthread_once(once, callback);
223 inline void althrd_deinit(void) { }
224 inline void althrd_thread_detach(void) { }
226 #endif
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);
234 int almtx_init(almtx_t *mtx, int type);
235 void almtx_destroy(almtx_t *mtx);
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);
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);
249 int altss_create(altss_t *tss_id, altss_dtor_t callback);
250 void altss_delete(altss_t tss_id);
252 int altimespec_get(struct timespec *ts, int base);
254 void al_nssleep(unsigned long nsec);
256 #ifdef __cplusplus
258 #endif
260 #endif /* AL_THREADS_H */