starpu_util.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 ("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(_MSC_VER)
  230. #undef STARPU_HAVE_STRERROR_R
  231. #endif
  232. #if defined(STARPU_HAVE_STRERROR_R)
  233. #if (! defined(__GLIBC__) || !__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && (! defined(_GNU_SOURCE)))
  234. /* XSI-compliant version of strerror_r returns an int */
  235. # define starpu_strerror_r(errnum, buf, buflen) \
  236. do \
  237. { \
  238. int _ret = strerror_r((errnum), (buf), (buflen)); \
  239. STARPU_ASSERT(_ret == 0); \
  240. } \
  241. while (0)
  242. #else
  243. /* GNU-specific version of strerror_r returns a char * */
  244. # define starpu_strerror_r(errnum, buf, buflen) \
  245. do \
  246. { \
  247. char * const _user_buf = (buf); \
  248. const size_t _user_buflen = (buflen); \
  249. /* the GNU-specific behaviour when 'buf' == NULL cannot be emulated with the XSI-compliant version */ \
  250. STARPU_ASSERT((buf) != NULL); \
  251. char * _tmp_buf = strerror_r((errnum), _user_buf, _user_buflen); \
  252. if (_tmp_buf != _user_buf) \
  253. { \
  254. if (_user_buflen > 0) \
  255. { \
  256. strncpy(_user_buf, _tmp_buf, _user_buflen-1); \
  257. _user_buf[_user_buflen-1] = '\0'; \
  258. } \
  259. } \
  260. } \
  261. while (0)
  262. #endif /* strerror_r ABI version */
  263. #endif /* STARPU_HAVE_STRERROR_R */
  264. /**
  265. Abort the program (after displaying \p message) if \p err has a
  266. value which is not 0.
  267. */
  268. #if defined(STARPU_HAVE_STRERROR_R)
  269. # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
  270. char xmessage[256]; starpu_strerror_r(-err, xmessage, 256); \
  271. fprintf(stderr, "[starpu] Unexpected value: <%d:%s> returned for " message "\n", err, xmessage, ## __VA_ARGS__); \
  272. STARPU_ABORT(); }}
  273. #else
  274. # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
  275. fprintf(stderr, "[starpu] Unexpected value: <%d> returned for " message "\n", err, ## __VA_ARGS__); \
  276. STARPU_ABORT(); }}
  277. #endif
  278. /**
  279. Abort the program (after displaying \p message) if \p err is
  280. different from \p value.
  281. */
  282. #if defined(STARPU_HAVE_STRERROR_R)
  283. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
  284. char xmessage[256]; starpu_strerror_r(-err, xmessage, 256); \
  285. fprintf(stderr, "[starpu] Unexpected value: <%d!=%d:%s> returned for " message "\n", err, value, xmessage, ## __VA_ARGS__); \
  286. STARPU_ABORT(); }}
  287. #else
  288. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
  289. fprintf(stderr, "[starpu] Unexpected value: <%d != %d> returned for " message "\n", err, value, ## __VA_ARGS__); \
  290. STARPU_ABORT(); }}
  291. #endif
  292. /* Note: do not use _starpu_cmpxchg / _starpu_xchg / _starpu_cmpxchgl /
  293. * _starpu_xchgl / _starpu_cmpxchg64 / _starpu_xchg64, which only
  294. * assembly-hand-written fallbacks used when building with an old gcc.
  295. * Rather use STARPU_VAL_COMPARE_AND_SWAP and STARPU_VAL_EXCHANGE available on
  296. * all platforms with a recent-enough gcc */
  297. #if defined(__i386__) || defined(__x86_64__)
  298. static __starpu_inline unsigned _starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
  299. {
  300. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  301. return old;
  302. }
  303. #define STARPU_HAVE_CMPXCHG
  304. static __starpu_inline unsigned _starpu_xchg(unsigned *ptr, unsigned next)
  305. {
  306. /* Note: xchg is always locked already */
  307. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  308. return next;
  309. }
  310. #define STARPU_HAVE_XCHG
  311. static __starpu_inline uint32_t _starpu_cmpxchg32(uint32_t *ptr, uint32_t old, uint32_t next)
  312. {
  313. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  314. return old;
  315. }
  316. #define STARPU_HAVE_CMPXCHG32
  317. static __starpu_inline uint32_t _starpu_xchg32(uint32_t *ptr, uint32_t next)
  318. {
  319. /* Note: xchg is always locked already */
  320. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  321. return next;
  322. }
  323. #define STARPU_HAVE_XCHG32
  324. #if defined(__i386__)
  325. static __starpu_inline unsigned long _starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
  326. {
  327. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  328. return old;
  329. }
  330. #define STARPU_HAVE_CMPXCHGL
  331. static __starpu_inline unsigned long _starpu_xchgl(unsigned long *ptr, unsigned long next)
  332. {
  333. /* Note: xchg is always locked already */
  334. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  335. return next;
  336. }
  337. #define STARPU_HAVE_XCHGL
  338. #endif
  339. #if defined(__x86_64__)
  340. static __starpu_inline unsigned long _starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
  341. {
  342. __asm__ __volatile__("lock cmpxchgq %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  343. return old;
  344. }
  345. #define STARPU_HAVE_CMPXCHGL
  346. static __starpu_inline unsigned long _starpu_xchgl(unsigned long *ptr, unsigned long next)
  347. {
  348. /* Note: xchg is always locked already */
  349. __asm__ __volatile__("xchgq %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  350. return next;
  351. }
  352. #define STARPU_HAVE_XCHGL
  353. #endif
  354. #if defined(__i386__)
  355. static __starpu_inline uint64_t _starpu_cmpxchg64(uint64_t *ptr, uint64_t old, uint64_t next)
  356. {
  357. uint32_t next_hi = next >> 32;
  358. uint32_t next_lo = next & 0xfffffffful;
  359. __asm__ __volatile__("lock cmpxchg8b %1": "+A" (old), "+m" (*ptr) : "c" (next_hi), "b" (next_lo) : "memory");
  360. return old;
  361. }
  362. #define STARPU_HAVE_CMPXCHG64
  363. #endif
  364. #if defined(__x86_64__)
  365. static __starpu_inline uint64_t _starpu_cmpxchg64(uint64_t *ptr, uint64_t old, uint64_t next)
  366. {
  367. __asm__ __volatile__("lock cmpxchgq %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  368. return old;
  369. }
  370. #define STARPU_HAVE_CMPXCHG64
  371. static __starpu_inline uint64_t _starpu_xchg64(uint64_t *ptr, uint64_t next)
  372. {
  373. /* Note: xchg is always locked already */
  374. __asm__ __volatile__("xchgq %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  375. return next;
  376. }
  377. #define STARPU_HAVE_XCHG64
  378. #endif
  379. #endif
  380. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  381. static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
  382. { \
  383. unsigned old, next; \
  384. while (1) \
  385. { \
  386. old = *ptr; \
  387. next = expr; \
  388. if (_starpu_cmpxchg(ptr, old, next) == old) \
  389. break; \
  390. }; \
  391. return expr; \
  392. }
  393. #define STARPU_ATOMIC_SOMETHINGL(name,expr) \
  394. static __starpu_inline unsigned long starpu_atomic_##name##l(unsigned long *ptr, unsigned long value) \
  395. { \
  396. unsigned long old, next; \
  397. while (1) \
  398. { \
  399. old = *ptr; \
  400. next = expr; \
  401. if (_starpu_cmpxchgl(ptr, old, next) == old) \
  402. break; \
  403. }; \
  404. return expr; \
  405. }
  406. #define STARPU_ATOMIC_SOMETHING64(name,expr) \
  407. static __starpu_inline uint64_t starpu_atomic_##name##64(uint64_t *ptr, uint64_t value) \
  408. { \
  409. uint64_t old, next; \
  410. while (1) \
  411. { \
  412. old = *ptr; \
  413. next = expr; \
  414. if (_starpu_cmpxchg64(ptr, old, next) == old) \
  415. break; \
  416. }; \
  417. return expr; \
  418. }
  419. /* Returns the new value */
  420. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  421. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  422. #define STARPU_ATOMIC_ADDL(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  423. #define STARPU_ATOMIC_ADD64(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  424. #else
  425. #if defined(STARPU_HAVE_CMPXCHG)
  426. STARPU_ATOMIC_SOMETHING(add, old + value)
  427. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  428. #endif
  429. #if defined(STARPU_HAVE_CMPXCHGL)
  430. STARPU_ATOMIC_SOMETHINGL(add, old + value)
  431. #define STARPU_ATOMIC_ADDL(ptr, value) starpu_atomic_addl(ptr, value)
  432. #endif
  433. #if defined(STARPU_HAVE_CMPXCHG64)
  434. STARPU_ATOMIC_SOMETHING64(add, old + value)
  435. #define STARPU_ATOMIC_ADD64(ptr, value) starpu_atomic_add64(ptr, value)
  436. #endif
  437. #endif
  438. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  439. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  440. #define STARPU_ATOMIC_ORL(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  441. #define STARPU_ATOMIC_OR64(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  442. #else
  443. #if defined(STARPU_HAVE_CMPXCHG)
  444. STARPU_ATOMIC_SOMETHING(or, old | value)
  445. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  446. #endif
  447. #if defined(STARPU_HAVE_CMPXCHGL)
  448. STARPU_ATOMIC_SOMETHINGL(or, old | value)
  449. #define STARPU_ATOMIC_ORL(ptr, value) starpu_atomic_orl(ptr, value)
  450. #endif
  451. #if defined(STARPU_HAVE_CMPXCHG64)
  452. STARPU_ATOMIC_SOMETHING64(or, old | value)
  453. #define STARPU_ATOMIC_OR64(ptr, value) starpu_atomic_or64(ptr, value)
  454. #endif
  455. #endif
  456. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  457. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  458. #define STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value)
  459. #define STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value)
  460. #else
  461. #ifdef STARPU_HAVE_CMPXCHG
  462. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (_starpu_cmpxchg((ptr), (old), (value)) == (old))
  463. #endif
  464. #ifdef STARPU_HAVE_CMPXCHG32
  465. #define STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value) (_starpu_cmpxchg32((ptr), (old), (value)) == (old))
  466. #endif
  467. #ifdef STARPU_HAVE_CMPXCHG64
  468. #define STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value) (_starpu_cmpxchg64((ptr), (old), (value)) == (old))
  469. #endif
  470. #endif
  471. #ifdef STARPU_HAVE_SYNC_VAL_COMPARE_AND_SWAP
  472. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value)))
  473. #define STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value)
  474. #define STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value)
  475. #else
  476. #ifdef STARPU_HAVE_CMPXCHG
  477. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (_starpu_cmpxchg((ptr), (old), (value)))
  478. #endif
  479. #ifdef STARPU_HAVE_CMPXCHG32
  480. #define STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value) (_starpu_cmpxchg32((ptr), (old), (value)))
  481. #endif
  482. #ifdef STARPU_HAVE_CMPXCHG64
  483. #define STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value) (_starpu_cmpxchg64((ptr), (old), (value)))
  484. #endif
  485. #endif
  486. #ifdef STARPU_HAVE_ATOMIC_EXCHANGE_N
  487. #define STARPU_VAL_EXCHANGE(ptr, value) (__atomic_exchange_n((ptr), (value), __ATOMIC_SEQ_CST))
  488. #define STARPU_VAL_EXCHANGEL(ptr, value) STARPU_VAL_EXCHANGE((ptr) (value))
  489. #define STARPU_VAL_EXCHANGE32(ptr, value) STARPU_VAL_EXCHANGE((ptr) (value))
  490. #define STARPU_VAL_EXCHANGE64(ptr, value) STARPU_VAL_EXCHANGE((ptr) (value))
  491. #else
  492. #ifdef STARPU_HAVE_XCHG
  493. #define STARPU_VAL_EXCHANGE(ptr, value) (_starpu_xchg((ptr), (value)))
  494. #endif
  495. #ifdef STARPU_HAVE_XCHGL
  496. #define STARPU_VAL_EXCHANGEL(ptr, value) (_starpu_xchgl((ptr), (value)))
  497. #endif
  498. #ifdef STARPU_HAVE_XCHG32
  499. #define STARPU_VAL_EXCHANGE32(ptr, value) (_starpu_xchg32((ptr), (value)))
  500. #endif
  501. #ifdef STARPU_HAVE_XCHG64
  502. #define STARPU_VAL_EXCHANGE64(ptr, value) (_starpu_xchg64((ptr), (value)))
  503. #endif
  504. #endif
  505. /* Returns the previous value */
  506. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  507. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  508. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  509. #elif defined(STARPU_HAVE_XCHG)
  510. #define STARPU_TEST_AND_SET(ptr, value) (_starpu_xchg((ptr), (value)))
  511. #define STARPU_RELEASE(ptr) (_starpu_xchg((ptr), 0))
  512. #endif
  513. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  514. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  515. #elif defined(__i386__)
  516. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  517. #elif defined(__KNC__) || defined(__KNF__)
  518. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  519. #elif defined(__x86_64__)
  520. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  521. #elif defined(__ppc__) || defined(__ppc64__)
  522. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  523. #endif
  524. /**
  525. This macro can be used to do a synchronization.
  526. */
  527. #if defined(__i386__)
  528. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  529. #elif defined(__KNC__) || defined(__KNF__)
  530. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  531. #elif defined(__x86_64__)
  532. #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
  533. #elif defined(__ppc__) || defined(__ppc64__)
  534. #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
  535. #else
  536. #define STARPU_RMB() STARPU_SYNCHRONIZE()
  537. #endif
  538. /**
  539. This macro can be used to do a synchronization.
  540. */
  541. #if defined(__i386__)
  542. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  543. #elif defined(__KNC__) || defined(__KNF__)
  544. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  545. #elif defined(__x86_64__)
  546. #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
  547. #elif defined(__ppc__) || defined(__ppc64__)
  548. #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
  549. #else
  550. #define STARPU_WMB() STARPU_SYNCHRONIZE()
  551. #endif
  552. #if defined(__i386__) || defined(__x86_64__)
  553. #define STARPU_CACHELINE_SIZE 64
  554. #elif defined(__ppc__) || defined(__ppc64__) || defined(__ia64__)
  555. #define STARPU_CACHELINE_SIZE 128
  556. #elif defined(__s390__) || defined(__s390x__)
  557. #define STARPU_CACHELINE_SIZE 256
  558. #else
  559. /* Conservative default */
  560. #define STARPU_CACHELINE_SIZE 1024
  561. #endif
  562. #ifdef _WIN32
  563. /* Try to fetch the system definition of timespec */
  564. #include <sys/types.h>
  565. #include <sys/stat.h>
  566. #ifdef HAVE_UNISTD_H
  567. #include <unistd.h>
  568. #endif
  569. #include <time.h>
  570. #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
  571. #include <pthread.h>
  572. #endif
  573. #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || (defined(_MSC_VER) && _MSC_VER < 1900)
  574. /* If it didn't get defined in the standard places, then define it ourself */
  575. #ifndef STARPU_TIMESPEC_DEFINED
  576. #define STARPU_TIMESPEC_DEFINED 1
  577. struct timespec
  578. {
  579. time_t tv_sec; /* Seconds */
  580. long tv_nsec; /* Nanoseconds */
  581. };
  582. #endif /* STARPU_TIMESPEC_DEFINED */
  583. #endif /* STARPU_HAVE_STRUCT_TIMESPEC */
  584. /* Fetch gettimeofday on mingw/cygwin */
  585. #if defined(__MINGW32__) || defined(__CYGWIN__)
  586. #include <sys/time.h>
  587. #endif
  588. #else
  589. #include <sys/time.h>
  590. #endif /* _WIN32 */
  591. /** @} */
  592. #ifdef __cplusplus
  593. }
  594. #endif
  595. #endif /* __STARPU_UTIL_H__ */