| 1 | // Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do compile { target c++17 } } |
| 19 | |
| 20 | #include <mutex> |
| 21 | |
| 22 | template<typename T, typename U> struct require_same; |
| 23 | template<typename T> struct require_same<T, T> { using type = void; }; |
| 24 | |
| 25 | template<typename T, typename U> |
| 26 | typename require_same<T, U>::type |
| 27 | check_type(U&) { } |
| 28 | |
| 29 | void |
| 30 | test01() |
| 31 | { |
| 32 | std::scoped_lock l0; |
| 33 | check_type<std::scoped_lock<>>(l0); |
| 34 | |
| 35 | struct BasicLockable { |
| 36 | void lock() { } |
| 37 | void unlock() { } |
| 38 | } m1; |
| 39 | |
| 40 | std::scoped_lock l1(m1); |
| 41 | check_type<std::scoped_lock<BasicLockable>>(l1); |
| 42 | |
| 43 | struct Lockable { |
| 44 | void lock() { } |
| 45 | void unlock() { } |
| 46 | bool try_lock() { return true; } |
| 47 | } m2; |
| 48 | |
| 49 | std::mutex m3; |
| 50 | std::scoped_lock l2(m2, m3); |
| 51 | check_type<std::scoped_lock<Lockable, std::mutex>>(l2); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | test02() |
| 56 | { |
| 57 | std::scoped_lock l0(std::adopt_lock); |
| 58 | check_type<std::scoped_lock<>>(l0); |
| 59 | |
| 60 | struct BasicLockable { |
| 61 | void lock() { } |
| 62 | void unlock() { } |
| 63 | } m1; |
| 64 | |
| 65 | std::scoped_lock l1(std::adopt_lock, m1); |
| 66 | check_type<std::scoped_lock<BasicLockable>>(l1); |
| 67 | |
| 68 | struct Lockable { |
| 69 | void lock() { } |
| 70 | void unlock() { } |
| 71 | bool try_lock() { return true; } |
| 72 | } m2; |
| 73 | |
| 74 | std::mutex m3; |
| 75 | std::scoped_lock l2(std::adopt_lock, m2, m3); |
| 76 | check_type<std::scoped_lock<Lockable, std::mutex>>(l2); |
| 77 | } |