Skip to contentSkip to conversation

make atomic_read() behave consistently across all architectures

PATCH In review Chris Snook 30 participants 311 messages Aug 9, 2007 – Sep 11, 2007 Page 2 of 2

Subsystems (5)
lore
Share & tools
lore
#284 lore raw
In reply to Paul E. McKenney
Quoted 4 lines · Paul E. McKenney

The one exception to this being the case where process-level code is communicating to an interrupt handler running on that same CPU -- on all CPUs that I am aware of, a given CPU always sees its own writes in order.

Yes but that is due to the code path effectively continuing in the interrupt handler. The cpu makes sure that op codes being executed always see memory in a consistent way. The basic ordering problem with out of order writes is therefore coming from other processors concurrently executing code and holding variables in registers that are modified elsewhere. The only solution that I know of are one or the other form of barrier.

#285 lore raw
Quoted 7 lines · Christoph Lameter
Quoted 4 lines · Paul E. McKenney

The one exception to this being the case where process-level code is communicating to an interrupt handler running on that same CPU -- on all CPUs that I am aware of, a given CPU always sees its own writes in order.

Quoted 7 lines

Yes but that is due to the code path effectively continuing in the interrupt handler. The cpu makes sure that op codes being executed always see memory in a consistent way. The basic ordering problem with out of order writes is therefore coming from other processors concurrently executing code and holding variables in registers that are modified elsewhere. The only solution that I know of are one or the other form of barrier.

So we are agreed then -- volatile accesses may be of some assistance when interacting with interrupt handlers running on the same CPU (presumably when using per-CPU variables), but are generally useless when sharing variables among CPUs. Correct?

Thanx, Paul

#286 lore raw
In reply to Christoph Lameter
Quoted 7 lines
Quoted 4 lines

"volatile" has nothing to do with reordering. atomic_dec() writes to memory, so it _does_ have "volatile semantics", implicitly, as long as the compiler cannot optimise the atomic variable away completely -- any store counts as a side effect.

Stores can be reordered. Only x86 has (mostly) implicit write ordering. So no atomic_dec has no volatile semantics

Read again: I said the C "volatile" construct has nothing to do with CPU memory access reordering.

Quoted 3 lines

and may be reordered on a variety
of processors. Writes to memory may not follow code order on several processors.

The _compiler_ isn't allowed to reorder things here. Yes, of course you do need stronger barriers for many purposes, volatile isn't all that useful you know.

Segher

#287 lore raw
In reply to Chris Snook
Quoted 5 lines
Quoted 2 lines

6674: while (atomic_read(&j->DSPWrite) > 0)
6675- atomic_dec(&j->DSPWrite);

If the maintainer of this code doesn't see a compelling reason to add cpu_relax() in this loop, then it should be patched.

Shouldn't it be just re-written without the loop:

	if ((tmp = atomic_read(&j->DSPWrite)) > 0)
		atomic_sub(&j->DSPWrite, tmp);

Has all the same bugs, but runs much faster :-)

-Tony

#288 lore raw
In reply to Ilpo Järvinen
Quoted 5 lines · Ilpo Järvinen
Quoted 2 lines · Herbert Xu

We've been through that already. If it's a busy-wait it should use cpu_relax.

Quoted 7 lines

I looked around a bit by using some command lines and ended up wondering if these are equal to busy-wait case (and should be fixed) or not:

./drivers/telephony/ixj.c
6674: while (atomic_read(&j->DSPWrite) > 0)
6675- atomic_dec(&j->DSPWrite);

...besides that, there are couple of more similar cases in the same file (with braces)...

atomic_dec() already has volatile behavior everywhere, so this is semantically okay, but this code (and any like it) should be calling cpu_relax() each iteration through the loop, unless there's a compelling reason not to. I'll allow that for some hardware drivers (possibly this one) such a compelling reason may exist, but hardware-independent core subsystems probably have no excuse.

