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