Mark a global variable declaration as extern
[openal-soft.git] / include / threads.h
bloba11405f78d870f48c79c27d43d9edb7db28c77a0
1 #ifndef AL_THREADS_H
2 #define AL_THREADS_H
4 #include <time.h>
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
10 enum {
11 althrd_success = 0,
12 althrd_error,
13 althrd_nomem,
14 althrd_timedout,
15 althrd_busy
18 enum {
19 almtx_plain = 0,
20 almtx_recursive = 1,
21 almtx_timed = 2
24 typedef int (*althrd_start_t)(void*);
25 typedef void (*altss_dtor_t)(void*);
28 #define AL_TIME_UTC 1
31 #ifdef _WIN32
32 #define WIN32_LEAN_AND_MEAN
33 #include <windows.h>
36 #if !defined(_TIMESPEC_DEFINED) && !(defined(_MSC_VER) && (_MSC_VER >= 1900))
37 #define _TIMESPEC_DEFINED
38 struct timespec {
39 time_t tv_sec;
40 long tv_nsec;
43 struct itimerspec {
44 struct timespec it_interval;
45 struct timespec it_value;
47 #endif
49 typedef DWORD althrd_t;
50 typedef CRITICAL_SECTION almtx_t;
51 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
52 typedef CONDITION_VARIABLE alcnd_t;
53 #else
54 typedef struct { void *Ptr; } alcnd_t;
55 #endif
56 typedef DWORD altss_t;
57 typedef LONG alonce_flag;
59 #define AL_ONCE_FLAG_INIT 0
61 int althrd_sleep(const struct timespec *ts, struct timespec *rem);
62 void alcall_once(alonce_flag *once, void (*callback)(void));
65 inline althrd_t althrd_current(void)
67 return GetCurrentThreadId();
70 inline int althrd_equal(althrd_t thr0, althrd_t thr1)
72 return thr0 == thr1;
75 inline void althrd_exit(int res)
77 ExitThread(res);
80 inline void althrd_yield(void)
82 SwitchToThread();
86 inline int almtx_lock(almtx_t *mtx)
88 if(!mtx) return althrd_error;
89 EnterCriticalSection(mtx);
90 return althrd_success;
93 inline int almtx_unlock(almtx_t *mtx)
95 if(!mtx) return althrd_error;
96 LeaveCriticalSection(mtx);
97 return althrd_success;
100 inline int almtx_trylock(almtx_t *mtx)
102 if(!mtx) return althrd_error;
103 if(!TryEnterCriticalSection(mtx))
104 return althrd_busy;
105 return althrd_success;
109 inline void *altss_get(altss_t tss_id)
111 return TlsGetValue(tss_id);
114 inline int altss_set(altss_t tss_id, void *val)
116 if(TlsSetValue(tss_id, val) == 0)
117 return althrd_error;
118 return althrd_success;
121 #else
123 #include <stdint.h>
124 #include <errno.h>
125 #include <pthread.h>
128 typedef pthread_t althrd_t;
129 typedef pthread_mutex_t almtx_t;
130 typedef pthread_cond_t alcnd_t;
131 typedef pthread_key_t altss_t;
132 typedef pthread_once_t alonce_flag;
134 #define AL_ONCE_FLAG_INIT PTHREAD_ONCE_INIT
137 inline althrd_t althrd_current(void)
139 return pthread_self();
142 inline int althrd_equal(althrd_t thr0, althrd_t thr1)
144 return pthread_equal(thr0, thr1);
147 inline void althrd_exit(int res)
149 pthread_exit((void*)(intptr_t)res);
152 inline void althrd_yield(void)
154 sched_yield();
157 inline int althrd_sleep(const struct timespec *ts, struct timespec *rem)
159 int ret = nanosleep(ts, rem);
160 if(ret != 0)
162 ret = ((errno==EINTR) ? -1 : -2);
163 errno = 0;
165 return ret;
169 inline int almtx_lock(almtx_t *mtx)
171 if(pthread_mutex_lock(mtx) != 0)
172 return althrd_error;
173 return althrd_success;
176 inline int almtx_unlock(almtx_t *mtx)
178 if(pthread_mutex_unlock(mtx) != 0)
179 return althrd_error;
180 return althrd_success;
183 inline int almtx_trylock(almtx_t *mtx)
185 int ret = pthread_mutex_trylock(mtx);
186 switch(ret)
188 case 0: return althrd_success;
189 case EBUSY: return althrd_busy;
191 return althrd_error;
195 inline void *altss_get(altss_t tss_id)
197 return pthread_getspecific(tss_id);
200 inline int altss_set(altss_t tss_id, void *val)
202 if(pthread_setspecific(tss_id, val) != 0)
203 return althrd_error;
204 return althrd_success;
208 inline void alcall_once(alonce_flag *once, void (*callback)(void))
210 pthread_once(once, callback);
213 #endif
216 int althrd_create(althrd_t *thr, althrd_start_t func, void *arg);
217 int althrd_detach(althrd_t thr);
218 int althrd_join(althrd_t thr, int *res);
219 void althrd_setname(althrd_t thr, const char *name);
221 int almtx_init(almtx_t *mtx, int type);
222 void almtx_destroy(almtx_t *mtx);
223 int almtx_timedlock(almtx_t *mtx, const struct timespec *ts);
225 int alcnd_init(alcnd_t *cond);
226 int alcnd_signal(alcnd_t *cond);
227 int alcnd_broadcast(alcnd_t *cond);
228 int alcnd_wait(alcnd_t *cond, almtx_t *mtx);
229 int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point);
230 void alcnd_destroy(alcnd_t *cond);
232 int altss_create(altss_t *tss_id, altss_dtor_t callback);
233 void altss_delete(altss_t tss_id);
235 int altimespec_get(struct timespec *ts, int base);
237 void al_nssleep(unsigned long nsec);
239 #ifdef __cplusplus
241 #endif
243 #endif /* AL_THREADS_H */