1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2 #include "test.h"
3
4 /*
5 Annotations usage example.
6
7 Tsan does not see synchronization in barrier_wait.
8 ANNOTATE_HAPPENS_BEFORE/AFTER communicate the synchronization to tsan
9 and prevent the race report.
10 */
11
12 int Global;
13
Thread1(void * x)14 void *Thread1(void *x) {
15 barrier_wait(&barrier);
16 ANNOTATE_HAPPENS_AFTER(&barrier);
17 Global++;
18 return NULL;
19 }
20
Thread2(void * x)21 void *Thread2(void *x) {
22 Global--;
23 ANNOTATE_HAPPENS_BEFORE(&barrier);
24 barrier_wait(&barrier);
25 return NULL;
26 }
27
main()28 int main() {
29 barrier_init(&barrier, 2);
30 pthread_t t[2];
31 pthread_create(&t[0], NULL, Thread1, NULL);
32 pthread_create(&t[1], NULL, Thread2, NULL);
33 pthread_join(t[0], NULL);
34 pthread_join(t[1], NULL);
35 fprintf(stderr, "DONE\n");
36 return 0;
37 }
38
39 // CHECK-NOT: WARNING: ThreadSanitizer: data race
40 // CHECK: DONE
41
42