pthread.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /* This is a minimal pthread implementation based on windows functions.
  17. * It is *not* intended to be complete - just complete enough to get
  18. * StarPU running.
  19. */
  20. #ifndef __STARPU_PTHREAD_H__
  21. #define __STARPU_PTHREAD_H__
  22. /* TODO:
  23. * pthread_rwlock_*
  24. * pthread_mutex_trylock
  25. */
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif /* __cplusplus */
  29. #include <windows.h>
  30. #undef interface
  31. #include <stdio.h>
  32. #include <errno.h>
  33. #ifdef __CYGWIN32__
  34. #include <sys/cygwin.h>
  35. #define setSystemErrno() errno = cygwin_internal(CW_GET_ERRNO_FROM_WINERROR, (GetLastError())
  36. #else
  37. #define setSystemErrno() errno = EIO
  38. #endif
  39. #define winPthreadAssertWindows(expr) do { if (!(expr)) { setSystemErrno(); return -1; } } while (0)
  40. #define winPthreadAssert(expr) do { if (!(expr)) return -1; } while (0)
  41. /***********
  42. * threads *
  43. ***********/
  44. typedef DWORD pthread_attr_t;
  45. typedef HANDLE pthread_t;
  46. static inline pthread_t pthread_self(void) {
  47. return GetCurrentThread();
  48. }
  49. static inline int pthread_attr_init (pthread_attr_t *attr) {
  50. *attr = 0;
  51. return 0;
  52. }
  53. #define PTHREAD_CREATE_DETACHED 1
  54. static inline int pthread_attr_setdetachstate (pthread_attr_t *attr, int yes) {
  55. /* not supported, ignore */
  56. return 0;
  57. }
  58. static inline int pthread_attr_setstacksize (pthread_attr_t *attr, size_t stacksize) {
  59. /* not supported, ignore */
  60. return 0;
  61. }
  62. static inline int pthread_attr_destroy (pthread_attr_t *attr) {
  63. return 0;
  64. }
  65. /* "real" cleanup handling not yet implemented */
  66. typedef struct {
  67. void (*routine) (void *);
  68. void *arg;
  69. } __pthread_cleanup_handler;
  70. void pthread_cleanup_push (void (*routine) (void *), void *arg);
  71. #define pthread_cleanup_push(routine, arg) do { \
  72. __pthread_cleanup_handler __cleanup_handler = {routine, arg};
  73. void pthread_cleanup_pop (int execute);
  74. #define pthread_cleanup_pop(execute) \
  75. if (execute) __cleanup_handler.routine(__cleanup_handler.arg); \
  76. } while (0);
  77. static inline int pthread_create (
  78. pthread_t *thread, const pthread_attr_t *attr,
  79. void * (*fun) (void *), void *arg
  80. ) {
  81. if (attr && *attr) {
  82. errno = EINVAL;
  83. return -1;
  84. }
  85. winPthreadAssertWindows(*thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) fun, arg, 0, NULL));
  86. return 0;
  87. }
  88. static inline int pthread_setcancelstate (int state, int *oldstate) {
  89. /* not yet implemented :( */
  90. return 0;
  91. }
  92. static inline int pthread_cancel (pthread_t thread) {
  93. /* This is quite harsh :( */
  94. winPthreadAssertWindows(TerminateThread(thread, 0));
  95. return 0;
  96. }
  97. static inline void pthread_exit (void *res) {
  98. ExitThread((DWORD) res);
  99. }
  100. static inline int pthread_join (pthread_t thread, void **res) {
  101. DWORD _res;
  102. while (1) {
  103. if (GetExitCodeThread(thread, &_res)) {
  104. if (res) *res = (void *)_res;
  105. return 0;
  106. }
  107. winPthreadAssertWindows(GetLastError() == STILL_ACTIVE);
  108. Sleep(1);
  109. }
  110. }
  111. /***********
  112. * mutexes *
  113. ***********/
  114. #define PTHREAD_MUTEX_INITIALIZER NULL
  115. #define PTHREAD_RWLOCK_INITIALIZER NULL
  116. typedef HANDLE pthread_mutex_t;
  117. #define PTHREAD_MUTEX_RECURSIVE 1
  118. typedef int pthread_mutexattr_t;
  119. static inline int pthread_mutexattr_init(pthread_mutexattr_t *attr) {
  120. *attr = PTHREAD_MUTEX_RECURSIVE;
  121. return 0;
  122. }
  123. static inline int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) {
  124. if (type != PTHREAD_MUTEX_RECURSIVE)
  125. return EINVAL;
  126. *attr = type;
  127. return 0;
  128. }
  129. static inline int pthread_mutex_init (pthread_mutex_t *mutex, pthread_mutexattr_t *attr) {
  130. if (attr && *attr!=PTHREAD_MUTEX_RECURSIVE) {
  131. errno = EINVAL;
  132. return -1;
  133. }
  134. winPthreadAssertWindows(*mutex = CreateMutex(NULL, FALSE, NULL));
  135. return 0;
  136. }
  137. static inline int pthread_mutex_unlock (pthread_mutex_t *mutex) {
  138. winPthreadAssertWindows(ReleaseMutex(*mutex));
  139. return 0;
  140. }
  141. static inline int pthread_mutex_lock (pthread_mutex_t *mutex);
  142. static inline int __pthread_mutex_alloc_concurrently (pthread_mutex_t *mutex) {
  143. HANDLE mutex_init_mutex;
  144. /* Get access to one global named mutex to serialize mutex initialization */
  145. winPthreadAssertWindows((mutex_init_mutex = CreateMutex(NULL, FALSE, "StarPU mutex init")));
  146. winPthreadAssert(!pthread_mutex_lock(&mutex_init_mutex));
  147. /* Now we are the one that can initialize it */
  148. if (!*mutex)
  149. winPthreadAssert(!pthread_mutex_init((pthread_mutex_t *) mutex,NULL));
  150. winPthreadAssert(!pthread_mutex_unlock(&mutex_init_mutex));
  151. winPthreadAssertWindows(CloseHandle(mutex_init_mutex));
  152. return 0;
  153. }
  154. static inline int pthread_mutex_lock (pthread_mutex_t *mutex) {
  155. if (!*mutex)
  156. __pthread_mutex_alloc_concurrently (mutex);
  157. again:
  158. switch (WaitForSingleObject(*mutex, INFINITE)) {
  159. default:
  160. case WAIT_FAILED:
  161. setSystemErrno();
  162. return -1;
  163. case WAIT_ABANDONED:
  164. case WAIT_OBJECT_0:
  165. return 0;
  166. case WAIT_TIMEOUT:
  167. goto again;
  168. }
  169. }
  170. static inline int pthread_mutex_trylock (pthread_mutex_t *mutex) {
  171. if (!*mutex)
  172. __pthread_mutex_alloc_concurrently (mutex);
  173. switch (WaitForSingleObject(*mutex, 0)) {
  174. default:
  175. case WAIT_FAILED:
  176. setSystemErrno();
  177. return errno;
  178. case WAIT_ABANDONED:
  179. case WAIT_OBJECT_0:
  180. return 0;
  181. case WAIT_TIMEOUT:
  182. return EBUSY;
  183. }
  184. }
  185. static inline int pthread_mutex_destroy (pthread_mutex_t *mutex) {
  186. winPthreadAssertWindows(CloseHandle(*mutex));
  187. return 0;
  188. }
  189. /********************************************
  190. * rwlock *
  191. * VERY LAZY, don't even look at it please! *
  192. * Should be fine unoptimized for now. *
  193. * TODO: FIXME, using conds for instance? *
  194. ********************************************/
  195. typedef pthread_mutex_t pthread_rwlock_t;
  196. #define pthread_rwlock_init(lock, attr) pthread_mutex_init(lock, NULL)
  197. #define pthread_rwlock_wrlock(lock) pthread_mutex_lock(lock)
  198. #define pthread_rwlock_rdlock(lock) pthread_mutex_lock(lock)
  199. #define pthread_rwlock_unlock(lock) pthread_mutex_unlock(lock)
  200. /**************
  201. * conditions *
  202. **************/
  203. typedef struct {
  204. HANDLE sem;
  205. volatile unsigned nbwait;
  206. } pthread_cond_t;
  207. #define PTHREAD_COND_INITIALIZER { NULL, 0}
  208. struct timespec {
  209. time_t tv_sec; /* Seconds */
  210. long tv_nsec; /* Nanoseconds */
  211. };
  212. typedef unsigned pthread_condattr_t;
  213. static inline int pthread_cond_init (pthread_cond_t *cond, const pthread_condattr_t *attr) {
  214. if (attr) {
  215. errno = EINVAL;
  216. return -1;
  217. }
  218. winPthreadAssertWindows(cond->sem = CreateSemaphore(NULL, 1, 1, NULL));
  219. cond->nbwait = 0;
  220. return 0;
  221. }
  222. static inline int pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *time) {
  223. cond->nbwait++;
  224. winPthreadAssert(pthread_mutex_unlock(mutex));
  225. again:
  226. switch (WaitForSingleObject(cond->sem, time->tv_sec*1000+time->tv_nsec/1000)) {
  227. default:
  228. case WAIT_FAILED:
  229. setSystemErrno();
  230. return -1;
  231. case WAIT_TIMEOUT:
  232. goto again;
  233. case WAIT_ABANDONED:
  234. case WAIT_OBJECT_0:
  235. break;
  236. }
  237. winPthreadAssert(pthread_mutex_lock(mutex));
  238. cond->nbwait--;
  239. return 0;
  240. }
  241. static inline int pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex) {
  242. cond->nbwait++;
  243. winPthreadAssert(pthread_mutex_unlock(mutex));
  244. again:
  245. switch (WaitForSingleObject(cond->sem, INFINITE)) {
  246. case WAIT_FAILED:
  247. setSystemErrno();
  248. return -1;
  249. case WAIT_TIMEOUT:
  250. goto again;
  251. case WAIT_ABANDONED:
  252. case WAIT_OBJECT_0:
  253. break;
  254. }
  255. winPthreadAssert(pthread_mutex_lock(mutex));
  256. cond->nbwait--;
  257. return 0;
  258. }
  259. static inline int pthread_cond_signal (pthread_cond_t *cond) {
  260. if (cond->nbwait)
  261. ReleaseSemaphore(cond->sem, 1, NULL);
  262. return 0;
  263. }
  264. static inline int pthread_cond_broadcast (pthread_cond_t *cond) {
  265. ReleaseSemaphore(cond->sem, cond->nbwait, NULL);
  266. return 0;
  267. }
  268. static inline int pthread_cond_destroy (pthread_cond_t *cond) {
  269. winPthreadAssertWindows(CloseHandle(cond->sem));
  270. return 0;
  271. }
  272. /*******
  273. * TLS *
  274. *******/
  275. typedef DWORD pthread_key_t;
  276. #define PTHREAD_ONCE_INIT {PTHREAD_MUTEX_INITIALIZER, 0}
  277. typedef struct {
  278. pthread_mutex_t mutex;
  279. unsigned done;
  280. } pthread_once_t;
  281. static inline int pthread_once (pthread_once_t *once, void (*oncefun)(void)) {
  282. pthread_mutex_lock(&once->mutex);
  283. if (!once->done) {
  284. oncefun();
  285. once->done = 1;
  286. }
  287. pthread_mutex_unlock(&once->mutex);
  288. return 0;
  289. }
  290. static inline int pthread_key_create (pthread_key_t *key, void (*freefun)(void *)) {
  291. DWORD res;
  292. winPthreadAssertWindows((res = TlsAlloc()) != 0xFFFFFFFF);
  293. *key = res;
  294. return 0;
  295. }
  296. static inline int pthread_key_delete (pthread_key_t key) {
  297. winPthreadAssertWindows(TlsFree(key));
  298. return 0;
  299. }
  300. static inline void *pthread_getspecific (pthread_key_t key) {
  301. void * res = TlsGetValue(key);
  302. if (!res)
  303. errno = EIO;
  304. return res;
  305. }
  306. static inline int pthread_setspecific (pthread_key_t key, const void *data) {
  307. winPthreadAssertWindows(TlsSetValue(key, (LPVOID) data));
  308. return 0;
  309. }
  310. #ifdef __cplusplus
  311. }
  312. #endif /* __cplusplus */
  313. #endif /* __STARPU_PTHREAD_H__ */