If the maintainer of this code doesn't see a compelling reason not to add cpu_relax() in this loop, then it should be patched.

-- Chris

#289 lore raw
In reply to Paul Mackerras
Quoted 12 lines · Paul Mackerras

The uses of atomic_read where one might want it to allow caching of the result seem to me to fall into 3 categories:

1. Places that are buggy because of a race arising from the way it's used.

2. Places where there is a race but it doesn't matter because we're doing some clever trick.

3. Places where there is some locking in place that eliminates any potential race.

Quoted 7 lines

In case 1, adding volatile won't solve the race, of course, but it's hard to argue that we shouldn't do something because it will slow down buggy code. Case 2 is hopefully pretty rare and accompanied by large comment blocks, and in those cases caching the result of atomic_read explicitly in a local variable would probably make the code clearer. And in case 3 there is no reason to use atomic_t at all; we might as well just use an int.

In 2 + 3 you may increment the atomic variable in some places. The value of the atomic variable may not matter because you only do optimizations.

Checking a atomic_t for a definite state has to involve either some side conditions (lock only taken if refcount is <= 0 or so) or done by changing the state (see f.e. atomic_inc_unless_zero).

Quoted 3 lines

So I don't see any good reason to make the atomic API more complex by having "volatile" and "non-volatile" versions of atomic_read. It should just have the "volatile" behaviour.

If you want to make it less complex then drop volatile which causes weird side effects without solving any problems as you just pointed out.

#290 lore raw
Quoted 7 lines · Christoph Lameter
Quoted 3 lines · Paul Mackerras

So I don't see any good reason to make the atomic API more complex by having "volatile" and "non-volatile" versions of atomic_read. It should just have the "volatile" behaviour.

If you want to make it less complex then drop volatile which causes weird side effects without solving any problems as you just pointed out.

The other set of problems are communication between process context and interrupt/NMI handlers. Volatile does help here. And the performance impact of volatile is pretty near zero, so why have the non-volatile variant?

Thanx, Paul

#291 lore raw
In reply to Satyam Sharma
Quoted 5 lines · Satyam Sharma

I can't speak for this particular case, but there could be similar code examples elsewhere, where we do the atomic ops on an atomic_t object inside a higher-level locking scheme that would take care of the kind of problem you're referring to here. It would be useful for such or similar code if the compiler kept the value of that atomic object in a register.

If there is a higher-level locking scheme then there is no point to using atomic_t variables. Atomic_t is specifically for the situation where multiple CPUs are updating a variable without locking.

Paul.

#292 lore raw
Quoted 9 lines
Quoted 5 lines

I can't speak for this particular case, but there could be similar code examples elsewhere, where we do the atomic ops on an atomic_t object inside a higher-level locking scheme that would take care of the kind of problem you're referring to here. It would be useful for such or similar code if the compiler kept the value of that atomic object in a register.

If there is a higher-level locking scheme then there is no point to using atomic_t variables. Atomic_t is specifically for the situation where multiple CPUs are updating a variable without locking.

And don't forget about the case where it is an I/O device that is updating the memory (in buffer descriptors or similar). The driver needs to do a "volatile" atomic read to get at the most recent version of that data, which can be important for optimising latency (or throughput even). There is no other way the kernel can get that info -- doing an MMIO read is way way too expensive.

Segher

#293 lore raw
In reply to Paul Mackerras
Quoted 11 lines
Quoted 3 lines

A volatile default would disable optimizations for atomic_read. atomic_read without volatile would allow for full optimization by the compiler. Seems that this is what one wants in many cases.

Name one such case.

An atomic_read should do a load from memory. If the programmer puts an atomic_read() in the code then the compiler should emit a load for it, not re-use a value returned by a previous atomic_read. I do not believe it would ever be useful for the compiler to collapse two atomic_read statements into a single load.

An atomic_read() implemented as a "normal" C variable read would allow that read to be combined with another "normal" read from that variable. This could perhaps be marginally useful, although I'd bet you cannot see it unless counting cycles on a simulator or counting bits in the binary size.

