Skip to contentSkip to conversation

READ_ONCE(x)++

Q Akira Yokosawa 2 participants 2 messages Dec 29, 2016

lore
Share & tools
lore
#0 lore raw

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.

Thanks, Akira

#1 lore raw
Quoted 20 lines · Akira Yokosawa

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.

(quoted diff) +1 -1
@@ -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.

Quoted 7 lines

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