threads.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef AL_THREADS_H
  2. #define AL_THREADS_H
  3. #include <time.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. enum {
  8. althrd_success = 0,
  9. althrd_error,
  10. althrd_nomem,
  11. althrd_timedout,
  12. althrd_busy
  13. };
  14. enum {
  15. almtx_plain = 0,
  16. almtx_recursive = 1,
  17. almtx_timed = 2
  18. };
  19. typedef int (*althrd_start_t)(void*);
  20. typedef void (*altss_dtor_t)(void*);
  21. #define AL_TIME_UTC 1
  22. #ifdef _WIN32
  23. #define WIN32_LEAN_AND_MEAN
  24. #include <windows.h>
  25. #ifndef HAVE_STRUCT_TIMESPEC
  26. struct timespec {
  27. time_t tv_sec;
  28. long tv_nsec;
  29. };
  30. #endif
  31. typedef DWORD althrd_t;
  32. typedef CRITICAL_SECTION almtx_t;
  33. #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
  34. typedef CONDITION_VARIABLE alcnd_t;
  35. #else
  36. typedef struct { void *Ptr; } alcnd_t;
  37. #endif
  38. typedef DWORD altss_t;
  39. typedef LONG alonce_flag;
  40. #define AL_ONCE_FLAG_INIT 0
  41. int althrd_sleep(const struct timespec *ts, struct timespec *rem);
  42. void alcall_once(alonce_flag *once, void (*callback)(void));
  43. inline althrd_t althrd_current(void)
  44. {
  45. return GetCurrentThreadId();
  46. }
  47. inline int althrd_equal(althrd_t thr0, althrd_t thr1)
  48. {
  49. return thr0 == thr1;
  50. }
  51. inline void althrd_exit(int res)
  52. {
  53. ExitThread(res);
  54. }
  55. inline void althrd_yield(void)
  56. {
  57. SwitchToThread();
  58. }
  59. inline int almtx_lock(almtx_t *mtx)
  60. {
  61. if(!mtx) return althrd_error;
  62. EnterCriticalSection(mtx);
  63. return althrd_success;
  64. }
  65. inline int almtx_unlock(almtx_t *mtx)
  66. {
  67. if(!mtx) return althrd_error;
  68. LeaveCriticalSection(mtx);
  69. return althrd_success;
  70. }
  71. inline int almtx_trylock(almtx_t *mtx)
  72. {
  73. if(!mtx) return althrd_error;
  74. if(!TryEnterCriticalSection(mtx))
  75. return althrd_busy;
  76. return althrd_success;
  77. }
  78. inline void *altss_get(altss_t tss_id)
  79. {
  80. return TlsGetValue(tss_id);
  81. }
  82. inline int altss_set(altss_t tss_id, void *val)
  83. {
  84. if(TlsSetValue(tss_id, val) == 0)
  85. return althrd_error;
  86. return althrd_success;
  87. }
  88. #else
  89. #include <stdint.h>
  90. #include <errno.h>
  91. #include <pthread.h>
  92. typedef pthread_t althrd_t;
  93. typedef pthread_mutex_t almtx_t;
  94. typedef pthread_cond_t alcnd_t;
  95. typedef pthread_key_t altss_t;
  96. typedef pthread_once_t alonce_flag;
  97. #define AL_ONCE_FLAG_INIT PTHREAD_ONCE_INIT
  98. inline althrd_t althrd_current(void)
  99. {
  100. return pthread_self();
  101. }
  102. inline int althrd_equal(althrd_t thr0, althrd_t thr1)
  103. {
  104. return pthread_equal(thr0, thr1);
  105. }
  106. inline void althrd_exit(int res)
  107. {
  108. pthread_exit((void*)(intptr_t)res);
  109. }
  110. inline void althrd_yield(void)
  111. {
  112. sched_yield();
  113. }
  114. inline int althrd_sleep(const struct timespec *ts, struct timespec *rem)
  115. {
  116. int ret = nanosleep(ts, rem);
  117. if(ret != 0)
  118. {
  119. ret = ((errno==EINTR) ? -1 : -2);
  120. errno = 0;
  121. }
  122. return ret;
  123. }
  124. inline int almtx_lock(almtx_t *mtx)
  125. {
  126. if(pthread_mutex_lock(mtx) != 0)
  127. return althrd_error;
  128. return althrd_success;
  129. }
  130. inline int almtx_unlock(almtx_t *mtx)
  131. {
  132. if(pthread_mutex_unlock(mtx) != 0)
  133. return althrd_error;
  134. return althrd_success;
  135. }
  136. inline int almtx_trylock(almtx_t *mtx)
  137. {
  138. int ret = pthread_mutex_trylock(mtx);
  139. switch(ret)
  140. {
  141. case 0: return althrd_success;
  142. case EBUSY: return althrd_busy;
  143. }
  144. return althrd_error;
  145. }
  146. inline void *altss_get(altss_t tss_id)
  147. {
  148. return pthread_getspecific(tss_id);
  149. }
  150. inline int altss_set(altss_t tss_id, void *val)
  151. {
  152. if(pthread_setspecific(tss_id, val) != 0)
  153. return althrd_error;
  154. return althrd_success;
  155. }
  156. inline void alcall_once(alonce_flag *once, void (*callback)(void))
  157. {
  158. pthread_once(once, callback);
  159. }
  160. #endif
  161. int althrd_create(althrd_t *thr, althrd_start_t func, void *arg);
  162. int althrd_detach(althrd_t thr);
  163. int althrd_join(althrd_t thr, int *res);
  164. void althrd_setname(althrd_t thr, const char *name);
  165. int almtx_init(almtx_t *mtx, int type);
  166. void almtx_destroy(almtx_t *mtx);
  167. int almtx_timedlock(almtx_t *mtx, const struct timespec *ts);
  168. int alcnd_init(alcnd_t *cond);
  169. int alcnd_signal(alcnd_t *cond);
  170. int alcnd_broadcast(alcnd_t *cond);
  171. int alcnd_wait(alcnd_t *cond, almtx_t *mtx);
  172. int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point);
  173. void alcnd_destroy(alcnd_t *cond);
  174. int altss_create(altss_t *tss_id, altss_dtor_t callback);
  175. void altss_delete(altss_t tss_id);
  176. int altimespec_get(struct timespec *ts, int base);
  177. void al_nssleep(unsigned long nsec);
  178. #ifdef __cplusplus
  179. }
  180. #endif
  181. #endif /* AL_THREADS_H */