Question about flushing registers to memory in locks

AndyCh sledge76@hotmail.com
Fri Sep 25 14:31:00 GMT 2009


Hello,

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.

Problem:
Let's consider following simple lock program.
// example1.c
LOCK(&mutex);
g_var++;
UNLOCK(&mutex);
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
program.
// example2.c
TM_BEGIN();
g_var++;
TM_END();
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
TM_BEGIN();
for (int i = 0; i < 1000*1000; i++)
  g_var++;
TM_END();
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
software.

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: http://www.nabble.com/Question-about-flushing-registers-to-memory-in-locks-tp25613161p25613161.html
Sent from the gcc - Help mailing list archive at Nabble.com.



More information about the Gcc-help mailing list