From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 65195 invoked by alias); 5 Mar 2017 18:38:19 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 65169 invoked by uid 89); 5 Mar 2017 18:38:19 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=bgcolor, recommendations, OTHER, 55674 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 05 Mar 2017 18:38:16 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DFB8B80460; Sun, 5 Mar 2017 18:38:15 +0000 (UTC) Received: from localhost (ovpn-116-41.phx2.redhat.com [10.3.116.41]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v25IcEbd027291; Sun, 5 Mar 2017 13:38:15 -0500 Date: Sun, 05 Mar 2017 18:38:00 -0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Add std::scoped_lock for C++17 Message-ID: <20170305183812.GA15543@redhat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="2oS5YaxWCcQjTEyO" Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.7.1 (2016-10-04) X-SW-Source: 2017-03/txt/msg00219.txt.bz2 --2oS5YaxWCcQjTEyO Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline Content-length: 467 This was just approved at the WG21 meeting. * doc/xml/manual/status_cxx2017.xml: Document P0156R2 status. * doc/html/*: Regenerate. * include/std/mutex (scoped_lock): Implement new C++17 template. * testsuite/30_threads/scoped_lock/cons/1.cc: New test. * testsuite/30_threads/scoped_lock/requirements/ explicit_instantiation.cc: New test. * testsuite/30_threads/scoped_lock/requirements/typedefs.cc: New test. Tested powerpc64le-linux, committed to trunk. --2oS5YaxWCcQjTEyO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" Content-length: 9465 commit 109ebf33d95668712c93627d523afe65354c51f9 Author: Jonathan Wakely Date: Sun Mar 5 17:39:00 2017 +0000 Add std::scoped_lock for C++17 * doc/xml/manual/status_cxx2017.xml: Document P0156R2 status. * doc/html/*: Regenerate. * include/std/mutex (scoped_lock): Implement new C++17 template. * testsuite/30_threads/scoped_lock/cons/1.cc: New test. * testsuite/30_threads/scoped_lock/requirements/ explicit_instantiation.cc: New test. * testsuite/30_threads/scoped_lock/requirements/typedefs.cc: New test. diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml index add0514..1053f2d 100644 --- a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml +++ b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml @@ -719,15 +719,14 @@ Feature-testing recommendations for C++. - Variadic lock_guard - - P0156R0 + + P0156R2 - No - __cpp_lib_lock_guard_variadic >= 201510 + 7 + __cpp_lib_scoped_lock >= 201703 diff --git a/libstdc++-v3/include/std/mutex b/libstdc++-v3/include/std/mutex index d6f3899..6c3f920 100644 --- a/libstdc++-v3/include/std/mutex +++ b/libstdc++-v3/include/std/mutex @@ -556,6 +556,74 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } } +#if __cplusplus > 201402L +#define __cpp_lib_scoped_lock 201703 + /** @brief A scoped lock type for multiple lockable objects. + * + * A scoped_lock controls mutex ownership within a scope, releasing + * ownership in the destructor. + */ + template + class scoped_lock + { + public: + explicit scoped_lock(_MutexTypes&... __m) : _M_devices(std::tie(__m...)) + { std::lock(__m...); } + + explicit scoped_lock(_MutexTypes&... __m, adopt_lock_t) noexcept + : _M_devices(std::tie(__m...)) + { } // calling thread owns mutex + + ~scoped_lock() + { + std::apply([](_MutexTypes&... __m) { + char __i[] __attribute__((__unused__)) = { (__m.unlock(), 0)... }; + }, _M_devices); + } + + scoped_lock(const scoped_lock&) = delete; + scoped_lock& operator=(const scoped_lock&) = delete; + + private: + tuple<_MutexTypes&...> _M_devices; + }; + + template<> + class scoped_lock<> + { + public: + explicit scoped_lock() = default; + explicit scoped_lock(adopt_lock_t) noexcept { } + ~scoped_lock() = default; + + scoped_lock(const scoped_lock&) = delete; + scoped_lock& operator=(const scoped_lock&) = delete; + }; + + template + class scoped_lock<_Mutex> + { + public: + using mutex_type = _Mutex; + + explicit scoped_lock(mutex_type& __m) : _M_device(__m) + { _M_device.lock(); } + + explicit scoped_lock(mutex_type& __m, adopt_lock_t) noexcept + : _M_device(__m) + { } // calling thread owns mutex + + ~scoped_lock() + { _M_device.unlock(); } + + scoped_lock(const scoped_lock&) = delete; + scoped_lock& operator=(const scoped_lock&) = delete; + + private: + mutex_type& _M_device; + }; +#endif // C++17 + #ifdef _GLIBCXX_HAS_GTHREADS /// once_flag struct once_flag diff --git a/libstdc++-v3/testsuite/30_threads/scoped_lock/cons/1.cc b/libstdc++-v3/testsuite/30_threads/scoped_lock/cons/1.cc new file mode 100644 index 0000000..9f1b48c --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/scoped_lock/cons/1.cc @@ -0,0 +1,133 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++1z } } +// { dg-require-cstdint "" } + +// Copyright (C) 2017 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + + +#include +#include + +struct BasicLockable +{ + BasicLockable() : locked(false) { } + + ~BasicLockable() noexcept(false) + { + if (locked) + throw 0; + } + + void lock() + { + if (locked) + throw 0; + locked = true; + } + + void unlock() + { + if (!locked) + throw 0; + locked = false; + } + + bool locked; +}; + +template +struct Lockable +{ + BasicLockable m; + void lock() { m.lock(); } + void unlock() { m.unlock(); } + bool try_lock() { if (m.locked) return false; m.lock(); return true; } +}; + +void test01() +{ + BasicLockable m; + + try + { + std::scoped_lock l(m); + VERIFY( m.locked ); + } + catch (...) + { + VERIFY( false ); + } + + VERIFY( !m.locked ); + + m.lock(); + + try + { + std::scoped_lock l(m, std::adopt_lock); + } + catch (...) + { + VERIFY( false ); + } + + VERIFY( !m.locked ); +} + +void test02() +{ + Lockable<1> m1; + Lockable<2> m2; + + try + { + std::scoped_lock, Lockable<2>> l(m1, m2); + VERIFY( m1.m.locked ); + VERIFY( m2.m.locked ); + } + catch (...) + { + VERIFY( false ); + } + + VERIFY( !m1.m.locked ); + VERIFY( !m2.m.locked ); + + m1.lock(); + m2.lock(); + + try + { + std::scoped_lock, Lockable<2>> l(m1, m2, std::adopt_lock); + VERIFY( m1.m.locked ); + VERIFY( m2.m.locked ); + } + catch (...) + { + VERIFY( false ); + } + + VERIFY( !m1.m.locked ); + VERIFY( !m2.m.locked ); +} + +int main() +{ + test01(); + test02(); +} diff --git a/libstdc++-v3/testsuite/30_threads/scoped_lock/requirements/explicit_instantiation.cc b/libstdc++-v3/testsuite/30_threads/scoped_lock/requirements/explicit_instantiation.cc new file mode 100644 index 0000000..cbf1075 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/scoped_lock/requirements/explicit_instantiation.cc @@ -0,0 +1,33 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++1z } } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } + +// Copyright (C) 2017 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + + +// NB: This file is for testing with NO OTHER INCLUDES. + +#include + +namespace std +{ + template class scoped_lock<>; + template class scoped_lock; + template class scoped_lock; +} diff --git a/libstdc++-v3/testsuite/30_threads/scoped_lock/requirements/typedefs.cc b/libstdc++-v3/testsuite/30_threads/scoped_lock/requirements/typedefs.cc new file mode 100644 index 0000000..55756d8 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/scoped_lock/requirements/typedefs.cc @@ -0,0 +1,33 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++1z } } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } + +// Copyright (C) 2017 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + + +// NB: This file is for testing with NO OTHER INCLUDES. + +#include + +void test01() +{ + // Check for required typedefs + typedef std::scoped_lock test_type; + typedef test_type::mutex_type mutex_type; +} --2oS5YaxWCcQjTEyO--