Debian Codegraph @

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_for.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, c++11
11
12 // <shared_mutex>
13
14 // template <class Mutex> class shared_lock;
15
16 // template <class Rep, class Period>
17 // bool try_lock_for(const chrono::duration<Rep, Period>& rel_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_for_called = false;
28
29 typedef std::chrono::milliseconds ms;
30
31 struct mutex
32 {
33 template <class Rep, class Period>
34 bool try_lock_shared_for(const std::chrono::duration<Rep, Period>& rel_time)
35 {
36 assert(rel_time == ms(5));
37 try_lock_for_called = !try_lock_for_called;
38 return try_lock_for_called;
39 }
40 void unlock_shared() {}
41 };
42
43 mutex m;
44
45 int main(int, char**)
46 {
47 std::shared_lock<mutex> lk(m, std::defer_lock);
48 assert(lk.try_lock_for(ms(5)) == true);
49 assert(try_lock_for_called == true);
50 assert(lk.owns_lock() == true);
51 #ifndef TEST_HAS_NO_EXCEPTIONS
52 try
53 {
54 TEST_IGNORE_NODISCARD lk.try_lock_for(ms(5));
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_for(ms(5)) == false);
64 assert(try_lock_for_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_for(ms(5));
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 }