starpu_thread_util.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 __STARPU_THREAD_UTIL_H__
  18. #define __STARPU_THREAD_UTIL_H__
  19. #include <starpu_util.h>
  20. #include <errno.h>
  21. /*
  22. * Encapsulation of the starpu_pthread_create_* functions.
  23. */
  24. #define STARPU_PTHREAD_CREATE_ON(name, thread, attr, routine, arg, where) do { \
  25. int p_ret = starpu_pthread_create_on((name), (thread), (attr), (routine), (arg), (where)); \
  26. if (STARPU_UNLIKELY(p_ret != 0)) { \
  27. fprintf(stderr, \
  28. "%s:%d starpu_pthread_create_on: %s\n", \
  29. __FILE__, __LINE__, strerror(p_ret)); \
  30. STARPU_ABORT(); \
  31. } \
  32. } while (0)
  33. #define STARPU_PTHREAD_CREATE(thread, attr, routine, arg) do { \
  34. int p_ret = starpu_pthread_create((thread), (attr), (routine), (arg)); \
  35. if (STARPU_UNLIKELY(p_ret != 0)) { \
  36. fprintf(stderr, \
  37. "%s:%d starpu_pthread_create: %s\n", \
  38. __FILE__, __LINE__, strerror(p_ret)); \
  39. STARPU_ABORT(); \
  40. } \
  41. } while (0)
  42. /*
  43. * Encapsulation of the starpu_pthread_mutex_* functions.
  44. */
  45. #define STARPU_PTHREAD_MUTEX_INIT(mutex, attr) do { \
  46. int p_ret = starpu_pthread_mutex_init((mutex), (attr)); \
  47. if (STARPU_UNLIKELY(p_ret)) { \
  48. fprintf(stderr, \
  49. "%s:%d starpu_pthread_mutex_init: %s\n", \
  50. __FILE__, __LINE__, strerror(p_ret)); \
  51. STARPU_ABORT(); \
  52. } \
  53. } while (0)
  54. #define STARPU_PTHREAD_MUTEX_DESTROY(mutex) do { \
  55. int p_ret = starpu_pthread_mutex_destroy(mutex); \
  56. if (STARPU_UNLIKELY(p_ret)) { \
  57. fprintf(stderr, \
  58. "%s:%d starpu_pthread_mutex_destroy: %s\n", \
  59. __FILE__, __LINE__, strerror(p_ret)); \
  60. STARPU_ABORT(); \
  61. } \
  62. } while(0)
  63. #define STARPU_PTHREAD_MUTEX_LOCK(mutex) do { \
  64. int p_ret = starpu_pthread_mutex_lock(mutex); \
  65. int workerid = starpu_worker_get_id(); \
  66. if(workerid != -1 && starpu_worker_mutex_is_sched_mutex(workerid, mutex)) \
  67. starpu_worker_set_flag_sched_mutex_locked(workerid, 1); \
  68. if (STARPU_UNLIKELY(p_ret)) { \
  69. fprintf(stderr, \
  70. "%s:%d starpu_pthread_mutex_lock: %s\n", \
  71. __FILE__, __LINE__, strerror(p_ret)); \
  72. STARPU_ABORT(); \
  73. } \
  74. } while (0)
  75. #define STARPU_PTHREAD_MUTEX_TRYLOCK(mutex) \
  76. _STARPU_PTHREAD_MUTEX_TRYLOCK(mutex, __FILE__, __LINE__)
  77. static STARPU_INLINE
  78. int _STARPU_PTHREAD_MUTEX_TRYLOCK(starpu_pthread_mutex_t *mutex, char *file, int line)
  79. {
  80. int p_ret = starpu_pthread_mutex_trylock(mutex);
  81. if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
  82. fprintf(stderr,
  83. "%s:%d starpu_pthread_mutex_trylock: %s\n",
  84. file, line, strerror(p_ret));
  85. STARPU_ABORT();
  86. }
  87. return p_ret;
  88. }
  89. #define STARPU_PTHREAD_MUTEX_UNLOCK(mutex) do { \
  90. int p_ret = starpu_pthread_mutex_unlock(mutex); \
  91. int workerid = starpu_worker_get_id(); \
  92. if(workerid != -1 && starpu_worker_mutex_is_sched_mutex(workerid, mutex)) \
  93. starpu_worker_set_flag_sched_mutex_locked(workerid, 0); \
  94. if (STARPU_UNLIKELY(p_ret)) { \
  95. fprintf(stderr, \
  96. "%s:%d starpu_pthread_mutex_unlock: %s\n", \
  97. __FILE__, __LINE__, strerror(p_ret)); \
  98. STARPU_ABORT(); \
  99. } \
  100. } while (0)
  101. /*
  102. * Encapsulation of the starpu_pthread_key_* functions.
  103. */
  104. #define STARPU_PTHREAD_KEY_CREATE(key, destr) do { \
  105. int p_ret = starpu_pthread_key_create((key), (destr)); \
  106. if (STARPU_UNLIKELY(p_ret != 0)) { \
  107. fprintf(stderr, \
  108. "%s:%d starpu_pthread_key_create: %s\n", \
  109. __FILE__, __LINE__, strerror(p_ret)); \
  110. } \
  111. } while (0)
  112. #define STARPU_PTHREAD_KEY_DELETE(key) do { \
  113. int p_ret = starpu_pthread_key_delete((key)); \
  114. if (STARPU_UNLIKELY(p_ret != 0)) { \
  115. fprintf(stderr, \
  116. "%s:%d starpu_pthread_key_delete: %s\n", \
  117. __FILE__, __LINE__, strerror(p_ret)); \
  118. } \
  119. } while (0)
  120. #define STARPU_PTHREAD_SETSPECIFIC(key, ptr) do { \
  121. int p_ret = starpu_pthread_setspecific((key), (ptr)); \
  122. if (STARPU_UNLIKELY(p_ret != 0)) { \
  123. fprintf(stderr, \
  124. "%s:%d starpu_pthread_setspecific: %s\n", \
  125. __FILE__, __LINE__, strerror(p_ret)); \
  126. }; \
  127. } while (0)
  128. #define STARPU_PTHREAD_GETSPECIFIC(key) starpu_pthread_getspecific((key))
  129. /*
  130. * Encapsulation of the starpu_pthread_rwlock_* functions.
  131. */
  132. #define STARPU_PTHREAD_RWLOCK_INIT(rwlock, attr) do { \
  133. int p_ret = starpu_pthread_rwlock_init((rwlock), (attr)); \
  134. if (STARPU_UNLIKELY(p_ret)) { \
  135. fprintf(stderr, \
  136. "%s:%d starpu_pthread_rwlock_init: %s\n", \
  137. __FILE__, __LINE__, strerror(p_ret)); \
  138. STARPU_ABORT(); \
  139. } \
  140. } while (0)
  141. #define STARPU_PTHREAD_RWLOCK_RDLOCK(rwlock) do { \
  142. int p_ret = starpu_pthread_rwlock_rdlock(rwlock); \
  143. if (STARPU_UNLIKELY(p_ret)) { \
  144. fprintf(stderr, \
  145. "%s:%d starpu_pthread_rwlock_rdlock: %s\n", \
  146. __FILE__, __LINE__, strerror(p_ret)); \
  147. STARPU_ABORT(); \
  148. } \
  149. } while (0)
  150. #define STARPU_PTHREAD_RWLOCK_TRYRDLOCK(rwlock) \
  151. _starpu_pthread_rwlock_tryrdlock(rwlock, __FILE__, __LINE__)
  152. static STARPU_INLINE
  153. int _starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock, char *file, int line)
  154. {
  155. int p_ret = starpu_pthread_rwlock_tryrdlock(rwlock);
  156. if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
  157. fprintf(stderr,
  158. "%s:%d starpu_pthread_rwlock_tryrdlock: %s\n",
  159. file, line, strerror(p_ret));
  160. STARPU_ABORT();
  161. }
  162. return p_ret;
  163. }
  164. #define STARPU_PTHREAD_RWLOCK_WRLOCK(rwlock) do { \
  165. int p_ret = starpu_pthread_rwlock_wrlock(rwlock); \
  166. if (STARPU_UNLIKELY(p_ret)) { \
  167. fprintf(stderr, \
  168. "%s:%d starpu_pthread_rwlock_wrlock: %s\n", \
  169. __FILE__, __LINE__, strerror(p_ret)); \
  170. STARPU_ABORT(); \
  171. } \
  172. } while (0)
  173. #define STARPU_PTHREAD_RWLOCK_TRYWRLOCK(rwlock) \
  174. _starpu_pthread_rwlock_trywrlock(rwlock, __FILE__, __LINE__)
  175. static STARPU_INLINE
  176. int _starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock, char *file, int line)
  177. {
  178. int p_ret = starpu_pthread_rwlock_trywrlock(rwlock);
  179. if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
  180. fprintf(stderr,
  181. "%s:%d starpu_pthread_rwlock_trywrlock: %s\n",
  182. file, line, strerror(p_ret));
  183. STARPU_ABORT();
  184. }
  185. return p_ret;
  186. }
  187. #define STARPU_PTHREAD_RWLOCK_UNLOCK(rwlock) do { \
  188. int p_ret = starpu_pthread_rwlock_unlock(rwlock); \
  189. if (STARPU_UNLIKELY(p_ret)) { \
  190. fprintf(stderr, \
  191. "%s:%d starpu_pthread_rwlock_unlock: %s\n", \
  192. __FILE__, __LINE__, strerror(p_ret)); \
  193. STARPU_ABORT(); \
  194. } \
  195. } while (0)
  196. #define STARPU_PTHREAD_RWLOCK_DESTROY(rwlock) do { \
  197. int p_ret = starpu_pthread_rwlock_destroy(rwlock); \
  198. if (STARPU_UNLIKELY(p_ret)) { \
  199. fprintf(stderr, \
  200. "%s:%d starpu_pthread_rwlock_destroy: %s\n", \
  201. __FILE__, __LINE__, strerror(p_ret)); \
  202. STARPU_ABORT(); \
  203. } \
  204. } while (0)
  205. /*
  206. * Encapsulation of the starpu_pthread_cond_* functions.
  207. */
  208. #define STARPU_PTHREAD_COND_INIT(cond, attr) do { \
  209. int p_ret = starpu_pthread_cond_init((cond), (attr)); \
  210. if (STARPU_UNLIKELY(p_ret)) { \
  211. fprintf(stderr, \
  212. "%s:%d starpu_pthread_cond_init: %s\n", \
  213. __FILE__, __LINE__, strerror(p_ret)); \
  214. STARPU_ABORT(); \
  215. } \
  216. } while (0)
  217. #define STARPU_PTHREAD_COND_DESTROY(cond) do { \
  218. int p_ret = starpu_pthread_cond_destroy(cond); \
  219. if (STARPU_UNLIKELY(p_ret)) { \
  220. fprintf(stderr, \
  221. "%s:%d starpu_pthread_cond_destroy: %s\n", \
  222. __FILE__, __LINE__, strerror(p_ret)); \
  223. STARPU_ABORT(); \
  224. } \
  225. } while (0)
  226. #define STARPU_PTHREAD_COND_SIGNAL(cond) do { \
  227. int p_ret = starpu_pthread_cond_signal(cond); \
  228. if (STARPU_UNLIKELY(p_ret)) { \
  229. fprintf(stderr, \
  230. "%s:%d starpu_pthread_cond_signal: %s\n", \
  231. __FILE__, __LINE__, strerror(p_ret)); \
  232. STARPU_ABORT(); \
  233. } \
  234. } while (0)
  235. #define STARPU_PTHREAD_COND_BROADCAST(cond) do { \
  236. int p_ret = starpu_pthread_cond_broadcast(cond); \
  237. if (STARPU_UNLIKELY(p_ret)) { \
  238. fprintf(stderr, \
  239. "%s:%d starpu_pthread_cond_broadcast: %s\n", \
  240. __FILE__, __LINE__, strerror(p_ret)); \
  241. STARPU_ABORT(); \
  242. } \
  243. } while (0)
  244. #define STARPU_PTHREAD_COND_WAIT(cond, mutex) do { \
  245. int p_ret = starpu_pthread_cond_wait((cond), (mutex)); \
  246. if (STARPU_UNLIKELY(p_ret)) { \
  247. fprintf(stderr, \
  248. "%s:%d starpu_pthread_cond_wait: %s\n", \
  249. __FILE__, __LINE__, strerror(p_ret)); \
  250. STARPU_ABORT(); \
  251. } \
  252. } while (0)
  253. /*
  254. * Encapsulation of the starpu_pthread_barrier_* functions.
  255. */
  256. #define STARPU_PTHREAD_BARRIER_INIT(barrier, attr, count) do { \
  257. int p_ret = pthread_barrier_init((barrier), (attr), (count)); \
  258. if (STARPU_UNLIKELY(p_ret)) { \
  259. fprintf(stderr, \
  260. "%s:%d pthread_barrier_init: %s\n", \
  261. __FILE__, __LINE__, strerror(p_ret)); \
  262. STARPU_ABORT(); \
  263. } \
  264. } while (0)
  265. #define STARPU_PTHREAD_BARRIER_DESTROY(barrier) do { \
  266. int p_ret = pthread_barrier_destroy((barrier)); \
  267. if (STARPU_UNLIKELY(p_ret)) { \
  268. fprintf(stderr, \
  269. "%s:%d pthread_barrier_destroy: %s\n", \
  270. __FILE__, __LINE__, strerror(p_ret)); \
  271. STARPU_ABORT(); \
  272. } \
  273. } while (0)
  274. #define STARPU_PTHREAD_BARRIER_WAIT(barrier) do { \
  275. int p_ret = pthread_barrier_wait((barrier)); \
  276. if (STARPU_UNLIKELY(!((p_ret == 0) || (p_ret == PTHREAD_BARRIER_SERIAL_THREAD)))) { \
  277. fprintf(stderr, \
  278. "%s:%d pthread_barrier_wait: %s\n", \
  279. __FILE__, __LINE__, strerror(p_ret)); \
  280. STARPU_ABORT(); \
  281. } \
  282. } while (0)
  283. #endif /* __STARPU_THREAD_UTIL_H__ */