Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / thread / thread.mutex / thread.lock / thread.lock.scoped / adopt_lock.pass.cpp
blob83b29ff669dcada22ed75a987a9afbb62b855e81
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
10 // UNSUPPORTED: c++03, c++11, c++14
12 // <mutex>
14 // template <class ...Mutex> class scoped_lock;
16 // scoped_lock(adopt_lock_t, Mutex&...);
18 #include <mutex>
19 #include <cassert>
20 #include "test_macros.h"
22 struct TestMutex {
23 bool locked = false;
24 TestMutex() = default;
26 void lock() { assert(!locked); locked = true; }
27 bool try_lock() { if (locked) return false; locked = true; return true; }
28 void unlock() { assert(locked); locked = false; }
30 TestMutex(TestMutex const&) = delete;
31 TestMutex& operator=(TestMutex const&) = delete;
34 int main(int, char**)
37 using LG = std::scoped_lock<>;
38 LG lg(std::adopt_lock);
41 TestMutex m1;
42 using LG = std::scoped_lock<TestMutex>;
43 m1.lock();
45 LG lg(std::adopt_lock, m1);
46 assert(m1.locked);
48 assert(!m1.locked);
51 TestMutex m1, m2;
52 using LG = std::scoped_lock<TestMutex, TestMutex>;
53 m1.lock(); m2.lock();
55 LG lg(std::adopt_lock, m1, m2);
56 assert(m1.locked && m2.locked);
58 assert(!m1.locked && !m2.locked);
61 TestMutex m1, m2, m3;
62 using LG = std::scoped_lock<TestMutex, TestMutex, TestMutex>;
63 m1.lock(); m2.lock(); m3.lock();
65 LG lg(std::adopt_lock, m1, m2, m3);
66 assert(m1.locked && m2.locked && m3.locked);
68 assert(!m1.locked && !m2.locked && !m3.locked);
72 return 0;