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 // template <class Mutex> class shared_lock;
15 
16 // template <class Clock, class Duration>
17 //   bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
18 
19 #include <cassert>
20 #include <chrono>
21 #include <mutex>
22 #include <shared_mutex>
23 #include <system_error>
24 
25 #include "test_macros.h"
26 
27 bool try_lock_until_called = false;
28 
29 struct mutex
30 {
31     template <class Clock, class Duration>
try_lock_shared_untilmutex32         bool try_lock_shared_until(const std::chrono::time_point<Clock, Duration>& abs_time)
33     {
34         typedef std::chrono::milliseconds ms;
35         assert(Clock::now() - abs_time < ms(5));
36         try_lock_until_called = !try_lock_until_called;
37         return try_lock_until_called;
38     }
unlock_sharedmutex39     void unlock_shared() {}
40 };
41 
42 mutex m;
43 
main(int,char **)44 int main(int, char**)
45 {
46     typedef std::chrono::steady_clock Clock;
47     std::shared_lock<mutex> lk(m, std::defer_lock);
48     assert(lk.try_lock_until(Clock::now()) == true);
49     assert(try_lock_until_called == true);
50     assert(lk.owns_lock() == true);
51 #ifndef TEST_HAS_NO_EXCEPTIONS
52     try
53     {
54         TEST_IGNORE_NODISCARD lk.try_lock_until(Clock::now());
55         assert(false);
56     }
57     catch (std::system_error& e)
58     {
59         assert(e.code().value() == EDEADLK);
60     }
61 #endif
62     lk.unlock();
63     assert(lk.try_lock_until(Clock::now()) == false);
64     assert(try_lock_until_called == false);
65     assert(lk.owns_lock() == false);
66     lk.release();
67 #ifndef TEST_HAS_NO_EXCEPTIONS
68     try
69     {
70         TEST_IGNORE_NODISCARD lk.try_lock_until(Clock::now());
71         assert(false);
72     }
73     catch (std::system_error& e)
74     {
75         assert(e.code().value() == EPERM);
76     }
77 #endif
78 
79   return 0;
80 }
81