public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "pdimov at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/114865] New: std::atomic<X>::compare_exchange_strong seems to hang under GCC 13 on Ubuntu 23.04
Date: Fri, 26 Apr 2024 15:50:28 +0000	[thread overview]
Message-ID: <bug-114865-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114865

            Bug ID: 114865
           Summary: std::atomic<X>::compare_exchange_strong seems to hang
                    under GCC 13 on Ubuntu 23.04
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pdimov at gmail dot com
  Target Milestone: ---

I'm getting weird hangs on Github Actions when using
`std::atomic<state_type>::compare_exchange_strong` under GCC 13 on Ubuntu 23.04
(only; GCC 12 and earlier on Ubuntu 22.04 and earlier work). `state_type` is
defined as

```
struct state_type
{
    std::uint64_t timestamp;
    std::uint16_t clock_seq;
};
```

and the code doing the CAS is

```
auto oldst = ps_->load( std::memory_order_relaxed );

for( ;; )
{
    auto newst = get_new_state( oldst );

    if( ps_->compare_exchange_strong( oldst, newst, std::memory_order_relaxed,
std::memory_order_relaxed ) )
    {
        state_ = newst;
        break;
    }
}
```

where `ps` is of type `std::atomic<state_type>*`.

At a glance, I see nothing immediately wrong with the generated code
(https://godbolt.org/z/8Ee3hrTz8).

However, when I change `state_type` to

```
struct state_type
{
    std::uint64_t timestamp;
    std::uint16_t clock_seq;
    std::uint16_t padding[ 3 ];
};
```
the hangs disappear. This leads me to think that the problem is caused by the
original struct having padding, which isn't being handled correctly for some
reason.

As we know, `std::atomic<T>::compare_exchange_strong` is carefully specified to
take and return `expected` by reference, such that it can both compare the
entire object as if via `memcmp` (including the padding), and return it as if
by `memcpy`, again including the padding. Even though the padding bits of the
initial value returned by the atomic load are unspecified, at most one
iteration of the loop would be required for the padding bits to converge and
for the CAS to succeed.

However, going by the symptoms alone, this doesn't seem to be the case here.

The problem may well be inside libatomic, of course; I have no way to tell.

One GHA run showing the issue is
https://github.com/boostorg/uuid/actions/runs/8821753835, where only the GCC 13
job times out.

             reply	other threads:[~2024-04-26 15:50 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-26 15:50 pdimov at gmail dot com [this message]
2024-04-26 17:47 ` [Bug libstdc++/114865] " pdimov at gmail dot com
2024-04-26 19:40 ` pdimov at gmail dot com
2024-04-26 23:14 ` [Bug libstdc++/114865] [13/14/15 Regression] " pinskia at gcc dot gnu.org
2024-04-26 23:16 ` pinskia at gcc dot gnu.org
2024-04-27  0:13 ` pdimov at gmail dot com
2024-04-27  0:16 ` pinskia at gcc dot gnu.org
2024-04-27  0:16 ` pinskia at gcc dot gnu.org
2024-04-27  0:22 ` pinskia at gcc dot gnu.org
2024-04-27  0:25 ` pinskia at gcc dot gnu.org
2024-04-27  0:29 ` pdimov at gmail dot com
2024-04-27  0:31 ` pdimov at gmail dot com
2024-04-27  0:35 ` [Bug libstdc++/114865] [13/14/15 Regression] std::atomic<X>::compare_exchange_strong seems to hang under GCC 13 for C++11 pinskia at gcc dot gnu.org
2024-04-27  0:38 ` pdimov at gmail dot com
2024-04-27  0:42 ` pinskia at gcc dot gnu.org
2024-04-27  0:42 ` pdimov at gmail dot com
2024-05-02 11:40 ` redi at gcc dot gnu.org
2024-05-02 11:41 ` redi at gcc dot gnu.org
2024-05-02 11:44 ` redi at gcc dot gnu.org
2024-05-02 11:47 ` redi at gcc dot gnu.org
2024-05-02 11:55 ` redi at gcc dot gnu.org
2024-05-02 15:13 ` pdimov at gmail dot com
2024-05-21  9:20 ` jakub at gcc dot gnu.org

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=bug-114865-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).