From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Yubin Ruan <ablacktshirt@gmail.com>
Cc: perfbook@vger.kernel.org, Akira Yokosawa <akiyks@gmail.com>
Subject: Re: synchronize with a non-atomic flag
Date: Sun, 8 Oct 2017 09:07:38 -0700 [thread overview]
Message-ID: <20171008160738.GZ3521@linux.vnet.ibm.com> (raw)
In-Reply-To: <CAJYFCiMpx4Y++K2C0yWbxKO7UqNY-MOgJWRsA6hipPWQc17wxg@mail.gmail.com>
On Sun, Oct 08, 2017 at 05:12:18PM +0800, Yubin Ruan wrote:
> 2017-10-06 13:52 GMT+08:00 Yubin Ruan <ablacktshirt@gmail.com>:
> > Hi,
> > I saw lots of discussions on the web about possible race when doing
> > synchronization between multiple threads/processes with lock or atomic
> > operations[1][2]. From my point of view most them are over-worrying.
> > But I want to point out some particular issue here to see whether
> > anyone have anything to say.
> >
> > Imagine two processes communicate using only a uint32_t variable in
> > shared memory, like this:
> >
> > // uint32_t variable in shared memory
> > uint32_t flag = 0;
> >
> > //process 1
> > while(1) {
> > if(READ_ONCE(flag) == 0) {
> > do_something();
> > WRITE_ONCE(flag, 1); // let another process to run
> > } else {
> > continue;
> > }
> > }
> >
> > //process 2
> > while(1) {
> > if(READ_ONCE(flag) == 1) {
> > printf("process 2 running...\n");
> > WRITE_ONCE(flag, 0); // let another process to run
> > } else {
> > continue;
> > }
> > }
> >
> > On X86 or X64, I expect this code to run correctly, that is, I will
> > got the two `printf' to printf one after one. That is because:
> >
> > 1) on X86/X64, load/store on 32-bits variable are atomic
>
> Ah...this assumption is wrong at the first place. Atomic access on
> 4-bytes integers is guaranteed only when these integer is aligned on a
> 4-bytes memory address boundary...
Indeed, accesses crossing cachelines normally won't guarantee you
much of anything other than painful debugging sessions. ;-)
Thanx, Paul
> Yubin
>
> > 2) I use READ_ONCE/WRITE_ONCE to prevent possibly harmful compiler
> > optimization on `flag'.
> > 3) I use only one variable to communicate between two processes,
> > so there is no need for any kind of barrier.
> >
> > Does anyone have any objection at that?
> >
> > I know using a lock or atomic operation will save me a lot of
> > argument, but I think those things are unnecessary at this
> > circumstance, and it matter where performance matter, so I am picky
> > here...
> >
> > Yubin
> >
> > [1]: https://software.intel.com/en-us/blogs/2013/01/06/benign-data-races-what-could-possibly-go-wrong
> > [2]: https://www.usenix.org/conference/osdi10/ad-hoc-synchronization-considered-harmful
>
next prev parent reply other threads:[~2017-10-08 16:07 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-06 5:52 synchronize with a non-atomic flag Yubin Ruan
2017-10-06 12:03 ` Akira Yokosawa
2017-10-06 12:35 ` Yubin Ruan
2017-10-06 19:12 ` Paul E. McKenney
2017-10-07 7:04 ` Yubin Ruan
2017-10-07 11:40 ` Akira Yokosawa
2017-10-07 13:43 ` Yubin Ruan
2017-10-07 14:36 ` Akira Yokosawa
2017-10-07 20:20 ` Paul E. McKenney
2017-10-08 9:12 ` Yubin Ruan
2017-10-08 16:07 ` Paul E. McKenney [this message]
2017-10-09 8:40 ` Yubin Ruan
2017-10-09 2:14 ` Paul E. McKenney
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=20171008160738.GZ3521@linux.vnet.ibm.com \
--to=paulmck@linux.vnet.ibm.com \
--cc=ablacktshirt@gmail.com \
--cc=akiyks@gmail.com \
--cc=perfbook@vger.kernel.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