1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4int X = 0;
5
6__attribute__((noinline)) void MySleep() {
7 sleep(seconds: 1); // the sleep that must appear in the report
8}
9
10void *Thread(void *p) {
11 barrier_wait(barrier: &barrier);
12 MySleep(); // Assume the main thread has done the write.
13 X = 42;
14 return 0;
15}
16
17int main() {
18 barrier_init(barrier: &barrier, count: 2);
19 pthread_t t;
20 pthread_create(newthread: &t, attr: 0, start_routine: Thread, arg: 0);
21 X = 43;
22 barrier_wait(barrier: &barrier);
23 pthread_join(th: t, thread_return: 0);
24 return 0;
25}
26
27// CHECK: WARNING: ThreadSanitizer: data race
28// ...
29// CHECK: As if synchronized via sleep:
30// CHECK-NEXT: #0 sleep
31// CHECK-NEXT: #1 MySleep
32// CHECK-NEXT: #2 Thread
33

source code of compiler-rt/test/tsan/sleep_sync.cpp