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