pthread.h 11 KB

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