pthread.h 11 KB

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