pthread.h 11 KB

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