]>
Commit | Line | Data |
---|---|---|
1 | // { dg-do run { target c++17 } } | |
2 | ||
3 | // Copyright (C) 2017-2024 Free Software Foundation, Inc. | |
4 | // | |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 3, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License along | |
17 | // with this library; see the file COPYING3. If not see | |
18 | // <http://www.gnu.org/licenses/>. | |
19 | ||
20 | ||
21 | #include <mutex> | |
22 | #include <testsuite_hooks.h> | |
23 | ||
24 | struct BasicLockable | |
25 | { | |
26 | BasicLockable() : locked(false) { } | |
27 | ||
28 | ~BasicLockable() noexcept(false) | |
29 | { | |
30 | if (locked) | |
31 | throw 0; | |
32 | } | |
33 | ||
34 | void lock() | |
35 | { | |
36 | if (locked) | |
37 | throw 0; | |
38 | locked = true; | |
39 | } | |
40 | ||
41 | void unlock() | |
42 | { | |
43 | if (!locked) | |
44 | throw 0; | |
45 | locked = false; | |
46 | } | |
47 | ||
48 | bool locked; | |
49 | }; | |
50 | ||
51 | template<int> | |
52 | struct Lockable | |
53 | { | |
54 | BasicLockable m; | |
55 | void lock() { m.lock(); } | |
56 | void unlock() { m.unlock(); } | |
57 | bool try_lock() { if (m.locked) return false; m.lock(); return true; } | |
58 | }; | |
59 | ||
60 | void test01() | |
61 | { | |
62 | BasicLockable m; | |
63 | ||
64 | try | |
65 | { | |
66 | std::scoped_lock<BasicLockable> l(m); | |
67 | VERIFY( m.locked ); | |
68 | } | |
69 | catch (...) | |
70 | { | |
71 | VERIFY( false ); | |
72 | } | |
73 | ||
74 | VERIFY( !m.locked ); | |
75 | ||
76 | m.lock(); | |
77 | ||
78 | try | |
79 | { | |
80 | std::scoped_lock<BasicLockable> l(std::adopt_lock, m); | |
81 | } | |
82 | catch (...) | |
83 | { | |
84 | VERIFY( false ); | |
85 | } | |
86 | ||
87 | VERIFY( !m.locked ); | |
88 | } | |
89 | ||
90 | void test02() | |
91 | { | |
92 | Lockable<1> m1; | |
93 | Lockable<2> m2; | |
94 | ||
95 | try | |
96 | { | |
97 | std::scoped_lock<Lockable<1>, Lockable<2>> l(m1, m2); | |
98 | VERIFY( m1.m.locked ); | |
99 | VERIFY( m2.m.locked ); | |
100 | } | |
101 | catch (...) | |
102 | { | |
103 | VERIFY( false ); | |
104 | } | |
105 | ||
106 | VERIFY( !m1.m.locked ); | |
107 | VERIFY( !m2.m.locked ); | |
108 | ||
109 | m1.lock(); | |
110 | m2.lock(); | |
111 | ||
112 | try | |
113 | { | |
114 | std::scoped_lock<Lockable<1>, Lockable<2>> l(std::adopt_lock, m1, m2); | |
115 | VERIFY( m1.m.locked ); | |
116 | VERIFY( m2.m.locked ); | |
117 | } | |
118 | catch (...) | |
119 | { | |
120 | VERIFY( false ); | |
121 | } | |
122 | ||
123 | VERIFY( !m1.m.locked ); | |
124 | VERIFY( !m2.m.locked ); | |
125 | } | |
126 | ||
127 | int main() | |
128 | { | |
129 | test01(); | |
130 | test02(); | |
131 | } |