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 | |
14 | void *Thread1(void *x) { |
15 | barrier_wait(barrier: &barrier); |
16 | ANNOTATE_HAPPENS_AFTER(&barrier); |
17 | Global++; |
18 | return NULL; |
19 | } |
20 | |
21 | void *Thread2(void *x) { |
22 | Global--; |
23 | ANNOTATE_HAPPENS_BEFORE(&barrier); |
24 | barrier_wait(barrier: &barrier); |
25 | return NULL; |
26 | } |
27 | |
28 | int main() { |
29 | barrier_init(barrier: &barrier, count: 2); |
30 | pthread_t t[2]; |
31 | pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL); |
32 | pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL); |
33 | pthread_join(th: t[0], NULL); |
34 | pthread_join(th: t[1], NULL); |
35 | fprintf(stderr, format: "DONE\n" ); |
36 | return 0; |
37 | } |
38 | |
39 | // CHECK-NOT: WARNING: ThreadSanitizer: data race |
40 | // CHECK: DONE |
41 | |
42 | |