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
13 // template <class L1, class L2, class... L3>
14 // int try_lock(L1&, L2&, L3&...);
19 #include "test_macros.h"
26 L0() : locked_(false) {}
34 void unlock() {locked_
= false;}
36 bool locked() const {return locked_
;}
44 L1() : locked_(false) {}
52 void unlock() {locked_
= false;}
54 bool locked() const {return locked_
;}
62 L2() : locked_(false) {}
70 void unlock() {locked_
= false;}
72 bool locked() const {return locked_
;}
80 assert(std::try_lock(l0
, l1
) == -1);
87 assert(std::try_lock(l0
, l1
) == 1);
94 assert(std::try_lock(l0
, l1
) == 0);
98 #ifndef TEST_HAS_NO_EXCEPTIONS
104 (void)std::try_lock(l0
, l1
);
109 assert(!l0
.locked());
110 assert(!l1
.locked());
118 (void)std::try_lock(l0
, l1
);
123 assert(!l0
.locked());
124 assert(!l1
.locked());
128 #if TEST_STD_VER >= 11
133 assert(std::try_lock(l0
, l1
, l2
) == -1);
142 assert(std::try_lock(l0
, l1
, l2
) == 0);
143 assert(!l0
.locked());
144 assert(!l1
.locked());
145 assert(!l2
.locked());
147 #ifndef TEST_HAS_NO_EXCEPTIONS
154 (void)std::try_lock(l0
, l1
, l2
);
159 assert(!l0
.locked());
160 assert(!l1
.locked());
161 assert(!l2
.locked());
168 assert(std::try_lock(l0
, l1
, l2
) == 1);
169 assert(!l0
.locked());
170 assert(!l1
.locked());
171 assert(!l2
.locked());
178 assert(std::try_lock(l0
, l1
, l2
) == 2);
179 assert(!l0
.locked());
180 assert(!l1
.locked());
181 assert(!l2
.locked());
187 assert(std::try_lock(l0
, l1
, l2
) == 1);
188 assert(!l0
.locked());
189 assert(!l1
.locked());
190 assert(!l2
.locked());
196 assert(std::try_lock(l0
, l1
, l2
) == 0);
197 assert(!l0
.locked());
198 assert(!l1
.locked());
199 assert(!l2
.locked());
201 #ifndef TEST_HAS_NO_EXCEPTIONS
208 (void)std::try_lock(l0
, l1
, l2
);
213 assert(!l0
.locked());
214 assert(!l1
.locked());
215 assert(!l2
.locked());
224 (void)std::try_lock(l0
, l1
, l2
);
229 assert(!l0
.locked());
230 assert(!l1
.locked());
231 assert(!l2
.locked());
240 (void)std::try_lock(l0
, l1
, l2
);
245 assert(!l0
.locked());
246 assert(!l1
.locked());
247 assert(!l2
.locked());
255 assert(std::try_lock(l0
, l1
, l2
) == 0);
256 assert(!l0
.locked());
257 assert(!l1
.locked());
258 assert(!l2
.locked());
264 assert(std::try_lock(l0
, l1
, l2
) == 0);
265 assert(!l0
.locked());
266 assert(!l1
.locked());
267 assert(!l2
.locked());
273 assert(std::try_lock(l0
, l1
, l2
) == 1);
274 assert(!l0
.locked());
275 assert(!l1
.locked());
276 assert(!l2
.locked());
278 #ifndef TEST_HAS_NO_EXCEPTIONS
283 assert(std::try_lock(l0
, l1
, l2
) == 0);
284 assert(!l0
.locked());
285 assert(!l1
.locked());
286 assert(!l2
.locked());
292 assert(std::try_lock(l0
, l1
, l2
) == 0);
293 assert(!l0
.locked());
294 assert(!l1
.locked());
295 assert(!l2
.locked());
303 (void)std::try_lock(l0
, l1
, l2
);
308 assert(!l0
.locked());
309 assert(!l1
.locked());
310 assert(!l2
.locked());
319 (void)std::try_lock(l0
, l1
, l2
);
324 assert(!l0
.locked());
325 assert(!l1
.locked());
326 assert(!l2
.locked());
335 (void)std::try_lock(l0
, l1
, l2
);
340 assert(!l0
.locked());
341 assert(!l1
.locked());
342 assert(!l2
.locked());
351 (void)std::try_lock(l0
, l1
, l2
);
356 assert(!l0
.locked());
357 assert(!l1
.locked());
358 assert(!l2
.locked());
367 (void)std::try_lock(l0
, l1
, l2
);
372 assert(!l0
.locked());
373 assert(!l1
.locked());
374 assert(!l2
.locked());
383 (void)std::try_lock(l0
, l1
, l2
);
388 assert(!l0
.locked());
389 assert(!l1
.locked());
390 assert(!l2
.locked());
397 assert(std::try_lock(l0
, l1
, l2
) == 0);
398 assert(!l0
.locked());
399 assert(!l1
.locked());
400 assert(!l2
.locked());
408 (void)std::try_lock(l0
, l1
, l2
);
413 assert(!l0
.locked());
414 assert(!l1
.locked());
415 assert(!l2
.locked());
422 assert(std::try_lock(l0
, l1
, l2
) == 0);
423 assert(!l0
.locked());
424 assert(!l1
.locked());
425 assert(!l2
.locked());
431 assert(std::try_lock(l0
, l1
, l2
) == 0);
432 assert(!l0
.locked());
433 assert(!l1
.locked());
434 assert(!l2
.locked());
442 (void)std::try_lock(l0
, l1
, l2
);
447 assert(!l0
.locked());
448 assert(!l1
.locked());
449 assert(!l2
.locked());
458 (void)std::try_lock(l0
, l1
, l2
);
463 assert(!l0
.locked());
464 assert(!l1
.locked());
465 assert(!l2
.locked());
468 #endif // TEST_HAS_NO_EXCEPTIONS
474 assert(std::try_lock(l0
, l1
, l2
, l3
) == -1);
485 assert(std::try_lock(l0
, l1
, l2
, l3
) == 0);
486 assert(!l0
.locked());
487 assert(!l1
.locked());
488 assert(!l2
.locked());
489 assert(!l3
.locked());
496 assert(std::try_lock(l0
, l1
, l2
, l3
) == 1);
497 assert(!l0
.locked());
498 assert(!l1
.locked());
499 assert(!l2
.locked());
500 assert(!l3
.locked());
507 assert(std::try_lock(l0
, l1
, l2
, l3
) == 2);
508 assert(!l0
.locked());
509 assert(!l1
.locked());
510 assert(!l2
.locked());
511 assert(!l3
.locked());
518 assert(std::try_lock(l0
, l1
, l2
, l3
) == 3);
519 assert(!l0
.locked());
520 assert(!l1
.locked());
521 assert(!l2
.locked());
522 assert(!l3
.locked());
524 #endif // TEST_STD_VER >= 11