Skip to contentSkip to conversation

make atomic_t volatile on all architectures

PATCH Bodo Eggert 3 participants 3 messages Aug 9, 2007 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 12 lines · Jerry Jiang

On Wed, 8 Aug 2007 21:18:25 -0700 (PDT)

Quoted 11 lines · Chris Snook
Quoted 7 lines

Some architectures currently do not declare the contents of an atomic_t to be
volatile. This causes confusion since atomic_read() might not actually read anything if an optimizing compiler re-uses a value stored in a register, which can break code that loops until something external changes the value of an atomic_t.

I'd be *much* happier with "atomic_read()" doing the "volatile" instead.

Quoted 7 lines
Quoted 2 lines

The fact is, volatile on data structures is a bug. It's a wart in the C language. It shouldn't be used.

Why? It's a wart! Is it due to unclear C standard on volatile related point?

Why the *volatile-accesses-in-code* is acceptable, does C standard make it clear?

http://lwn.net/Articles/233482/

Signature · 4 lines
Fun things to slip into your budget
Heisenberg Compensator upgrade kit

Friß, Spammer: [email protected]
#4 lore raw
Quoted 5 lines · On Thu, 09 Aug 2007 11:10:16 +0200 Bodo Eggert <[email protected]> wrote:
Quoted 3 lines

Why the *volatile-accesses-in-code* is acceptable, does C standard make it clear?

http://lwn.net/Articles/233482/

I have read this article before, but What Linus said only focusing on the conclusion-- The semantics of it are so unclear as
to be totally useless.

and still not to said "Why the *volatile-accesses-in-code* is acceptable"

-- Jerry

Quoted 5 lines
Signature · 4 lines
Fun things to slip into your budget
Heisenberg Compensator upgrade kit

Friß, Spammer: [email protected]
#5 lore raw
Quoted 3 lines · Jerry Jiang

and still not to said "Why the *volatile-accesses-in-code* is acceptable"

I don't think volatile is necessarily wonderful in code _either_. So I think the "atomic_read()" issue would be even better off if we just made sure everybody behaves well and has the right barriers.

So the difference between "volatile in code" and "volatile on data structures" is that the latter is *always* wrong (and wrong for very fundamental reasons).

But "volatile in code" can be perfectly fine. If you hide the volatile in an accessor function, and just specify that the rules for that function is that it always loads from memory but doesn't imply any memory barriers, than that is fine. And I think those kinds of semantics may well be perfectly sane for "atomic_read()".

So I think a volatile access inside "atomic_read()" at least has well-defined semantics. Are they the best possible semantics? I dunno. I can imagine that it's perfectly fine, but on the other hand, it would be interesting to see any code for which it matters - it's entirely possible that the code itself is the problem, and should instead have a "cpu_relax()" or similar that implies a barrier.

Linus