This is the mail archive of the
mailing list for the GCC project.
RE: Optimization of conditional access to globals: thread-unsafe?
On 29 October 2007 01:38, David Miller wrote:
> From: "Dave Korn" <email@example.com>
> Date: Mon, 29 Oct 2007 01:16:07 -0000
>> Thing is, if you disable all optimisations that are potentially
>> unsafe in the presence of threads, won't you just get the same
>> effect as if you had used volatile anyway, only on every single
>> variable in the program instead of just the ones the programmer has
>> designated as sensitive?
> This is not really what is being suggested at all.
> The compiler simply cannot speculatively load or store to variables
> with global visibility.
You'll be back. Next week, you'll discover a corner case where caching a
shared variable in a register can be a bad thing when one thread uses locks
and the other doesn't, and you'll be back to demand that optimisation is
removed as well.
BTW, you and Tomash should get your stories in synch. He says speculative
loads are ok, just no stores, and wants a kind of half-volatile flag that
would only suppress stores. I think you're already looking one step further
down the road than he is and have realised that speculative loads will give
you problems too.
> Suggesting volatile is totally impractical and in fact overkill.
I keep hearing this claim, but nobody says why. What /else/ does it do that
isn't necessary for correctness in this (or other) case(s)?
> Even basic correct single-threaded UNIX programs are broken by these
> speculative stores. If I use a conditional test to protect access to
> memory mmap()'d with a read-only attribute, GCC's optimization will
> cause write-protection exceptions.
Hmm, that's a far more substantial argument. It raises the question: is the
compiler entitled to assume that a non-const pointer always points to
Can't think of a witty .sigline today....