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