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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads
10 // UNSUPPORTED: c++03, c++11
14 // class shared_timed_mutex;
18 #include <shared_mutex>
25 #include "make_test_thread.h"
27 int main(int, char**) {
28 // Exclusive-lock a mutex that is not locked yet. This should succeed.
30 std::shared_timed_mutex m
;
35 // Exclusive-lock a mutex that is already locked exclusively. This should block until it is unlocked.
37 std::atomic
<bool> ready(false);
38 std::shared_timed_mutex m
;
40 std::atomic
<bool> is_locked_from_main(true);
42 std::thread t
= support::make_test_thread([&] {
45 assert(!is_locked_from_main
);
52 // We would rather signal this after we unlock, but that would create a race condition.
53 // We instead signal it before we unlock, which means that it's technically possible for the thread
54 // to take the lock while we're still holding it and for the test to still pass.
55 is_locked_from_main
= false;
61 // Exclusive-lock a mutex that is already share-locked. This should block until it is unlocked.
63 std::atomic
<bool> ready(false);
64 std::shared_timed_mutex m
;
66 std::atomic
<bool> is_locked_from_main(true);
68 std::thread t
= support::make_test_thread([&] {
71 assert(!is_locked_from_main
);
78 // We would rather signal this after we unlock, but that would create a race condition.
79 // We instead signal it before we unlock, which means that it's technically possible for
80 // the thread to take the lock while we're still holding it and for the test to still pass.
81 is_locked_from_main
= false;
87 // Make sure that at most one thread can acquire the mutex concurrently.
89 std::atomic
<int> counter(0);
90 std::shared_timed_mutex mutex
;
92 std::vector
<std::thread
> threads
;
93 for (int i
= 0; i
!= 10; ++i
) {
94 threads
.push_back(support::make_test_thread([&] {
103 for (auto& t
: threads
)