public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "Simon.Richter at hogyros dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug rtl-optimization/109930] transform atomic exchange to unconditional store when old value is unused?
Date: Thu, 01 Jun 2023 01:40:48 +0000	[thread overview]
Message-ID: <bug-109930-4-7IpWeiULyP@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-109930-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #5 from Simon Richter <Simon.Richter at hogyros dot de> ---
> Btw if you know the old state then there is presumably no concurrent access here and so you don't need atomic, let alone sequential consistency.

I know it in some, but not all cases.

Basically, what I do is

        auto old_x = x.load();

    retry:
        switch(old_x) {
        case 1:
            if(!x.compare_exchange_weak(old_x, 2))
                 goto retry;
            stop_timer();
            old_x = x.exchange(4);
            assert(old_x == 2);
            break;
        case 2:
            // we must have preempted another instance of this function
            // do nothing
            break;
        case 3:
            // handle timeout
            ...
            break;
        case 4:
            // handle operation complete
            ...
        }

This is in code for timeout handling in a realtime system, the timer interrupt
can preempt this. State 1 is "operation in progress", state 2 is "operation
finished", state 3 is "operation timed out", and state 4 is "operation finished
and timer stopped", and the timer interrupt will try to switch from 1 to 3.

The transient state 2 then solves the race between the timer expiring and
stopping the timer (which is asynchronous because the interrupt controller has
a few cycles delay).

So the switch from state 2 to state 4 has release semantics.

      parent reply	other threads:[~2023-06-01  1:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-22 10:49 [Bug rtl-optimization/109930] New: " Simon.Richter at hogyros dot de
2023-05-22 12:38 ` [Bug rtl-optimization/109930] " rguenth at gcc dot gnu.org
2023-05-22 15:31 ` pinskia at gcc dot gnu.org
2023-05-22 17:55 ` Simon.Richter at hogyros dot de
2023-05-31 17:29 ` wilco at gcc dot gnu.org
2023-06-01  1:40 ` Simon.Richter at hogyros dot de [this message]

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-109930-4-7IpWeiULyP@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).