This is the mail archive of the
mailing list for the GCC project.
Re: Optimization of conditional access to globals: thread-unsafe?
On Mon, Oct 29, 2007 at 20:37:52 +0100, Duncan Sands wrote:
> I don't see this with gcc 4.1 or 4.2. Just a data point.
Yes, thanks for pointing this. It fails with gcc (GCC) 4.3.0 20071021
(experimental) though. It turns out that GCC 4.2 and below don't do
this optimization for pointers (even when known to be non-null).
Formally, POSIX requires mprotect() to work only on mmap()'ed regions,
which are accessed through pointers. Technically you can make any
page read-only, including the one that holds globals, but this won't
pass GCC lawyers.
Still, I believe the example proves the general idea. It shows that
speculative store is never safe, because every 'if' may be an 'if not
read-only'-one. And if optimization is not being performed, then it's
only for good: the program is thread-safe, and disabling optimization
for other cases won't affect performance of pointer case.