While optimization and gshared data

Timo Sintonen t.sintonen at luukku.com
Thu Dec 6 10:28:13 PST 2012


I have some memory mapped hardware in my arm controller.
I define struct regs to describe the registers and __gshared 
regs* p to point to the actual address.
I have a loop like: while (p.status==0) {...}

The body of the loop does not touch status and so gdc optimizer 
does not evaluate status again, resulting to an endless loop. Gcc 
does the same, unless the variable is defined as volatile.

This is ok in tls variables, but gshared variables are not thread 
safe. Actually I can see from assembler code that this occurs in 
all shared variables, like:
__gshared int a=0;
while (a==0) {}
with nothing in the body generates just a jump to itself and the 
program stops.

Another thread, interrupt or hardware can change a __gshared 
variable any time, so I think that the evalution should not be 
optimized away.


Another related question: is there a way to make the pointer p 
immutable but leave the data mutable? The pointer is always set 
to the fixed address of the hardware, but making the pointer 
const makes also the data immutable.


More information about the D.gnu mailing list