1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: no-threads
14 // class recursive_mutex;
24 #include "make_test_thread.h"
26 bool is_lockable(std::recursive_mutex
& m
) {
28 std::thread t
= support::make_test_thread([&] {
29 did_lock
= m
.try_lock();
31 m
.unlock(); // undo side effects
38 int main(int, char**) {
39 // Try to lock a mutex that is not locked yet. This should succeed.
41 std::recursive_mutex m
;
42 bool succeeded
= m
.try_lock();
47 // Try to lock a mutex that is already locked by this thread. This should succeed and the mutex should only
48 // be unlocked after a matching number of calls to unlock() on the same thread.
50 std::recursive_mutex m
;
52 for (int i
= 0; i
!= 10; ++i
) {
56 while (lock_count
!= 0) {
57 assert(!is_lockable(m
));
61 assert(is_lockable(m
));
64 // Try to lock a mutex that is already locked by another thread. This should fail.
66 std::recursive_mutex m
;
69 std::thread t
= support::make_test_thread([&] {
70 for (int i
= 0; i
!= 10; ++i
) {
71 bool succeeded
= m
.try_lock();