thread.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  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. #ifndef __COMMON_THREAD_H__
  18. #define __COMMON_THREAD_H__
  19. #include <starpu.h>
  20. #include <pthread.h>
  21. #ifdef STARPU_SIMGRID
  22. #include <xbt/synchro_core.h>
  23. #include <msg/msg.h>
  24. #endif
  25. #ifdef STARPU_SIMGRID
  26. typedef xbt_mutex_t _starpu_pthread_mutex_t;
  27. #else
  28. typedef pthread_mutex_t _starpu_pthread_mutex_t;
  29. #endif
  30. int _starpu_check_mutex_deadlock(_starpu_pthread_mutex_t *mutex);
  31. struct _starpu_pthread_args {
  32. void *(*f)(void*);
  33. void *arg;
  34. };
  35. int _starpu_simgrid_thread_start(int argc, char *argv[]);
  36. #ifdef STARPU_SIMGRID
  37. #define _STARPU_PTHREAD_CREATE_ON(name, thread, attr, routine, threadarg, where) do {\
  38. struct _starpu_pthread_args *_args = malloc(sizeof(*_args)); \
  39. xbt_dynar_t _hosts; \
  40. _args->f = routine; \
  41. _args->arg = threadarg; \
  42. _hosts = MSG_hosts_as_dynar(); \
  43. MSG_process_create((name), _starpu_simgrid_thread_start, _args, \
  44. xbt_dynar_get_as(_hosts, (where), msg_host_t)); \
  45. xbt_dynar_free(&_hosts); \
  46. } while (0)
  47. #else
  48. #define _STARPU_PTHREAD_CREATE_ON(name, thread, attr, routine, arg, where) do {\
  49. int p_ret = pthread_create((thread), (attr), (routine), (arg)); \
  50. if (STARPU_UNLIKELY(p_ret != 0)) { \
  51. fprintf(stderr, \
  52. "%s:%d pthread_create: %s\n", \
  53. __FILE__, __LINE__, strerror(p_ret)); \
  54. } \
  55. } while (0)
  56. #endif
  57. #define _STARPU_PTHREAD_CREATE(name, thread, attr, routine, arg) \
  58. _STARPU_PTHREAD_CREATE_ON(name, thread, attr, routine, arg, 0)
  59. /*
  60. * Encapsulation of the pthread_key_* functions.
  61. */
  62. #ifdef STARPU_SIMGRID
  63. typedef int _starpu_pthread_key_t;
  64. int _starpu_pthread_key_create(_starpu_pthread_key_t *key);
  65. #define _STARPU_PTHREAD_KEY_CREATE(key, destr) _starpu_pthread_key_create(key)
  66. int _starpu_pthread_key_delete(_starpu_pthread_key_t key);
  67. #define _STARPU_PTHREAD_KEY_DELETE(key) _starpu_pthread_key_delete(key)
  68. int _starpu_pthread_setspecific(_starpu_pthread_key_t key, void *ptr);
  69. #define _STARPU_PTHREAD_SETSPECIFIC(key, ptr) _starpu_pthread_setspecific(key, ptr)
  70. void *_starpu_pthread_getspecific(_starpu_pthread_key_t key);
  71. #define _STARPU_PTHREAD_GETSPECIFIC(key) _starpu_pthread_getspecific(key)
  72. #else
  73. typedef pthread_key_t _starpu_pthread_key_t;
  74. #define _STARPU_PTHREAD_KEY_CREATE(key, destr) do { \
  75. int p_ret = pthread_key_create((key), (destr)); \
  76. if (STARPU_UNLIKELY(p_ret != 0)) { \
  77. fprintf(stderr, \
  78. "%s:%d pthread_key_create: %s\n", \
  79. __FILE__, __LINE__, strerror(p_ret)); \
  80. } \
  81. } while (0)
  82. #define _STARPU_PTHREAD_KEY_DELETE(key) do { \
  83. int p_ret = pthread_key_delete((key)); \
  84. if (STARPU_UNLIKELY(p_ret != 0)) { \
  85. fprintf(stderr, \
  86. "%s:%d pthread_key_delete: %s\n", \
  87. __FILE__, __LINE__, strerror(p_ret)); \
  88. } \
  89. } while (0)
  90. #define _STARPU_PTHREAD_SETSPECIFIC(key, ptr) do { \
  91. int p_ret = pthread_setspecific((key), (ptr)); \
  92. if (STARPU_UNLIKELY(p_ret != 0)) { \
  93. fprintf(stderr, \
  94. "%s:%d pthread_setspecific: %s\n", \
  95. __FILE__, __LINE__, strerror(p_ret)); \
  96. }; \
  97. } while (0)
  98. #define _STARPU_PTHREAD_GETSPECIFIC(key) pthread_getspecific((key))
  99. #endif
  100. /*
  101. * Encapsulation of the pthread_mutex_* functions.
  102. */
  103. #ifdef STARPU_SIMGRID
  104. #define _STARPU_PTHREAD_MUTEX_INITIALIZER NULL
  105. #define _STARPU_PTHREAD_MUTEX_INIT(mutex, attr) do { \
  106. (*mutex) = xbt_mutex_init(); \
  107. } while (0)
  108. #else
  109. #define _STARPU_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  110. #define _STARPU_PTHREAD_MUTEX_INIT(mutex, attr) do { \
  111. int p_ret = pthread_mutex_init((mutex), (attr)); \
  112. if (STARPU_UNLIKELY(p_ret)) { \
  113. fprintf(stderr, \
  114. "%s:%d pthread_mutex_init: %s\n", \
  115. __FILE__, __LINE__, strerror(p_ret)); \
  116. STARPU_ABORT(); \
  117. } \
  118. } while (0)
  119. #endif
  120. #ifdef STARPU_SIMGRID
  121. #define _STARPU_PTHREAD_MUTEX_DESTROY(mutex) do { \
  122. if (*mutex) \
  123. xbt_mutex_destroy((*mutex)); \
  124. } while (0)
  125. #else
  126. #define _STARPU_PTHREAD_MUTEX_DESTROY(mutex) do { \
  127. int p_ret = pthread_mutex_destroy(mutex); \
  128. if (STARPU_UNLIKELY(p_ret)) { \
  129. fprintf(stderr, \
  130. "%s:%d pthread_mutex_destroy: %s\n", \
  131. __FILE__, __LINE__, strerror(p_ret)); \
  132. STARPU_ABORT(); \
  133. } \
  134. } while(0)
  135. #endif
  136. #ifdef STARPU_SIMGRID
  137. #define _STARPU_PTHREAD_MUTEX_LOCK(mutex) do { \
  138. if (!(*mutex)) _STARPU_PTHREAD_MUTEX_INIT((mutex), NULL); \
  139. xbt_mutex_acquire((*mutex)); \
  140. } while (0)
  141. #else
  142. #define _STARPU_PTHREAD_MUTEX_LOCK(mutex) do { \
  143. int p_ret = pthread_mutex_lock(mutex); \
  144. if (STARPU_UNLIKELY(p_ret)) { \
  145. fprintf(stderr, \
  146. "%s:%d pthread_mutex_lock: %s\n", \
  147. __FILE__, __LINE__, strerror(p_ret)); \
  148. STARPU_ABORT(); \
  149. } \
  150. } while (0)
  151. #endif
  152. #ifdef STARPU_SIMGRID
  153. #define _STARPU_PTHREAD_MUTEX_TRYLOCK(mutex) (xbt_mutex_acquire(*mutex), 0)
  154. #else
  155. #define _STARPU_PTHREAD_MUTEX_TRYLOCK(mutex) pthread_mutex_trylock(mutex)
  156. #endif
  157. #ifdef STARPU_SIMGRID
  158. #define _STARPU_PTHREAD_MUTEX_UNLOCK(mutex) do { \
  159. xbt_mutex_release((*mutex)); \
  160. } while (0)
  161. #else
  162. #define _STARPU_PTHREAD_MUTEX_UNLOCK(mutex) do { \
  163. int p_ret = pthread_mutex_unlock(mutex); \
  164. if (STARPU_UNLIKELY(p_ret)) { \
  165. fprintf(stderr, \
  166. "%s:%d pthread_mutex_unlock: %s\n", \
  167. __FILE__, __LINE__, strerror(p_ret)); \
  168. STARPU_ABORT(); \
  169. } \
  170. } while (0)
  171. #endif
  172. #ifdef STARPU_SIMGRID
  173. typedef xbt_mutex_t _starpu_pthread_rwlock_t;
  174. #else
  175. typedef pthread_rwlock_t _starpu_pthread_rwlock_t;
  176. #endif
  177. /*
  178. * Encapsulation of the pthread_rwlock_* functions.
  179. */
  180. #ifdef STARPU_SIMGRID
  181. #define _STARPU_PTHREAD_RWLOCK_INIT(rwlock, attr) _STARPU_PTHREAD_MUTEX_INIT(rwlock, attr)
  182. #else
  183. #define _STARPU_PTHREAD_RWLOCK_INIT(rwlock, attr) do { \
  184. int p_ret = pthread_rwlock_init((rwlock), (attr)); \
  185. if (STARPU_UNLIKELY(p_ret)) { \
  186. fprintf(stderr, \
  187. "%s:%d pthread_rwlock_init: %s\n", \
  188. __FILE__, __LINE__, strerror(p_ret)); \
  189. STARPU_ABORT(); \
  190. } \
  191. } while (0)
  192. #endif
  193. #ifdef STARPU_SIMGRID
  194. #define _STARPU_PTHREAD_RWLOCK_RDLOCK(rwlock) _STARPU_PTHREAD_MUTEX_LOCK(rwlock)
  195. #else
  196. #define _STARPU_PTHREAD_RWLOCK_RDLOCK(rwlock) do { \
  197. int p_ret = pthread_rwlock_rdlock(rwlock); \
  198. if (STARPU_UNLIKELY(p_ret)) { \
  199. fprintf(stderr, \
  200. "%s:%d pthread_rwlock_rdlock: %s\n", \
  201. __FILE__, __LINE__, strerror(p_ret)); \
  202. STARPU_ABORT(); \
  203. } \
  204. } while (0)
  205. #endif
  206. #ifdef STARPU_SIMGRID
  207. #define _STARPU_PTHREAD_RWLOCK_WRLOCK(rwlock) _STARPU_PTHREAD_MUTEX_LOCK(rwlock)
  208. #else
  209. #define _STARPU_PTHREAD_RWLOCK_WRLOCK(rwlock) do { \
  210. int p_ret = pthread_rwlock_wrlock(rwlock); \
  211. if (STARPU_UNLIKELY(p_ret)) { \
  212. fprintf(stderr, \
  213. "%s:%d pthread_rwlock_wrlock: %s\n", \
  214. __FILE__, __LINE__, strerror(p_ret)); \
  215. STARPU_ABORT(); \
  216. } \
  217. } while (0)
  218. #endif
  219. #ifdef STARPU_SIMGRID
  220. #define _STARPU_PTHREAD_RWLOCK_UNLOCK(rwlock) _STARPU_PTHREAD_MUTEX_UNLOCK(rwlock)
  221. #else
  222. #define _STARPU_PTHREAD_RWLOCK_UNLOCK(rwlock) do { \
  223. int p_ret = pthread_rwlock_unlock(rwlock); \
  224. if (STARPU_UNLIKELY(p_ret)) { \
  225. fprintf(stderr, \
  226. "%s:%d pthread_rwlock_unlock: %s\n", \
  227. __FILE__, __LINE__, strerror(p_ret)); \
  228. STARPU_ABORT(); \
  229. } \
  230. } while (0)
  231. #endif
  232. #ifdef STARPU_SIMGRID
  233. #define _STARPU_PTHREAD_RWLOCK_DESTROY(rwlock) _STARPU_PTHREAD_MUTEX_DESTROY(rwlock)
  234. #else
  235. #define _STARPU_PTHREAD_RWLOCK_DESTROY(rwlock) do { \
  236. int p_ret = pthread_rwlock_destroy(rwlock); \
  237. if (STARPU_UNLIKELY(p_ret)) { \
  238. fprintf(stderr, \
  239. "%s:%d pthread_rwlock_destroy: %s\n", \
  240. __FILE__, __LINE__, strerror(p_ret)); \
  241. STARPU_ABORT(); \
  242. } \
  243. } while (0)
  244. #endif
  245. #ifdef STARPU_SIMGRID
  246. typedef xbt_cond_t _starpu_pthread_cond_t;
  247. #else
  248. typedef pthread_cond_t _starpu_pthread_cond_t;
  249. #endif
  250. /*
  251. * Encapsulation of the pthread_cond_* functions.
  252. */
  253. #ifdef STARPU_SIMGRID
  254. #define _STARPU_PTHREAD_COND_INITIALIZER NULL
  255. #define _STARPU_PTHREAD_COND_INIT(cond, attr) do { \
  256. (*cond) = xbt_cond_init(); \
  257. } while (0)
  258. #else
  259. #define _STARPU_PTHREAD_COND_INITIALIZER PTHREAD_COND_INITIALIZER
  260. #define _STARPU_PTHREAD_COND_INIT(cond, attr) do { \
  261. int p_ret = pthread_cond_init((cond), (attr)); \
  262. if (STARPU_UNLIKELY(p_ret)) { \
  263. fprintf(stderr, \
  264. "%s:%d pthread_cond_init: %s\n", \
  265. __FILE__, __LINE__, strerror(p_ret)); \
  266. STARPU_ABORT(); \
  267. } \
  268. } while (0)
  269. #endif
  270. #ifdef STARPU_SIMGRID
  271. #define _STARPU_PTHREAD_COND_DESTROY(cond) do { \
  272. if (*cond) \
  273. xbt_cond_destroy((*cond)); \
  274. } while (0)
  275. #else
  276. #define _STARPU_PTHREAD_COND_DESTROY(cond) do { \
  277. int p_ret = pthread_cond_destroy(cond); \
  278. if (STARPU_UNLIKELY(p_ret)) { \
  279. fprintf(stderr, \
  280. "%s:%d pthread_cond_destroy: %s\n", \
  281. __FILE__, __LINE__, strerror(p_ret)); \
  282. STARPU_ABORT(); \
  283. } \
  284. } while (0)
  285. #endif
  286. #ifdef STARPU_SIMGRID
  287. #define _STARPU_PTHREAD_COND_SIGNAL(cond) do { \
  288. if (!*cond) \
  289. _STARPU_PTHREAD_COND_INIT(cond, NULL); \
  290. xbt_cond_signal((*cond)); \
  291. } while (0)
  292. #else
  293. #define _STARPU_PTHREAD_COND_SIGNAL(cond) do { \
  294. int p_ret = pthread_cond_signal(cond); \
  295. if (STARPU_UNLIKELY(p_ret)) { \
  296. fprintf(stderr, \
  297. "%s:%d pthread_cond_signal: %s\n", \
  298. __FILE__, __LINE__, strerror(p_ret)); \
  299. STARPU_ABORT(); \
  300. } \
  301. } while (0)
  302. #endif
  303. #ifdef STARPU_SIMGRID
  304. #define _STARPU_PTHREAD_COND_BROADCAST(cond) do { \
  305. if (!*cond) \
  306. _STARPU_PTHREAD_COND_INIT(cond, NULL); \
  307. xbt_cond_broadcast((*cond)); \
  308. } while (0)
  309. #else
  310. #define _STARPU_PTHREAD_COND_BROADCAST(cond) do { \
  311. int p_ret = pthread_cond_broadcast(cond); \
  312. if (STARPU_UNLIKELY(p_ret)) { \
  313. fprintf(stderr, \
  314. "%s:%d pthread_cond_broadcast: %s\n", \
  315. __FILE__, __LINE__, strerror(p_ret)); \
  316. STARPU_ABORT(); \
  317. } \
  318. } while (0)
  319. #endif
  320. #ifdef STARPU_SIMGRID
  321. #define _STARPU_PTHREAD_COND_WAIT(cond, mutex) do { \
  322. if (!*cond) \
  323. _STARPU_PTHREAD_COND_INIT(cond, NULL); \
  324. xbt_cond_wait((*cond), (*mutex)); \
  325. } while (0)
  326. #else
  327. #define _STARPU_PTHREAD_COND_WAIT(cond, mutex) do { \
  328. int p_ret = pthread_cond_wait((cond), (mutex)); \
  329. if (STARPU_UNLIKELY(p_ret)) { \
  330. fprintf(stderr, \
  331. "%s:%d pthread_cond_wait: %s\n", \
  332. __FILE__, __LINE__, strerror(p_ret)); \
  333. STARPU_ABORT(); \
  334. } \
  335. } while (0)
  336. #endif
  337. #define _starpu_pthread_barrier_t pthread_barrier_t
  338. /*
  339. * Encapsulation of the pthread_barrier_* functions.
  340. */
  341. #define _STARPU_PTHREAD_BARRIER_INIT(barrier, attr, count) do { \
  342. int p_ret = pthread_barrier_init((barrier), (attr), (count)); \
  343. if (STARPU_UNLIKELY(p_ret)) { \
  344. fprintf(stderr, \
  345. "%s:%d pthread_barrier_init: %s\n", \
  346. __FILE__, __LINE__, strerror(p_ret)); \
  347. STARPU_ABORT(); \
  348. } \
  349. } while (0)
  350. #define _STARPU_PTHREAD_BARRIER_DESTROY(barrier) do { \
  351. int p_ret = pthread_barrier_destroy((barrier)); \
  352. if (STARPU_UNLIKELY(p_ret)) { \
  353. fprintf(stderr, \
  354. "%s:%d pthread_barrier_destroy: %s\n", \
  355. __FILE__, __LINE__, strerror(p_ret)); \
  356. STARPU_ABORT(); \
  357. } \
  358. } while (0)
  359. #define _STARPU_PTHREAD_BARRIER_WAIT(barrier) do { \
  360. int p_ret = pthread_barrier_wait(barrier); \
  361. if (STARPU_UNLIKELY(!((p_ret == 0) || (p_ret == PTHREAD_BARRIER_SERIAL_THREAD)))) { \
  362. fprintf(stderr, \
  363. "%s:%d pthread_barrier_wait: %s\n", \
  364. __FILE__, __LINE__, strerror(p_ret)); \
  365. STARPU_ABORT(); \
  366. } \
  367. } while (0)
  368. #ifdef HAVE_PTHREAD_SPIN_LOCK
  369. typedef pthread_spinlock_t _starpu_pthread_spinlock_t;
  370. #endif
  371. #endif /* __COMMON_THREAD_H__ */