21 typedef int (*althrd_start_t
)(void*);
22 typedef void (*altss_dtor_t
)(void*);
29 #define WIN32_LEAN_AND_MEAN
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
)
61 inline void althrd_exit(int res
)
66 inline void althrd_yield(void)
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
))
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)
104 return althrd_success
;
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)
142 inline int althrd_sleep(const struct timespec
*ts
, struct timespec
*rem
)
144 int ret
= nanosleep(ts
, rem
);
147 ret
= ((errno
==EINTR
) ? -1 : -2);
154 inline int almtx_lock(almtx_t
*mtx
)
156 if(pthread_mutex_lock(mtx
) != 0)
158 return althrd_success
;
161 inline int almtx_unlock(almtx_t
*mtx
)
163 if(pthread_mutex_unlock(mtx
) != 0)
165 return althrd_success
;
168 inline int almtx_trylock(almtx_t
*mtx
)
170 int ret
= pthread_mutex_trylock(mtx
);
173 case 0: return althrd_success
;
174 case EBUSY
: return althrd_busy
;
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)
189 return althrd_success
;
193 inline void alcall_once(alonce_flag
*once
, void (*callback
)(void))
195 pthread_once(once
, callback
);
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 */