]>
Commit | Line | Data |
---|---|---|
d1236680 RO |
1 | // { dg-do run } |
2 | // { dg-options "-pthread" } | |
71c54f8e | 3 | // { dg-require-effective-target c++11 } |
d1236680 | 4 | // { dg-require-effective-target pthread } |
9b2b801a JW |
5 | // { dg-require-gthreads "" } |
6 | ||
a5544970 | 7 | // Copyright (C) 2010-2019 Free Software Foundation, Inc. |
9b2b801a JW |
8 | // |
9 | // This file is part of the GNU ISO C++ Library. This library is free | |
10 | // software; you can redistribute it and/or modify it under the | |
11 | // terms of the GNU General Public License as published by the | |
12 | // Free Software Foundation; either version 3, or (at your option) | |
13 | // any later version. | |
14 | ||
15 | // This library is distributed in the hope that it will be useful, | |
16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | // GNU General Public License for more details. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License along | |
21 | // with this library; see the file COPYING3. If not see | |
22 | // <http://www.gnu.org/licenses/>. | |
23 | ||
24 | ||
25 | #include <mutex> | |
26 | #include <testsuite_hooks.h> | |
27 | ||
28 | struct unreliable_lock | |
29 | { | |
30 | std::mutex m; | |
31 | std::unique_lock<std::mutex> l; | |
32 | ||
33 | static int count; | |
34 | static int throw_on; | |
35 | static int lock_on; | |
36 | ||
37 | unreliable_lock() : l(m, std::defer_lock) { } | |
38 | ||
39 | ~unreliable_lock() | |
40 | { | |
9b2b801a JW |
41 | VERIFY( !l.owns_lock() ); |
42 | } | |
43 | ||
44 | void lock() | |
45 | { | |
46 | if (count == throw_on) | |
47 | throw throw_on; | |
48 | ++count; | |
49 | l.lock(); | |
50 | } | |
51 | bool try_lock() | |
52 | { | |
53 | if (count == throw_on) | |
54 | throw throw_on; | |
55 | std::unique_lock<std::mutex> l2(m, std::defer_lock); | |
56 | if (count == lock_on) | |
57 | l2.lock(); | |
58 | ++count; | |
59 | return l.try_lock(); | |
60 | } | |
61 | ||
62 | void unlock() | |
63 | { | |
9b2b801a JW |
64 | VERIFY( l.owns_lock() ); |
65 | l.unlock(); | |
66 | } | |
67 | ||
68 | }; | |
69 | ||
70 | int unreliable_lock::count = 0; | |
71 | int unreliable_lock::throw_on = -1; | |
72 | int unreliable_lock::lock_on = -1; | |
73 | ||
74 | void test01() | |
75 | { | |
9b2b801a JW |
76 | unreliable_lock l1, l2, l3; |
77 | ||
78 | try | |
79 | { | |
80 | unreliable_lock::count = 0; | |
81 | int result = std::try_lock(l1, l2, l3); | |
82 | VERIFY( result == -1 ); | |
83 | VERIFY( unreliable_lock::count == 3 ); | |
84 | l1.unlock(); | |
85 | l2.unlock(); | |
86 | l3.unlock(); | |
87 | } | |
88 | catch (...) | |
89 | { | |
90 | VERIFY( false ); | |
91 | } | |
92 | } | |
93 | ||
94 | void test02() | |
95 | { | |
9b2b801a JW |
96 | unreliable_lock l1, l2, l3; |
97 | ||
98 | try | |
99 | { | |
100 | // test behaviour when a lock is already held | |
101 | unreliable_lock::lock_on = 0; | |
102 | while (unreliable_lock::lock_on < 3) | |
103 | { | |
104 | unreliable_lock::count = 0; | |
105 | int failed = std::try_lock(l1, l2, l3); | |
106 | VERIFY( failed == unreliable_lock::lock_on ); | |
107 | ++unreliable_lock::lock_on; | |
108 | } | |
109 | } | |
110 | catch (...) | |
111 | { | |
112 | VERIFY( false ); | |
113 | } | |
114 | } | |
115 | ||
116 | void test03() | |
117 | { | |
9b2b801a JW |
118 | unreliable_lock l1, l2, l3; |
119 | ||
120 | try | |
121 | { | |
122 | // test behaviour when an exception is thrown | |
123 | unreliable_lock::throw_on = 0; | |
124 | while (unreliable_lock::throw_on < 3) | |
125 | { | |
126 | unreliable_lock::count = 0; | |
726d3136 JW |
127 | try |
128 | { | |
129 | std::try_lock(l1, l2, l3); | |
130 | VERIFY( false ); | |
131 | } | |
132 | catch (int e) | |
133 | { | |
134 | VERIFY( e == unreliable_lock::throw_on ); | |
135 | } | |
9b2b801a JW |
136 | ++unreliable_lock::throw_on; |
137 | } | |
138 | } | |
139 | catch (...) | |
140 | { | |
141 | VERIFY( false ); | |
142 | } | |
143 | } | |
144 | ||
145 | int main() | |
146 | { | |
147 | test01(); | |
148 | test02(); | |
149 | test03(); | |
150 | return 0; | |
151 | } |