Subject: Re: [patch] ipvs: force read of atomic_t in while loop

From: Martin Schwidefsky <[email-masked]>
Date: Thu, 09 Aug 2007 14:49:25 +0200

On Thu, 2007-08-09 at 08:40 -0400, Chris Snook wrote:
> > #define reload_var(x) __asm__ __volatile__ (whatever, x)
> > 
> > I don't know inline assembly that much, but isn't it possible
> > with that to kind of "fake-touch" the variable, so the compiler
> > must reload it (and only it) to make sure it's up to date?
> 
> We can do it in C, like this:
> 
> -#define atomic_read(v) ((v)->counter)
> +#define atomic_read(v) (*(volatile int *)&(v)->counter)
> 
> By casting it volatile at the precise piece of code where we want to
> guarantee a read from memory, there's little risk of the compiler
> getting creative in its interpretation of the code.

To answer the inline assembler question:

asm volatile ("" : "=m" (counter)) : "m" (counter) )

will force the compiler to reload the value from memory. But the cast to
(volatile int *) is even better.

-- 
blue skies,
  Martin.

"Reality continues to ruin my life." - Calvin.