1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2 #include "test.h"
3
4 extern "C" {
5 void AnnotateIgnoreReadsBegin(const char *f, int l);
6 void AnnotateIgnoreReadsEnd(const char *f, int l);
7 void AnnotateIgnoreWritesBegin(const char *f, int l);
8 void AnnotateIgnoreWritesEnd(const char *f, int l);
9 }
10
11 int *g;
12
Thread(void * a)13 void *Thread(void *a) {
14 int *p = 0;
15 while ((p = __atomic_load_n(&g, __ATOMIC_RELAXED)) == 0)
16 usleep(100); // spin-wait
17 *p = 42;
18 return 0;
19 }
20
main()21 int main() {
22 pthread_t t;
23 pthread_create(&t, 0, Thread, 0);
24 AnnotateIgnoreWritesBegin(__FILE__, __LINE__);
25 int *p = new int(0);
26 AnnotateIgnoreWritesEnd(__FILE__, __LINE__);
27 __atomic_store_n(&g, p, __ATOMIC_RELAXED);
28 pthread_join(t, 0);
29 delete p;
30 fprintf(stderr, "OK\n");
31 return 0;
32 }
33
34 // CHECK-NOT: WARNING: ThreadSanitizer: data race
35 // CHECK: OK
36