Discussions of the Parallel Programming book
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Akira Yokosawa <akiyks@gmail.com>
Cc: perfbook@vger.kernel.org
Subject: Re: [Q] READ_ONCE(x)++
Date: Wed, 28 Dec 2016 19:43:31 -0800	[thread overview]
Message-ID: <20161229034331.GA3742@linux.vnet.ibm.com> (raw)
In-Reply-To: <0da22bdc-5d80-ee43-0abc-745abb8a9155@gmail.com>

On Thu, Dec 29, 2016 at 11:54:02AM +0900, Akira Yokosawa wrote:
> Hi Paul,
> 
> As I'm not so familiar with Linux kernel programming,
> I found the following hunk in commit 7945ae1a06c5 difficult to grasp at first glance.
> 
> @@ -564,7 +584,7 @@ the other will wait until the first thread releases the lock.
>   38     exit(-1);
>   39   }
>   40   for (i = 0; i < 3; i++) {
> - 41     ACCESS_ONCE(x)++;
> + 41     READ_ONCE(x)++;
>   42     poll(NULL, 0, 5);
>   43   }
>   44   if (pthread_mutex_unlock(pmlp) != 0) {
> 
> "The name READ_ONCE() seems to imply it is used for read access, but what
> happens when it is used with a "++" operator?" was what I thought.
> 
> "ACCESS_ONCE(x)++" was already somewhat confusing for me.
> 
> Once you know its definition, you can see there is no problem. But it still
> looks strange...
> 
> Don't kernel programmers feel strangeness in "READ_ONCE(x)++"?
> 
> This is just a random question. But if you could add some explanation of the
> usage, that would be of help for novice programmers.

Heh!  Good catch, thank you!

This would give a compiler error in the Linux kernel (I think, anyway),
but worked by accident given the definitions perfbook uses.

I have fixed this with your Reported-by.

							Thanx, Paul


      reply	other threads:[~2016-12-29  3:43 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-29  2:54 [Q] READ_ONCE(x)++ Akira Yokosawa
2016-12-29  3:43 ` Paul E. McKenney [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=20161229034331.GA3742@linux.vnet.ibm.com \
    --to=paulmck@linux.vnet.ibm.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