Question about flushing registers to memory in locks

Fri Sep 25 14:31:00 GMT 2009


Short Question:
I have a question about how GCC flushes register values to memory in locks.

Long Question:
I am implementing hardware transactional memory system and I need my code to
touch every read and write set between two code points.
As far as I know, GCC automatically detects lock start and lock end so it
reads data freshly from the memory and flush everything before lock end.
I do not need the actual spin lock but I need GCC's fresh read and flush
write mechanism on hardware transactional memory program compilation.
In fact, I cannot use any system calls for this solution because my hardware
simulator won't accept system calls. So please answer my question without
using system calls.

Let's consider following simple lock program.
// example1.c
As far as I know, GCC would read fresh value of g_var and flush down g_var
to the memory before UNLOCK(). Now I have this hardware transactional memory
// example2.c
Those TM_BEGIN() and TM_END() macros are nothing but reserved assembly
instruction telling the hardware to isolate and atomicize accesses between
TM_BEGIN() and TM_END().
The problem is that GCC doesn't recognize the critical section as critical
section and it does not read fresh nor flush before TM_END().
I could have used volatile keyword on g_var. But this next example would be
disaster for volatile-based code.
// example3.c
for (int i = 0; i < 1000*1000; i++)
Doesn't this mean 1 million memory access instead of 1 memory read and 1
memory write at the end?
I could have coded in assembly myself to force this fresh read and flush on
exit, but there are so many code lines to deal with. Also, I could have used
optimization level -O1 or lower to not worry about this, but the code
becomes so slow and it's not the optimization people will use for the real

So having GCC's read fresh and flush on exit as a separate function or macro
is critical on my research.
I tried searching, but I don't know how you call these features so searching
didn't help me much.
Could you help me on this?

Thanks for reading this email and thanks in advance.
View this message in context:
Sent from the gcc - Help mailing list archive at

More information about the Gcc-help mailing list