• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // UNSUPPORTED: c++03, c++98, c++11
12 
13 // FLAKY_TEST.
14 
15 // <shared_mutex>
16 
17 // class shared_timed_mutex;
18 
19 // void lock();
20 
21 #include <shared_mutex>
22 #include <thread>
23 #include <cstdlib>
24 #include <cassert>
25 
26 #include "test_macros.h"
27 
28 std::shared_timed_mutex m;
29 
30 typedef std::chrono::system_clock Clock;
31 typedef Clock::time_point time_point;
32 typedef Clock::duration duration;
33 typedef std::chrono::milliseconds ms;
34 typedef std::chrono::nanoseconds ns;
35 
36 
37 ms WaitTime = ms(250);
38 
39 // Thread sanitizer causes more overhead and will sometimes cause this test
40 // to fail. To prevent this we give Thread sanitizer more time to complete the
41 // test.
42 #if !TEST_HAS_FEATURE(thread_sanitizer)
43 ms Tolerance = ms(50);
44 #else
45 ms Tolerance = ms(100);
46 #endif
47 
48 
f()49 void f()
50 {
51     time_point t0 = Clock::now();
52     m.lock();
53     time_point t1 = Clock::now();
54     m.unlock();
55     ns d = t1 - t0 - ms(250);
56     assert(d < ms(50));  // within 50ms
57 }
58 
main()59 int main()
60 {
61     m.lock();
62     std::thread t(f);
63     std::this_thread::sleep_for(ms(250));
64     m.unlock();
65     t.join();
66 }
67