AMDGPU: Fix verifier assert with out of bounds subregister indexes (#119799)
[llvm-project.git] / libcxx / test / std / thread / thread.mutex / thread.lock.algorithm / try_lock.pass.cpp
blobac508b5f9161f7eadf5e24f2958809c6e0c9f531
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: no-threads
11 // <mutex>
13 // template <class L1, class L2, class... L3>
14 // int try_lock(L1&, L2&, L3&...);
16 #include <mutex>
17 #include <cassert>
19 #include "test_macros.h"
21 class L0
23 bool locked_;
25 public:
26 L0() : locked_(false) {}
28 bool try_lock()
30 locked_ = true;
31 return locked_;
34 void unlock() {locked_ = false;}
36 bool locked() const {return locked_;}
39 class L1
41 bool locked_;
43 public:
44 L1() : locked_(false) {}
46 bool try_lock()
48 locked_ = false;
49 return locked_;
52 void unlock() {locked_ = false;}
54 bool locked() const {return locked_;}
57 class L2
59 bool locked_;
61 public:
62 L2() : locked_(false) {}
64 bool try_lock()
66 TEST_THROW(1);
67 return locked_;
70 void unlock() {locked_ = false;}
72 bool locked() const {return locked_;}
75 int main(int, char**)
78 L0 l0;
79 L0 l1;
80 assert(std::try_lock(l0, l1) == -1);
81 assert(l0.locked());
82 assert(l1.locked());
85 L0 l0;
86 L1 l1;
87 assert(std::try_lock(l0, l1) == 1);
88 assert(!l0.locked());
89 assert(!l1.locked());
92 L1 l0;
93 L0 l1;
94 assert(std::try_lock(l0, l1) == 0);
95 assert(!l0.locked());
96 assert(!l1.locked());
98 #ifndef TEST_HAS_NO_EXCEPTIONS
100 L0 l0;
101 L2 l1;
104 (void)std::try_lock(l0, l1);
105 assert(false);
107 catch (int)
109 assert(!l0.locked());
110 assert(!l1.locked());
114 L2 l0;
115 L0 l1;
118 (void)std::try_lock(l0, l1);
119 assert(false);
121 catch (int)
123 assert(!l0.locked());
124 assert(!l1.locked());
127 #endif
128 #if TEST_STD_VER >= 11
130 L0 l0;
131 L0 l1;
132 L0 l2;
133 assert(std::try_lock(l0, l1, l2) == -1);
134 assert(l0.locked());
135 assert(l1.locked());
136 assert(l2.locked());
139 L1 l0;
140 L1 l1;
141 L1 l2;
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
149 L2 l0;
150 L2 l1;
151 L2 l2;
154 (void)std::try_lock(l0, l1, l2);
155 assert(false);
157 catch (int)
159 assert(!l0.locked());
160 assert(!l1.locked());
161 assert(!l2.locked());
165 L0 l0;
166 L1 l1;
167 L2 l2;
168 assert(std::try_lock(l0, l1, l2) == 1);
169 assert(!l0.locked());
170 assert(!l1.locked());
171 assert(!l2.locked());
173 #endif
175 L0 l0;
176 L0 l1;
177 L1 l2;
178 assert(std::try_lock(l0, l1, l2) == 2);
179 assert(!l0.locked());
180 assert(!l1.locked());
181 assert(!l2.locked());
184 L0 l0;
185 L1 l1;
186 L0 l2;
187 assert(std::try_lock(l0, l1, l2) == 1);
188 assert(!l0.locked());
189 assert(!l1.locked());
190 assert(!l2.locked());
193 L1 l0;
194 L0 l1;
195 L0 l2;
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
203 L0 l0;
204 L0 l1;
205 L2 l2;
208 (void)std::try_lock(l0, l1, l2);
209 assert(false);
211 catch (int)
213 assert(!l0.locked());
214 assert(!l1.locked());
215 assert(!l2.locked());
219 L0 l0;
220 L2 l1;
221 L0 l2;
224 (void)std::try_lock(l0, l1, l2);
225 assert(false);
227 catch (int)
229 assert(!l0.locked());
230 assert(!l1.locked());
231 assert(!l2.locked());
235 L2 l0;
236 L0 l1;
237 L0 l2;
240 (void)std::try_lock(l0, l1, l2);
241 assert(false);
243 catch (int)
245 assert(!l0.locked());
246 assert(!l1.locked());
247 assert(!l2.locked());
250 #endif
252 L1 l0;
253 L1 l1;
254 L0 l2;
255 assert(std::try_lock(l0, l1, l2) == 0);
256 assert(!l0.locked());
257 assert(!l1.locked());
258 assert(!l2.locked());
261 L1 l0;
262 L0 l1;
263 L1 l2;
264 assert(std::try_lock(l0, l1, l2) == 0);
265 assert(!l0.locked());
266 assert(!l1.locked());
267 assert(!l2.locked());
270 L0 l0;
271 L1 l1;
272 L1 l2;
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
280 L1 l0;
281 L1 l1;
282 L2 l2;
283 assert(std::try_lock(l0, l1, l2) == 0);
284 assert(!l0.locked());
285 assert(!l1.locked());
286 assert(!l2.locked());
289 L1 l0;
290 L2 l1;
291 L1 l2;
292 assert(std::try_lock(l0, l1, l2) == 0);
293 assert(!l0.locked());
294 assert(!l1.locked());
295 assert(!l2.locked());
298 L2 l0;
299 L1 l1;
300 L1 l2;
303 (void)std::try_lock(l0, l1, l2);
304 assert(false);
306 catch (int)
308 assert(!l0.locked());
309 assert(!l1.locked());
310 assert(!l2.locked());
314 L2 l0;
315 L2 l1;
316 L0 l2;
319 (void)std::try_lock(l0, l1, l2);
320 assert(false);
322 catch (int)
324 assert(!l0.locked());
325 assert(!l1.locked());
326 assert(!l2.locked());
330 L2 l0;
331 L0 l1;
332 L2 l2;
335 (void)std::try_lock(l0, l1, l2);
336 assert(false);
338 catch (int)
340 assert(!l0.locked());
341 assert(!l1.locked());
342 assert(!l2.locked());
346 L0 l0;
347 L2 l1;
348 L2 l2;
351 (void)std::try_lock(l0, l1, l2);
352 assert(false);
354 catch (int)
356 assert(!l0.locked());
357 assert(!l1.locked());
358 assert(!l2.locked());
362 L2 l0;
363 L2 l1;
364 L1 l2;
367 (void)std::try_lock(l0, l1, l2);
368 assert(false);
370 catch (int)
372 assert(!l0.locked());
373 assert(!l1.locked());
374 assert(!l2.locked());
378 L2 l0;
379 L1 l1;
380 L2 l2;
383 (void)std::try_lock(l0, l1, l2);
384 assert(false);
386 catch (int)
388 assert(!l0.locked());
389 assert(!l1.locked());
390 assert(!l2.locked());
394 L1 l0;
395 L2 l1;
396 L2 l2;
397 assert(std::try_lock(l0, l1, l2) == 0);
398 assert(!l0.locked());
399 assert(!l1.locked());
400 assert(!l2.locked());
403 L0 l0;
404 L2 l1;
405 L1 l2;
408 (void)std::try_lock(l0, l1, l2);
409 assert(false);
411 catch (int)
413 assert(!l0.locked());
414 assert(!l1.locked());
415 assert(!l2.locked());
419 L1 l0;
420 L0 l1;
421 L2 l2;
422 assert(std::try_lock(l0, l1, l2) == 0);
423 assert(!l0.locked());
424 assert(!l1.locked());
425 assert(!l2.locked());
428 L1 l0;
429 L2 l1;
430 L0 l2;
431 assert(std::try_lock(l0, l1, l2) == 0);
432 assert(!l0.locked());
433 assert(!l1.locked());
434 assert(!l2.locked());
437 L2 l0;
438 L0 l1;
439 L1 l2;
442 (void)std::try_lock(l0, l1, l2);
443 assert(false);
445 catch (int)
447 assert(!l0.locked());
448 assert(!l1.locked());
449 assert(!l2.locked());
453 L2 l0;
454 L1 l1;
455 L0 l2;
458 (void)std::try_lock(l0, l1, l2);
459 assert(false);
461 catch (int)
463 assert(!l0.locked());
464 assert(!l1.locked());
465 assert(!l2.locked());
468 #endif // TEST_HAS_NO_EXCEPTIONS
470 L0 l0;
471 L0 l1;
472 L0 l2;
473 L0 l3;
474 assert(std::try_lock(l0, l1, l2, l3) == -1);
475 assert(l0.locked());
476 assert(l1.locked());
477 assert(l2.locked());
478 assert(l3.locked());
481 L1 l0;
482 L0 l1;
483 L0 l2;
484 L0 l3;
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());
492 L0 l0;
493 L1 l1;
494 L0 l2;
495 L0 l3;
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());
503 L0 l0;
504 L0 l1;
505 L1 l2;
506 L0 l3;
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());
514 L0 l0;
515 L0 l1;
516 L0 l2;
517 L1 l3;
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
526 return 0;