This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Optimization of conditional access to globals: thread-unsafe?


On Mon, Oct 29, 2007 at 15:53:56 +0100, Michael Matz wrote:
> No it won't, because without further information GCC can't know that a 
> memory access won't trap.  Ergo it will not move it out of its control 
> region, exactly because it would potentially introduce traps where there 
> were none before.

Good reasoning, and that's exactly what some of us are asking for.
Please see the example:


  #include <sys/mman.h>
  #include <unistd.h>
  #include <stdio.h>


  int
  f(int read_only, int a[])
  {
    int res = a[0];

    if (! read_only)
      a[0] = 1;
  
    return res;
  }


  int
  main(void)
  {
    const long page_size = sysconf(_SC_PAGESIZE);

    int *a1 = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    int *a2 = mmap(NULL, page_size, PROT_READ,
                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

    f(0, a1);
    f(1, a2);

    fputs("GCC is the best compiler ever!\n", stdout);
  }


It gives:

  moonlight:/tmp$ /usr/local/gcc-4.3-trunk/bin/gcc -O0 mmap.c -o mmap
  moonlight:/tmp$ ./mmap
  GCC is the best compiler ever!
  moonlight:/tmp$ /usr/local/gcc-4.3-trunk/bin/gcc -O1 mmap.c -o mmap
  moonlight:/tmp$ ./mmap
  Segmentation fault


:-/


The discussion is not pointless, just please try to understand what
other people are trying to say.  No one is stupid, we all just not on
the same page yet.


-- 
   Tomash Brechko


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]