2003-07-31 10:00:10

by Kirill Korotaev

[permalink] [raw]
Subject: atomic_set & gcc. atomicity question

Hi!

Thinking about atomicity of some operations in kernel I've got the following question about atomic_XXX operations.
atomic_set and atomic_read (on i386+ and some others) are simple write to and read from memory, i.e. they are defined as:
#define atomic_set(v,i) (((v)->counter) = (i))
#define atomic_read(v) ((v)->counter)

If we call atomic_set() with constant 2nd argument it's ok - it's a simple write to var. But what if we do atomic_set(var, var1+var2)?
Probably, it can happen that compiler will do "var=var1; var+=var2", can't it? If so, atomic_read() can return intermediate value and write won't seem atomic at all. Who guarentees that compiler won't compile it this way? Optimization? gcc developers?

Kirill


2003-07-31 10:40:08

by Raj Inguva

[permalink] [raw]
Subject: Re: atomic_set & gcc. atomicity question

> #define atomic_set(v,i) (((v)->counter) = (i))

'v' is of type atomic_t which is a structure.

> If we call atomic_set() with constant 2nd argument it's ok - it's a > simple write to var. But what if we do atomic_set(var, var1+var2)?

It will expand to (((var)->counter) = (var1+var2))

'counter' is 'volatile' .