From: "H. Peter Anvin" <hpa@zytor.com>
To: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] [2.5] asm-generic/atomic.h and changes to arm, parisc, mips, m68k, sh, cris to use it
Date: 8 Aug 2002 20:52:01 -0700 [thread overview]
Message-ID: <aive91$r53$1@cesium.transmeta.com> (raw)
In-Reply-To: 1028851871.28883.126.camel@irongate.swansea.linux.org.uk
Followup to: <1028851871.28883.126.camel@irongate.swansea.linux.org.uk>
By author: Alan Cox <alan@lxorguk.ukuu.org.uk>
In newsgroup: linux.dev.kernel
>
> On Thu, 2002-08-08 at 23:40, Luca Barbieri wrote:
> > > The compiler can cache the value in a register
> > It shouldn't since it is volatile and the machine has instructions with
> > memory operands.
>
> I'm curious what part of C99 guarantees that it must generate
>
> add 1 to memory
>
> not
>
> load memory
> add 1
> store memory
>
> It certainly guarantees not to cache it for use next statement, but does
> it actually persuade the compiler to use direct operations on memory ?
>
> I'm not a C99 language lawyer but genuinely curious
>
C99 basically doesn't guarantee *anything* about "volatile", except
that it works as a keyword. It really can't, since the C standard
doesn't have a model for either hardware I/O or multithreaded
execution, effectively the two cases for which "volatile" matters; the
exact wording in the standard is § 6.7.3.6:
An object that has volatile-qualified type may be modified in ways
unknown to the implementation or have other unknown side
effects. Therefore any expression referring to such an object
shall be evaluated strictly according to the rules of the abstract
machine, as described in 5.1.2.3. Furthermore, at every sequence
point the value last stored in the object shall agree with that
prescribed by the abstract machine, except as modified by the
unknown factors mentioned previously.114) What constitutes an
access to an object that has volatile-qualified type is
implementation-defined.
114) A volatile declaration may be used to describe an object
corresponding to a memory-mapped input/output port or an object
accessed by an asynchronously interrupting function. Actions on
objects so declared shall not be optimized out by an
implementation or reordered except as permitted by the rules for
evaluating expressions.
It therefore becomes a quality of implementation issue, assuming there
are instructions that do that. Even so, it's iffy... should it
generate "lock incl" on x86, for example?
*In practice*, what can be safely assumed for a volatile object is
that:
a) A store to the volatile location will correspond to exactly one
"store" hardware operations (note that multiple stores to the same
object between sequence points are illegal in C);
b) If there is exactly one read reference to a volatile object between
sequence points, it will correspond to exactly one "load" operation
in hardware. If there is are n read references to the same
volatile objects between adjacent sequence points, it will
correspond to some number m "load" operations such that 1 <= m <= n.
c) For either of these, if the volatile object is too large or has the
wrong alignment to handle atomically in hardware, it will in effect
be treated like some number of smaller atomic objects.
-hpa
--
<hpa@transmeta.com> at work, <hpa@zytor.com> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt <amsp@zytor.com>
next prev parent reply other threads:[~2002-08-09 3:48 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-08-08 21:43 [PATCH] [2.5] asm-generic/atomic.h and changes to arm, parisc, mips, m68k, sh, cris to use it Luca Barbieri
2002-08-08 21:58 ` Roman Zippel
2002-08-08 22:11 ` Luca Barbieri
2002-08-08 22:27 ` Roman Zippel
2002-08-08 22:40 ` Luca Barbieri
2002-08-08 23:04 ` Roman Zippel
2002-08-09 0:11 ` Alan Cox
2002-08-08 23:09 ` Luca Barbieri
2002-08-09 3:52 ` H. Peter Anvin [this message]
2002-08-08 23:45 ` Alan Cox
2002-08-12 11:16 ` David Woodhouse
2002-08-12 12:03 ` Luca Barbieri
2002-08-08 23:29 ` Russell King
2002-08-09 0:41 ` [PATCH] [2.5] (v2) " Luca Barbieri
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='aive91$r53$1@cesium.transmeta.com' \
--to=hpa@zytor.com \
--cc=linux-kernel@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