Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #1841

Re: forcing the compiler to reload from memory with c++0x

From Joshua Maurice <joshuamaurice@gmail.com>
Newsgroups comp.programming.threads
Subject Re: forcing the compiler to reload from memory with c++0x
Date 2011-02-01 15:19 -0800
Organization http://groups.google.com
Message-ID <1fff7f23-005c-4f0a-9a28-1566bd1da544@o7g2000prn.googlegroups.com> (permalink)
References (10 earlier) <ii745u$5q3$1@news.eternal-september.org> <87sjw8w4b5.fsf@justsoftwaresolutions.co.uk> <4D480800.1C5132D7@web.de> <87k4hjx3ph.fsf@justsoftwaresolutions.co.uk> <4D482797.CBE41A86@web.de>

Show all headers | View raw


On Feb 1, 7:32 am, Alexander Terekhov <terek...@web.de> wrote:
> Anthony Williams wrote:
> > The C++ Standard defines "observable behaviour" to be the set of
> > accesses to volatile variables and I/O performed by the program.
>
> > The only requirement on a compiled program is that it produce the same
> > set of observable behaviour as that required by the abstract machine,
> > which includes accesses to volatile variables.
>
> > How you are supposed to observe the accesses to volatile variables is
> > not specified.
>
> Yet you call it observable? <chuckles>

I'm pretty sure the C and C++ standards are phrased such that a
conforming implementation could compile the code to whatever target
language entirely ignoring the volatile keyword. Yes volatile reads
and writes are observable behavior, but what "observable behavior"
means is entirely platform specific.

One of volatile's major purposes is a hook to provide MMIO, which is
inherently platform specific. Talking about volatile MMIO and memory
reads and writes observable behavior in terms of the C or C++ standard
is silly. You need to look at the platform specs at that point.

> > > int main() {
> > >   volatile int i;
> > >   // volatile write = has no observable effect
> > >   i = 1;
> > >   // volatile read = has no observable effect
> > >   int n = i;
> > >   // output: 1
> > >   std::cout << n;
> > > }
>
> > > int main() {
> > >   // output: 1
> > >   std::cout << 1;
> > > }
>
> > > can be translated into the same executable code.
>
> > FYI, gcc doesn't treat them the same, even with -O3: the first gets
> > translated to a store of 1 to a stack variable followed by a load from
> > that stack variable into a register, then a call to write that to
> > cout. The second just gets translated into a load of 1 into a register,
> > then a call to write that to cout. If you omit the "volatile" from the
> > first example then it is translated the same as the second.
>
> The gcc' utter stupidity doesn't prove anything.

I don't think that this is utterly stupid.

First, as mentioned above, it's was intended in part to be a hook for
platform specific MMIO operations. Perhaps the way gcc compiles it
down is required for MMIO on that platform. (From my knowledge -
unlikely. James Kanze has mentioned that volatile alone is not
sufficient for MMIO on a large number of desktop and server
platforms.)

Second, volatile still has purposes besides MMIO operations. It's
other two original purposes is communication with signal handlers and
communication across setjump longjump nonsense. What gcc does with
volatile could be explained in terms of that as well.

Back to comp.programming.threads | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: forcing the compiler to reload from memory with c++0x Alexander Terekhov <terekhov@web.de> - 2011-02-01 14:17 +0100
  Re: forcing the compiler to reload from memory with c++0x Alexander Terekhov <terekhov@web.de> - 2011-02-02 14:45 +0100
    Re: forcing the compiler to reload from memory with c++0x Dmitriy Vyukov <dvyukov@gmail.com> - 2011-02-02 07:02 -0800
      Re: forcing the compiler to reload from memory with c++0x Alexander Terekhov <terekhov@web.de> - 2011-02-02 16:33 +0100
  Re: forcing the compiler to reload from memory with c++0x Alexander Terekhov <terekhov@web.de> - 2011-02-01 16:32 +0100
    Re: forcing the compiler to reload from memory with c++0x Alexander Terekhov <terekhov@web.de> - 2011-02-02 12:13 +0100
      Re: forcing the compiler to reload from memory with c++0x Dmitriy Vyukov <dvyukov@gmail.com> - 2011-02-02 04:54 -0800
    Re: forcing the compiler to reload from memory with c++0x Dmitriy Vyukov <dvyukov@gmail.com> - 2011-02-01 21:02 -0800
    Re: forcing the compiler to reload from memory with c++0x Joshua Maurice <joshuamaurice@gmail.com> - 2011-02-01 15:19 -0800
  Re: forcing the compiler to reload from memory with c++0x Anthony Williams <anthony.ajw@gmail.com> - 2011-02-01 13:51 +0000

csiph-web