With an asm() implementation, the compiler can not do this; with a "volatile" implementation (either volatile variable or volatile-cast), this invokes undefined behaviour (in both C and GCC).

Segher

#294 lore raw
In reply to Paul Mackerras
Quoted 8 lines · Paul Mackerras

Satyam Sharma writes:
In the kernel we use atomic variables in precisely those situations where a variable is potentially accessed concurrently by multiple CPUs, and where each CPU needs to see updates done by other CPUs in a timely fashion. That is what they are for. Therefore the compiler must not cache values of atomic variables in registers; each atomic_read must result in a load and each atomic_set must result in a store. Anything else will just lead to subtle bugs.

Amen.
--
vda

#295 lore raw
Quoted 11 lines · Denys Vlasenko
Quoted 8 lines · Paul Mackerras

Satyam Sharma writes:
In the kernel we use atomic variables in precisely those situations where a variable is potentially accessed concurrently by multiple CPUs, and where each CPU needs to see updates done by other CPUs in a timely fashion. That is what they are for. Therefore the compiler must not cache values of atomic variables in registers; each atomic_read must result in a load and each atomic_set must result in a store. Anything else will just lead to subtle bugs.

Amen.

A "timely" fashion? One cannot rely on something like that when coding. The visibility of updates is insured by barriers and not by some fuzzy notion of "timeliness".

#296 lore raw
Quoted 11 lines · Christoph Lameter
Quoted 10 lines · Denys Vlasenko
Quoted 8 lines · Paul Mackerras

Satyam Sharma writes:
In the kernel we use atomic variables in precisely those situations where a variable is potentially accessed concurrently by multiple CPUs, and where each CPU needs to see updates done by other CPUs in a timely fashion. That is what they are for. Therefore the compiler must not cache values of atomic variables in registers; each atomic_read must result in a load and each atomic_set must result in a store. Anything else will just lead to subtle bugs.

Quoted 5 lines
Quoted 1 line

Amen.

A "timely" fashion? One cannot rely on something like that when coding. The visibility of updates is insured by barriers and not by some fuzzy notion of "timeliness".

But here you do have some notion of time:

	while (atomic_read(&x))
		continue;

"continue when other CPU(s) decrement it down to zero". If "read" includes an insn which accesses RAM, you will see "new" value sometime after other CPU decrements it. "Sometime after" is on the order of nanoseconds here. It is a valid concept of time, right?

The whole confusion is about whether atomic_read implies "read from RAM" or not. I am in a camp which thinks it does. You are in an opposite one.

We just need a less ambiguous name.

What about this:

/**
* atomic_read - read atomic variable
* @v: pointer of type atomic_t
*
* Atomically reads the value of @v.
* No compiler barrier implied.
*/
#define atomic_read(v) ((v)->counter)

+/**
+ * atomic_read_uncached - read atomic variable from memory
+ * @v: pointer of type atomic_t
+ *
+ * Atomically reads the value of @v. This is guaranteed to emit an insn
+ * which accesses memory, atomically. No ordering guarantees!
+ */
+#define atomic_read_uncached(v) asm_or_volatile_ptr_magic(v)

I was thinking of s/atomic_read/atomic_get/ too, but it implies "taking" atomic a-la get_cpu()...
--
vda

#297 lore raw
In reply to Satyam Sharma
Quoted 5 lines · Satyam Sharma

(C)

$ cat tp3.c
int a;

void func(void)

Quoted 10 lines
{
	*(volatile int *)&a = 10;
	*(volatile int *)&a = 20;
}
$ gcc -Os -S tp3.c
$ cat tp3.s
...
movl    $10, a
movl    $20, a
...

I'm curious about one minor tangential point. Why, instead of:

	b = *(volatile int *)&a;

why can't this just be expressed as:

	b = (volatile int)a;

