From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Make barrier::arrival_token a move-only class type
Date: Thu, 11 Mar 2021 17:54:10 +0000 [thread overview]
Message-ID: <YEpZQq1gPa3aBTeh@redhat.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 900 bytes --]
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.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2814 bytes --]
commit 5643f6f396ef7f60d317aef07dd98978cec6afd0
Author: Jonathan Wakely <jwakely@redhat.com>
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()
reply other threads:[~2021-03-11 17:54 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YEpZQq1gPa3aBTeh@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).