24 typedef int (*althrd_start_t
)(void*);
25 typedef void (*altss_dtor_t
)(void*);
32 #define WIN32_LEAN_AND_MEAN
36 #ifndef _TIMESPEC_DEFINED
37 #define _TIMESPEC_DEFINED
44 struct timespec it_interval
;
45 struct timespec it_value
;
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
;
54 typedef struct { void *Ptr
; } alcnd_t
;
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
)
75 inline void althrd_exit(int res
)
80 inline void althrd_yield(void)
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
))
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)
118 return althrd_success
;
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)
157 inline int althrd_sleep(const struct timespec
*ts
, struct timespec
*rem
)
159 int ret
= nanosleep(ts
, rem
);
162 ret
= ((errno
==EINTR
) ? -1 : -2);
169 inline int almtx_lock(almtx_t
*mtx
)
171 if(pthread_mutex_lock(mtx
) != 0)
173 return althrd_success
;
176 inline int almtx_unlock(almtx_t
*mtx
)
178 if(pthread_mutex_unlock(mtx
) != 0)
180 return althrd_success
;
183 inline int almtx_trylock(almtx_t
*mtx
)
185 int ret
= pthread_mutex_trylock(mtx
);
188 case 0: return althrd_success
;
189 case EBUSY
: return althrd_busy
;
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)
204 return althrd_success
;
208 inline void alcall_once(alonce_flag
*once
, void (*callback
)(void))
210 pthread_once(once
, callback
);
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
);
243 #endif /* AL_THREADS_H */