starpu_util.h 22 KB

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