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