pthread.h 12 KB

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