19 #ifndef __STARPU_UTIL_H__
20 #define __STARPU_UTIL_H__
34 #ifdef STARPU_SIMGRID_MC
35 #include <simgrid/modelchecker.h>
54 #if defined __GNUC__ && defined __GNUC_MINOR__
55 # define STARPU_GNUC_PREREQ(maj, min) \
56 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
58 # define STARPU_GNUC_PREREQ(maj, min) 0
66 # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
68 # define STARPU_UNLIKELY(expr) (expr)
76 # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
78 # define STARPU_LIKELY(expr) (expr)
85 # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
87 # define STARPU_ATTRIBUTE_UNUSED
94 # define STARPU_ATTRIBUTE_NORETURN __attribute__((noreturn))
96 # define STARPU_ATTRIBUTE_NORETURN
103 # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
105 # define STARPU_ATTRIBUTE_INTERNAL
112 # define STARPU_ATTRIBUTE_MALLOC __attribute__((malloc))
114 # define STARPU_ATTRIBUTE_MALLOC
121 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
123 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT
130 # define STARPU_ATTRIBUTE_PURE __attribute__((pure))
132 # define STARPU_ATTRIBUTE_PURE
139 # define STARPU_ATTRIBUTE_ALIGNED(size) __attribute__((aligned(size)))
141 # define STARPU_ATTRIBUTE_ALIGNED(size)
145 # define STARPU_ATTRIBUTE_FORMAT(type, string, first) __attribute__((format(type, string, first)))
147 # define STARPU_ATTRIBUTE_FORMAT(type, string, first)
152 #if defined(c_plusplus) || defined(__cplusplus)
153 # define STARPU_INLINE inline
154 #elif defined(_MSC_VER) || defined(__HP_cc)
155 # define STARPU_INLINE __inline
157 # define STARPU_INLINE __inline__
160 #if STARPU_GNUC_PREREQ(4, 3)
161 # define STARPU_ATTRIBUTE_CALLOC_SIZE(num,size) __attribute__((alloc_size(num,size)))
162 # define STARPU_ATTRIBUTE_ALLOC_SIZE(size) __attribute__((alloc_size(size)))
164 # define STARPU_ATTRIBUTE_CALLOC_SIZE(num,size)
165 # define STARPU_ATTRIBUTE_ALLOC_SIZE(size)
168 #if STARPU_GNUC_PREREQ(3, 1) && !defined(BUILDING_STARPU) && !defined(STARPU_USE_DEPRECATED_API) && !defined(STARPU_USE_DEPRECATED_ONE_ZERO_API)
169 #define STARPU_DEPRECATED __attribute__((__deprecated__))
171 #define STARPU_DEPRECATED
174 #if STARPU_GNUC_PREREQ(3,3)
175 #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
177 #define STARPU_WARN_UNUSED_RESULT
180 #define STARPU_BACKTRACE_LENGTH 32
182 # define STARPU_DUMP_BACKTRACE() do { \
183 void *__ptrs[STARPU_BACKTRACE_LENGTH]; \
184 int __n = backtrace(__ptrs, STARPU_BACKTRACE_LENGTH); \
185 backtrace_symbols_fd(__ptrs, __n, 2); \
188 # define STARPU_DUMP_BACKTRACE() do { } while (0)
191 #ifdef STARPU_SIMGRID_MC
192 #define STARPU_SIMGRID_ASSERT(x) MC_assert(!!(x))
194 #define STARPU_SIMGRID_ASSERT(x)
201 #ifdef STARPU_NO_ASSERT
202 #define STARPU_ASSERT(x) do { if (0) { (void) (x); } } while(0)
204 # if defined(__CUDACC__) || defined(STARPU_HAVE_WINDOWS)
205 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); STARPU_SIMGRID_ASSERT(x); *(int*)NULL = 0; } } while(0)
207 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); STARPU_SIMGRID_ASSERT(x); assert(x); } } while (0)
211 #ifdef STARPU_NO_ASSERT
212 #define STARPU_ASSERT_ACCESSIBLE(x) do { if (0) { (void) (x); } } while(0)
214 #define STARPU_ASSERT_ACCESSIBLE(ptr) do { volatile char __c STARPU_ATTRIBUTE_UNUSED = *(char*) (ptr); } while(0)
222 #ifdef STARPU_NO_ASSERT
223 #define STARPU_ASSERT_MSG(x, msg, ...) do { if (0) { (void) (x); (void) msg; } } while(0)
225 # if defined(__CUDACC__) || defined(STARPU_HAVE_WINDOWS)
226 # 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)
228 # 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)
233 # ifdef __clang_analyzer__
234 # define _starpu_abort() exit(42)
236 # define _starpu_abort() *(volatile int*)NULL = 0
239 # define _starpu_abort() abort()
245 #define STARPU_ABORT() do { \
246 STARPU_DUMP_BACKTRACE(); \
247 fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \
256 #define STARPU_ABORT_MSG(msg, ...) do { \
257 STARPU_DUMP_BACKTRACE(); \
258 fprintf(stderr, "[starpu][abort][%s()@%s:%d] " msg "\n", __starpu_func__, __FILE__, __LINE__, ## __VA_ARGS__); \
262 #if defined(STARPU_HAVE_STRERROR_R)
263 #if (! defined(__GLIBC__) || !__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && (! defined(_GNU_SOURCE)))
265 # define starpu_strerror_r(errnum, buf, buflen) \
268 int _ret = strerror_r((errnum), (buf), (buflen)); \
269 STARPU_ASSERT(_ret == 0); \
274 # define starpu_strerror_r(errnum, buf, buflen) \
277 char * const _user_buf = (buf); \
278 const size_t _user_buflen = (buflen); \
280 STARPU_ASSERT((buf) != NULL); \
281 char * _tmp_buf = strerror_r((errnum), _user_buf, _user_buflen); \
282 if (_tmp_buf != _user_buf) \
284 if (_user_buflen > 0) \
286 strncpy(_user_buf, _tmp_buf, _user_buflen-1); \
287 _user_buf[_user_buflen-1] = '\0'; \
299 #if defined(STARPU_HAVE_STRERROR_R)
300 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
301 char xmessage[256]; starpu_strerror_r(-err, xmessage, 256); \
302 fprintf(stderr, "[starpu] Unexpected value: <%d:%s> returned for " message "\n", err, xmessage, ## __VA_ARGS__); \
305 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
306 fprintf(stderr, "[starpu] Unexpected value: <%d> returned for " message "\n", err, ## __VA_ARGS__); \
314 #if defined(STARPU_HAVE_STRERROR_R)
315 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
316 char xmessage[256]; starpu_strerror_r(-err, xmessage, 256); \
317 fprintf(stderr, "[starpu] Unexpected value: <%d!=%d:%s> returned for " message "\n", err, value, xmessage, ## __VA_ARGS__); \
320 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
321 fprintf(stderr, "[starpu] Unexpected value: <%d != %d> returned for " message "\n", err, value, ## __VA_ARGS__); \
331 #if defined(__i386__) || defined(__x86_64__)
332 static __starpu_inline
unsigned starpu_cmpxchg(
unsigned *ptr,
unsigned old,
unsigned next)
334 __asm__ __volatile__(
"lock cmpxchgl %2,%1":
"+a" (old),
"+m" (*ptr) :
"q" (next) :
"memory");
337 #define STARPU_HAVE_CMPXCHG
338 static __starpu_inline
unsigned starpu_xchg(
unsigned *ptr,
unsigned next)
341 __asm__ __volatile__(
"xchgl %1,%0":
"+m" (*ptr),
"+q" (next) : :
"memory");
344 #define STARPU_HAVE_XCHG
346 static __starpu_inline uint32_t starpu_cmpxchg32(uint32_t *ptr, uint32_t old, uint32_t next)
348 __asm__ __volatile__(
"lock cmpxchgl %2,%1":
"+a" (old),
"+m" (*ptr) :
"q" (next) :
"memory");
351 #define STARPU_HAVE_CMPXCHG32
352 static __starpu_inline uint32_t starpu_xchg32(uint32_t *ptr, uint32_t next)
355 __asm__ __volatile__(
"xchgl %1,%0":
"+m" (*ptr),
"+q" (next) : :
"memory");
358 #define STARPU_HAVE_XCHG32
360 #if defined(__i386__)
361 static __starpu_inline
unsigned long starpu_cmpxchgl(
unsigned long *ptr,
unsigned long old,
unsigned long next)
363 __asm__ __volatile__(
"lock cmpxchgl %2,%1":
"+a" (old),
"+m" (*ptr) :
"q" (next) :
"memory");
366 #define STARPU_HAVE_CMPXCHGL
367 static __starpu_inline
unsigned long starpu_xchgl(
unsigned long *ptr,
unsigned long next)
370 __asm__ __volatile__(
"xchgl %1,%0":
"+m" (*ptr),
"+q" (next) : :
"memory");
373 #define STARPU_HAVE_XCHGL
376 #if defined(__x86_64__)
377 static __starpu_inline
unsigned long starpu_cmpxchgl(
unsigned long *ptr,
unsigned long old,
unsigned long next)
379 __asm__ __volatile__(
"lock cmpxchgq %2,%1":
"+a" (old),
"+m" (*ptr) :
"q" (next) :
"memory");
382 #define STARPU_HAVE_CMPXCHGL
383 static __starpu_inline
unsigned long starpu_xchgl(
unsigned long *ptr,
unsigned long next)
386 __asm__ __volatile__(
"xchgq %1,%0":
"+m" (*ptr),
"+q" (next) : :
"memory");
389 #define STARPU_HAVE_XCHGL
392 #if defined(__i386__)
393 static __starpu_inline uint64_t starpu_cmpxchg64(uint64_t *ptr, uint64_t old, uint64_t next)
395 uint32_t next_hi = next >> 32;
396 uint32_t next_lo = next & 0xfffffffful;
397 __asm__ __volatile__(
"lock cmpxchg8b %1":
"+A" (old),
"+m" (*ptr) :
"c" (next_hi),
"b" (next_lo) :
"memory");
400 #define STARPU_HAVE_CMPXCHG64
403 #if defined(__x86_64__)
404 static __starpu_inline uint64_t starpu_cmpxchg64(uint64_t *ptr, uint64_t old, uint64_t next)
406 __asm__ __volatile__(
"lock cmpxchgq %2,%1":
"+a" (old),
"+m" (*ptr) :
"q" (next) :
"memory");
409 #define STARPU_HAVE_CMPXCHG64
410 static __starpu_inline uint64_t starpu_xchg64(uint64_t *ptr, uint64_t next)
413 __asm__ __volatile__(
"xchgq %1,%0":
"+m" (*ptr),
"+q" (next) : :
"memory");
416 #define STARPU_HAVE_XCHG64
421 #define STARPU_ATOMIC_SOMETHING(name,expr) \
422 static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
424 unsigned old, next; \
429 if (starpu_cmpxchg(ptr, old, next) == old) \
434 #define STARPU_ATOMIC_SOMETHINGL(name,expr) \
435 static __starpu_inline unsigned long starpu_atomic_##name##l(unsigned long *ptr, unsigned long value) \
437 unsigned long old, next; \
442 if (starpu_cmpxchgl(ptr, old, next) == old) \
447 #define STARPU_ATOMIC_SOMETHING64(name,expr) \
448 static __starpu_inline uint64_t starpu_atomic_##name##64(uint64_t *ptr, uint64_t value) \
450 uint64_t old, next; \
455 if (starpu_cmpxchg64(ptr, old, next) == old) \
462 #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
463 #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
464 #define STARPU_ATOMIC_ADDL(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
465 #define STARPU_ATOMIC_ADD64(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
467 #if defined(STARPU_HAVE_CMPXCHG)
468 STARPU_ATOMIC_SOMETHING(add, old + value)
469 #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
471 #if defined(STARPU_HAVE_CMPXCHGL)
472 STARPU_ATOMIC_SOMETHINGL(add, old + value)
473 #define STARPU_ATOMIC_ADDL(ptr, value) starpu_atomic_addl(ptr, value)
475 #if defined(STARPU_HAVE_CMPXCHG64)
476 STARPU_ATOMIC_SOMETHING64(add, old + value)
477 #define STARPU_ATOMIC_ADD64(ptr, value) starpu_atomic_add64(ptr, value)
481 #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
482 #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
483 #define STARPU_ATOMIC_ORL(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
484 #define STARPU_ATOMIC_OR64(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
486 #if defined(STARPU_HAVE_CMPXCHG)
487 STARPU_ATOMIC_SOMETHING(or, old | value)
488 #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
490 #if defined(STARPU_HAVE_CMPXCHGL)
491 STARPU_ATOMIC_SOMETHINGL(or, old | value)
492 #define STARPU_ATOMIC_ORL(ptr, value) starpu_atomic_orl(ptr, value)
494 #if defined(STARPU_HAVE_CMPXCHG64)
495 STARPU_ATOMIC_SOMETHING64(or, old | value)
496 #define STARPU_ATOMIC_OR64(ptr, value) starpu_atomic_or64(ptr, value)
500 #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
501 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
502 #define STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value)
503 #define STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value) STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value)
505 #ifdef STARPU_HAVE_CMPXCHG
506 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
508 #ifdef STARPU_HAVE_CMPXCHG32
509 #define STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value) (starpu_cmpxchg32((ptr), (old), (value)) == (old))
511 #ifdef STARPU_HAVE_CMPXCHG64
512 #define STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value) (starpu_cmpxchg64((ptr), (old), (value)) == (old))
516 #ifdef STARPU_HAVE_SYNC_VAL_COMPARE_AND_SWAP
517 #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value)))
518 #define STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value)
519 #define STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value) STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value)
521 #ifdef STARPU_HAVE_CMPXCHG
522 #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)))
524 #ifdef STARPU_HAVE_CMPXCHG32
525 #define STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value) (starpu_cmpxchg32((ptr), (old), (value)))
527 #ifdef STARPU_HAVE_CMPXCHG64
528 #define STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value) (starpu_cmpxchg64((ptr), (old), (value)))
533 #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
534 #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
535 #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
536 #elif defined(STARPU_HAVE_XCHG)
537 #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
538 #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
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")
556 #if defined(__i386__)
557 #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
558 #elif defined(__KNC__) || defined(__KNF__)
559 #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
560 #elif defined(__x86_64__)
561 #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
562 #elif defined(__ppc__) || defined(__ppc64__)
563 #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
565 #define STARPU_RMB() STARPU_SYNCHRONIZE()
571 #if defined(__i386__)
572 #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
573 #elif defined(__KNC__) || defined(__KNF__)
574 #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
575 #elif defined(__x86_64__)
576 #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
577 #elif defined(__ppc__) || defined(__ppc64__)
578 #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
580 #define STARPU_WMB() STARPU_SYNCHRONIZE()
585 #include <sys/types.h>
586 #include <sys/stat.h>
591 #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
594 #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || (defined(_MSC_VER) && _MSC_VER < 1900)
596 #ifndef STARPU_TIMESPEC_DEFINED
597 #define STARPU_TIMESPEC_DEFINED 1
606 #if defined(__MINGW32__) || defined(__CYGWIN__)
607 #include <sys/time.h>
610 #include <sys/time.h>