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