From: Yubin Ruan <ablacktshirt@gmail.com>
To: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Cc: perfbook@vger.kernel.org, Akira Yokosawa <akiyks@gmail.com>
Subject: Re: synchronize with a non-atomic flag
Date: Mon, 9 Oct 2017 16:40:11 +0800 [thread overview]
Message-ID: <20171009084009.GA5758@HP> (raw)
In-Reply-To: <20171008160738.GZ3521@linux.vnet.ibm.com>
On Sun, Oct 08, 2017 at 09:07:38AM -0700, Paul E. McKenney wrote:
> 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. ;-)
I see similar interfaces in the Linux kernel source[1]:
#define atomic_set(v, i) ((v)->counter = (i))
#define atomic_read(v) ((v)->counter)
which set and read 'atomically' from a atomic variable, and by `atomic', they
simply mean:
The setting is atomic in that the return values of the atomic operations by
all threads are guaranteed to be correct reflecting either the value that
has been set with this operation or set with another operation.
The read is atomic in that the return value is guaranteed to be one of the
values initialized or modified with the interface operations if a proper
implicit or explicit memory barrier is used after possible runtime
initialization by any other thread and the value is modified only with the
interface operations.
(but still, the compare-and-swap operations still involve lock)
Are those operations atomic because the `atomic_t' is defined as a struct
typedef struct { int counter; } atomic_t;
and therefore proper alignment and atomic attribute is guaranteed by the
compiler and the CPU? If I do something like this:
atomic_t v = ATOMIC_INIT(0); // globally visible
atomic_set(&v, 1); //process 1
atomic_set(&v, 2); //process 2
int i = atomic_read(&v); // process 3
will process 3 see any intermediate value between 1 and 2?
Yubin
[1]: Documentation/core-api/atomic_ops.rst
next prev parent reply other threads:[~2017-10-09 8:40 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
2017-10-09 8:40 ` Yubin Ruan [this message]
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=20171009084009.GA5758@HP \
--to=ablacktshirt@gmail.com \
--cc=akiyks@gmail.com \
--cc=paulmck@linux.vnet.ibm.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