llvm-project
45 строк · 1.3 Кб
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"3
4int Global;5pthread_mutex_t mtx1;6pthread_mutex_t mtx2;7
8void *Thread1(void *x) {9barrier_wait(&barrier);10pthread_mutex_lock(&mtx1);11pthread_mutex_lock(&mtx2);12Global++;13pthread_mutex_unlock(&mtx2);14pthread_mutex_unlock(&mtx1);15return NULL;16}
17
18void *Thread2(void *x) {19Global--;20barrier_wait(&barrier);21return NULL;22}
23
24int main() {25barrier_init(&barrier, 2);26// CHECK: WARNING: ThreadSanitizer: data race27// CHECK: Write of size 4 at {{.*}} by thread T128// CHECK: (mutexes: write [[M1:M[0-9]+]], write [[M2:M[0-9]+]]):29// CHECK: Previous write of size 4 at {{.*}} by thread T2:30// CHECK: Mutex [[M1]] (0x{{.*}}) created at:31// CHECK: #0 pthread_mutex_init32// CHECK: #1 main {{.*}}mutexset3.cpp:[[@LINE+4]]33// CHECK: Mutex [[M2]] (0x{{.*}}) created at:34// CHECK: #0 pthread_mutex_init35// CHECK: #1 main {{.*}}mutexset3.cpp:[[@LINE+2]]36pthread_mutex_init(&mtx1, 0);37pthread_mutex_init(&mtx2, 0);38pthread_t t[2];39pthread_create(&t[0], NULL, Thread1, NULL);40pthread_create(&t[1], NULL, Thread2, NULL);41pthread_join(t[0], NULL);42pthread_join(t[1], NULL);43pthread_mutex_destroy(&mtx1);44pthread_mutex_destroy(&mtx2);45}
46