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))
33 typedef int (*althrd_start_t
)(void*);
34 typedef void (*altss_dtor_t
)(void*);
41 #define WIN32_LEAN_AND_MEAN
45 #ifndef HAVE_STRUCT_TIMESPEC
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
;
57 typedef struct { void *Ptr
; } alcnd_t
;
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
)
82 inline void althrd_exit(int res
)
87 inline void althrd_yield(void)
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
))
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)
125 return althrd_success
;
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)
166 inline int althrd_sleep(const struct timespec
*ts
, struct timespec
*rem
)
168 int ret
= nanosleep(ts
, rem
);
171 ret
= ((errno
==EINTR
) ? -1 : -2);
178 inline int almtx_lock(almtx_t
*mtx
)
180 if(pthread_mutex_lock(mtx
) != 0)
182 return althrd_success
;
185 inline int almtx_unlock(almtx_t
*mtx
)
187 if(pthread_mutex_unlock(mtx
) != 0)
189 return althrd_success
;
192 inline int almtx_trylock(almtx_t
*mtx
)
194 int ret
= pthread_mutex_trylock(mtx
);
197 case 0: return althrd_success
;
198 case EBUSY
: return althrd_busy
;
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)
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) { }
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
);
260 #endif /* AL_THREADS_H */