pthread.h 12 KB

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