pthread.h 12 KB

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