Isn't it the contents of a that's volatile, i.e. it's value can change invisibly to the compiler, and that's why you want to force a read from memory? Why do you need the "*(volatile int *)&" construct?

-Bill

#298 lore raw

Hi Bill,

Quoted 22 lines · Bill Fink
Quoted 5 lines · Satyam Sharma

(C)

$ cat tp3.c
int a;

void func(void)

Quoted 10 lines
{
	*(volatile int *)&a = 10;
	*(volatile int *)&a = 20;
}
$ gcc -Os -S tp3.c
$ cat tp3.s
...
movl    $10, a
movl    $20, a
...

I'm curious about one minor tangential point. Why, instead of:

	b = *(volatile int *)&a;
Quoted 7 lines

why can't this just be expressed as:

	b = (volatile int)a;

Isn't it the contents of a that's volatile, i.e. it's value can change invisibly to the compiler, and that's why you want to force a read from memory? Why do you need the "*(volatile int *)&" construct?

"b = (volatile int)a;" doesn't help us because a cast to a qualified type has the same effect as a cast to an unqualified version of that type, as mentioned in 6.5.4:4 (footnote 86) of the standard. Note that "volatile" is a type-qualifier, not a type itself, so a cast of the _object_ itself to a qualified-type i.e. (volatile int) would not make the access itself volatile-qualified.

To serve our purposes, it is necessary for us to take the address of this (non-volatile) object, cast the resulting _pointer_ to the corresponding volatile-qualified pointer-type, and then dereference it. This makes that particular _access_ be volatile-qualified, without the object itself being such. Also note that the (dereferenced) result is also a valid lvalue and hence can be used in "*(volatile int *)&a = b;" kind of construction (which we use for the atomic_set case).

Satyam

#299 lore raw
Quoted 42 lines · Satyam Sharma

Hi Bill,

Quoted 22 lines · Bill Fink
Quoted 5 lines · Satyam Sharma

(C)

$ cat tp3.c
int a;

void func(void)

Quoted 10 lines
{
	*(volatile int *)&a = 10;
	*(volatile int *)&a = 20;
}
$ gcc -Os -S tp3.c
$ cat tp3.s
...
movl    $10, a
movl    $20, a
...

I'm curious about one minor tangential point. Why, instead of:

	b = *(volatile int *)&a;
Quoted 7 lines

why can't this just be expressed as:

	b = (volatile int)a;

Isn't it the contents of a that's volatile, i.e. it's value can change invisibly to the compiler, and that's why you want to force a read from memory? Why do you need the "*(volatile int *)&" construct?

"b = (volatile int)a;" doesn't help us because a cast to a qualified type has the same effect as a cast to an unqualified version of that type, as mentioned in 6.5.4:4 (footnote 86) of the standard. Note that "volatile" is a type-qualifier, not a type itself, so a cast of the _object_ itself to a qualified-type i.e. (volatile int) would not make the access itself volatile-qualified.

Quoted 7 lines

To serve our purposes, it is necessary for us to take the address of this (non-volatile) object, cast the resulting _pointer_ to the corresponding volatile-qualified pointer-type, and then dereference it. This makes that particular _access_ be volatile-qualified, without the object itself being such. Also note that the (dereferenced) result is also a valid lvalue and hence can be used in "*(volatile int *)&a = b;" kind of construction (which we use for the atomic_set case).

Here, I should obviously admit that the semantics of *(volatile int *)& aren't any neater or well-defined in the _language standard_ at all. The standard does say (verbatim) "precisely what constitutes as access to object of volatile-qualified type is implementation-defined", but GCC does help us out here by doing the right thing. Accessing the non-volatile object there using the volatile-qualified pointer-type cast makes GCC treat the object stored at that memory address itself as if it were a volatile object, thus making the access end up having what we're calling as "volatility" semantics here.

Honestly, given such confusion, and the propensity of the "volatile" type-qualifier keyword to be ill-defined (or at least poorly understood, often inconsistently implemented), I'd (again) express my opinion that it would be best to avoid its usage, given other alternatives do exist.

