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 // dylib support for shared_mutex was added in macosx10.12
13 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.11
14 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.10
15 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.9
16 
17 // <shared_mutex>
18 
19 // class timed_mutex;
20 
21 // template <class Rep, class Period>
22 //   shared_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
23 
24 #include <thread>
25 
26 #include <atomic>
27 #include <cassert>
28 #include <cstdlib>
29 #include <shared_mutex>
30 #include <vector>
31 
32 #include "make_test_thread.h"
33 #include "test_macros.h"
34 
35 std::shared_timed_mutex m;
36 
37 typedef std::chrono::steady_clock Clock;
38 typedef Clock::time_point time_point;
39 typedef Clock::duration duration;
40 typedef std::chrono::milliseconds ms;
41 typedef std::chrono::nanoseconds ns;
42 
43 ms LongTime = ms(5000);
44 ms ShortTime = ms(50);
45 
46 static const unsigned Threads = 5;
47 
48 std::atomic<unsigned> CountDown(Threads);
49 
50 void f1()
51 {
52   // Preemptive scheduling means that one cannot make assumptions about when
53   // code executes and therefore we cannot assume anthing about when the mutex
54   // starts waiting relative to code in the main thread. We can however prove
55   // that a timeout occured and that implies that this code is waiting.
56   // See f2() below.
57   //
58   // Nevertheless, we should at least try to ensure that the mutex waits and
59   // therefore we use an atomic variable to signal to the main thread that this
60   // code is just a few instructions away from waiting.
61   --CountDown;
62   std::shared_lock<std::shared_timed_mutex> lk(m, LongTime);
63   assert(lk.owns_lock() == true);
64 }
65 
66 void f2()
67 {
68   time_point t0 = Clock::now();
69   std::shared_lock<std::shared_timed_mutex> lk(m, ShortTime);
70   time_point t1 = Clock::now();
71   assert(lk.owns_lock() == false);
72   assert(t1 - t0 >= ShortTime);
73 }
74 
75 int main(int, char**)
76 {
77   {
78     m.lock();
79     std::vector<std::thread> v;
80     for (unsigned i = 0; i < Threads; ++i)
81       v.push_back(support::make_test_thread(f1));
82     while (CountDown > 0)
83       std::this_thread::yield();
84     // Give one more chance for threads to block and wait for the mutex.
85     std::this_thread::yield();
86     std::this_thread::sleep_for(ShortTime);
87     m.unlock();
88     for (auto& t : v)
89       t.join();
90   }
91   {
92     m.lock();
93     std::vector<std::thread> v;
94     for (unsigned i = 0; i < Threads; ++i)
95       v.push_back(support::make_test_thread(f2));
96     for (auto& t : v)
97       t.join();
98     m.unlock();
99   }
100 
101   return 0;
102 }
103