starpu_util.h 21 KB

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