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, c++14
11 
12 // ALLOW_RETRIES: 2
13 
14 // shared_mutex was introduced in macosx10.12
15 // UNSUPPORTED: with_system_cxx_lib=macosx10.11
16 // UNSUPPORTED: with_system_cxx_lib=macosx10.10
17 // UNSUPPORTED: with_system_cxx_lib=macosx10.9
18 
19 // <shared_mutex>
20 
21 // class shared_mutex;
22 
23 // void lock();
24 
25 #include <shared_mutex>
26 #include <thread>
27 #include <cstdlib>
28 #include <cassert>
29 
30 #include "test_macros.h"
31 
32 std::shared_mutex m;
33 
34 typedef std::chrono::system_clock Clock;
35 typedef Clock::time_point time_point;
36 typedef Clock::duration duration;
37 typedef std::chrono::milliseconds ms;
38 typedef std::chrono::nanoseconds ns;
39 
40 #if !TEST_SLOW_HOST()
41 ms WaitTime = ms(250);
42 #else
43 ms WaitTime = ms(750);
44 #endif
45 
46 // Thread sanitizer causes more overhead and will sometimes cause this test
47 // to fail. To prevent this we give Thread sanitizer more time to complete the
48 // test.
49 #if !defined(TEST_HAS_SANITIZERS) && !TEST_SLOW_HOST()
50 ms Tolerance = ms(50);
51 #else
52 ms Tolerance = ms(50 * 5);
53 #endif
54 
f()55 void f()
56 {
57     time_point t0 = Clock::now();
58     m.lock();
59     time_point t1 = Clock::now();
60     m.unlock();
61     ns d = t1 - t0 - WaitTime;
62     assert(d < Tolerance);  // within tolerance
63 }
64 
main(int,char **)65 int main(int, char**)
66 {
67     m.lock();
68     std::thread t(f);
69     std::this_thread::sleep_for(WaitTime);
70     m.unlock();
71     t.join();
72 
73   return 0;
74 }
75