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-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 
19 #ifndef __STARPU_UTIL_H__
20 #define __STARPU_UTIL_H__
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <assert.h>
27 
28 #include <starpu_config.h>
29 
30 #ifdef __GLIBC__
31 #include <execinfo.h>
32 #endif
33 
34 #ifdef STARPU_SIMGRID_MC
35 #include <simgrid/modelchecker.h>
36 #endif
37 
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #endif
42 
54 #if defined __GNUC__ && defined __GNUC_MINOR__
55 # define STARPU_GNUC_PREREQ(maj, min) \
56  ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
57 #else
58 # define STARPU_GNUC_PREREQ(maj, min) 0
59 #endif
60 
65 #ifdef __GNUC__
66 # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
67 #else
68 # define STARPU_UNLIKELY(expr) (expr)
69 #endif
70 
75 #ifdef __GNUC__
76 # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
77 #else
78 # define STARPU_LIKELY(expr) (expr)
79 #endif
80 
84 #ifdef __GNUC__
85 # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
86 #else
87 # define STARPU_ATTRIBUTE_UNUSED
88 #endif
89 
93 #ifdef __GNUC__
94 # define STARPU_ATTRIBUTE_NORETURN __attribute__((noreturn))
95 #else
96 # define STARPU_ATTRIBUTE_NORETURN
97 #endif
98 
102 #ifdef __GNUC__
103 # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
104 #else
105 # define STARPU_ATTRIBUTE_INTERNAL
106 #endif
107 
111 #ifdef __GNUC__
112 # define STARPU_ATTRIBUTE_MALLOC __attribute__((malloc))
113 #else
114 # define STARPU_ATTRIBUTE_MALLOC
115 #endif
116 
120 #ifdef __GNUC__
121 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
122 #else
123 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT
124 #endif
125 
129 #ifdef __GNUC__
130 # define STARPU_ATTRIBUTE_PURE __attribute__((pure))
131 #else
132 # define STARPU_ATTRIBUTE_PURE
133 #endif
134 
138 #ifdef __GNUC__
139 # define STARPU_ATTRIBUTE_ALIGNED(size) __attribute__((aligned(size)))
140 #else
141 # define STARPU_ATTRIBUTE_ALIGNED(size)
142 #endif
143 
144 #ifdef __GNUC__
145 # define STARPU_ATTRIBUTE_FORMAT(type, string, first) __attribute__((format(type, string, first)))
146 #else
147 # define STARPU_ATTRIBUTE_FORMAT(type, string, first)
148 #endif
149 
150 /* Note that if we're compiling C++, then just use the "inline"
151  keyword, since it's part of C++ */
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
156 #else
157 # define STARPU_INLINE __inline__
158 #endif
159 
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)))
163 #else
164 # define STARPU_ATTRIBUTE_CALLOC_SIZE(num,size)
165 # define STARPU_ATTRIBUTE_ALLOC_SIZE(size)
166 #endif
167 
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__))
170 #else
171 #define STARPU_DEPRECATED
172 #endif /* __GNUC__ */
173 
174 #if STARPU_GNUC_PREREQ(3,3)
175 #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
176 #else
177 #define STARPU_WARN_UNUSED_RESULT
178 #endif /* __GNUC__ */
179 
180 #define STARPU_BACKTRACE_LENGTH 32
181 #ifdef __GLIBC__
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); \
186 } while (0)
187 #else
188 # define STARPU_DUMP_BACKTRACE() do { } while (0)
189 #endif
190 
191 #ifdef STARPU_SIMGRID_MC
192 #define STARPU_SIMGRID_ASSERT(x) MC_assert(!!(x))
193 #else
194 #define STARPU_SIMGRID_ASSERT(x)
195 #endif
196 
201 #ifdef STARPU_NO_ASSERT
202 #define STARPU_ASSERT(x) do { if (0) { (void) (x); } } while(0)
203 #else
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)
206 # else
207 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); STARPU_SIMGRID_ASSERT(x); assert(x); } } while (0)
208 # endif
209 #endif
210 
211 #ifdef STARPU_NO_ASSERT
212 #define STARPU_ASSERT_ACCESSIBLE(x) do { if (0) { (void) (x); } } while(0)
213 #else
214 #define STARPU_ASSERT_ACCESSIBLE(ptr) do { volatile char __c STARPU_ATTRIBUTE_UNUSED = *(char*) (ptr); } while(0)
215 #endif
216 
222 #ifdef STARPU_NO_ASSERT
223 #define STARPU_ASSERT_MSG(x, msg, ...) do { if (0) { (void) (x); (void) msg; } } while(0)
224 #else
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)
227 # else
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)
229 # endif
230 #endif
231 
232 #ifdef __APPLE_CC__
233 # ifdef __clang_analyzer__
234 # define _starpu_abort() exit(42)
235 # else
236 # define _starpu_abort() *(volatile int*)NULL = 0
237 # endif
238 #else
239 # define _starpu_abort() abort()
240 #endif
241 
245 #define STARPU_ABORT() do { \
246  STARPU_DUMP_BACKTRACE(); \
247  fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \
248  _starpu_abort(); \
249 } while(0)
250 
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__); \
259  _starpu_abort(); \
260 } while(0)
261 
262 #if defined(STARPU_HAVE_STRERROR_R)
263 #if (! defined(__GLIBC__) || !__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && (! defined(_GNU_SOURCE)))
264 /* XSI-compliant version of strerror_r returns an int */
265 # define starpu_strerror_r(errnum, buf, buflen) \
266  do \
267  { \
268  int _ret = strerror_r((errnum), (buf), (buflen)); \
269  STARPU_ASSERT(_ret == 0); \
270  } \
271  while (0)
272 #else
273 /* GNU-specific version of strerror_r returns a char * */
274 # define starpu_strerror_r(errnum, buf, buflen) \
275  do \
276  { \
277  char * const _user_buf = (buf); \
278  const size_t _user_buflen = (buflen); \
279  /* the GNU-specific behaviour when 'buf' == NULL cannot be emulated with the XSI-compliant version */ \
280  STARPU_ASSERT((buf) != NULL); \
281  char * _tmp_buf = strerror_r((errnum), _user_buf, _user_buflen); \
282  if (_tmp_buf != _user_buf) \
283  { \
284  if (_user_buflen > 0) \
285  { \
286  strncpy(_user_buf, _tmp_buf, _user_buflen-1); \
287  _user_buf[_user_buflen-1] = '\0'; \
288  } \
289  } \
290  } \
291  while (0)
292 #endif /* strerror_r ABI version */
293 #endif /* STARPU_HAVE_STRERROR_R */
294 
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__); \
303  STARPU_ABORT(); }}
304 #else
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__); \
307  STARPU_ABORT(); }}
308 #endif
309 
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__); \
318  STARPU_ABORT(); }}
319 #else
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__); \
322  STARPU_ABORT(); }}
323 #endif
324 
325 /* Note: do not use starpu_cmpxchg / starpu_xchg / starpu_cmpxchgl /
326  * starpu_xchgl / starpu_cmpxchg64 / starpu_xchg64, which only
327  * assembly-hand-written fallbacks used when building with an old gcc.
328  * Rather use STARPU_VAL_COMPARE_AND_SWAP available on all platforms with a
329  * recent-enough gcc */
330 
331 #if defined(__i386__) || defined(__x86_64__)
332 static __starpu_inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
333 {
334  __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
335  return old;
336 }
337 #define STARPU_HAVE_CMPXCHG
338 static __starpu_inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
339 {
340  /* Note: xchg is always locked already */
341  __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
342  return next;
343 }
344 #define STARPU_HAVE_XCHG
345 
346 static __starpu_inline uint32_t starpu_cmpxchg32(uint32_t *ptr, uint32_t old, uint32_t next)
347 {
348  __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
349  return old;
350 }
351 #define STARPU_HAVE_CMPXCHG32
352 static __starpu_inline uint32_t starpu_xchg32(uint32_t *ptr, uint32_t next)
353 {
354  /* Note: xchg is always locked already */
355  __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
356  return next;
357 }
358 #define STARPU_HAVE_XCHG32
359 
360 #if defined(__i386__)
361 static __starpu_inline unsigned long starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
362 {
363  __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
364  return old;
365 }
366 #define STARPU_HAVE_CMPXCHGL
367 static __starpu_inline unsigned long starpu_xchgl(unsigned long *ptr, unsigned long next)
368 {
369  /* Note: xchg is always locked already */
370  __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
371  return next;
372 }
373 #define STARPU_HAVE_XCHGL
374 #endif
375 
376 #if defined(__x86_64__)
377 static __starpu_inline unsigned long starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
378 {
379  __asm__ __volatile__("lock cmpxchgq %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
380  return old;
381 }
382 #define STARPU_HAVE_CMPXCHGL
383 static __starpu_inline unsigned long starpu_xchgl(unsigned long *ptr, unsigned long next)
384 {
385  /* Note: xchg is always locked already */
386  __asm__ __volatile__("xchgq %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
387  return next;
388 }
389 #define STARPU_HAVE_XCHGL
390 #endif
391 
392 #if defined(__i386__)
393 static __starpu_inline uint64_t starpu_cmpxchg64(uint64_t *ptr, uint64_t old, uint64_t next)
394 {
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");
398  return old;
399 }
400 #define STARPU_HAVE_CMPXCHG64
401 #endif
402 
403 #if defined(__x86_64__)
404 static __starpu_inline uint64_t starpu_cmpxchg64(uint64_t *ptr, uint64_t old, uint64_t next)
405 {
406  __asm__ __volatile__("lock cmpxchgq %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
407  return old;
408 }
409 #define STARPU_HAVE_CMPXCHG64
410 static __starpu_inline uint64_t starpu_xchg64(uint64_t *ptr, uint64_t next)
411 {
412  /* Note: xchg is always locked already */
413  __asm__ __volatile__("xchgq %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
414  return next;
415 }
416 #define STARPU_HAVE_XCHG64
417 #endif
418 
419 #endif
420 
421 #define STARPU_ATOMIC_SOMETHING(name,expr) \
422 static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
423 { \
424  unsigned old, next; \
425  while (1) \
426  { \
427  old = *ptr; \
428  next = expr; \
429  if (starpu_cmpxchg(ptr, old, next) == old) \
430  break; \
431  }; \
432  return expr; \
433 }
434 #define STARPU_ATOMIC_SOMETHINGL(name,expr) \
435 static __starpu_inline unsigned long starpu_atomic_##name##l(unsigned long *ptr, unsigned long value) \
436 { \
437  unsigned long old, next; \
438  while (1) \
439  { \
440  old = *ptr; \
441  next = expr; \
442  if (starpu_cmpxchgl(ptr, old, next) == old) \
443  break; \
444  }; \
445  return expr; \
446 }
447 #define STARPU_ATOMIC_SOMETHING64(name,expr) \
448 static __starpu_inline uint64_t starpu_atomic_##name##64(uint64_t *ptr, uint64_t value) \
449 { \
450  uint64_t old, next; \
451  while (1) \
452  { \
453  old = *ptr; \
454  next = expr; \
455  if (starpu_cmpxchg64(ptr, old, next) == old) \
456  break; \
457  }; \
458  return expr; \
459 }
460 
461 /* Returns the new value */
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))
466 #else
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)
470 #endif
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)
474 #endif
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)
478 #endif
479 #endif
480 
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)))
485 #else
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)
489 #endif
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)
493 #endif
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)
497 #endif
498 #endif
499 
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)
504 #else
505 #ifdef STARPU_HAVE_CMPXCHG
506 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
507 #endif
508 #ifdef STARPU_HAVE_CMPXCHG32
509 #define STARPU_BOOL_COMPARE_AND_SWAP32(ptr, old, value) (starpu_cmpxchg32((ptr), (old), (value)) == (old))
510 #endif
511 #ifdef STARPU_HAVE_CMPXCHG64
512 #define STARPU_BOOL_COMPARE_AND_SWAP64(ptr, old, value) (starpu_cmpxchg64((ptr), (old), (value)) == (old))
513 #endif
514 #endif
515 
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)
520 #else
521 #ifdef STARPU_HAVE_CMPXCHG
522 #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)))
523 #endif
524 #ifdef STARPU_HAVE_CMPXCHG32
525 #define STARPU_VAL_COMPARE_AND_SWAP32(ptr, old, value) (starpu_cmpxchg32((ptr), (old), (value)))
526 #endif
527 #ifdef STARPU_HAVE_CMPXCHG64
528 #define STARPU_VAL_COMPARE_AND_SWAP64(ptr, old, value) (starpu_cmpxchg64((ptr), (old), (value)))
529 #endif
530 #endif
531 
532 /* Returns the previous 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))
539 #endif
540 
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")
551 #endif
552 
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")
564 #else
565 #define STARPU_RMB() STARPU_SYNCHRONIZE()
566 #endif
567 
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")
579 #else
580 #define STARPU_WMB() STARPU_SYNCHRONIZE()
581 #endif
582 
583 #ifdef _WIN32
584 /* Try to fetch the system definition of timespec */
585 #include <sys/types.h>
586 #include <sys/stat.h>
587 #ifdef HAVE_UNISTD_H
588 #include <unistd.h>
589 #endif
590 #include <time.h>
591 #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
592 #include <pthread.h>
593 #endif
594 #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || (defined(_MSC_VER) && _MSC_VER < 1900)
595 /* If it didn't get defined in the standard places, then define it ourself */
596 #ifndef STARPU_TIMESPEC_DEFINED
597 #define STARPU_TIMESPEC_DEFINED 1
598 struct timespec
599 {
600  time_t tv_sec; /* Seconds */
601  long tv_nsec; /* Nanoseconds */
602 };
603 #endif /* STARPU_TIMESPEC_DEFINED */
604 #endif /* STARPU_HAVE_STRUCT_TIMESPEC */
605 /* Fetch gettimeofday on mingw/cygwin */
606 #if defined(__MINGW32__) || defined(__CYGWIN__)
607 #include <sys/time.h>
608 #endif
609 #else
610 #include <sys/time.h>
611 #endif /* _WIN32 */
612 
615 #ifdef __cplusplus
616 }
617 #endif
618 
619 #endif /* __STARPU_UTIL_H__ */