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++98, c++03, c++11
15 // template <class Mutex> class shared_lock;
19 #include <shared_mutex>
25 #include "test_macros.h"
27 std::shared_timed_mutex m;
29 typedef std::chrono::system_clock Clock;
30 typedef Clock::time_point time_point;
31 typedef Clock::duration duration;
32 typedef std::chrono::milliseconds ms;
33 typedef std::chrono::nanoseconds ns;
35 ms WaitTime = ms(250);
37 // Thread sanitizer causes more overhead and will sometimes cause this test
38 // to fail. To prevent this we give Thread sanitizer more time to complete the
40 #if !defined(TEST_HAS_SANITIZERS)
41 ms Tolerance = ms(25);
43 ms Tolerance = ms(25 * 5);
49 std::shared_lock<std::shared_timed_mutex> lk(m, std::defer_lock);
50 time_point t0 = Clock::now();
52 time_point t1 = Clock::now();
53 assert(lk.owns_lock() == true);
54 ns d = t1 - t0 - WaitTime;
55 assert(d < Tolerance); // within tolerance
56 #ifndef TEST_HAS_NO_EXCEPTIONS
62 catch (std::system_error& e)
64 assert(e.code().value() == EDEADLK);
69 #ifndef TEST_HAS_NO_EXCEPTIONS
75 catch (std::system_error& e)
77 assert(e.code().value() == EPERM);
85 std::vector<std::thread> v;
86 for (int i = 0; i < 5; ++i)
87 v.push_back(std::thread(f));
88 std::this_thread::sleep_for(WaitTime);