The lower value of the gain vector contains the closest target value
[openal-soft.git] / include / threads.h
blob40f523fa2dcf0993c04964682c5150c3a2f16ed7
1 #ifndef AL_THREADS_H
2 #define AL_THREADS_H
4 #include <time.h>
7 enum {
8 althrd_success = 0,
9 althrd_error,
10 althrd_nomem,
11 althrd_timedout,
12 althrd_busy
15 enum {
16 almtx_plain = 0,
17 almtx_recursive = 1,
18 almtx_timed = 2
21 typedef int (*althrd_start_t)(void*);
22 typedef void (*altss_dtor_t)(void*);
25 #define AL_TIME_UTC 1
28 #ifdef _WIN32
29 #define WIN32_LEAN_AND_MEAN
30 #include <windows.h>
33 #ifndef __MINGW32__
34 struct timespec {
35 time_t tv_sec;
36 long tv_nsec;
38 #endif
40 typedef DWORD althrd_t;
41 typedef CRITICAL_SECTION almtx_t;
42 typedef DWORD altss_t;
43 typedef LONG alonce_flag;
45 #define AL_ONCE_FLAG_INIT 0
47 int althrd_sleep(const struct timespec *ts, struct timespec *rem);
48 void alcall_once(alonce_flag *once, void (*callback)(void));
51 inline althrd_t althrd_current(void)
53 return GetCurrentThreadId();
56 inline int althrd_equal(althrd_t thr0, althrd_t thr1)
58 return thr0 == thr1;
61 inline void althrd_exit(int res)
63 ExitThread(res);
66 inline void althrd_yield(void)
68 SwitchToThread();
72 inline int almtx_lock(almtx_t *mtx)
74 if(!mtx) return althrd_error;
75 EnterCriticalSection(mtx);
76 return althrd_success;
79 inline int almtx_unlock(almtx_t *mtx)
81 if(!mtx) return althrd_error;
82 LeaveCriticalSection(mtx);
83 return althrd_success;
86 inline int almtx_trylock(almtx_t *mtx)
88 if(!mtx) return althrd_error;
89 if(!TryEnterCriticalSection(mtx))
90 return althrd_busy;
91 return althrd_success;
95 inline void *altss_get(altss_t tss_id)
97 return TlsGetValue(tss_id);
100 inline int altss_set(altss_t tss_id, void *val)
102 if(TlsSetValue(tss_id, val) == 0)
103 return althrd_error;
104 return althrd_success;
107 #else
109 #include <stdint.h>
110 #include <errno.h>
111 #include <pthread.h>
114 typedef pthread_t althrd_t;
115 typedef pthread_mutex_t almtx_t;
116 typedef pthread_key_t altss_t;
117 typedef pthread_once_t alonce_flag;
119 #define AL_ONCE_FLAG_INIT PTHREAD_ONCE_INIT
122 inline althrd_t althrd_current(void)
124 return pthread_self();
127 inline int althrd_equal(althrd_t thr0, althrd_t thr1)
129 return pthread_equal(thr0, thr1);
132 inline void althrd_exit(int res)
134 pthread_exit((void*)(intptr_t)res);
137 inline void althrd_yield(void)
139 sched_yield();
142 inline int althrd_sleep(const struct timespec *ts, struct timespec *rem)
144 int ret = nanosleep(ts, rem);
145 if(ret != 0)
147 ret = ((errno==EINTR) ? -1 : -2);
148 errno = 0;
150 return ret;
154 inline int almtx_lock(almtx_t *mtx)
156 if(pthread_mutex_lock(mtx) != 0)
157 return althrd_error;
158 return althrd_success;
161 inline int almtx_unlock(almtx_t *mtx)
163 if(pthread_mutex_unlock(mtx) != 0)
164 return althrd_error;
165 return althrd_success;
168 inline int almtx_trylock(almtx_t *mtx)
170 int ret = pthread_mutex_trylock(mtx);
171 switch(ret)
173 case 0: return althrd_success;
174 case EBUSY: return althrd_busy;
176 return althrd_error;
180 inline void *altss_get(altss_t tss_id)
182 return pthread_getspecific(tss_id);
185 inline int altss_set(altss_t tss_id, void *val)
187 if(pthread_setspecific(tss_id, val) != 0)
188 return althrd_error;
189 return althrd_success;
193 inline void alcall_once(alonce_flag *once, void (*callback)(void))
195 pthread_once(once, callback);
198 #endif
201 int althrd_create(althrd_t *thr, althrd_start_t func, void *arg);
202 int althrd_detach(althrd_t thr);
203 int althrd_join(althrd_t thr, int *res);
204 void althrd_setname(althrd_t thr, const char *name);
206 int almtx_init(almtx_t *mtx, int type);
207 void almtx_destroy(almtx_t *mtx);
208 int almtx_timedlock(almtx_t *mtx, const struct timespec *ts);
210 int altss_create(altss_t *tss_id, altss_dtor_t callback);
211 void altss_delete(altss_t tss_id);
213 int altimespec_get(struct timespec *ts, int base);
215 void al_nssleep(time_t sec, long nsec);
217 #endif /* AL_THREADS_H */