]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/adopt_lock.pass.cpp
Vendor import of libc++ trunk r300422:
[FreeBSD/FreeBSD.git] / test / std / thread / thread.mutex / thread.lock / thread.lock.scoped / adopt_lock.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // UNSUPPORTED: libcpp-has-no-threads
11 // UNSUPPORTED: c++98, c++03, c++11, c++14
12
13 // <mutex>
14
15 // template <class ...Mutex> class scoped_lock;
16
17 // scoped_lock(Mutex&..., adopt_lock_t);
18
19 #include <mutex>
20 #include <cassert>
21 #include "test_macros.h"
22
23 struct TestMutex {
24     bool locked = false;
25     TestMutex() = default;
26
27     void lock() { assert(!locked); locked = true; }
28     bool try_lock() { if (locked) return false; locked = true; return true; }
29     void unlock() { assert(locked); locked = false; }
30
31     TestMutex(TestMutex const&) = delete;
32     TestMutex& operator=(TestMutex const&) = delete;
33 };
34
35 int main()
36 {
37     {
38         using LG = std::scoped_lock<>;
39         LG lg(std::adopt_lock);
40     }
41     {
42         TestMutex m1;
43         using LG = std::scoped_lock<TestMutex>;
44         m1.lock();
45         {
46             LG lg(m1, std::adopt_lock);
47             assert(m1.locked);
48         }
49         assert(!m1.locked);
50     }
51     {
52         TestMutex m1, m2;
53         using LG = std::scoped_lock<TestMutex, TestMutex>;
54         m1.lock(); m2.lock();
55         {
56             LG lg(m1, m2, std::adopt_lock);
57             assert(m1.locked && m2.locked);
58         }
59         assert(!m1.locked && !m2.locked);
60     }
61     {
62         TestMutex m1, m2, m3;
63         using LG = std::scoped_lock<TestMutex, TestMutex, TestMutex>;
64         m1.lock(); m2.lock(); m3.lock();
65         {
66             LG lg(m1, m2, m3, std::adopt_lock);
67             assert(m1.locked && m2.locked && m3.locked);
68         }
69         assert(!m1.locked && !m2.locked && !m3.locked);
70     }
71
72 }