Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1 | #include <pthread.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <unistd.h> |
| 5 | #include <dlfcn.h> |
| 6 | #include <stddef.h> |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 7 | #include <sched.h> |
| 8 | #include <stdarg.h> |
| 9 | |
| 10 | #ifdef __APPLE__ |
| 11 | #include <mach/mach_time.h> |
| 12 | #endif |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 13 | |
| 14 | // TSan-invisible barrier. |
| 15 | // Tests use it to establish necessary execution order in a way that does not |
| 16 | // interfere with tsan (does not establish synchronization between threads). |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 17 | typedef unsigned long long invisible_barrier_t; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 18 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | void __tsan_testonly_barrier_init(invisible_barrier_t *barrier, |
| 23 | unsigned count); |
| 24 | void __tsan_testonly_barrier_wait(invisible_barrier_t *barrier); |
| 25 | #ifdef __cplusplus |
| 26 | } |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 27 | #endif |
| 28 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 29 | static inline void barrier_init(invisible_barrier_t *barrier, unsigned count) { |
| 30 | __tsan_testonly_barrier_init(barrier, count); |
| 31 | } |
| 32 | |
| 33 | static inline void barrier_wait(invisible_barrier_t *barrier) { |
| 34 | __tsan_testonly_barrier_wait(barrier); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Default instance of the barrier, but a test can declare more manually. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 38 | invisible_barrier_t barrier; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 39 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 40 | void print_address(const char *str, int n, ...) { |
| 41 | fprintf(stderr, "%s", str); |
| 42 | va_list ap; |
| 43 | va_start(ap, n); |
| 44 | while (n--) { |
| 45 | void *p = va_arg(ap, void *); |
| 46 | #if defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__) |
| 47 | // On FreeBSD, the %p conversion specifier works as 0x%x and thus does not |
| 48 | // match to the format used in the diagnotic message. |
| 49 | fprintf(stderr, "0x%012lx ", (unsigned long) p); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 50 | #elif defined(__mips64) |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 51 | fprintf(stderr, "0x%010lx ", (unsigned long) p); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 52 | #endif |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 53 | } |
| 54 | fprintf(stderr, "\n"); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 55 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 56 | |
| 57 | #ifdef __APPLE__ |
| 58 | unsigned long long monotonic_clock_ns() { |
| 59 | static mach_timebase_info_data_t timebase_info; |
| 60 | if (timebase_info.denom == 0) mach_timebase_info(&timebase_info); |
| 61 | return (mach_absolute_time() * timebase_info.numer) / timebase_info.denom; |
| 62 | } |
| 63 | #else |
| 64 | unsigned long long monotonic_clock_ns() { |
| 65 | struct timespec t; |
| 66 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 67 | return (unsigned long long)t.tv_sec * 1000000000ull + t.tv_nsec; |
| 68 | } |
| 69 | #endif |