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: no-threads
10// UNSUPPORTED: c++03, c++11
11
12// <shared_mutex>
13
14// class shared_timed_mutex;
15
16// template <class Clock, class Duration>
17// shared_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
18
19#include <thread>
20
21#include <atomic>
22#include <chrono>
23#include <cassert>
24#include <cstdlib>
25#include <shared_mutex>
26#include <vector>
27
28#include "make_test_thread.h"
29#include "test_macros.h"
30
31std::shared_timed_mutex m;
32
33typedef std::chrono::steady_clock Clock;
34typedef Clock::time_point time_point;
35typedef Clock::duration duration;
36typedef std::chrono::milliseconds ms;
37typedef std::chrono::nanoseconds ns;
38
39ms LongTime = ms(5000);
40ms ShortTime = ms(50);
41
42static constexpr unsigned Threads = 5;
43
44std::atomic<unsigned> CountDown(Threads);
45
46void f1()
47{
48 --CountDown;
49 time_point t0 = Clock::now();
50 std::shared_lock<std::shared_timed_mutex> lk(m, t0 + LongTime);
51 time_point t1 = Clock::now();
52 assert(lk.owns_lock() == true);
53 assert(t1 - t0 <= LongTime);
54}
55
56void f2()
57{
58 time_point t0 = Clock::now();
59 std::shared_lock<std::shared_timed_mutex> lk(m, t0 + ShortTime);
60 time_point t1 = Clock::now();
61 assert(lk.owns_lock() == false);
62 assert(t1 - t0 >= ShortTime);
63}
64
65int main(int, char**)
66{
67 {
68 m.lock();
69 std::vector<std::thread> v;
70 for (unsigned i = 0; i < Threads; ++i)
71 v.push_back(support::make_test_thread(f1));
72 while (CountDown > 0)
73 std::this_thread::yield();
74 std::this_thread::sleep_for(rtime: ShortTime);
75 m.unlock();
76 for (auto& t : v)
77 t.join();
78 }
79 {
80 m.lock();
81 std::vector<std::thread> v;
82 for (unsigned i = 0; i < Threads; ++i)
83 v.push_back(support::make_test_thread(f2));
84 for (auto& t : v)
85 t.join();
86 m.unlock();
87 }
88
89 return 0;
90}
91

source code of libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_time_point.pass.cpp