74 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// UNSUPPORTED: no-threads10// UNSUPPORTED: c++03, c++11, c++1411 12// <mutex>13 14// template <class ...Mutex> class scoped_lock;15 16// scoped_lock(adopt_lock_t, Mutex&...);17 18#include <mutex>19#include <cassert>20#include "test_macros.h"21 22struct TestMutex {23 bool locked = false;24 TestMutex() = default;25 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; }29 30 TestMutex(TestMutex const&) = delete;31 TestMutex& operator=(TestMutex const&) = delete;32};33 34int main(int, char**)35{36 {37 using LG = std::scoped_lock<>;38 LG lg(std::adopt_lock);39 }40 {41 TestMutex m1;42 using LG = std::scoped_lock<TestMutex>;43 m1.lock();44 {45 LG lg(std::adopt_lock, m1);46 assert(m1.locked);47 }48 assert(!m1.locked);49 }50 {51 TestMutex m1, m2;52 using LG = std::scoped_lock<TestMutex, TestMutex>;53 m1.lock(); m2.lock();54 {55 LG lg(std::adopt_lock, m1, m2);56 assert(m1.locked && m2.locked);57 }58 assert(!m1.locked && !m2.locked);59 }60 {61 TestMutex m1, m2, m3;62 using LG = std::scoped_lock<TestMutex, TestMutex, TestMutex>;63 m1.lock(); m2.lock(); m3.lock();64 {65 LG lg(std::adopt_lock, m1, m2, m3);66 assert(m1.locked && m2.locked && m3.locked);67 }68 assert(!m1.locked && !m2.locked && !m3.locked);69 }70 71 72 return 0;73}74