[docs] Fix build-docs.sh
[llvm-project.git] / compiler-rt / test / tsan / test.h
blobefd66cbf91a4374ae7e94ef3309fc967d6a54b44
1 #include <pthread.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <dlfcn.h>
6 #include <stddef.h>
7 #include <sched.h>
8 #include <stdarg.h>
9 #include "sanitizer_common/print_address.h"
11 #include <sanitizer/tsan_interface.h>
13 #ifdef __APPLE__
14 #include <mach/mach_time.h>
15 #endif
17 #ifndef TSAN_VECTORIZE
18 # define TSAN_VECTORIZE __SSE4_2__
19 #endif
21 #if TSAN_VECTORIZE
22 # include <emmintrin.h>
23 # include <smmintrin.h>
24 #else
25 struct __m128i {
26 unsigned long long x[2];
28 #endif
30 // TSan-invisible barrier.
31 // Tests use it to establish necessary execution order in a way that does not
32 // interfere with tsan (does not establish synchronization between threads).
33 typedef unsigned invisible_barrier_t;
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 void __tsan_testonly_barrier_init(invisible_barrier_t *barrier,
39 unsigned count);
40 void __tsan_testonly_barrier_wait(invisible_barrier_t *barrier);
41 unsigned long __tsan_testonly_shadow_stack_current_size();
42 #ifdef __cplusplus
44 #endif
46 static inline void barrier_init(invisible_barrier_t *barrier, unsigned count) {
47 __tsan_testonly_barrier_init(barrier, count);
50 static inline void barrier_wait(invisible_barrier_t *barrier) {
51 __tsan_testonly_barrier_wait(barrier);
54 // Default instance of the barrier, but a test can declare more manually.
55 invisible_barrier_t barrier;
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;
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;
69 #endif
71 //The const kPCInc must be in sync with StackTrace::GetPreviousInstructionPc
72 #if defined(__s390__) || defined(__i386__) || defined(__x86_64__)
73 const int kPCInc = 1;
74 #elif defined(__sparc__) || defined(__mips__)
75 const int kPCInc = 8;
76 #else
77 const int kPCInc = 4;
78 #endif
80 #ifdef __cplusplus
81 extern "C" {
82 #endif
84 void AnnotateThreadName(const char *f, int l, const char *name);
86 void AnnotateRWLockCreate(const char *f, int l, void *m);
87 void AnnotateRWLockCreateStatic(const char *f, int l, void *m);
88 void AnnotateRWLockDestroy(const char *f, int l, void *m);
89 void AnnotateRWLockAcquired(const char *f, int l, void *m, long is_w);
90 void AnnotateRWLockReleased(const char *f, int l, void *m, long is_w);
92 void AnnotateIgnoreReadsBegin(const char *f, int l);
93 void AnnotateIgnoreReadsEnd(const char *f, int l);
94 void AnnotateIgnoreWritesBegin(const char *f, int l);
95 void AnnotateIgnoreWritesEnd(const char *f, int l);
97 void AnnotateIgnoreSyncBegin(const char *f, int l);
98 void AnnotateIgnoreSyncEnd(const char *f, int l);
100 void AnnotateHappensBefore(const char *f, int l, void *addr);
101 void AnnotateHappensAfter(const char *f, int l, void *addr);
103 void AnnotateBenignRaceSized(const char *f, int l, const volatile void *mem,
104 unsigned int size, const char *desc);
105 void WTFAnnotateBenignRaceSized(const char *f, int l, const volatile void *mem,
106 unsigned int size, const char *desc);
108 #ifdef __cplusplus
110 #endif
112 #define ANNOTATE_RWLOCK_CREATE(m) \
113 AnnotateRWLockCreate(__FILE__, __LINE__, m)
114 #define ANNOTATE_RWLOCK_CREATE_STATIC(m) \
115 AnnotateRWLockCreateStatic(__FILE__, __LINE__, m)
116 #define ANNOTATE_RWLOCK_DESTROY(m) \
117 AnnotateRWLockDestroy(__FILE__, __LINE__, m)
118 #define ANNOTATE_RWLOCK_ACQUIRED(m, is_w) \
119 AnnotateRWLockAcquired(__FILE__, __LINE__, m, is_w)
120 #define ANNOTATE_RWLOCK_RELEASED(m, is_w) \
121 AnnotateRWLockReleased(__FILE__, __LINE__, m, is_w)
122 #define ANNOTATE_HAPPENS_BEFORE(addr) \
123 AnnotateHappensBefore(__FILE__, __LINE__, (void *)(addr))
124 #define ANNOTATE_HAPPENS_AFTER(addr) \
125 AnnotateHappensAfter(__FILE__, __LINE__, (void *)(addr))
126 #define ANNOTATE_BENIGN_RACE(var) \
127 AnnotateBenignRaceSized(__FILE__, __LINE__, &(var), sizeof(var), #var)
128 #define WTF_ANNOTATE_BENIGN_RACE(var) \
129 WTFAnnotateBenignRaceSized(__FILE__, __LINE__, &(var), sizeof(var), #var)
131 #ifdef __APPLE__
132 #define ASM_SYMBOL(symbol) "_" #symbol
133 #else
134 #define ASM_SYMBOL(symbol) #symbol
135 #endif