starpu_util.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. <<<<<<< HEAD
  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 available on all platforms with a
  296. * recent-enough gcc */
  297. #if defined(__i386__) || defined(__x86_64__)
  298. =======
  299. #if defined(__i386__) || defined(__x86_64__)
  300. >>>>>>> 8e2d401663e25038bae3c01c68fb544e638ec51c
  301. static __starpu_inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
  302. {
  303. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  304. return old;
  305. }
  306. <<<<<<< HEAD
  307. #define STARPU_HAVE_CMPXCHG
  308. =======
  309. >>>>>>> 8e2d401663e25038bae3c01c68fb544e638ec51c
  310. static __starpu_inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
  311. {
  312. /* Note: xchg is always locked already */
  313. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  314. return next;
  315. }
  316. #define STARPU_HAVE_XCHG
  317. <<<<<<< HEAD
  318. static __starpu_inline uint32_t starpu_cmpxchg32(uint32_t *ptr, uint32_t old, uint32_t next)
  319. {
  320. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  321. return old;
  322. }
  323. #define STARPU_HAVE_CMPXCHG32
  324. static __starpu_inline uint32_t starpu_xchg32(uint32_t *ptr, uint32_t next)
  325. {
  326. /* Note: xchg is always locked already */
  327. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  328. return next;
  329. }
  330. #define STARPU_HAVE_XCHG32
  331. =======
  332. >>>>>>> 8e2d401663e25038bae3c01c68fb544e638ec51c
  333. #if defined(__i386__)
  334. static __starpu_inline unsigned long starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
  335. {
  336. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  337. return old;
  338. }
  339. <<<<<<< HEAD
  340. #define STARPU_HAVE_CMPXCHGL
  341. =======
  342. >>>>>>> 8e2d401663e25038bae3c01c68fb544e638ec51c
  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__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  347. return next;
  348. }
  349. #define STARPU_HAVE_XCHGL
  350. #endif
  351. #if defined(__x86_64__)
  352. static __starpu_inline unsigned long starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
  353. {
  354. __asm__ __volatile__("lock cmpxchgq %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  355. return old;
  356. }
  357. <<<<<<< HEAD
  358. #define STARPU_HAVE_CMPXCHGL
  359. =======
  360. >>>>>>> 8e2d401663e25038bae3c01c68fb544e638ec51c
  361. static __starpu_inline unsigned long starpu_xchgl(unsigned long *ptr, unsigned long next)
  362. {
  363. /* Note: xchg is always locked already */
  364. __asm__ __volatile__("xchgq %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  365. return next;
  366. }
  367. #define STARPU_HAVE_XCHGL
  368. #endif
  369. <<<<<<< HEAD
  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. =======
  396. >>>>>>> 8e2d401663e25038bae3c01c68fb544e638ec51c
  397. #endif
  398. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  399. static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
  400. { \
  401. unsigned old, next; \
  402. while (1) \
  403. { \
  404. old = *ptr; \
  405. next = expr; \
  406. if (starpu_cmpxchg(ptr, old, next) == old) \
  407. break; \
  408. }; \
  409. return expr; \
  410. }
  411. #define STARPU_ATOMIC_SOMETHINGL(name,expr) \
  412. static __starpu_inline unsigned long starpu_atomic_##name##l(unsigned long *ptr, unsigned long value) \
  413. { \
  414. unsigned long old, next; \
  415. while (1) \
  416. { \
  417. old = *ptr; \
  418. next = expr; \
  419. if (starpu_cmpxchgl(ptr, old, next) == old) \
  420. break; \
  421. }; \
  422. return expr; \
  423. }
  424. <<<<<<< HEAD
  425. #define STARPU_ATOMIC_SOMETHING64(name,expr) \
  426. static __starpu_inline uint64_t starpu_atomic_##name##64(uint64_t *ptr, uint64_t value) \
  427. { \
  428. uint64_t old, next; \
  429. while (1) \
  430. { \
  431. old = *ptr; \
  432. next = expr; \
  433. if (starpu_cmpxchg64(ptr, old, next) == old) \
  434. break; \
  435. }; \
  436. return expr; \
  437. }
  438. =======
  439. >>>>>>> 8e2d401663e25038bae3c01c68fb544e638ec51c
  440. /* Returns the new value */
  441. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  442. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  443. #define STARPU_ATOMIC_ADDL(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  444. <<<<<<< HEAD
  445. #define STARPU_ATOMIC_ADD64(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  446. #else
  447. #if defined(STARPU_HAVE_CMPXCHG)
  448. STARPU_ATOMIC_SOMETHING(add, old + value)
  449. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  450. #endif
  451. #if defined(STARPU_HAVE_CMPXCHGL)
  452. STARPU_ATOMIC_SOMETHINGL(add, old + value)
  453. #define STARPU_ATOMIC_ADDL(ptr, value) starpu_atomic_addl(ptr, value)
  454. #endif
  455. #if defined(STARPU_HAVE_CMPXCHG64)
  456. STARPU_ATOMIC_SOMETHING64(add, old + value)
  457. #define STARPU_ATOMIC_ADD64(ptr, value) starpu_atomic_add64(ptr, value)
  458. #endif
  459. =======
  460. #else
  461. #if defined(STARPU_HAVE_XCHG)
  462. STARPU_ATOMIC_SOMETHING(add, old + value)
  463. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  464. #endif
  465. #if defined(STARPU_HAVE_XCHGL)
  466. STARPU_ATOMIC_SOMETHINGL(add, old + value)
  467. #define STARPU_ATOMIC_ADDL(ptr, value) starpu_atomic_addl(ptr, value)
  468. #endif
  469. >>>>>>> 8e2d401663e25038bae3c01c68fb544e638ec51c
  470. #endif
  471. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  472. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  473. #define STARPU_ATOMIC_ORL(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  474. <<<<<<< HEAD
  475. #define STARPU_ATOMIC_OR64(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  476. #else
  477. #if defined(STARPU_HAVE_CMPXCHG)
  478. STARPU_ATOMIC_SOMETHING(or, old | value)
  479. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  480. #endif
  481. #if defined(STARPU_HAVE_CMPXCHGL)
  482. STARPU_ATOMIC_SOMETHINGL(or, old | value)
  483. #define STARPU_ATOMIC_ORL(ptr, value) starpu_atomic_orl(ptr, value)
  484. #endif
  485. #if defined(STARPU_HAVE_CMPXCHG64)
  486. STARPU_ATOMIC_SOMETHING64(or, old | value)
  487. #define STARPU_ATOMIC_OR64(ptr, value) starpu_atomic_or64(ptr, value)
  488. #endif
  489. =======
  490. #else
  491. #if defined(STARPU_HAVE_XCHG)
  492. STARPU_ATOMIC_SOMETHING(or, old | value)
  493. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  494. #endif
  495. #if defined(STARPU_HAVE_XCHGL)
  496. STARPU_ATOMIC_SOMETHINGL(or, old | value)
  497. #define STARPU_ATOMIC_ORL(ptr, value) starpu_atomic_orl(ptr, value)
  498. #endif
  499. >>>>>>> 8e2d401663e25038bae3c01c68fb544e638ec51c
  500. #endif
  501. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  502. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  503. <<<<<<< HEAD
  504. #define STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value)
  505. #define STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value)
  506. #else
  507. #ifdef STARPU_HAVE_CMPXCHG
  508. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  509. #endif
  510. #ifdef STARPU_HAVE_CMPXCHG32
  511. #define STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value) (starpu_cmpxchg32((ptr), (old), (value)) == (old))
  512. #endif
  513. #ifdef STARPU_HAVE_CMPXCHG64
  514. #define STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value) (starpu_cmpxchg64((ptr), (old), (value)) == (old))
  515. #endif
  516. #endif
  517. #ifdef STARPU_HAVE_SYNC_VAL_COMPARE_AND_SWAP
  518. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value)))
  519. #define STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value)
  520. #define STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value)
  521. #else
  522. #ifdef STARPU_HAVE_CMPXCHG
  523. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)))
  524. #endif
  525. #ifdef STARPU_HAVE_CMPXCHG32
  526. #define STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value) (starpu_cmpxchg32((ptr), (old), (value)))
  527. #endif
  528. #ifdef STARPU_HAVE_CMPXCHG64
  529. #define STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value) (starpu_cmpxchg64((ptr), (old), (value)))
  530. #endif
  531. #endif
  532. =======
  533. #elif defined(STARPU_HAVE_XCHG)
  534. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  535. #endif
  536. #ifdef STARPU_HAVE_SYNC_VAL_COMPARE_AND_SWAP
  537. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value)))
  538. #elif defined(STARPU_HAVE_XCHG)
  539. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)))
  540. #endif
  541. >>>>>>> 8e2d401663e25038bae3c01c68fb544e638ec51c
  542. /* Returns the previous value */
  543. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  544. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  545. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  546. #elif defined(STARPU_HAVE_XCHG)
  547. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  548. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  549. #endif
  550. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  551. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  552. #elif defined(__i386__)
  553. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  554. #elif defined(__KNC__) || defined(__KNF__)
  555. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  556. #elif defined(__x86_64__)
  557. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  558. #elif defined(__ppc__) || defined(__ppc64__)
  559. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  560. #endif
  561. /**
  562. This macro can be used to do a synchronization.
  563. */
  564. #if defined(__i386__)
  565. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  566. #elif defined(__KNC__) || defined(__KNF__)
  567. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  568. #elif defined(__x86_64__)
  569. #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
  570. #elif defined(__ppc__) || defined(__ppc64__)
  571. #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
  572. #else
  573. #define STARPU_RMB() STARPU_SYNCHRONIZE()
  574. #endif
  575. /**
  576. This macro can be used to do a synchronization.
  577. */
  578. #if defined(__i386__)
  579. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  580. #elif defined(__KNC__) || defined(__KNF__)
  581. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  582. #elif defined(__x86_64__)
  583. #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
  584. #elif defined(__ppc__) || defined(__ppc64__)
  585. #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
  586. #else
  587. #define STARPU_WMB() STARPU_SYNCHRONIZE()
  588. #endif
  589. #ifdef _WIN32
  590. /* Try to fetch the system definition of timespec */
  591. #include <sys/types.h>
  592. #include <sys/stat.h>
  593. #ifdef HAVE_UNISTD_H
  594. #include <unistd.h>
  595. #endif
  596. #include <time.h>
  597. #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
  598. #include <pthread.h>
  599. #endif
  600. #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || (defined(_MSC_VER) && _MSC_VER < 1900)
  601. /* If it didn't get defined in the standard places, then define it ourself */
  602. #ifndef STARPU_TIMESPEC_DEFINED
  603. #define STARPU_TIMESPEC_DEFINED 1
  604. struct timespec
  605. {
  606. time_t tv_sec; /* Seconds */
  607. long tv_nsec; /* Nanoseconds */
  608. };
  609. #endif /* STARPU_TIMESPEC_DEFINED */
  610. #endif /* STARPU_HAVE_STRUCT_TIMESPEC */
  611. /* Fetch gettimeofday on mingw/cygwin */
  612. #if defined(__MINGW32__) || defined(__CYGWIN__)
  613. #include <sys/time.h>
  614. #endif
  615. #else
  616. #include <sys/time.h>
  617. #endif /* _WIN32 */
  618. /** @} */
  619. #ifdef __cplusplus
  620. }
  621. #endif
  622. #endif /* __STARPU_UTIL_H__ */