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
15 // template <class Mutex> class shared_lock;
17 // explicit shared_lock(mutex_type& m);
19 // template<class _Mutex> shared_lock(shared_lock<_Mutex>)
20 // -> shared_lock<_Mutex>; // C++17
25 #include <shared_mutex>
29 #include "make_test_thread.h"
30 #include "test_macros.h"
32 typedef std::chrono::system_clock Clock
;
33 typedef Clock::time_point time_point
;
34 typedef Clock::duration duration
;
35 typedef std::chrono::milliseconds ms
;
36 typedef std::chrono::nanoseconds ns
;
38 ms WaitTime
= ms(250);
40 // Thread sanitizer causes more overhead and will sometimes cause this test
41 // to fail. To prevent this we give Thread sanitizer more time to complete the
43 #if !defined(TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT)
44 ms Tolerance
= ms(50);
46 ms Tolerance
= ms(50 * 5);
49 std::shared_timed_mutex m
;
53 time_point t0
= Clock::now();
56 std::shared_lock
<std::shared_timed_mutex
> ul(m
);
59 ns d
= t1
- t0
- WaitTime
;
60 assert(d
< Tolerance
); // within tolerance
65 time_point t0
= Clock::now();
68 std::shared_lock
<std::shared_timed_mutex
> ul(m
);
72 assert(d
< Tolerance
); // within tolerance
77 std::vector
<std::thread
> v
;
80 for (int i
= 0; i
< 5; ++i
)
81 v
.push_back(support::make_test_thread(f
));
82 std::this_thread::sleep_for(WaitTime
);
90 t
= support::make_test_thread(g
);
91 std::thread q
= support::make_test_thread(f
);
92 std::this_thread::sleep_for(WaitTime
);
99 #if TEST_STD_VER >= 17
100 std::shared_lock
sl(m
);
101 static_assert((std::is_same
<decltype(sl
), std::shared_lock
<decltype(m
)>>::value
), "" );