Debian Codegraph @

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_until.pass.cpp

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