Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / mutex_cycle_long.c
blobb5d67c16c321d79a007d48ce2138a2c86ce52c9f
1 // RUN: %clangxx_tsan %s -o %t
2 // RUN: not %run %t 5 2>&1 | FileCheck %s
3 // RUN: not %run %t 10 2>&1 | FileCheck %s
4 // RUN: not %run %t 15 2>&1 | FileCheck %s
5 // RUN: not %run %t 20 2>&1 | FileCheck %s
6 // RUN: %run %t 30 2>&1 | FileCheck %s --check-prefix=CHECK-TOO-LONG-CYCLE
8 #include <pthread.h>
9 #include <stdio.h>
10 #include <stdlib.h>
12 int main(int argc, char *argv[]) {
13 int num_mutexes = 5;
14 if (argc > 1) num_mutexes = atoi(argv[1]);
16 pthread_mutex_t m[num_mutexes];
17 for (int i = 0; i < num_mutexes; ++i)
18 pthread_mutex_init(&m[i], NULL);
20 for (int i = 0; i < num_mutexes - 1; ++i) {
21 pthread_mutex_lock(&m[i]);
22 pthread_mutex_lock(&m[i + 1]);
24 pthread_mutex_unlock(&m[i]);
25 pthread_mutex_unlock(&m[i + 1]);
28 pthread_mutex_lock(&m[num_mutexes - 1]);
29 pthread_mutex_lock(&m[0]);
31 pthread_mutex_unlock(&m[num_mutexes - 1]);
32 pthread_mutex_unlock(&m[0]);
34 for (int i = 0; i < num_mutexes; ++i)
35 pthread_mutex_destroy(&m[i]);
37 fprintf(stderr, "PASS\n");
40 // CHECK: ThreadSanitizer: lock-order-inversion (potential deadlock)
41 // CHECK-TOO-LONG-CYCLE: WARNING: too long mutex cycle found
42 // CHECK: PASS