1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4pthread_rwlock_t rwlock;
5int GLOB;
6
7void *Thread1(void *p) {
8 (void)p;
9 pthread_rwlock_rdlock(rwlock: &rwlock);
10 barrier_wait(barrier: &barrier);
11 // Write under reader lock.
12 GLOB++;
13 pthread_rwlock_unlock(rwlock: &rwlock);
14 return 0;
15}
16
17int main(int argc, char *argv[]) {
18 barrier_init(barrier: &barrier, count: 2);
19 pthread_rwlock_init(rwlock: &rwlock, NULL);
20 pthread_rwlock_rdlock(rwlock: &rwlock);
21 pthread_t t;
22 pthread_create(newthread: &t, attr: 0, start_routine: Thread1, arg: 0);
23 volatile int x = GLOB;
24 (void)x;
25 pthread_rwlock_unlock(rwlock: &rwlock);
26 barrier_wait(barrier: &barrier);
27 pthread_join(th: t, thread_return: 0);
28 pthread_rwlock_destroy(rwlock: &rwlock);
29 return 0;
30}
31
32// CHECK: WARNING: ThreadSanitizer: data race
33// CHECK: Write of size 4 at {{.*}} by thread T1{{.*}}:
34// CHECK: #0 Thread1(void*) {{.*}}write_in_reader_lock.cpp:12
35// CHECK: Previous read of size 4 at {{.*}} by main thread{{.*}}:
36// CHECK: #0 main {{.*}}write_in_reader_lock.cpp:23
37

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