From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 6C7683897818 for ; Thu, 11 Mar 2021 17:54:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6C7683897818 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-549-0tmL5wKNM5O_kIgp6n0m3g-1; Thu, 11 Mar 2021 12:54:12 -0500 X-MC-Unique: 0tmL5wKNM5O_kIgp6n0m3g-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 842D38030DB; Thu, 11 Mar 2021 17:54:11 +0000 (UTC) Received: from localhost (unknown [10.33.36.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 29B6E5D9F2; Thu, 11 Mar 2021 17:54:11 +0000 (UTC) Date: Thu, 11 Mar 2021 17:54:10 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Make barrier::arrival_token a move-only class type Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="JdhrmRCOrNCCkaB8" Content-Disposition: inline X-Spam-Status: No, score=-14.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Mar 2021 17:54:18 -0000 --JdhrmRCOrNCCkaB8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline The standard only specifies that barrier::arrival_token is a move constructible and move assignable type. We originally used a scoped enum type, but that means we do not diagnose non-portable code that makes copies of arrival tokens (or compares them for equality, or uses them as keys in map!) This wraps the enum in a move-only class type, so that users are forced to pass it correctly. The move constructor and move assignment operator of the new class do not zero out the moved-from token, as that would add additional instructions. That means that passing a moved-from token will work with our implementation, despite being a bug in the user code. We could consider doing that zeroing out in debug mode. libstdc++-v3/ChangeLog: * include/std/barrier (barrier::arrival_token): New move-only class that encapsulates the underlying token value. Tested powerpc64le-linux. Committed to trunk. --JdhrmRCOrNCCkaB8 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 5643f6f396ef7f60d317aef07dd98978cec6afd0 Author: Jonathan Wakely Date: Thu Mar 11 16:57:20 2021 libstdc++: Make barrier::arrival_token a move-only class type The standard only specifies that barrier::arrival_token is a move constructible and move assignable type. We originally used a scoped enum type, but that means we do not diagnose non-portable code that makes copies of arrival tokens (or compares them for equality, or uses them as keys in map!) This wraps the enum in a move-only class type, so that users are forced to pass it correctly. The move constructor and move assignment operator of the new class do not zero out the moved-from token, as that would add additional instructions. That means that passing a moved-from token will work with our implementation, despite being a bug in the user code. We could consider doing that zeroing out in debug mode. libstdc++-v3/ChangeLog: * include/std/barrier (barrier::arrival_token): New move-only class that encapsulates the underlying token value. diff --git a/libstdc++-v3/include/std/barrier b/libstdc++-v3/include/std/barrier index e09212dfcb9..6f2b9873500 100644 --- a/libstdc++-v3/include/std/barrier +++ b/libstdc++-v3/include/std/barrier @@ -209,15 +209,27 @@ It looks different from literature pseudocode for two main reasons: __algorithm_t _M_b; public: - using arrival_token = typename __tree_barrier<_CompletionF>::arrival_token; + class arrival_token final + { + public: + arrival_token(arrival_token&&) = default; + arrival_token& operator=(arrival_token&&) = default; + ~arrival_token() = default; + + private: + friend class barrier; + using __token = typename __algorithm_t::arrival_token; + explicit arrival_token(__token __tok) noexcept : _M_tok(__tok) { } + __token _M_tok; + }; static constexpr ptrdiff_t max() noexcept { return __algorithm_t::max(); } - explicit barrier(ptrdiff_t __count, - _CompletionF __completion = _CompletionF()) - : _M_b(__count, std::move(__completion)) + explicit + barrier(ptrdiff_t __count, _CompletionF __completion = _CompletionF()) + : _M_b(__count, std::move(__completion)) { } barrier(barrier const&) = delete; @@ -225,11 +237,11 @@ It looks different from literature pseudocode for two main reasons: [[nodiscard]] arrival_token arrive(ptrdiff_t __update = 1) - { return _M_b.arrive(__update); } + { return arrival_token{_M_b.arrive(__update)}; } void wait(arrival_token&& __phase) const - { _M_b.wait(std::move(__phase)); } + { _M_b.wait(std::move(__phase._M_tok)); } void arrive_and_wait() --JdhrmRCOrNCCkaB8--