xref: /aosp_15_r20/external/compiler-rt/test/tsan/Linux/mutex_robust.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | 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 
thr(void * p)10*7c3d14c8STreehugger Robot void *thr(void *p) {
11*7c3d14c8STreehugger Robot   pthread_mutex_lock(&m);
12*7c3d14c8STreehugger Robot   return 0;
13*7c3d14c8STreehugger Robot }
14*7c3d14c8STreehugger Robot 
main()15*7c3d14c8STreehugger Robot int main() {
16*7c3d14c8STreehugger Robot   pthread_mutexattr_t a;
17*7c3d14c8STreehugger Robot   pthread_mutexattr_init(&a);
18*7c3d14c8STreehugger Robot   pthread_mutexattr_setrobust(&a, PTHREAD_MUTEX_ROBUST);
19*7c3d14c8STreehugger Robot   pthread_mutex_init(&m, &a);
20*7c3d14c8STreehugger Robot   pthread_t th;
21*7c3d14c8STreehugger Robot   pthread_create(&th, 0, thr, 0);
22*7c3d14c8STreehugger Robot   sleep(1);
23*7c3d14c8STreehugger Robot   if (pthread_mutex_lock(&m) != EOWNERDEAD) {
24*7c3d14c8STreehugger Robot     fprintf(stderr, "not EOWNERDEAD\n");
25*7c3d14c8STreehugger Robot     exit(1);
26*7c3d14c8STreehugger Robot   }
27*7c3d14c8STreehugger Robot   pthread_join(th, 0);
28*7c3d14c8STreehugger Robot   fprintf(stderr, "DONE\n");
29*7c3d14c8STreehugger Robot }
30*7c3d14c8STreehugger Robot 
31*7c3d14c8STreehugger Robot // This is a correct code, and tsan must not bark.
32*7c3d14c8STreehugger Robot // CHECK-NOT: WARNING: ThreadSanitizer
33*7c3d14c8STreehugger Robot // CHECK-NOT: EOWNERDEAD
34*7c3d14c8STreehugger Robot // CHECK: DONE
35*7c3d14c8STreehugger Robot // CHECK-NOT: WARNING: ThreadSanitizer
36*7c3d14c8STreehugger Robot 
37