1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // UNSUPPORTED: libcpp-has-no-threads
11 
12 // <shared_mutex>
13 
14 // template <class Mutex> class shared_lock;
15 
16 // void lock();
17 
18 #include <shared_mutex>
19 #include <thread>
20 #include <vector>
21 #include <cstdlib>
22 #include <cassert>
23 
24 #if _LIBCPP_STD_VER > 11
25 
26 std::shared_timed_mutex m;
27 
28 typedef std::chrono::system_clock Clock;
29 typedef Clock::time_point time_point;
30 typedef Clock::duration duration;
31 typedef std::chrono::milliseconds ms;
32 typedef std::chrono::nanoseconds ns;
33 
f()34 void f()
35 {
36     std::shared_lock<std::shared_timed_mutex> lk(m, std::defer_lock);
37     time_point t0 = Clock::now();
38     lk.lock();
39     time_point t1 = Clock::now();
40     assert(lk.owns_lock() == true);
41     ns d = t1 - t0 - ms(250);
42     assert(d < ms(25));  // within 25ms
43     try
44     {
45         lk.lock();
46         assert(false);
47     }
48     catch (std::system_error& e)
49     {
50         assert(e.code().value() == EDEADLK);
51     }
52     lk.unlock();
53     lk.release();
54     try
55     {
56         lk.lock();
57         assert(false);
58     }
59     catch (std::system_error& e)
60     {
61         assert(e.code().value() == EPERM);
62     }
63 }
64 
65 #endif  // _LIBCPP_STD_VER > 11
66 
main()67 int main()
68 {
69 #if _LIBCPP_STD_VER > 11
70     m.lock();
71     std::vector<std::thread> v;
72     for (int i = 0; i < 5; ++i)
73         v.push_back(std::thread(f));
74     std::this_thread::sleep_for(ms(250));
75     m.unlock();
76     for (auto& t : v)
77         t.join();
78 #endif  // _LIBCPP_STD_VER > 11
79 }
80