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