#300 lore raw

[ Bill tells me in private communication he gets this already, but I
think it's more complicated than the shoddy explanation I'd made earlier so would wish to make this clearer in detail one last time, for the benefit of others listening in or reading the archives. ]

Quoted 22 lines · Satyam Sharma

On Thu, 16 Aug 2007, Satyam Sharma wrote:
[...]

Quoted 20 lines · Bill Fink
Quoted 5 lines

[...]
I'm curious about one minor tangential point. Why, instead of:

	b = *(volatile int *)&a;
Quoted 7 lines

why can't this just be expressed as:

	b = (volatile int)a;

Isn't it the contents of a that's volatile, i.e. it's value can change invisibly to the compiler, and that's why you want to force a read from memory? Why do you need the "*(volatile int *)&" construct?

"b = (volatile int)a;" doesn't help us because a cast to a qualified type has the same effect as a cast to an unqualified version of that type, as mentioned in 6.5.4:4 (footnote 86) of the standard. Note that "volatile" is a type-qualifier, not a type itself, so a cast of the _object_ itself to a qualified-type i.e. (volatile int) would not make the access itself volatile-qualified.

Casts don't produce lvalues, and the cast ((volatile int)a) does not produce the object-int-a-qualified-as-"volatile" -- in fact, the result of the above cast is whatever is the _value_ of "int a", with the access to that object having _already_ taken place, as per the actual type-qualification of the object (that was originally declared as being _non-volatile_, in fact). Hence, defining atomic_read() as:

#define atomic_read(v) ((volatile int)((v)->counter))

would be buggy and not give "volatility" semantics at all, unless the "counter" object itself isn't volatile-qualified already (which it isn't).

The result of the cast itself being the _value_ of the int object, and not the object itself (i.e., not an lvalue), is thereby independent of type-qualification in that cast itself (it just wouldn't make any difference), hence the "cast to a qualified type has the same effect as a cast to an unqualified version of that type" bit in section 6.5.4:4 of the standard.

Quoted 7 lines
Quoted 7 lines

To serve our purposes, it is necessary for us to take the address of this (non-volatile) object, cast the resulting _pointer_ to the corresponding volatile-qualified pointer-type, and then dereference it. This makes that particular _access_ be volatile-qualified, without the object itself being such. Also note that the (dereferenced) result is also a valid lvalue and hence can be used in "*(volatile int *)&a = b;" kind of construction (which we use for the atomic_set case).

Dereferencing using the *(pointer-type-cast)& construct, OTOH, serves us well:

#define atomic_read(v) (*(volatile int *)&(v)->counter)

Firstly, note that the cast here being (volatile int *) and not (int * volatile) qualifies the type of the _object_ being pointed to by the pointer in question as being volatile-qualified, and not the pointer itself (6.2.5:27 of the standard, and 6.3.2.3:2 allows us to convert from a pointer-to-non-volatile-qualified-int to a pointer-to- volatile-qualified-int, which suits us just fine) -- but note that the _access_ to that address itself has not yet occurred.

_After_ specifying the memory address as containing a volatile-qualified- int-type object, (and GCC co-operates as mentioned below), we proceed to dereference it, which is when the _actual access_ occurs, therefore with "volatility" semantics this time.

Interesting.

Quoted 10 lines

Here, I should obviously admit that the semantics of *(volatile int *)& aren't any neater or well-defined in the _language standard_ at all. The standard does say (verbatim) "precisely what constitutes as access to object of volatile-qualified type is implementation-defined", but GCC does help us out here by doing the right thing. Accessing the non-volatile object there using the volatile-qualified pointer-type cast makes GCC treat the object stored at that memory address itself as if it were a volatile object, thus making the access end up having what we're calling as "volatility" semantics here.

Quoted 4 lines

Honestly, given such confusion, and the propensity of the "volatile" type-qualifier keyword to be ill-defined (or at least poorly understood, often inconsistently implemented), I'd (again) express my opinion that it would be best to avoid its usage, given other alternatives do exist.

Satyam

#301 lore raw
In reply to Satyam Sharma
Quoted 5 lines

Here, I should obviously admit that the semantics of *(volatile int *)& aren't any neater or well-defined in the _language standard_ at all. The standard does say (verbatim) "precisely what constitutes as access to object of volatile-qualified type is implementation-defined", but GCC does help us out here by doing the right thing.

Where do you get that idea? GCC manual, section 6.1, "When is a Volatile Object Accessed?" doesn't say anything of the kind. PR33053 and some others.

Quoted 4 lines

Honestly, given such confusion, and the propensity of the "volatile" type-qualifier keyword to be ill-defined (or at least poorly understood, often inconsistently implemented), I'd (again) express my opinion that it would be best to avoid its usage, given other alternatives do exist.

Yeah. Or we can have an email thread like this every time someone proposes a patch that uses an atomic variable ;-)

