1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // UNSUPPORTED: libcpp-has-no-threads
10 // UNSUPPORTED: c++98, c++03, c++11
11 
12 // shared_timed_mutex was introduced in macosx10.12
13 // UNSUPPORTED: with_system_cxx_lib=macosx10.11
14 // UNSUPPORTED: with_system_cxx_lib=macosx10.10
15 // UNSUPPORTED: with_system_cxx_lib=macosx10.9
16 
17 // <shared_mutex>
18 
19 // class shared_timed_mutex;
20 
21 // void lock();
22 
23 #include <shared_mutex>
24 #include <thread>
25 #include <cstdlib>
26 #include <cassert>
27 
28 #include "test_macros.h"
29 
30 std::shared_timed_mutex m;
31 
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;
37 
38 std::atomic<bool> ready(false);
39 time_point start;
40 
41 ms WaitTime = ms(250);
42 
43 void f()
44 {
45   ready.store(true);
46   m.lock();
47   time_point t0 = start;
48   time_point t1 = Clock::now();
49   m.unlock();
50   assert(t0.time_since_epoch() > ms(0));
51   assert(t1 - t0 >= WaitTime);
52 }
53 
54 int main(int, char**)
55 {
56   m.lock();
57   std::thread t(f);
58   while (!ready)
59     std::this_thread::yield();
60   start = Clock::now();
61   std::this_thread::sleep_for(WaitTime);
62   m.unlock();
63   t.join();
64 
65   return 0;
66 }
67