Re: spinlock

David Wragg (dpw@doc.ic.ac.uk)
01 Dec 1999 13:32:27 +0000


Jamie Lokier <lkd@tantalophile.demon.co.uk> writes:
> David Wragg wrote:
> > > If volatile causes asm "m" constraints to always use the original memory
> > > location, that would be quite useful but it is not documented to do so.
> >
> > As far as I know, it doesn't and isn't.
> >
> > However, the volatile qualifier is defined in the ISO C standard. If
> > gcc breaks the restrictions on what it is allowed to do with volatile
> > objects implied by that definition, then it has a bug. If the same
> > restrictions don't also apply for the C expressions in inline asm
> > constraints, then that would also be a bug, since it would be
> > worthless as a feature.
>
> As far as I know, volatile requires that the compiler actually perform
> all reads and writes implied by the program, in whatever order is
> implied by the program.

It isn't defined in those terms though: I don't have a copy of the old
or new standard to hand from which to quote chapter and verse, but
both say that the implementation must strictly follow the rules of the
abstract machine with respect to a volatile object.

This implies that it must do all reads and writes. But it implies more
than that.

> In the case of GCC's extended asm, that means it is ok to read a
> volatile, pass the effective address of the /copy/ as the asm operand,
> then write the modified copy back to the volatile.

In the abstract machine, there are no copies of objects.

Of course, inline asm is not covered by the standard, and so gcc
cannot be required to respect volatile for inline asm in the name of
standards comformance. But the cases where you might want to use
inline asm are likely to be cases where, if you use volatile, you want
the compiler to strictly observe it. So for it to ignore volatile for
inline asm would be stupid enough that I would feel justified in
calling it a bug.

I ought to say that this is purely my opinion, and I have no idea what
the position of the gcc developers is on this matter. There are people
around who know far more about gcc than me, so if my reasoning is
flawed, hopefully they will enlighten us.

David Wragg

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/