pthread.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. */
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif /* __cplusplus */
  28. #include <windows.h>
  29. #undef interface
  30. #include <stdio.h>
  31. #include <errno.h>
  32. #ifdef __CYGWIN32__
  33. #include <sys/cygwin.h>
  34. #define setSystemErrno() errno = cygwin_internal(CW_GET_ERRNO_FROM_WINERROR, (GetLastError())
  35. #else
  36. #if 0
  37. #define setSystemErrno() do { fprintf(stderr,"%s:%d: win %d\n", __FILE__, __LINE__, GetLastError()); errno = EIO; } while (0)
  38. #else
  39. #define setSystemErrno() errno = EIO
  40. #endif
  41. #endif
  42. #define winPthreadAssertWindows(expr) do { if (!(expr)) { setSystemErrno(); return -1; } } while (0)
  43. #if 0
  44. #define winPthreadAssert(expr) do { if (!(expr)) { fprintf(stderr,"%s:%d: %d\n", __FILE__, __LINE__, errno); return -1; } } while (0)
  45. #else
  46. #define winPthreadAssert(expr) do { if (!(expr)) return -1; } while (0)
  47. #endif
  48. /***********
  49. * threads *
  50. ***********/
  51. typedef DWORD pthread_attr_t;
  52. typedef HANDLE pthread_t;
  53. static inline pthread_t pthread_self(void) {
  54. return GetCurrentThread();
  55. }
  56. static inline int pthread_attr_init (pthread_attr_t *attr) {
  57. *attr = 0;
  58. return 0;
  59. }
  60. #define PTHREAD_CREATE_DETACHED 1
  61. static inline int pthread_attr_setdetachstate (pthread_attr_t *attr, int yes) {
  62. /* not supported, ignore */
  63. return 0;
  64. }
  65. static inline int pthread_attr_setstacksize (pthread_attr_t *attr, size_t stacksize) {
  66. /* not supported, ignore */
  67. return 0;
  68. }
  69. static inline int pthread_attr_destroy (pthread_attr_t *attr) {
  70. return 0;
  71. }
  72. /* "real" cleanup handling not yet implemented */
  73. typedef struct {
  74. void (*routine) (void *);
  75. void *arg;
  76. } __pthread_cleanup_handler;
  77. void pthread_cleanup_push (void (*routine) (void *), void *arg);
  78. #define pthread_cleanup_push(routine, arg) do { \
  79. __pthread_cleanup_handler __cleanup_handler = {routine, arg};
  80. void pthread_cleanup_pop (int execute);
  81. #define pthread_cleanup_pop(execute) \
  82. if (execute) __cleanup_handler.routine(__cleanup_handler.arg); \
  83. } while (0);
  84. static inline int pthread_create (
  85. pthread_t *thread, const pthread_attr_t *attr,
  86. void * (*fun) (void *), void *arg
  87. ) {
  88. if (attr && *attr) {
  89. errno = EINVAL;
  90. return -1;
  91. }
  92. winPthreadAssertWindows(*thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) fun, arg, 0, NULL));
  93. return 0;
  94. }
  95. static inline int pthread_setcancelstate (int state, int *oldstate) {
  96. /* not yet implemented :( */
  97. return 0;
  98. }
  99. static inline int pthread_cancel (pthread_t thread) {
  100. /* This is quite harsh :( */
  101. winPthreadAssertWindows(TerminateThread(thread, 0));
  102. return 0;
  103. }
  104. static inline void pthread_exit (void *res) {
  105. ExitThread((DWORD) res);
  106. }
  107. static inline int pthread_join (pthread_t thread, void **res) {
  108. DWORD _res;
  109. while (1) {
  110. if (GetExitCodeThread(thread, &_res)) {
  111. if (res) *res = (void *)_res;
  112. return 0;
  113. }
  114. winPthreadAssertWindows(GetLastError() == STILL_ACTIVE);
  115. Sleep(1);
  116. }
  117. }
  118. /***********
  119. * mutexes *
  120. ***********/
  121. #define PTHREAD_MUTEX_INITIALIZER NULL
  122. #define PTHREAD_RWLOCK_INITIALIZER NULL
  123. typedef HANDLE pthread_mutex_t;
  124. #define PTHREAD_MUTEX_RECURSIVE 1
  125. typedef int pthread_mutexattr_t;
  126. static inline int pthread_mutexattr_init(pthread_mutexattr_t *attr) {
  127. *attr = PTHREAD_MUTEX_RECURSIVE;
  128. return 0;
  129. }
  130. static inline int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) {
  131. if (type != PTHREAD_MUTEX_RECURSIVE)
  132. return EINVAL;
  133. *attr = type;
  134. return 0;
  135. }
  136. static inline int pthread_mutex_init (pthread_mutex_t *mutex, pthread_mutexattr_t *attr) {
  137. if (attr && *attr!=PTHREAD_MUTEX_RECURSIVE) {
  138. errno = EINVAL;
  139. return -1;
  140. }
  141. winPthreadAssertWindows(*mutex = CreateMutex(NULL, FALSE, NULL));
  142. return 0;
  143. }
  144. static inline int pthread_mutex_unlock (pthread_mutex_t *mutex) {
  145. winPthreadAssertWindows(ReleaseMutex(*mutex));
  146. return 0;
  147. }
  148. static inline int pthread_mutex_lock (pthread_mutex_t *mutex);
  149. static inline int __pthread_mutex_alloc_concurrently (pthread_mutex_t *mutex) {
  150. HANDLE mutex_init_mutex;
  151. /* Get access to one global named mutex to serialize mutex initialization */
  152. winPthreadAssertWindows((mutex_init_mutex = CreateMutex(NULL, FALSE, "StarPU mutex init")));
  153. winPthreadAssert(!pthread_mutex_lock(&mutex_init_mutex));
  154. /* Now we are the one that can initialize it */
  155. if (!*mutex)
  156. winPthreadAssert(!pthread_mutex_init(mutex,NULL));
  157. winPthreadAssert(!pthread_mutex_unlock(&mutex_init_mutex));
  158. winPthreadAssertWindows(CloseHandle(mutex_init_mutex));
  159. return 0;
  160. }
  161. static inline int pthread_mutex_lock (pthread_mutex_t *mutex) {
  162. if (!*mutex)
  163. __pthread_mutex_alloc_concurrently (mutex);
  164. again:
  165. switch (WaitForSingleObject(*mutex, INFINITE)) {
  166. default:
  167. case WAIT_FAILED:
  168. setSystemErrno();
  169. return -1;
  170. case WAIT_ABANDONED:
  171. case WAIT_OBJECT_0:
  172. return 0;
  173. case WAIT_TIMEOUT:
  174. goto again;
  175. }
  176. }
  177. static inline int pthread_mutex_trylock (pthread_mutex_t *mutex) {
  178. if (!*mutex)
  179. __pthread_mutex_alloc_concurrently (mutex);
  180. switch (WaitForSingleObject(*mutex, 0)) {
  181. default:
  182. case WAIT_FAILED:
  183. setSystemErrno();
  184. return errno;
  185. case WAIT_ABANDONED:
  186. case WAIT_OBJECT_0:
  187. return 0;
  188. case WAIT_TIMEOUT:
  189. return EBUSY;
  190. }
  191. }
  192. static inline int pthread_mutex_destroy (pthread_mutex_t *mutex) {
  193. winPthreadAssertWindows(CloseHandle(*mutex));
  194. *mutex = INVALID_HANDLE_VALUE;
  195. return 0;
  196. }
  197. /********************************************
  198. * rwlock *
  199. * VERY LAZY, don't even look at it please! *
  200. * Should be fine unoptimized for now. *
  201. * TODO: FIXME, using conds for instance? *
  202. ********************************************/
  203. typedef pthread_mutex_t pthread_rwlock_t;
  204. #define pthread_rwlock_init(lock, attr) pthread_mutex_init(lock, NULL)
  205. #define pthread_rwlock_wrlock(lock) pthread_mutex_lock(lock)
  206. #define pthread_rwlock_rdlock(lock) pthread_mutex_lock(lock)
  207. #define pthread_rwlock_unlock(lock) pthread_mutex_unlock(lock)
  208. /**************
  209. * conditions *
  210. **************/
  211. typedef struct {
  212. HANDLE sem;
  213. volatile unsigned nbwait;
  214. } pthread_cond_t;
  215. #define PTHREAD_COND_INITIALIZER { NULL, 0}
  216. struct timespec {
  217. time_t tv_sec; /* Seconds */
  218. long tv_nsec; /* Nanoseconds */
  219. };
  220. typedef unsigned pthread_condattr_t;
  221. static inline int pthread_cond_init (pthread_cond_t *cond, const pthread_condattr_t *attr) {
  222. if (attr) {
  223. errno = EINVAL;
  224. return -1;
  225. }
  226. winPthreadAssertWindows(cond->sem = CreateSemaphore(NULL, 0, MAXLONG, NULL));
  227. cond->nbwait = 0;
  228. return 0;
  229. }
  230. static inline int pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *time) {
  231. if (!cond->sem)
  232. winPthreadAssert(!pthread_cond_init(cond,NULL));
  233. cond->nbwait++;
  234. winPthreadAssert(!pthread_mutex_unlock(mutex));
  235. again:
  236. switch (WaitForSingleObject(cond->sem, time->tv_sec*1000+time->tv_nsec/1000)) {
  237. default:
  238. case WAIT_FAILED:
  239. setSystemErrno();
  240. winPthreadAssert(!pthread_mutex_lock(mutex));
  241. return -1;
  242. case WAIT_TIMEOUT:
  243. goto again;
  244. case WAIT_ABANDONED:
  245. case WAIT_OBJECT_0:
  246. break;
  247. }
  248. winPthreadAssert(!pthread_mutex_lock(mutex));
  249. cond->nbwait--;
  250. return 0;
  251. }
  252. static inline int pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex) {
  253. if (!cond->sem)
  254. winPthreadAssert(!pthread_cond_init(cond,NULL));
  255. cond->nbwait++;
  256. winPthreadAssert(!pthread_mutex_unlock(mutex));
  257. again:
  258. switch (WaitForSingleObject(cond->sem, INFINITE)) {
  259. case WAIT_FAILED:
  260. setSystemErrno();
  261. winPthreadAssert(!pthread_mutex_lock(mutex));
  262. return -1;
  263. case WAIT_TIMEOUT:
  264. goto again;
  265. case WAIT_ABANDONED:
  266. case WAIT_OBJECT_0:
  267. break;
  268. }
  269. winPthreadAssert(!pthread_mutex_lock(mutex));
  270. cond->nbwait--;
  271. return 0;
  272. }
  273. static inline int pthread_cond_signal (pthread_cond_t *cond) {
  274. if (!cond->sem)
  275. winPthreadAssert(!pthread_cond_init(cond,NULL));
  276. if (cond->nbwait)
  277. ReleaseSemaphore(cond->sem, 1, NULL);
  278. return 0;
  279. }
  280. static inline int pthread_cond_broadcast (pthread_cond_t *cond) {
  281. if (!cond->sem)
  282. winPthreadAssert(!pthread_cond_init(cond,NULL));
  283. ReleaseSemaphore(cond->sem, cond->nbwait, NULL);
  284. return 0;
  285. }
  286. static inline int pthread_cond_destroy (pthread_cond_t *cond) {
  287. if (cond->sem) {
  288. winPthreadAssertWindows(CloseHandle(cond->sem));
  289. cond->sem = NULL;
  290. }
  291. return 0;
  292. }
  293. /*******
  294. * TLS *
  295. *******/
  296. typedef DWORD pthread_key_t;
  297. #define PTHREAD_ONCE_INIT {PTHREAD_MUTEX_INITIALIZER, 0}
  298. typedef struct {
  299. pthread_mutex_t mutex;
  300. unsigned done;
  301. } pthread_once_t;
  302. static inline int pthread_once (pthread_once_t *once, void (*oncefun)(void)) {
  303. winPthreadAssert(pthread_mutex_lock(&once->mutex));
  304. if (!once->done) {
  305. oncefun();
  306. once->done = 1;
  307. }
  308. winPthreadAssert(!pthread_mutex_unlock(&once->mutex));
  309. return 0;
  310. }
  311. static inline int pthread_key_create (pthread_key_t *key, void (*freefun)(void *)) {
  312. DWORD res;
  313. winPthreadAssertWindows((res = TlsAlloc()) != 0xFFFFFFFF);
  314. *key = res;
  315. return 0;
  316. }
  317. static inline int pthread_key_delete (pthread_key_t key) {
  318. winPthreadAssertWindows(TlsFree(key));
  319. return 0;
  320. }
  321. static inline void *pthread_getspecific (pthread_key_t key) {
  322. void * res = TlsGetValue(key);
  323. if (!res)
  324. errno = EIO;
  325. return res;
  326. }
  327. static inline int pthread_setspecific (pthread_key_t key, const void *data) {
  328. winPthreadAssertWindows(TlsSetValue(key, (LPVOID) data));
  329. return 0;
  330. }
  331. #ifdef __cplusplus
  332. }
  333. #endif /* __cplusplus */
  334. #endif /* __STARPU_PTHREAD_H__ */