Segher

#302 lore raw
Quoted 7 lines · Segher Boessenkool
Quoted 5 lines

Here, I should obviously admit that the semantics of *(volatile int *)& aren't any neater or well-defined in the _language standard_ at all. The standard does say (verbatim) "precisely what constitutes as access to object of volatile-qualified type is implementation-defined", but GCC does help us out here by doing the right thing.

Where do you get that idea?

Try a testcase (experimentally verify).

Quoted 3 lines

GCC manual, section 6.1, "When
is a Volatile Object Accessed?" doesn't say anything of the kind.

True, "implementation-defined" as per the C standard _is_ supposed to mean "unspecified behaviour where each implementation documents how the choice is made". So ok, probably GCC isn't "documenting" this implementation-defined behaviour which it is supposed to, but can't really fault them much for this, probably.

#303 lore raw
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

Here, I should obviously admit that the semantics of *(volatile int *)& aren't any neater or well-defined in the _language standard_ at all. The standard does say (verbatim) "precisely what constitutes as access to object of volatile-qualified type is implementation-defined", but GCC does help us out here by doing the right thing.

Where do you get that idea?

Try a testcase (experimentally verify).

That doesn't prove anything. Experiments can only disprove things.

Quoted 9 lines
Quoted 3 lines

GCC manual, section 6.1, "When
is a Volatile Object Accessed?" doesn't say anything of the kind.

True, "implementation-defined" as per the C standard _is_ supposed to mean "unspecified behaviour where each implementation documents how the choice is made". So ok, probably GCC isn't "documenting" this implementation-defined behaviour which it is supposed to, but can't really fault them much for this, probably.

GCC _is_ documenting this, namely in this section 6.1. It doesn't mention volatile-casted stuff. Draw your own conclusions.

Segher

#304 lore raw
Quoted 1 line · Segher Boessenkool
Quoted 1 line
Quoted 1 line

GCC manual, section 6.1, "When

      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 1 line
Quoted 1 line
Quoted 1 line

is a Volatile Object Accessed?" doesn't say anything of the

      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 1 line
Quoted 1 line
Quoted 1 line

kind.

      ^^^^^
Quoted 1 line
Quoted 1 line

True, "implementation-defined" as per the C standard _is_ supposed to mean

    ^^^^^
Quoted 2 lines
Quoted 2 lines

"unspecified behaviour where each implementation documents how the choice is made". So ok, probably GCC isn't "documenting" this

                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 4 lines
Quoted 2 lines

implementation-defined behaviour which it is supposed to, but can't really fault them much for this, probably.

GCC _is_ documenting this, namely in this section 6.1.

(Again totally petty, but) Yes, but ...

Quoted 1 line

It doesn't

^^^^^^^^^^

Quoted 1 line

mention volatile-casted stuff. Draw your own conclusions.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

... exactly. So that's why I said "GCC isn't documenting _this_".

Man, try _reading_ mails before replying to them ...

