This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [PATCH][RFC] Remove volatile from data members in libstdc++
- From: Mark Mitchell <mark at codesourcery dot com>
- To: "Boehm, Hans" <hans dot boehm at hp dot com>
- Cc: Richard Guenther <rguenther at suse dot de>, Ian Lance Taylor <iant at google dot com>, Paolo Carlini <pcarlini at suse dot de>, gcc-patches at gcc dot gnu dot org, libstdc++ at gcc dot gnu dot org
- Date: Mon, 17 Jul 2006 17:53:12 -0700
- Subject: Re: [PATCH][RFC] Remove volatile from data members in libstdc++
- References: <65953E8166311641A685BDF71D865826C1E2AA@cacexc12.americas.cpqcorp.net>
Boehm, Hans wrote:
> I'm not at all sure we're understanding each other correctly, here.
>
> I'm saying that if I write, say:
>
> unsigned global;
>
> unsigned f(void)
> {
> unsigned local = global;
> unsigned i, sum = 0;
>
> for (i = 0; i < local; ++i) sum += i;
> ...unrelated code that doesn't touch i, sum, local, or global ...
> for (i = 0; i < local; ++i) sum -= i;
> return sum;
> }
>
> then f may return a nonzero value since the second reference to local
> may involve an implicit reload of local from global, and hence may not
> get the same value as the first time around. I claim that doesn't make
> the load from global look very atomic, though it perhaps fails in a way
> that different from what you might have expected.
This function, as written, will always return zero. There is no
"implicit reload of local from global". This function reads global
once, stores it in local, and that's that. It doesn't matter whether or
not global is volatile.
If local is volatile, the situation is more complex. Here, the compiler
should reload local each time -- but not by rereading global, just by
reloading from the address of local. If local isn't placed in memory
(and nothing says it must be) then even that won't happen. In practice,
using volatile for local, non-static variables isn't often useful,
except in the presence of setjmp.
--
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713