1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads
11 // This test hangs forever when built against libstdc++ (Oct 2016).
12 // UNSUPPORTED: stdlib=libstdc++
14 // This test isn't quite standards-conforming: it's testing our specific
15 // algorithm, where when lx.try_lock() fails we start the next attempt
16 // with an unconditional lx.lock(). Thus our algorithm can handle a list
17 // of mutexes where at-most-one of them is of the evil type `class L1`,
18 // but will loop forever if two or more of them are `class L1`.
22 // template <class L1, class L2, class... L3>
23 // void lock(L1&, L2&, L3&...);
28 #include "test_macros.h"
35 L0() : locked_(false) {}
48 void unlock() {locked_
= false;}
50 bool locked() const {return locked_
;}
58 L1() : locked_(false) {}
71 void unlock() {locked_
= false;}
73 bool locked() const {return locked_
;}
81 L2() : locked_(false) {}
94 void unlock() {locked_
= false;}
96 bool locked() const {return locked_
;}
122 #ifndef TEST_HAS_NO_EXCEPTIONS
133 assert(!l0
.locked());
134 assert(!l1
.locked());
147 assert(!l0
.locked());
148 assert(!l1
.locked());
161 assert(!l0
.locked());
162 assert(!l1
.locked());
175 assert(!l0
.locked());
176 assert(!l1
.locked());
189 assert(!l0
.locked());
190 assert(!l1
.locked());
194 #if TEST_STD_VER >= 11
199 std::lock(l0
, l1
, l2
);
204 #ifndef TEST_HAS_NO_EXCEPTIONS
211 std::lock(l0
, l1
, l2
);
216 assert(!l0
.locked());
217 assert(!l1
.locked());
218 assert(!l2
.locked());
226 std::lock(l0
, l1
, l2
);
235 std::lock(l0
, l1
, l2
);
244 std::lock(l0
, l1
, l2
);
249 #ifndef TEST_HAS_NO_EXCEPTIONS
256 std::lock(l0
, l1
, l2
);
261 assert(!l0
.locked());
262 assert(!l1
.locked());
263 assert(!l2
.locked());
272 std::lock(l0
, l1
, l2
);
277 assert(!l0
.locked());
278 assert(!l1
.locked());
279 assert(!l2
.locked());
288 std::lock(l0
, l1
, l2
);
293 assert(!l0
.locked());
294 assert(!l1
.locked());
295 assert(!l2
.locked());
304 std::lock(l0
, l1
, l2
);
309 assert(!l0
.locked());
310 assert(!l1
.locked());
311 assert(!l2
.locked());
320 std::lock(l0
, l1
, l2
);
325 assert(!l0
.locked());
326 assert(!l1
.locked());
327 assert(!l2
.locked());
336 std::lock(l0
, l1
, l2
);
341 assert(!l0
.locked());
342 assert(!l1
.locked());
343 assert(!l2
.locked());
352 std::lock(l0
, l1
, l2
);
357 assert(!l0
.locked());
358 assert(!l1
.locked());
359 assert(!l2
.locked());
368 std::lock(l0
, l1
, l2
);
373 assert(!l0
.locked());
374 assert(!l1
.locked());
375 assert(!l2
.locked());
384 std::lock(l0
, l1
, l2
);
389 assert(!l0
.locked());
390 assert(!l1
.locked());
391 assert(!l2
.locked());
394 #endif // TEST_HAS_NO_EXCEPTIONS
400 std::lock(l0
, l1
, l2
, l3
);
411 std::lock(l0
, l1
, l2
, l3
);
422 std::lock(l0
, l1
, l2
, l3
);
433 std::lock(l0
, l1
, l2
, l3
);
444 std::lock(l0
, l1
, l2
, l3
);
450 #ifndef TEST_HAS_NO_EXCEPTIONS
458 std::lock(l0
, l1
, l2
, l3
);
463 assert(!l0
.locked());
464 assert(!l1
.locked());
465 assert(!l2
.locked());
466 assert(!l3
.locked());
476 std::lock(l0
, l1
, l2
, l3
);
481 assert(!l0
.locked());
482 assert(!l1
.locked());
483 assert(!l2
.locked());
484 assert(!l3
.locked());
494 std::lock(l0
, l1
, l2
, l3
);
499 assert(!l0
.locked());
500 assert(!l1
.locked());
501 assert(!l2
.locked());
502 assert(!l3
.locked());
512 std::lock(l0
, l1
, l2
, l3
);
517 assert(!l0
.locked());
518 assert(!l1
.locked());
519 assert(!l2
.locked());
520 assert(!l3
.locked());
523 #endif // TEST_HAS_NO_EXCEPTIONS
524 #endif // TEST_STD_VER >= 11