1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4pthread_mutex_t mtx;
5
6__attribute__((noinline)) void Func1() {
7 pthread_mutex_lock(mutex: &mtx);
8 __tsan_check_no_mutexes_held();
9 pthread_mutex_unlock(mutex: &mtx);
10}
11
12__attribute__((noinline)) void Func2() {
13 pthread_mutex_lock(mutex: &mtx);
14 pthread_mutex_unlock(mutex: &mtx);
15 __tsan_check_no_mutexes_held();
16}
17
18int main() {
19 pthread_mutex_init(mutex: &mtx, NULL);
20 Func1();
21 Func2();
22 return 0;
23}
24
25// CHECK: WARNING: ThreadSanitizer: mutex held in the wrong context
26// CHECK: {{.*}}__tsan_check_no_mutexes_held{{.*}}
27// CHECK: {{.*}}Func1{{.*}}
28// CHECK: {{.*}}main{{.*}}
29// CHECK: Mutex {{.*}} created at:
30// CHECK: {{.*}}pthread_mutex_init{{.*}}
31// CHECK: {{.*}}main{{.*}}
32// CHECK: SUMMARY: ThreadSanitizer: mutex held in the wrong context {{.*}}mutex_held_wrong_context.cpp{{.*}}Func1
33
34// CHECK-NOT: SUMMARY: ThreadSanitizer: mutex held in the wrong context {{.*}}mutex_held_wrong_context.cpp{{.*}}Func2
35

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