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 19:20:25 +0300, Tomash Brechko wrote:
> Good reasoning, and that's exactly what some of us are asking for.
> Please see the example:

At higher optimization levels GCC may inline f(), or not call it at
all, so below is a more complete case:


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


int
f(int read_only, int a[]) __attribute__((__noinline__));


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

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


int
main(void)
{
  int res;

  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);

  res += f(0, a1);
  res += f(1, a2);

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

  return res;
}



-- 
   Tomash Brechko


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