Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] boost::call_once() optimization idea on Win32
From: Thomas Jarosch (thomas.jarosch_at_[hidden])
Date: 2010-11-18 06:34:20


On Thursday, 18. November 2010 11:50:55 you wrote:
> Or does the mutex/memory barrier ensure the compiler
> isn't allowed to do register caching?

Ok, quickly checked this with gcc 4.4.4 on linux:
-test.c------------------
int *foobar = 0x1234;
int a;

int main(void)
{
  __sync_synchronize();
  a = *foobar;
  __sync_synchronize();
  return *foobar;
}
-------------------------

gcc -O4 -S test.c:
main:
.LFB0:
        .cfi_startproc
        mfence
        movq foobar(%rip), %rax
        movl (%rax), %eax
        movl %eax, a(%rip)
        mfence
        movq foobar(%rip), %rax
        movl (%rax), %eax
        ret
        .cfi_endproc

-> The memory barrier invalidates the register cache.

Without the second memory barrier in the C source,
the compiler uses the register cached value of *foobar.

Thanks for your help on this again, Tony.

Cheers,
Thomas


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net