This is the mail archive of the
mailing list for the GCC project.
Re: Optimization of conditional access to globals: thread-unsafe?
On Sun, 28 Oct 2007, David Miller wrote:
> The compiler simply cannot speculatively load or store to variables with
> global visibility.
> Suggesting volatile is totally impractical and in fact overkill.
> 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.
No it won't, because without further information GCC can't know that a
memory access won't trap. Ergo it will not move it out of its control
region, exactly because it would potentially introduce traps where there
were none before.
It also will not blindly do speculative loads and stores as you suggested
above, please get your facts straight to not muddy the water. It will for
instance not move stores over memory barriers. It will not move them over
(non-const) function calls. These guarantees are completely sufficient to
write thread-safe code, e.g. by including a mem barrier after a store to a
shared global (inside the control region still).