StarPU Handbook
starpu_util.h
Go to the documentation of this file.
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 
17 #ifndef __STARPU_UTIL_H__
18 #define __STARPU_UTIL_H__
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <assert.h>
25 
26 #include <starpu_config.h>
27 
28 #ifdef __GLIBC__
29 #include <execinfo.h>
30 #endif
31 
32 #ifdef STARPU_SIMGRID_MC
33 #include <simgrid/modelchecker.h>
34 #endif
35 
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40 
52 #if defined __GNUC__ && defined __GNUC_MINOR__
53 # define STARPU_GNUC_PREREQ(maj, min) \
54  ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
55 #else
56 # define STARPU_GNUC_PREREQ(maj, min) 0
57 #endif
58 
63 #ifdef __GNUC__
64 # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
65 #else
66 # define STARPU_UNLIKELY(expr) (expr)
67 #endif
68 
73 #ifdef __GNUC__
74 # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
75 #else
76 # define STARPU_LIKELY(expr) (expr)
77 #endif
78 
82 #ifdef __GNUC__
83 # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
84 #else
85 # define STARPU_ATTRIBUTE_UNUSED
86 #endif
87 
91 #ifdef __GNUC__
92 # define STARPU_ATTRIBUTE_NORETURN __attribute__((noreturn))
93 #else
94 # define STARPU_ATTRIBUTE_NORETURN
95 #endif
96 
100 #ifdef __GNUC__
101 # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
102 #else
103 # define STARPU_ATTRIBUTE_INTERNAL
104 #endif
105 
109 #ifdef __GNUC__
110 # define STARPU_ATTRIBUTE_MALLOC __attribute__((malloc))
111 #else
112 # define STARPU_ATTRIBUTE_MALLOC
113 #endif
114 
118 #ifdef __GNUC__
119 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
120 #else
121 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT
122 #endif
123 
127 #ifdef __GNUC__
128 # define STARPU_ATTRIBUTE_PURE __attribute__((pure))
129 #else
130 # define STARPU_ATTRIBUTE_PURE
131 #endif
132 
136 #ifdef __GNUC__
137 # define STARPU_ATTRIBUTE_ALIGNED(size) __attribute__((aligned(size)))
138 #else
139 # define STARPU_ATTRIBUTE_ALIGNED(size)
140 #endif
141 
142 #ifdef __GNUC__
143 # define STARPU_ATTRIBUTE_FORMAT(type, string, first) __attribute__((format(type, string, first)))
144 #else
145 # define STARPU_ATTRIBUTE_FORMAT(type, string, first)
146 #endif
147 
148 /* Note that if we're compiling C++, then just use the "inline"
149  keyword, since it's part of C++ */
150 #if defined(c_plusplus) || defined(__cplusplus)
151 # define STARPU_INLINE inline
152 #elif defined(_MSC_VER) || defined(__HP_cc)
153 # define STARPU_INLINE __inline
154 #else
155 # define STARPU_INLINE __inline__
156 #endif
157 
158 #if STARPU_GNUC_PREREQ(4, 3)
159 # define STARPU_ATTRIBUTE_CALLOC_SIZE(num,size) __attribute__((alloc_size(num,size)))
160 # define STARPU_ATTRIBUTE_ALLOC_SIZE(size) __attribute__((alloc_size(size)))
161 #else
162 # define STARPU_ATTRIBUTE_CALLOC_SIZE(num,size)
163 # define STARPU_ATTRIBUTE_ALLOC_SIZE(size)
164 #endif
165 
166 #if STARPU_GNUC_PREREQ(3, 1) && !defined(BUILDING_STARPU) && !defined(STARPU_USE_DEPRECATED_API) && !defined(STARPU_USE_DEPRECATED_ONE_ZERO_API)
167 #define STARPU_DEPRECATED __attribute__((__deprecated__))
168 #else
169 #define STARPU_DEPRECATED
170 #endif /* __GNUC__ */
171 
172 #if STARPU_GNUC_PREREQ(3,3)
173 #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
174 #else
175 #define STARPU_WARN_UNUSED_RESULT
176 #endif /* __GNUC__ */
177 
178 #define STARPU_BACKTRACE_LENGTH 32
179 #ifdef __GLIBC__
180 # define STARPU_DUMP_BACKTRACE() do { \
181  void *__ptrs[STARPU_BACKTRACE_LENGTH]; \
182  int __n = backtrace(__ptrs, STARPU_BACKTRACE_LENGTH); \
183  backtrace_symbols_fd(__ptrs, __n, 2); \
184 } while (0)
185 #else
186 # define STARPU_DUMP_BACKTRACE() do { } while (0)
187 #endif
188 
189 #ifdef STARPU_SIMGRID_MC
190 #define STARPU_SIMGRID_ASSERT(x) MC_assert(!!(x))
191 #else
192 #define STARPU_SIMGRID_ASSERT(x)
193 #endif
194 
199 #ifdef STARPU_NO_ASSERT
200 #define STARPU_ASSERT(x) do { if (0) { (void) (x); } } while(0)
201 #else
202 # if defined(__CUDACC__) || defined(STARPU_HAVE_WINDOWS)
203 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); STARPU_SIMGRID_ASSERT(0 && #x); *(int*)NULL = 0; } } while(0)
204 # else
205 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); STARPU_SIMGRID_ASSERT(0 && #x); assert(0 && #x); } } while (0)
206 # endif
207 #endif
208 
209 #ifdef STARPU_NO_ASSERT
210 #define STARPU_ASSERT_ACCESSIBLE(x) do { if (0) { (void) (x); } } while(0)
211 #else
212 #define STARPU_ASSERT_ACCESSIBLE(ptr) do { volatile char __c STARPU_ATTRIBUTE_UNUSED = *(char*) (ptr); } while(0)
213 #endif
214 
220 #ifdef STARPU_NO_ASSERT
221 #define STARPU_ASSERT_MSG(x, msg, ...) do { if (0) { (void) (x); (void) msg; } } while(0)
222 #else
223 # if defined(__INTEL_COMPILER)
224 # pragma warning disable 279 // otherwise icc triggers "warning #279: controlling expression is constant" (probably because of assert(0 && #x))
225 # endif
226 # if defined(__CUDACC__) || defined(STARPU_HAVE_WINDOWS)
227 # 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(0 && #x); *(int*)NULL = 0; }} while(0)
228 # else
229 # 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(0 && #x); assert(0 && #x); } } while(0)
230 # endif
231 #endif
232 
233 #ifdef __APPLE_CC__
234 # ifdef __clang_analyzer__
235 # define _starpu_abort() exit(42)
236 # else
237 # define _starpu_abort() *(volatile int*)NULL = 0
238 # endif
239 #else
240 # define _starpu_abort() abort()
241 #endif
242 
246 #define STARPU_ABORT() do { \
247  STARPU_DUMP_BACKTRACE(); \
248  fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \
249  _starpu_abort(); \
250 } while(0)
251 
257 #define STARPU_ABORT_MSG(msg, ...) do { \
258  STARPU_DUMP_BACKTRACE(); \
259  fprintf(stderr, "[starpu][abort][%s()@%s:%d] " msg "\n", __starpu_func__, __FILE__, __LINE__, ## __VA_ARGS__); \
260  _starpu_abort(); \
261 } while(0)
262 
263 #if defined(_MSC_VER)
264  #undef STARPU_HAVE_STRERROR_R
265 #endif
266 
267 #if defined(STARPU_HAVE_STRERROR_R)
268 #if (! defined(__GLIBC__) || !__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && (! defined(_GNU_SOURCE)))
269 /* XSI-compliant version of strerror_r returns an int */
270 # define starpu_strerror_r(errnum, buf, buflen) \
271  do \
272  { \
273  int _ret = strerror_r((errnum), (buf), (buflen)); \
274  STARPU_ASSERT(_ret == 0); \
275  } \
276  while (0)
277 #else
278 /* GNU-specific version of strerror_r returns a char * */
279 # define starpu_strerror_r(errnum, buf, buflen) \
280  do \
281  { \
282  char * const _user_buf = (buf); \
283  const size_t _user_buflen = (buflen); \
284  /* the GNU-specific behaviour when 'buf' == NULL cannot be emulated with the XSI-compliant version */ \
285  STARPU_ASSERT((buf) != NULL); \
286  char * _tmp_buf = strerror_r((errnum), _user_buf, _user_buflen); \
287  if (_tmp_buf != _user_buf) \
288  { \
289  if (_user_buflen > 0) \
290  { \
291  strncpy(_user_buf, _tmp_buf, _user_buflen-1); \
292  _user_buf[_user_buflen-1] = '\0'; \
293  } \
294  } \
295  } \
296  while (0)
297 #endif /* strerror_r ABI version */
298 #endif /* STARPU_HAVE_STRERROR_R */
299 
304 #if defined(STARPU_HAVE_STRERROR_R)
305 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
306  char xmessage[256]; starpu_strerror_r(-err, xmessage, 256); \
307  fprintf(stderr, "[starpu] Unexpected value: <%d:%s> returned for " message "\n", err, xmessage, ## __VA_ARGS__); \
308  STARPU_ABORT(); }}
309 #else
310 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
311  fprintf(stderr, "[starpu] Unexpected value: <%d> returned for " message "\n", err, ## __VA_ARGS__); \
312  STARPU_ABORT(); }}
313 #endif
314 
319 #if defined(STARPU_HAVE_STRERROR_R)
320 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
321  char xmessage[256]; starpu_strerror_r(-err, xmessage, 256); \
322  fprintf(stderr, "[starpu] Unexpected value: <%d!=%d:%s> returned for " message "\n", err, value, xmessage, ## __VA_ARGS__); \
323  STARPU_ABORT(); }}
324 #else
325 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
326  fprintf(stderr, "[starpu] Unexpected value: <%d != %d> returned for " message "\n", err, value, ## __VA_ARGS__); \
327  STARPU_ABORT(); }}
328 #endif
329 
330 /* Note: do not use _starpu_cmpxchg / _starpu_xchg / _starpu_cmpxchgl /
331  * _starpu_xchgl / _starpu_cmpxchg64 / _starpu_xchg64, which only
332  * assembly-hand-written fallbacks used when building with an old gcc.
333  * Rather use STARPU_VAL_COMPARE_AND_SWAP and STARPU_VAL_EXCHANGE available on
334  * all platforms with a recent-enough gcc */
335 
336 #if defined(__i386__) || defined(__x86_64__)
337 static __starpu_inline unsigned _starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
338 {
339  __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
340  return old;
341 }
342 #define STARPU_HAVE_CMPXCHG
343 static __starpu_inline unsigned _starpu_xchg(unsigned *ptr, unsigned 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_XCHG
350 
351 static __starpu_inline uint32_t _starpu_cmpxchg32(uint32_t *ptr, uint32_t old, uint32_t next)
352 {
353  __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
354  return old;
355 }
356 #define STARPU_HAVE_CMPXCHG32
357 static __starpu_inline uint32_t _starpu_xchg32(uint32_t *ptr, uint32_t next)
358 {
359  /* Note: xchg is always locked already */
360  __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
361  return next;
362 }
363 #define STARPU_HAVE_XCHG32
364 
365 #if defined(__i386__)
366 static __starpu_inline unsigned long _starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
367 {
368  __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
369  return old;
370 }
371 #define STARPU_HAVE_CMPXCHGL
372 static __starpu_inline unsigned long _starpu_xchgl(unsigned long *ptr, unsigned long next)
373 {
374  /* Note: xchg is always locked already */
375  __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
376  return next;
377 }
378 #define STARPU_HAVE_XCHGL
379 #endif
380 
381 #if defined(__x86_64__)
382 static __starpu_inline unsigned long _starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
383 {
384  __asm__ __volatile__("lock cmpxchgq %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
385  return old;
386 }
387 #define STARPU_HAVE_CMPXCHGL
388 static __starpu_inline unsigned long _starpu_xchgl(unsigned long *ptr, unsigned long next)
389 {
390  /* Note: xchg is always locked already */
391  __asm__ __volatile__("xchgq %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
392  return next;
393 }
394 #define STARPU_HAVE_XCHGL
395 #endif
396 
397 #if defined(__i386__)
398 static __starpu_inline uint64_t _starpu_cmpxchg64(uint64_t *ptr, uint64_t old, uint64_t next)
399 {
400  uint32_t next_hi = next >> 32;
401  uint32_t next_lo = next & 0xfffffffful;
402  __asm__ __volatile__("lock cmpxchg8b %1": "+A" (old), "+m" (*ptr) : "c" (next_hi), "b" (next_lo) : "memory");
403  return old;
404 }
405 #define STARPU_HAVE_CMPXCHG64
406 #endif
407 
408 #if defined(__x86_64__)
409 static __starpu_inline uint64_t _starpu_cmpxchg64(uint64_t *ptr, uint64_t old, uint64_t next)
410 {
411  __asm__ __volatile__("lock cmpxchgq %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
412  return old;
413 }
414 #define STARPU_HAVE_CMPXCHG64
415 static __starpu_inline uint64_t _starpu_xchg64(uint64_t *ptr, uint64_t next)
416 {
417  /* Note: xchg is always locked already */
418  __asm__ __volatile__("xchgq %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
419  return next;
420 }
421 #define STARPU_HAVE_XCHG64
422 #endif
423 
424 #endif
425 
426 #define STARPU_ATOMIC_SOMETHING(name,expr) \
427 static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
428 { \
429  unsigned old, next; \
430  while (1) \
431  { \
432  old = *ptr; \
433  next = expr; \
434  if (_starpu_cmpxchg(ptr, old, next) == old) \
435  break; \
436  }; \
437  return expr; \
438 }
439 #define STARPU_ATOMIC_SOMETHINGL(name,expr) \
440 static __starpu_inline unsigned long starpu_atomic_##name##l(unsigned long *ptr, unsigned long value) \
441 { \
442  unsigned long old, next; \
443  while (1) \
444  { \
445  old = *ptr; \
446  next = expr; \
447  if (_starpu_cmpxchgl(ptr, old, next) == old) \
448  break; \
449  }; \
450  return expr; \
451 }
452 #define STARPU_ATOMIC_SOMETHING64(name,expr) \
453 static __starpu_inline uint64_t starpu_atomic_##name##64(uint64_t *ptr, uint64_t value) \
454 { \
455  uint64_t old, next; \
456  while (1) \
457  { \
458  old = *ptr; \
459  next = expr; \
460  if (_starpu_cmpxchg64(ptr, old, next) == old) \
461  break; \
462  }; \
463  return expr; \
464 }
465 
466 /* Returns the new value */
467 #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
468 #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
469 #define STARPU_ATOMIC_ADDL(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
470 #define STARPU_ATOMIC_ADD64(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
471 #else
472 #if defined(STARPU_HAVE_CMPXCHG)
473 STARPU_ATOMIC_SOMETHING(add, old + value)
474 #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
475 #endif
476 #if defined(STARPU_HAVE_CMPXCHGL)
477 STARPU_ATOMIC_SOMETHINGL(add, old + value)
478 #define STARPU_ATOMIC_ADDL(ptr, value) starpu_atomic_addl(ptr, value)
479 #endif
480 #if defined(STARPU_HAVE_CMPXCHG64)
481 STARPU_ATOMIC_SOMETHING64(add, old + value)
482 #define STARPU_ATOMIC_ADD64(ptr, value) starpu_atomic_add64(ptr, value)
483 #endif
484 #endif
485 
486 #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
487 #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
488 #define STARPU_ATOMIC_ORL(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
489 #define STARPU_ATOMIC_OR64(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
490 #else
491 #if defined(STARPU_HAVE_CMPXCHG)
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_CMPXCHGL)
496 STARPU_ATOMIC_SOMETHINGL(or, old | value)
497 #define STARPU_ATOMIC_ORL(ptr, value) starpu_atomic_orl(ptr, value)
498 #endif
499 #if defined(STARPU_HAVE_CMPXCHG64)
500 STARPU_ATOMIC_SOMETHING64(or, old | value)
501 #define STARPU_ATOMIC_OR64(ptr, value) starpu_atomic_or64(ptr, value)
502 #endif
503 #endif
504 
505 /* Try to replace `old' with `value' at `ptr'. Returns true iff the swap was successful. */
506 #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
507 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
508 #define STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value)
509 #define STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value)
510 #else
511 #ifdef STARPU_HAVE_CMPXCHG
512 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (_starpu_cmpxchg((ptr), (old), (value)) == (old))
513 #endif
514 #ifdef STARPU_HAVE_CMPXCHG32
515 #define STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value) (_starpu_cmpxchg32((ptr), (old), (value)) == (old))
516 #endif
517 #ifdef STARPU_HAVE_CMPXCHG64
518 #define STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value) (_starpu_cmpxchg64((ptr), (old), (value)) == (old))
519 #endif
520 #endif
521 
522 #if UINTPTR_MAX == UINT64_MAX
523 #define STARPU_BOOL_COMPARE_AND_SWAP_PTR(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value)
524 #else
525 #define STARPU_BOOL_COMPARE_AND_SWAP_PTR(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value)
526 #endif
527 
528 /* Try to replace `old' with `value' at `ptr'. Returns the value actually seen at `ptr'. */
529 #ifdef STARPU_HAVE_SYNC_VAL_COMPARE_AND_SWAP
530 #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value)))
531 #define STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value)
532 #define STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value)
533 #else
534 #ifdef STARPU_HAVE_CMPXCHG
535 #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (_starpu_cmpxchg((ptr), (old), (value)))
536 #endif
537 #ifdef STARPU_HAVE_CMPXCHG32
538 #define STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value) (_starpu_cmpxchg32((ptr), (old), (value)))
539 #endif
540 #ifdef STARPU_HAVE_CMPXCHG64
541 #define STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value) (_starpu_cmpxchg64((ptr), (old), (value)))
542 #endif
543 #endif
544 
545 #if UINTPTR_MAX == UINT64_MAX
546 #define STARPU_VAL_COMPARE_AND_SWAP_PTR(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value)
547 #else
548 #define STARPU_VAL_COMPARE_AND_SWAP_PTR(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value)
549 #endif
550 
551 #ifdef STARPU_HAVE_ATOMIC_EXCHANGE_N
552 #define STARPU_VAL_EXCHANGE(ptr, value) (__atomic_exchange_n((ptr), (value), __ATOMIC_SEQ_CST))
553 #define STARPU_VAL_EXCHANGEL(ptr, value) STARPU_VAL_EXCHANGE((ptr) (value))
554 #define STARPU_VAL_EXCHANGE32(ptr, value) STARPU_VAL_EXCHANGE((ptr) (value))
555 #define STARPU_VAL_EXCHANGE64(ptr, value) STARPU_VAL_EXCHANGE((ptr) (value))
556 #else
557 #ifdef STARPU_HAVE_XCHG
558 #define STARPU_VAL_EXCHANGE(ptr, value) (_starpu_xchg((ptr), (value)))
559 #endif
560 #ifdef STARPU_HAVE_XCHGL
561 #define STARPU_VAL_EXCHANGEL(ptr, value) (_starpu_xchgl((ptr), (value)))
562 #endif
563 #ifdef STARPU_HAVE_XCHG32
564 #define STARPU_VAL_EXCHANGE32(ptr, value) (_starpu_xchg32((ptr), (value)))
565 #endif
566 #ifdef STARPU_HAVE_XCHG64
567 #define STARPU_VAL_EXCHANGE64(ptr, value) (_starpu_xchg64((ptr), (value)))
568 #endif
569 #endif
570 
571 /* Returns the previous value */
572 #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
573 #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
574 #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
575 #elif defined(STARPU_HAVE_XCHG)
576 #define STARPU_TEST_AND_SET(ptr, value) (_starpu_xchg((ptr), (value)))
577 #define STARPU_RELEASE(ptr) (_starpu_xchg((ptr), 0))
578 #endif
579 
580 #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
581 #define STARPU_SYNCHRONIZE() __sync_synchronize()
582 #elif defined(__i386__)
583 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
584 #elif defined(__KNC__) || defined(__KNF__)
585 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
586 #elif defined(__x86_64__)
587 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
588 #elif defined(__ppc__) || defined(__ppc64__)
589 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
590 #endif
591 
595 #if defined(__x86_64__)
596 #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
597 #elif defined(__aarch64__)
598 #define STARPU_RMB() __asm__ __volatile__("dsb ld" ::: "memory")
599 #else
600 #define STARPU_RMB() STARPU_SYNCHRONIZE()
601 #endif
602 
606 #if defined(__x86_64__)
607 #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
608 #elif defined(__aarch64__)
609 #define STARPU_WMB() __asm__ __volatile__("dsb st" ::: "memory")
610 #else
611 #define STARPU_WMB() STARPU_SYNCHRONIZE()
612 #endif
613 
614 #if defined(__i386__) || defined(__x86_64__)
615 #define STARPU_CACHELINE_SIZE 64
616 #elif defined(__ppc__) || defined(__ppc64__) || defined(__ia64__)
617 #define STARPU_CACHELINE_SIZE 128
618 #elif defined(__s390__) || defined(__s390x__)
619 #define STARPU_CACHELINE_SIZE 256
620 #else
621 /* Conservative default */
622 #define STARPU_CACHELINE_SIZE 1024
623 #endif
624 
625 #ifdef _WIN32
626 /* Try to fetch the system definition of timespec */
627 #include <sys/types.h>
628 #include <sys/stat.h>
629 #ifdef HAVE_UNISTD_H
630 #include <unistd.h>
631 #endif
632 #include <time.h>
633 #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
634 #include <pthread.h>
635 #endif
636 #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || (defined(_MSC_VER) && _MSC_VER < 1900)
637 /* If it didn't get defined in the standard places, then define it ourself */
638 #ifndef STARPU_TIMESPEC_DEFINED
639 #define STARPU_TIMESPEC_DEFINED 1
640 struct timespec
641 {
642  time_t tv_sec; /* Seconds */
643  long tv_nsec; /* Nanoseconds */
644 };
645 #endif /* STARPU_TIMESPEC_DEFINED */
646 #endif /* STARPU_HAVE_STRUCT_TIMESPEC */
647 /* Fetch gettimeofday on mingw/cygwin */
648 #if defined(__MINGW32__) || defined(__CYGWIN__)
649 #include <sys/time.h>
650 #endif
651 #else
652 #include <sys/time.h>
653 #endif /* _WIN32 */
654 
657 #ifdef __cplusplus
658 }
659 #endif
660 
661 #endif /* __STARPU_UTIL_H__ */