starpu_thread_util.h 14 KB

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