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
12 int main(int argc
, char *argv
[]) {
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