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(adopt_lock_t, Mutex&...);
18
19 #include <mutex>
20 #include <cassert>
21 #include "test_macros.h"
22
23 struct TestMutex {
24 bool locked = false;
25 TestMutex() = default;
26
lockTestMutex27 void lock() { assert(!locked); locked = true; }
try_lockTestMutex28 bool try_lock() { if (locked) return false; locked = true; return true; }
unlockTestMutex29 void unlock() { assert(locked); locked = false; }
30
31 TestMutex(TestMutex const&) = delete;
32 TestMutex& operator=(TestMutex const&) = delete;
33 };
34
main()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(std::adopt_lock, m1);
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(std::adopt_lock, m1, m2);
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(std::adopt_lock, m1, m2, m3);
67 assert(m1.locked && m2.locked && m3.locked);
68 }
69 assert(!m1.locked && !m2.locked && !m3.locked);
70 }
71
72 }
73