mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-17 11:57:00 +00:00

This adds a new helper that can be called from application code to ensure that no mutexes are held on specific code paths. This is useful for multiple scenarios, including ensuring no locks are held: - at thread exit - in peformance-critical code - when a coroutine is suspended (can cause deadlocks) See this discourse thread for more discussion: https://discourse.llvm.org/t/add-threadsanitizer-check-to-prevent-coroutine-suspending-while-holding-a-lock-potential-deadlock/74051 This resubmits and fixes #69372 (was reverted because of build breakage). This also includes the followup change #71471 (to fix a land race).
35 lines
999 B
C++
35 lines
999 B
C++
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
|
|
#include "test.h"
|
|
|
|
pthread_mutex_t mtx;
|
|
|
|
__attribute__((noinline)) void Func1() {
|
|
pthread_mutex_lock(&mtx);
|
|
__tsan_check_no_mutexes_held();
|
|
pthread_mutex_unlock(&mtx);
|
|
}
|
|
|
|
__attribute__((noinline)) void Func2() {
|
|
pthread_mutex_lock(&mtx);
|
|
pthread_mutex_unlock(&mtx);
|
|
__tsan_check_no_mutexes_held();
|
|
}
|
|
|
|
int main() {
|
|
pthread_mutex_init(&mtx, NULL);
|
|
Func1();
|
|
Func2();
|
|
return 0;
|
|
}
|
|
|
|
// CHECK: WARNING: ThreadSanitizer: mutex held in the wrong context
|
|
// CHECK: {{.*}}__tsan_check_no_mutexes_held{{.*}}
|
|
// CHECK: {{.*}}Func1{{.*}}
|
|
// CHECK: {{.*}}main{{.*}}
|
|
// CHECK: Mutex {{.*}} created at:
|
|
// CHECK: {{.*}}pthread_mutex_init{{.*}}
|
|
// CHECK: {{.*}}main{{.*}}
|
|
// CHECK: SUMMARY: ThreadSanitizer: mutex held in the wrong context {{.*}}mutex_held_wrong_context.cpp{{.*}}Func1
|
|
|
|
// CHECK-NOT: SUMMARY: ThreadSanitizer: mutex held in the wrong context {{.*}}mutex_held_wrong_context.cpp{{.*}}Func2
|