pthread.h 11 KB

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