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 // <mutex>
12
13 // template <class L1, class L2, class... L3>
14 // int try_lock(L1&, L2&, L3&...);
15
16 #include <mutex>
17 #include <cassert>
18
19 #include "test_macros.h"
20
21 class L0
22 {
23 bool locked_;
24
25 public:
L0()26 L0() : locked_(false) {}
27
try_lock()28 bool try_lock()
29 {
30 locked_ = true;
31 return locked_;
32 }
33
unlock()34 void unlock() {locked_ = false;}
35
locked() const36 bool locked() const {return locked_;}
37 };
38
39 class L1
40 {
41 bool locked_;
42
43 public:
L1()44 L1() : locked_(false) {}
45
try_lock()46 bool try_lock()
47 {
48 locked_ = false;
49 return locked_;
50 }
51
unlock()52 void unlock() {locked_ = false;}
53
locked() const54 bool locked() const {return locked_;}
55 };
56
57 class L2
58 {
59 bool locked_;
60
61 public:
L2()62 L2() : locked_(false) {}
63
try_lock()64 bool try_lock()
65 {
66 TEST_THROW(1);
67 return locked_;
68 }
69
unlock()70 void unlock() {locked_ = false;}
71
locked() const72 bool locked() const {return locked_;}
73 };
74
main(int,char **)75 int main(int, char**)
76 {
77 {
78 L0 l0;
79 L0 l1;
80 assert(std::try_lock(l0, l1) == -1);
81 assert(l0.locked());
82 assert(l1.locked());
83 }
84 {
85 L0 l0;
86 L1 l1;
87 assert(std::try_lock(l0, l1) == 1);
88 assert(!l0.locked());
89 assert(!l1.locked());
90 }
91 {
92 L1 l0;
93 L0 l1;
94 assert(std::try_lock(l0, l1) == 0);
95 assert(!l0.locked());
96 assert(!l1.locked());
97 }
98 #ifndef TEST_HAS_NO_EXCEPTIONS
99 {
100 L0 l0;
101 L2 l1;
102 try
103 {
104 (void)std::try_lock(l0, l1);
105 assert(false);
106 }
107 catch (int)
108 {
109 assert(!l0.locked());
110 assert(!l1.locked());
111 }
112 }
113 {
114 L2 l0;
115 L0 l1;
116 try
117 {
118 (void)std::try_lock(l0, l1);
119 assert(false);
120 }
121 catch (int)
122 {
123 assert(!l0.locked());
124 assert(!l1.locked());
125 }
126 }
127 #endif
128 #if TEST_STD_VER >= 11
129 {
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());
137 }
138 {
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());
146 }
147 #ifndef TEST_HAS_NO_EXCEPTIONS
148 {
149 L2 l0;
150 L2 l1;
151 L2 l2;
152 try
153 {
154 (void)std::try_lock(l0, l1, l2);
155 assert(false);
156 }
157 catch (int)
158 {
159 assert(!l0.locked());
160 assert(!l1.locked());
161 assert(!l2.locked());
162 }
163 }
164 {
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());
172 }
173 #endif
174 {
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());
182 }
183 {
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());
191 }
192 {
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());
200 }
201 #ifndef TEST_HAS_NO_EXCEPTIONS
202 {
203 L0 l0;
204 L0 l1;
205 L2 l2;
206 try
207 {
208 (void)std::try_lock(l0, l1, l2);
209 assert(false);
210 }
211 catch (int)
212 {
213 assert(!l0.locked());
214 assert(!l1.locked());
215 assert(!l2.locked());
216 }
217 }
218 {
219 L0 l0;
220 L2 l1;
221 L0 l2;
222 try
223 {
224 (void)std::try_lock(l0, l1, l2);
225 assert(false);
226 }
227 catch (int)
228 {
229 assert(!l0.locked());
230 assert(!l1.locked());
231 assert(!l2.locked());
232 }
233 }
234 {
235 L2 l0;
236 L0 l1;
237 L0 l2;
238 try
239 {
240 (void)std::try_lock(l0, l1, l2);
241 assert(false);
242 }
243 catch (int)
244 {
245 assert(!l0.locked());
246 assert(!l1.locked());
247 assert(!l2.locked());
248 }
249 }
250 #endif
251 {
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());
259 }
260 {
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());
268 }
269 {
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());
277 }
278 #ifndef TEST_HAS_NO_EXCEPTIONS
279 {
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());
287 }
288 {
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());
296 }
297 {
298 L2 l0;
299 L1 l1;
300 L1 l2;
301 try
302 {
303 (void)std::try_lock(l0, l1, l2);
304 assert(false);
305 }
306 catch (int)
307 {
308 assert(!l0.locked());
309 assert(!l1.locked());
310 assert(!l2.locked());
311 }
312 }
313 {
314 L2 l0;
315 L2 l1;
316 L0 l2;
317 try
318 {
319 (void)std::try_lock(l0, l1, l2);
320 assert(false);
321 }
322 catch (int)
323 {
324 assert(!l0.locked());
325 assert(!l1.locked());
326 assert(!l2.locked());
327 }
328 }
329 {
330 L2 l0;
331 L0 l1;
332 L2 l2;
333 try
334 {
335 (void)std::try_lock(l0, l1, l2);
336 assert(false);
337 }
338 catch (int)
339 {
340 assert(!l0.locked());
341 assert(!l1.locked());
342 assert(!l2.locked());
343 }
344 }
345 {
346 L0 l0;
347 L2 l1;
348 L2 l2;
349 try
350 {
351 (void)std::try_lock(l0, l1, l2);
352 assert(false);
353 }
354 catch (int)
355 {
356 assert(!l0.locked());
357 assert(!l1.locked());
358 assert(!l2.locked());
359 }
360 }
361 {
362 L2 l0;
363 L2 l1;
364 L1 l2;
365 try
366 {
367 (void)std::try_lock(l0, l1, l2);
368 assert(false);
369 }
370 catch (int)
371 {
372 assert(!l0.locked());
373 assert(!l1.locked());
374 assert(!l2.locked());
375 }
376 }
377 {
378 L2 l0;
379 L1 l1;
380 L2 l2;
381 try
382 {
383 (void)std::try_lock(l0, l1, l2);
384 assert(false);
385 }
386 catch (int)
387 {
388 assert(!l0.locked());
389 assert(!l1.locked());
390 assert(!l2.locked());
391 }
392 }
393 {
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());
401 }
402 {
403 L0 l0;
404 L2 l1;
405 L1 l2;
406 try
407 {
408 (void)std::try_lock(l0, l1, l2);
409 assert(false);
410 }
411 catch (int)
412 {
413 assert(!l0.locked());
414 assert(!l1.locked());
415 assert(!l2.locked());
416 }
417 }
418 {
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());
426 }
427 {
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());
435 }
436 {
437 L2 l0;
438 L0 l1;
439 L1 l2;
440 try
441 {
442 (void)std::try_lock(l0, l1, l2);
443 assert(false);
444 }
445 catch (int)
446 {
447 assert(!l0.locked());
448 assert(!l1.locked());
449 assert(!l2.locked());
450 }
451 }
452 {
453 L2 l0;
454 L1 l1;
455 L0 l2;
456 try
457 {
458 (void)std::try_lock(l0, l1, l2);
459 assert(false);
460 }
461 catch (int)
462 {
463 assert(!l0.locked());
464 assert(!l1.locked());
465 assert(!l2.locked());
466 }
467 }
468 #endif // TEST_HAS_NO_EXCEPTIONS
469 {
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());
479 }
480 {
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());
490 }
491 {
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());
501 }
502 {
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());
512 }
513 {
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());
523 }
524 #endif // TEST_STD_VER >= 11
525
526 return 0;
527 }
528