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++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 // TODO(ldionne): This test fails on Ubuntu Focal on our CI nodes (and only there), in 32 bit mode.
16 // UNSUPPORTED: linux && 32bits-on-64bits
17 
18 // <shared_mutex>
19 
20 // class shared_timed_mutex;
21 
22 // void lock();
23 
24 #include <thread>
25 
26 #include <atomic>
27 #include <cstdlib>
28 #include <cassert>
29 #include <shared_mutex>
30 
31 #include "make_test_thread.h"
32 #include "test_macros.h"
33 
34 std::shared_timed_mutex m;
35 
36 typedef std::chrono::system_clock Clock;
37 typedef Clock::time_point time_point;
38 typedef Clock::duration duration;
39 typedef std::chrono::milliseconds ms;
40 typedef std::chrono::nanoseconds ns;
41 
42 std::atomic<bool> ready(false);
43 time_point start;
44 
45 ms WaitTime = ms(250);
46 
47 void f()
48 {
49   ready.store(true);
50   m.lock();
51   time_point t0 = start;
52   time_point t1 = Clock::now();
53   m.unlock();
54   assert(t0.time_since_epoch() > ms(0));
55   assert(t1 - t0 >= WaitTime);
56 }
57 
58 int main(int, char**)
59 {
60   m.lock();
61   std::thread t = support::make_test_thread(f);
62   while (!ready)
63     std::this_thread::yield();
64   start = Clock::now();
65   std::this_thread::sleep_for(WaitTime);
66   m.unlock();
67   t.join();
68 
69   return 0;
70 }
71