1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: libcpp-has-no-threads
11 // UNSUPPORTED: c++03, c++98, c++11
17 // class shared_timed_mutex;
19 // template <class Rep, class Period>
20 // bool try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
22 #include <shared_mutex>
28 #include "test_macros.h"
30 std::shared_timed_mutex m;
32 typedef std::chrono::steady_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_HAS_SANITIZERS)
44 ms Tolerance = ms(50);
46 ms Tolerance = ms(50 * 5);
51 time_point t0 = Clock::now();
52 assert(m.try_lock_shared_for(WaitTime + Tolerance) == true);
53 time_point t1 = Clock::now();
55 ns d = t1 - t0 - WaitTime;
56 assert(d < Tolerance); // within 50ms
61 time_point t0 = Clock::now();
62 assert(m.try_lock_shared_for(WaitTime) == false);
63 time_point t1 = Clock::now();
64 ns d = t1 - t0 - WaitTime;
65 assert(d < Tolerance); // within 50ms
72 std::vector<std::thread> v;
73 for (int i = 0; i < 5; ++i)
74 v.push_back(std::thread(f1));
75 std::this_thread::sleep_for(WaitTime);
82 std::vector<std::thread> v;
83 for (int i = 0; i < 5; ++i)
84 v.push_back(std::thread(f2));
85 std::this_thread::sleep_for(WaitTime + Tolerance);