xref: /aosp_15_r20/external/compiler-rt/test/tsan/Linux/mutex_robust2.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2*7c3d14c8STreehugger Robot #include <pthread.h>
3*7c3d14c8STreehugger Robot #include <stdlib.h>
4*7c3d14c8STreehugger Robot #include <stdio.h>
5*7c3d14c8STreehugger Robot #include <unistd.h>
6*7c3d14c8STreehugger Robot #include <errno.h>
7*7c3d14c8STreehugger Robot 
8*7c3d14c8STreehugger Robot pthread_mutex_t m;
9*7c3d14c8STreehugger Robot int x;
10*7c3d14c8STreehugger Robot 
thr(void * p)11*7c3d14c8STreehugger Robot void *thr(void *p) {
12*7c3d14c8STreehugger Robot   pthread_mutex_lock(&m);
13*7c3d14c8STreehugger Robot   x = 42;
14*7c3d14c8STreehugger Robot   return 0;
15*7c3d14c8STreehugger Robot }
16*7c3d14c8STreehugger Robot 
main()17*7c3d14c8STreehugger Robot int main() {
18*7c3d14c8STreehugger Robot   pthread_mutexattr_t a;
19*7c3d14c8STreehugger Robot   pthread_mutexattr_init(&a);
20*7c3d14c8STreehugger Robot   pthread_mutexattr_setrobust(&a, PTHREAD_MUTEX_ROBUST);
21*7c3d14c8STreehugger Robot   pthread_mutex_init(&m, &a);
22*7c3d14c8STreehugger Robot   pthread_t th;
23*7c3d14c8STreehugger Robot   pthread_create(&th, 0, thr, 0);
24*7c3d14c8STreehugger Robot   sleep(1);
25*7c3d14c8STreehugger Robot   if (pthread_mutex_trylock(&m) != EOWNERDEAD) {
26*7c3d14c8STreehugger Robot     fprintf(stderr, "not EOWNERDEAD\n");
27*7c3d14c8STreehugger Robot     exit(1);
28*7c3d14c8STreehugger Robot   }
29*7c3d14c8STreehugger Robot   x = 43;
30*7c3d14c8STreehugger Robot   pthread_join(th, 0);
31*7c3d14c8STreehugger Robot   fprintf(stderr, "DONE\n");
32*7c3d14c8STreehugger Robot }
33*7c3d14c8STreehugger Robot 
34*7c3d14c8STreehugger Robot // This is a false positive, tsan must not bark at the data race.
35*7c3d14c8STreehugger Robot // But currently it does.
36*7c3d14c8STreehugger Robot // CHECK-NOT: WARNING: ThreadSanitizer WARNING: double lock of mutex
37*7c3d14c8STreehugger Robot // CHECK: WARNING: ThreadSanitizer: data race
38*7c3d14c8STreehugger Robot // CHECK-NOT: EOWNERDEAD
39*7c3d14c8STreehugger Robot // CHECK: DONE
40*7c3d14c8STreehugger Robot // CHECK-NOT: WARNING: ThreadSanitizer
41*7c3d14c8STreehugger Robot 
42