• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2 #include <pthread.h>
3 #include <unistd.h>
4 
5 pthread_rwlock_t rwlock;
6 int GLOB;
7 
Thread1(void * p)8 void *Thread1(void *p) {
9   (void)p;
10   pthread_rwlock_rdlock(&rwlock);
11   // Write under reader lock.
12   sleep(1);
13   GLOB++;
14   pthread_rwlock_unlock(&rwlock);
15   return 0;
16 }
17 
main(int argc,char * argv[])18 int main(int argc, char *argv[]) {
19   pthread_rwlock_init(&rwlock, NULL);
20   pthread_rwlock_rdlock(&rwlock);
21   pthread_t t;
22   pthread_create(&t, 0, Thread1, 0);
23   volatile int x = GLOB;
24   (void)x;
25   pthread_rwlock_unlock(&rwlock);
26   pthread_join(t, 0);
27   pthread_rwlock_destroy(&rwlock);
28   return 0;
29 }
30 
31 // CHECK: WARNING: ThreadSanitizer: data race
32 // CHECK:   Write of size 4 at {{.*}} by thread T1{{.*}}:
33 // CHECK:     #0 Thread1(void*) {{.*}}write_in_reader_lock.cc:13
34 // CHECK:   Previous read of size 4 at {{.*}} by main thread{{.*}}:
35 // CHECK:     #0 main {{.*}}write_in_reader_lock.cc:23
36