• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
12 
13 // <mutex>
14 
15 // template <class ...Mutex> class lock_guard;
16 
17 // lock_guard(Mutex&..., adopt_lock_t);
18 
19 // MODULES_DEFINES: _LIBCPP_ABI_VARIADIC_LOCK_GUARD
20 #define _LIBCPP_ABI_VARIADIC_LOCK_GUARD
21 #include <mutex>
22 #include <cassert>
23 
24 struct TestMutex {
25     bool locked = false;
26     TestMutex() = default;
27 
lockTestMutex28     void lock() { assert(!locked); locked = true; }
try_lockTestMutex29     bool try_lock() { if (locked) return false; locked = true; return true; }
unlockTestMutex30     void unlock() { assert(locked); locked = false; }
31 
32     TestMutex(TestMutex const&) = delete;
33     TestMutex& operator=(TestMutex const&) = delete;
34 };
35 
main()36 int main()
37 {
38     {
39         using LG = std::lock_guard<>;
40         LG lg(std::adopt_lock);
41     }
42     {
43         TestMutex m1, m2;
44         using LG = std::lock_guard<TestMutex, TestMutex>;
45         m1.lock(); m2.lock();
46         {
47             LG lg(m1, m2, std::adopt_lock);
48             assert(m1.locked && m2.locked);
49         }
50         assert(!m1.locked && !m2.locked);
51     }
52     {
53         TestMutex m1, m2, m3;
54         using LG = std::lock_guard<TestMutex, TestMutex, TestMutex>;
55         m1.lock(); m2.lock(); m3.lock();
56         {
57             LG lg(m1, m2, m3, std::adopt_lock);
58             assert(m1.locked && m2.locked && m3.locked);
59         }
60         assert(!m1.locked && !m2.locked && !m3.locked);
61     }
62 
63 }
64