#305 lore raw
In reply to Satyam Sharma
Quoted 4 lines

Note that "volatile"
is a type-qualifier, not a type itself, so a cast of the _object_ itself to a qualified-type i.e. (volatile int) would not make the access itself volatile-qualified.

There is no such thing as "volatile-qualified access" defined anywhere; there only is the concept of a "volatile-qualified *object*".

Quoted 7 lines

To serve our purposes, it is necessary for us to take the address of this (non-volatile) object, cast the resulting _pointer_ to the corresponding volatile-qualified pointer-type, and then dereference it. This makes that particular _access_ be volatile-qualified, without the object itself being such. Also note that the (dereferenced) result is also a valid lvalue and hence can be used in "*(volatile int *)&a = b;" kind of construction (which we use for the atomic_set case).

There is a quite convincing argument that such an access _is_ an access to a volatile object; see GCC PR21568 comment #9. This probably isn't the last word on the matter though...

Segher

#306 lore raw
Quoted 4 lines

There is a quite convincing argument that such an access _is_ an access to a volatile object; see GCC PR21568 comment #9. This probably isn't the last word on the matter though...

I find this argument completely convincing and retract the contrary argument that I've made many times in this forum and others. You learn something new every day.

Just in case it wasn't clear:
int i;
*(volatile int *)&i=2;

In this case, there *is* an access to a volatile object. This is the end result of the the standard's definition of what it means to apply the 'volatile int *' cast to '&i' and then apply the '*' operator to the result and use it as an lvalue.

C does not define the type of an object by how it is defined but by how it is accessed!

DS

#307 lore raw

[ Your mailer drops Cc: lists, munges headers,
does all sorts of badness. Please fix that. ]

Quoted 9 lines · David Schwartz
Quoted 4 lines

There is a quite convincing argument that such an access _is_ an access to a volatile object; see GCC PR21568 comment #9. This probably isn't the last word on the matter though...

I find this argument completely convincing and retract the contrary argument that I've made many times in this forum and others. You learn something new every day.

Quoted 8 lines

Just in case it wasn't clear:
int i;
*(volatile int *)&i=2;

In this case, there *is* an access to a volatile object. This is the end result of the the standard's definition of what it means to apply the 'volatile int *' cast to '&i' and then apply the '*' operator to the result and use it as an lvalue.

True, see my last mail in this sub-thread that explains precisely this :-)

Satyam

#308 lore raw
In reply to Segher Boessenkool
Quoted 8 lines · Segher Boessenkool
Quoted 4 lines

Note that "volatile"
is a type-qualifier, not a type itself, so a cast of the _object_ itself to a qualified-type i.e. (volatile int) would not make the access itself volatile-qualified.

There is no such thing as "volatile-qualified access" defined anywhere; there only is the concept of a "volatile-qualified *object*".

Sure, "volatile-qualified access" was not some standard term I used there. Just something to mean "an access that would make the compiler treat the object at that memory as if it were an object with a volatile-qualified type".

Now the second wording *IS* technically correct, but come on, it's 24 words long whereas the original one was 3 -- and hopefully anybody reading the shorter phrase *would* have known anyway what was meant, without having to be pedantic about it :-)

Satyam

#309 lore raw
Quoted 4 lines

Now the second wording *IS* technically correct, but come on, it's 24 words long whereas the original one was 3 -- and hopefully anybody reading the shorter phrase *would* have known anyway what was meant, without having to be pedantic about it :-)

Well you were talking pretty formal (and detailed) stuff, so IMHO it's good to have that exactly correct. Sure it's nicer to use small words most of the time :-)

Segher

#310 lore raw
In reply to Stefan Richter
Quoted 5 lines · Stefan Richter

LDD3 says on page 125: "The following operations are defined for the type [atomic_t] and are guaranteed to be atomic with respect to all processors of an SMP computer."

Doesn't "atomic WRT all processors" require volatility?

Atomic operations only require exclusive access to the cacheline while the value is modified.