pthread.h 11 KB

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