starpu_util.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2019 Université de Bordeaux
  4. * Copyright (C) 2011,2012,2017 Inria
  5. * Copyright (C) 2010-2017, 2019 CNRS
  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. #ifndef __STARPU_UTIL_H__
  19. #define __STARPU_UTIL_H__
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. #include <starpu_config.h>
  26. #ifdef __GLIBC__
  27. #include <execinfo.h>
  28. #endif
  29. #ifdef STARPU_SIMGRID_MC
  30. #include <simgrid/modelchecker.h>
  31. #endif
  32. #ifdef __cplusplus
  33. extern "C"
  34. {
  35. #endif
  36. /**
  37. @defgroup API_Toolbox Toolbox
  38. @brief The following macros allow to make GCC extensions portable,
  39. and to have a code which can be compiled with any C compiler.
  40. @{
  41. */
  42. /**
  43. Return true (non-zero) if GCC version \p maj.\p min or later is
  44. being used (macro taken from glibc.)
  45. */
  46. #if defined __GNUC__ && defined __GNUC_MINOR__
  47. # define STARPU_GNUC_PREREQ(maj, min) \
  48. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  49. #else
  50. # define STARPU_GNUC_PREREQ(maj, min) 0
  51. #endif
  52. /**
  53. When building with a GNU C Compiler, allow programmers to mark an
  54. expression as unlikely.
  55. */
  56. #ifdef __GNUC__
  57. # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
  58. #else
  59. # define STARPU_UNLIKELY(expr) (expr)
  60. #endif
  61. /**
  62. When building with a GNU C Compiler, allow programmers to mark an
  63. expression as likely.
  64. */
  65. #ifdef __GNUC__
  66. # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
  67. #else
  68. # define STARPU_LIKELY(expr) (expr)
  69. #endif
  70. /**
  71. When building with a GNU C Compiler, defined to __attribute__((unused))
  72. */
  73. #ifdef __GNUC__
  74. # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
  75. #else
  76. # define STARPU_ATTRIBUTE_UNUSED
  77. #endif
  78. /**
  79. When building with a GNU C Compiler, defined to __attribute__((noreturn))
  80. */
  81. #ifdef __GNUC__
  82. # define STARPU_ATTRIBUTE_NORETURN __attribute__((noreturn))
  83. #else
  84. # define STARPU_ATTRIBUTE_NORETURN
  85. #endif
  86. /**
  87. When building with a GNU C Compiler, defined to __attribute__((visibility ("internal")))
  88. */
  89. #ifdef __GNUC__
  90. # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
  91. #else
  92. # define STARPU_ATTRIBUTE_INTERNAL
  93. #endif
  94. /**
  95. When building with a GNU C Compiler, defined to __attribute__((malloc))
  96. */
  97. #ifdef __GNUC__
  98. # define STARPU_ATTRIBUTE_MALLOC __attribute__((malloc))
  99. #else
  100. # define STARPU_ATTRIBUTE_MALLOC
  101. #endif
  102. /**
  103. When building with a GNU C Compiler, defined to __attribute__((warn_unused_result))
  104. */
  105. #ifdef __GNUC__
  106. # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  107. #else
  108. # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT
  109. #endif
  110. /**
  111. When building with a GNU C Compiler, defined to __attribute__((pure))
  112. */
  113. #ifdef __GNUC__
  114. # define STARPU_ATTRIBUTE_PURE __attribute__((pure))
  115. #else
  116. # define STARPU_ATTRIBUTE_PURE
  117. #endif
  118. /**
  119. When building with a GNU C Compiler, defined to__attribute__((aligned(size)))
  120. */
  121. #ifdef __GNUC__
  122. # define STARPU_ATTRIBUTE_ALIGNED(size) __attribute__((aligned(size)))
  123. #else
  124. # define STARPU_ATTRIBUTE_ALIGNED(size)
  125. #endif
  126. #ifdef __GNUC__
  127. # define STARPU_ATTRIBUTE_FORMAT(type, string, first) __attribute__((format(type, string, first)))
  128. #else
  129. # define STARPU_ATTRIBUTE_FORMAT(type, string, first)
  130. #endif
  131. /* Note that if we're compiling C++, then just use the "inline"
  132. keyword, since it's part of C++ */
  133. #if defined(c_plusplus) || defined(__cplusplus)
  134. # define STARPU_INLINE inline
  135. #elif defined(_MSC_VER) || defined(__HP_cc)
  136. # define STARPU_INLINE __inline
  137. #else
  138. # define STARPU_INLINE __inline__
  139. #endif
  140. #if STARPU_GNUC_PREREQ(4, 3)
  141. # define STARPU_ATTRIBUTE_CALLOC_SIZE(num,size) __attribute__((alloc_size(num,size)))
  142. # define STARPU_ATTRIBUTE_ALLOC_SIZE(size) __attribute__((alloc_size(size)))
  143. #else
  144. # define STARPU_ATTRIBUTE_CALLOC_SIZE(num,size)
  145. # define STARPU_ATTRIBUTE_ALLOC_SIZE(size)
  146. #endif
  147. #if STARPU_GNUC_PREREQ(3, 1) && !defined(BUILDING_STARPU) && !defined(STARPU_USE_DEPRECATED_API) && !defined(STARPU_USE_DEPRECATED_ONE_ZERO_API)
  148. #define STARPU_DEPRECATED __attribute__((__deprecated__))
  149. #else
  150. #define STARPU_DEPRECATED
  151. #endif /* __GNUC__ */
  152. #if STARPU_GNUC_PREREQ(3,3)
  153. #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  154. #else
  155. #define STARPU_WARN_UNUSED_RESULT
  156. #endif /* __GNUC__ */
  157. #define STARPU_BACKTRACE_LENGTH 32
  158. #ifdef __GLIBC__
  159. # define STARPU_DUMP_BACKTRACE() do { \
  160. void *__ptrs[STARPU_BACKTRACE_LENGTH]; \
  161. int __n = backtrace(__ptrs, STARPU_BACKTRACE_LENGTH); \
  162. backtrace_symbols_fd(__ptrs, __n, 2); \
  163. } while (0)
  164. #else
  165. # define STARPU_DUMP_BACKTRACE() do { } while (0)
  166. #endif
  167. #ifdef STARPU_SIMGRID_MC
  168. #define STARPU_SIMGRID_ASSERT(x) MC_assert(!!(x))
  169. #else
  170. #define STARPU_SIMGRID_ASSERT(x)
  171. #endif
  172. /**
  173. Unless StarPU has been configured with the option \ref enable-fast
  174. "--enable-fast", this macro will abort if the expression \p x is false.
  175. */
  176. #ifdef STARPU_NO_ASSERT
  177. #define STARPU_ASSERT(x) do { if (0) { (void) (x); } } while(0)
  178. #else
  179. # if defined(__CUDACC__) || defined(STARPU_HAVE_WINDOWS)
  180. # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); STARPU_SIMGRID_ASSERT(x); *(int*)NULL = 0; } } while(0)
  181. # else
  182. # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); STARPU_SIMGRID_ASSERT(x); assert(x); } } while (0)
  183. # endif
  184. #endif
  185. #ifdef STARPU_NO_ASSERT
  186. #define STARPU_ASSERT_ACCESSIBLE(x) do { if (0) { (void) (x); } } while(0)
  187. #else
  188. #define STARPU_ASSERT_ACCESSIBLE(ptr) do { volatile char __c STARPU_ATTRIBUTE_UNUSED = *(char*) (ptr); } while(0)
  189. #endif
  190. /**
  191. Unless StarPU has been configured with the option \ref enable-fast
  192. "--enable-fast", this macro will abort if the expression \p x is false.
  193. The string \p msg will be displayed.
  194. */
  195. #ifdef STARPU_NO_ASSERT
  196. #define STARPU_ASSERT_MSG(x, msg, ...) do { if (0) { (void) (x); (void) msg; } } while(0)
  197. #else
  198. # if defined(__CUDACC__) || defined(STARPU_HAVE_WINDOWS)
  199. # define STARPU_ASSERT_MSG(x, msg, ...) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); fprintf(stderr, "\n[starpu][%s][assert failure] " msg "\n\n", __starpu_func__, ## __VA_ARGS__); STARPU_SIMGRID_ASSERT(x); *(int*)NULL = 0; }} while(0)
  200. # else
  201. # define STARPU_ASSERT_MSG(x, msg, ...) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); fprintf(stderr, "\n[starpu][%s][assert failure] " msg "\n\n", __starpu_func__, ## __VA_ARGS__); STARPU_SIMGRID_ASSERT(x); assert(x); } } while(0)
  202. # endif
  203. #endif
  204. #ifdef __APPLE_CC__
  205. # ifdef __clang_analyzer__
  206. # define _starpu_abort() exit(42)
  207. # else
  208. # define _starpu_abort() *(volatile int*)NULL = 0
  209. # endif
  210. #else
  211. # define _starpu_abort() abort()
  212. #endif
  213. /**
  214. Abort the program.
  215. */
  216. #define STARPU_ABORT() do { \
  217. STARPU_DUMP_BACKTRACE(); \
  218. fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \
  219. _starpu_abort(); \
  220. } while(0)
  221. /**
  222. Print the string '[starpu][abort][name of the calling function:name
  223. of the file:line in the file]' followed by the given string \p msg
  224. and abort the program
  225. */
  226. #define STARPU_ABORT_MSG(msg, ...) do { \
  227. STARPU_DUMP_BACKTRACE(); \
  228. fprintf(stderr, "[starpu][abort][%s()@%s:%d] " msg "\n", __starpu_func__, __FILE__, __LINE__, ## __VA_ARGS__); \
  229. _starpu_abort(); \
  230. } while(0)
  231. #if defined(STARPU_HAVE_STRERROR_R)
  232. #if (! defined(__GLIBC__) || !__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && (! defined(_GNU_SOURCE)))
  233. /* XSI-compliant version of strerror_r returns an int */
  234. # define starpu_strerror_r(errnum, buf, buflen) \
  235. do \
  236. { \
  237. int _ret = strerror_r((errnum), (buf), (buflen)); \
  238. STARPU_ASSERT(_ret == 0); \
  239. } \
  240. while (0)
  241. #else
  242. /* GNU-specific version of strerror_r returns a char * */
  243. # define starpu_strerror_r(errnum, buf, buflen) \
  244. do \
  245. { \
  246. char * const _user_buf = (buf); \
  247. const size_t _user_buflen = (buflen); \
  248. /* the GNU-specific behaviour when 'buf' == NULL cannot be emulated with the XSI-compliant version */ \
  249. STARPU_ASSERT((buf) != NULL); \
  250. char * _tmp_buf = strerror_r((errnum), _user_buf, _user_buflen); \
  251. if (_tmp_buf != _user_buf) \
  252. { \
  253. if (_user_buflen > 0) \
  254. { \
  255. strncpy(_user_buf, _tmp_buf, _user_buflen-1); \
  256. _user_buf[_user_buflen-1] = '\0'; \
  257. } \
  258. } \
  259. } \
  260. while (0)
  261. #endif /* strerror_r ABI version */
  262. #endif /* STARPU_HAVE_STRERROR_R */
  263. /**
  264. Abort the program (after displaying \p message) if \p err has a
  265. value which is not 0.
  266. */
  267. #if defined(STARPU_HAVE_STRERROR_R)
  268. # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
  269. char xmessage[256]; starpu_strerror_r(-err, xmessage, 256); \
  270. fprintf(stderr, "[starpu] Unexpected value: <%d:%s> returned for " message "\n", err, xmessage, ## __VA_ARGS__); \
  271. STARPU_ABORT(); }}
  272. #else
  273. # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
  274. fprintf(stderr, "[starpu] Unexpected value: <%d> returned for " message "\n", err, ## __VA_ARGS__); \
  275. STARPU_ABORT(); }}
  276. #endif
  277. /**
  278. Abort the program (after displaying \p message) if \p err is
  279. different from \p value.
  280. */
  281. #if defined(STARPU_HAVE_STRERROR_R)
  282. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
  283. char xmessage[256]; starpu_strerror_r(-err, xmessage, 256); \
  284. fprintf(stderr, "[starpu] Unexpected value: <%d!=%d:%s> returned for " message "\n", err, value, xmessage, ## __VA_ARGS__); \
  285. STARPU_ABORT(); }}
  286. #else
  287. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
  288. fprintf(stderr, "[starpu] Unexpected value: <%d != %d> returned for " message "\n", err, value, ## __VA_ARGS__); \
  289. STARPU_ABORT(); }}
  290. #endif
  291. /* Note: do not use starpu_cmpxchg / starpu_xchg / starpu_cmpxchgl /
  292. * starpu_xchgl / starpu_cmpxchg64 / starpu_xchg64, which only
  293. * assembly-hand-written fallbacks used when building with an old gcc.
  294. * Rather use STARPU_VAL_COMPARE_AND_SWAP available on all platforms with a
  295. * recent-enough gcc */
  296. #if defined(__i386__) || defined(__x86_64__)
  297. static __starpu_inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
  298. {
  299. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  300. return old;
  301. }
  302. #define STARPU_HAVE_CMPXCHG
  303. static __starpu_inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
  304. {
  305. /* Note: xchg is always locked already */
  306. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  307. return next;
  308. }
  309. #define STARPU_HAVE_XCHG
  310. static __starpu_inline uint32_t starpu_cmpxchg32(uint32_t *ptr, uint32_t old, uint32_t next)
  311. {
  312. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  313. return old;
  314. }
  315. #define STARPU_HAVE_CMPXCHG32
  316. static __starpu_inline uint32_t starpu_xchg32(uint32_t *ptr, uint32_t next)
  317. {
  318. /* Note: xchg is always locked already */
  319. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  320. return next;
  321. }
  322. #define STARPU_HAVE_XCHG32
  323. #if defined(__i386__)
  324. static __starpu_inline unsigned long starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
  325. {
  326. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  327. return old;
  328. }
  329. #define STARPU_HAVE_CMPXCHGL
  330. static __starpu_inline unsigned long starpu_xchgl(unsigned long *ptr, unsigned long next)
  331. {
  332. /* Note: xchg is always locked already */
  333. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  334. return next;
  335. }
  336. #define STARPU_HAVE_XCHGL
  337. #endif
  338. #if defined(__x86_64__)
  339. static __starpu_inline unsigned long starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
  340. {
  341. __asm__ __volatile__("lock cmpxchgq %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  342. return old;
  343. }
  344. #define STARPU_HAVE_CMPXCHGL
  345. static __starpu_inline unsigned long starpu_xchgl(unsigned long *ptr, unsigned long next)
  346. {
  347. /* Note: xchg is always locked already */
  348. __asm__ __volatile__("xchgq %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  349. return next;
  350. }
  351. #define STARPU_HAVE_XCHGL
  352. #endif
  353. #if defined(__i386__)
  354. static __starpu_inline uint64_t starpu_cmpxchg64(uint64_t *ptr, uint64_t old, uint64_t next)
  355. {
  356. uint32_t next_hi = next >> 32;
  357. uint32_t next_lo = next & 0xfffffffful;
  358. __asm__ __volatile__("lock cmpxchg8b %1": "+A" (old), "+m" (*ptr) : "c" (next_hi), "b" (next_lo) : "memory");
  359. return old;
  360. }
  361. #define STARPU_HAVE_CMPXCHG64
  362. #endif
  363. #if defined(__x86_64__)
  364. static __starpu_inline uint64_t starpu_cmpxchg64(uint64_t *ptr, uint64_t old, uint64_t next)
  365. {
  366. __asm__ __volatile__("lock cmpxchgq %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  367. return old;
  368. }
  369. #define STARPU_HAVE_CMPXCHG64
  370. static __starpu_inline uint64_t starpu_xchg64(uint64_t *ptr, uint64_t next)
  371. {
  372. /* Note: xchg is always locked already */
  373. __asm__ __volatile__("xchgq %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  374. return next;
  375. }
  376. #define STARPU_HAVE_XCHG64
  377. #endif
  378. #endif
  379. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  380. static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
  381. { \
  382. unsigned old, next; \
  383. while (1) \
  384. { \
  385. old = *ptr; \
  386. next = expr; \
  387. if (starpu_cmpxchg(ptr, old, next) == old) \
  388. break; \
  389. }; \
  390. return expr; \
  391. }
  392. #define STARPU_ATOMIC_SOMETHINGL(name,expr) \
  393. static __starpu_inline unsigned long starpu_atomic_##name##l(unsigned long *ptr, unsigned long value) \
  394. { \
  395. unsigned long old, next; \
  396. while (1) \
  397. { \
  398. old = *ptr; \
  399. next = expr; \
  400. if (starpu_cmpxchgl(ptr, old, next) == old) \
  401. break; \
  402. }; \
  403. return expr; \
  404. }
  405. /* Returns the new value */
  406. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  407. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  408. #define STARPU_ATOMIC_ADDL(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  409. #else
  410. #if defined(STARPU_HAVE_CMPXCHG)
  411. STARPU_ATOMIC_SOMETHING(add, old + value)
  412. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  413. #endif
  414. #if defined(STARPU_HAVE_CMPXCHGL)
  415. STARPU_ATOMIC_SOMETHINGL(add, old + value)
  416. #define STARPU_ATOMIC_ADDL(ptr, value) starpu_atomic_addl(ptr, value)
  417. #endif
  418. #endif
  419. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  420. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  421. #define STARPU_ATOMIC_ORL(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  422. #else
  423. #if defined(STARPU_HAVE_CMPXCHG)
  424. STARPU_ATOMIC_SOMETHING(or, old | value)
  425. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  426. #endif
  427. #if defined(STARPU_HAVE_CMPXCHGL)
  428. STARPU_ATOMIC_SOMETHINGL(or, old | value)
  429. #define STARPU_ATOMIC_ORL(ptr, value) starpu_atomic_orl(ptr, value)
  430. #endif
  431. #endif
  432. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  433. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  434. #define STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value)
  435. #define STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value)
  436. #else
  437. #ifdef STARPU_HAVE_CMPXCHG
  438. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  439. #endif
  440. #ifdef STARPU_HAVE_CMPXCHG32
  441. #define STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value) (starpu_cmpxchg32((ptr), (old), (value)) == (old))
  442. #endif
  443. #ifdef STARPU_HAVE_CMPXCHG64
  444. #define STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value) (starpu_cmpxchg64((ptr), (old), (value)) == (old))
  445. #endif
  446. #endif
  447. #ifdef STARPU_HAVE_SYNC_VAL_COMPARE_AND_SWAP
  448. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value)))
  449. #define STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value)
  450. #define STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value)
  451. #else
  452. #ifdef STARPU_HAVE_CMPXCHG
  453. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)))
  454. #endif
  455. #ifdef STARPU_HAVE_CMPXCHG32
  456. #define STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value) (starpu_cmpxchg32((ptr), (old), (value)))
  457. #endif
  458. #ifdef STARPU_HAVE_CMPXCHG64
  459. #define STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value) (starpu_cmpxchg64((ptr), (old), (value)))
  460. #endif
  461. #endif
  462. /* Returns the previous value */
  463. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  464. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  465. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  466. #elif defined(STARPU_HAVE_XCHG)
  467. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  468. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  469. #endif
  470. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  471. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  472. #elif defined(__i386__)
  473. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  474. #elif defined(__KNC__) || defined(__KNF__)
  475. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  476. #elif defined(__x86_64__)
  477. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  478. #elif defined(__ppc__) || defined(__ppc64__)
  479. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  480. #endif
  481. /**
  482. This macro can be used to do a synchronization.
  483. */
  484. #if defined(__i386__)
  485. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  486. #elif defined(__KNC__) || defined(__KNF__)
  487. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  488. #elif defined(__x86_64__)
  489. #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
  490. #elif defined(__ppc__) || defined(__ppc64__)
  491. #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
  492. #else
  493. #define STARPU_RMB() STARPU_SYNCHRONIZE()
  494. #endif
  495. /**
  496. This macro can be used to do a synchronization.
  497. */
  498. #if defined(__i386__)
  499. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  500. #elif defined(__KNC__) || defined(__KNF__)
  501. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  502. #elif defined(__x86_64__)
  503. #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
  504. #elif defined(__ppc__) || defined(__ppc64__)
  505. #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
  506. #else
  507. #define STARPU_WMB() STARPU_SYNCHRONIZE()
  508. #endif
  509. #ifdef _WIN32
  510. /* Try to fetch the system definition of timespec */
  511. #include <sys/types.h>
  512. #include <sys/stat.h>
  513. #ifdef HAVE_UNISTD_H
  514. #include <unistd.h>
  515. #endif
  516. #include <time.h>
  517. #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
  518. #include <pthread.h>
  519. #endif
  520. #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || (defined(_MSC_VER) && _MSC_VER < 1900)
  521. /* If it didn't get defined in the standard places, then define it ourself */
  522. #ifndef STARPU_TIMESPEC_DEFINED
  523. #define STARPU_TIMESPEC_DEFINED 1
  524. struct timespec
  525. {
  526. time_t tv_sec; /* Seconds */
  527. long tv_nsec; /* Nanoseconds */
  528. };
  529. #endif /* STARPU_TIMESPEC_DEFINED */
  530. #endif /* STARPU_HAVE_STRUCT_TIMESPEC */
  531. /* Fetch gettimeofday on mingw/cygwin */
  532. #if defined(__MINGW32__) || defined(__CYGWIN__)
  533. #include <sys/time.h>
  534. #endif
  535. #else
  536. #include <sys/time.h>
  537. #endif /* _WIN32 */
  538. /** @} */
  539. #ifdef __cplusplus
  540. }
  541. #endif
  542. #endif /* __STARPU_UTIL_H__ */