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: libcpp-has-no-threads
10
11 // This test hangs forever when built against libstdc++ and MSVC. In order to allow
12 // validation of the test suite against other STLs we have to mark it
13 // unsupported.
14 // UNSUPPORTED: libstdc++, msvc
15
16 // <mutex>
17
18 // template <class L1, class L2, class... L3>
19 // void lock(L1&, L2&, L3&...);
20
21 #include <mutex>
22 #include <cassert>
23
24 #include "test_macros.h"
25
26 class L0
27 {
28 bool locked_;
29
30 public:
L0()31 L0() : locked_(false) {}
32
lock()33 void lock()
34 {
35 locked_ = true;
36 }
37
try_lock()38 bool try_lock()
39 {
40 locked_ = true;
41 return locked_;
42 }
43
unlock()44 void unlock() {locked_ = false;}
45
locked() const46 bool locked() const {return locked_;}
47 };
48
49 class L1
50 {
51 bool locked_;
52
53 public:
L1()54 L1() : locked_(false) {}
55
lock()56 void lock()
57 {
58 locked_ = true;
59 }
60
try_lock()61 bool try_lock()
62 {
63 locked_ = false;
64 return locked_;
65 }
66
unlock()67 void unlock() {locked_ = false;}
68
locked() const69 bool locked() const {return locked_;}
70 };
71
72 class L2
73 {
74 bool locked_;
75
76 public:
L2()77 L2() : locked_(false) {}
78
lock()79 void lock()
80 {
81 TEST_THROW(1);
82 }
83
try_lock()84 bool try_lock()
85 {
86 TEST_THROW(1);
87 return locked_;
88 }
89
unlock()90 void unlock() {locked_ = false;}
91
locked() const92 bool locked() const {return locked_;}
93 };
94
main(int,char **)95 int main(int, char**)
96 {
97 {
98 L0 l0;
99 L0 l1;
100 std::lock(l0, l1);
101 assert(l0.locked());
102 assert(l1.locked());
103 }
104 {
105 L0 l0;
106 L1 l1;
107 std::lock(l0, l1);
108 assert(l0.locked());
109 assert(l1.locked());
110 }
111 {
112 L1 l0;
113 L0 l1;
114 std::lock(l0, l1);
115 assert(l0.locked());
116 assert(l1.locked());
117 }
118 #ifndef TEST_HAS_NO_EXCEPTIONS
119 {
120 L0 l0;
121 L2 l1;
122 try
123 {
124 std::lock(l0, l1);
125 assert(false);
126 }
127 catch (int)
128 {
129 assert(!l0.locked());
130 assert(!l1.locked());
131 }
132 }
133 {
134 L2 l0;
135 L0 l1;
136 try
137 {
138 std::lock(l0, l1);
139 assert(false);
140 }
141 catch (int)
142 {
143 assert(!l0.locked());
144 assert(!l1.locked());
145 }
146 }
147 {
148 L1 l0;
149 L2 l1;
150 try
151 {
152 std::lock(l0, l1);
153 assert(false);
154 }
155 catch (int)
156 {
157 assert(!l0.locked());
158 assert(!l1.locked());
159 }
160 }
161 {
162 L2 l0;
163 L1 l1;
164 try
165 {
166 std::lock(l0, l1);
167 assert(false);
168 }
169 catch (int)
170 {
171 assert(!l0.locked());
172 assert(!l1.locked());
173 }
174 }
175 {
176 L2 l0;
177 L2 l1;
178 try
179 {
180 std::lock(l0, l1);
181 assert(false);
182 }
183 catch (int)
184 {
185 assert(!l0.locked());
186 assert(!l1.locked());
187 }
188 }
189 #endif
190 #if TEST_STD_VER >= 11
191 {
192 L0 l0;
193 L0 l1;
194 L0 l2;
195 std::lock(l0, l1, l2);
196 assert(l0.locked());
197 assert(l1.locked());
198 assert(l2.locked());
199 }
200 #ifndef TEST_HAS_NO_EXCEPTIONS
201 {
202 L2 l0;
203 L2 l1;
204 L2 l2;
205 try
206 {
207 std::lock(l0, l1, l2);
208 assert(false);
209 }
210 catch (int)
211 {
212 assert(!l0.locked());
213 assert(!l1.locked());
214 assert(!l2.locked());
215 }
216 }
217 #endif
218 {
219 L0 l0;
220 L0 l1;
221 L1 l2;
222 std::lock(l0, l1, l2);
223 assert(l0.locked());
224 assert(l1.locked());
225 assert(l2.locked());
226 }
227 {
228 L0 l0;
229 L1 l1;
230 L0 l2;
231 std::lock(l0, l1, l2);
232 assert(l0.locked());
233 assert(l1.locked());
234 assert(l2.locked());
235 }
236 {
237 L1 l0;
238 L0 l1;
239 L0 l2;
240 std::lock(l0, l1, l2);
241 assert(l0.locked());
242 assert(l1.locked());
243 assert(l2.locked());
244 }
245 #ifndef TEST_HAS_NO_EXCEPTIONS
246 {
247 L0 l0;
248 L0 l1;
249 L2 l2;
250 try
251 {
252 std::lock(l0, l1, l2);
253 assert(false);
254 }
255 catch (int)
256 {
257 assert(!l0.locked());
258 assert(!l1.locked());
259 assert(!l2.locked());
260 }
261 }
262 {
263 L0 l0;
264 L2 l1;
265 L0 l2;
266 try
267 {
268 std::lock(l0, l1, l2);
269 assert(false);
270 }
271 catch (int)
272 {
273 assert(!l0.locked());
274 assert(!l1.locked());
275 assert(!l2.locked());
276 }
277 }
278 {
279 L2 l0;
280 L0 l1;
281 L0 l2;
282 try
283 {
284 std::lock(l0, l1, l2);
285 assert(false);
286 }
287 catch (int)
288 {
289 assert(!l0.locked());
290 assert(!l1.locked());
291 assert(!l2.locked());
292 }
293 }
294 {
295 L2 l0;
296 L2 l1;
297 L0 l2;
298 try
299 {
300 std::lock(l0, l1, l2);
301 assert(false);
302 }
303 catch (int)
304 {
305 assert(!l0.locked());
306 assert(!l1.locked());
307 assert(!l2.locked());
308 }
309 }
310 {
311 L2 l0;
312 L0 l1;
313 L2 l2;
314 try
315 {
316 std::lock(l0, l1, l2);
317 assert(false);
318 }
319 catch (int)
320 {
321 assert(!l0.locked());
322 assert(!l1.locked());
323 assert(!l2.locked());
324 }
325 }
326 {
327 L0 l0;
328 L2 l1;
329 L2 l2;
330 try
331 {
332 std::lock(l0, l1, l2);
333 assert(false);
334 }
335 catch (int)
336 {
337 assert(!l0.locked());
338 assert(!l1.locked());
339 assert(!l2.locked());
340 }
341 }
342 {
343 L2 l0;
344 L2 l1;
345 L1 l2;
346 try
347 {
348 std::lock(l0, l1, l2);
349 assert(false);
350 }
351 catch (int)
352 {
353 assert(!l0.locked());
354 assert(!l1.locked());
355 assert(!l2.locked());
356 }
357 }
358 {
359 L2 l0;
360 L1 l1;
361 L2 l2;
362 try
363 {
364 std::lock(l0, l1, l2);
365 assert(false);
366 }
367 catch (int)
368 {
369 assert(!l0.locked());
370 assert(!l1.locked());
371 assert(!l2.locked());
372 }
373 }
374 {
375 L1 l0;
376 L2 l1;
377 L2 l2;
378 try
379 {
380 std::lock(l0, l1, l2);
381 assert(false);
382 }
383 catch (int)
384 {
385 assert(!l0.locked());
386 assert(!l1.locked());
387 assert(!l2.locked());
388 }
389 }
390 #endif // TEST_HAS_NO_EXCEPTIONS
391 {
392 L0 l0;
393 L0 l1;
394 L0 l2;
395 L0 l3;
396 std::lock(l0, l1, l2, l3);
397 assert(l0.locked());
398 assert(l1.locked());
399 assert(l2.locked());
400 assert(l3.locked());
401 }
402 {
403 L0 l0;
404 L0 l1;
405 L0 l2;
406 L1 l3;
407 std::lock(l0, l1, l2, l3);
408 assert(l0.locked());
409 assert(l1.locked());
410 assert(l2.locked());
411 assert(l3.locked());
412 }
413 {
414 L0 l0;
415 L0 l1;
416 L1 l2;
417 L0 l3;
418 std::lock(l0, l1, l2, l3);
419 assert(l0.locked());
420 assert(l1.locked());
421 assert(l2.locked());
422 assert(l3.locked());
423 }
424 {
425 L0 l0;
426 L1 l1;
427 L0 l2;
428 L0 l3;
429 std::lock(l0, l1, l2, l3);
430 assert(l0.locked());
431 assert(l1.locked());
432 assert(l2.locked());
433 assert(l3.locked());
434 }
435 {
436 L1 l0;
437 L0 l1;
438 L0 l2;
439 L0 l3;
440 std::lock(l0, l1, l2, l3);
441 assert(l0.locked());
442 assert(l1.locked());
443 assert(l2.locked());
444 assert(l3.locked());
445 }
446 #ifndef TEST_HAS_NO_EXCEPTIONS
447 {
448 L0 l0;
449 L0 l1;
450 L0 l2;
451 L2 l3;
452 try
453 {
454 std::lock(l0, l1, l2, l3);
455 assert(false);
456 }
457 catch (int)
458 {
459 assert(!l0.locked());
460 assert(!l1.locked());
461 assert(!l2.locked());
462 assert(!l3.locked());
463 }
464 }
465 {
466 L0 l0;
467 L0 l1;
468 L2 l2;
469 L0 l3;
470 try
471 {
472 std::lock(l0, l1, l2, l3);
473 assert(false);
474 }
475 catch (int)
476 {
477 assert(!l0.locked());
478 assert(!l1.locked());
479 assert(!l2.locked());
480 assert(!l3.locked());
481 }
482 }
483 {
484 L0 l0;
485 L2 l1;
486 L0 l2;
487 L0 l3;
488 try
489 {
490 std::lock(l0, l1, l2, l3);
491 assert(false);
492 }
493 catch (int)
494 {
495 assert(!l0.locked());
496 assert(!l1.locked());
497 assert(!l2.locked());
498 assert(!l3.locked());
499 }
500 }
501 {
502 L2 l0;
503 L0 l1;
504 L0 l2;
505 L0 l3;
506 try
507 {
508 std::lock(l0, l1, l2, l3);
509 assert(false);
510 }
511 catch (int)
512 {
513 assert(!l0.locked());
514 assert(!l1.locked());
515 assert(!l2.locked());
516 assert(!l3.locked());
517 }
518 }
519 #endif // TEST_HAS_NO_EXCEPTIONS
520 #endif // TEST_STD_VER >= 11
521
522 return 0;
523 }
524