J
Jorge Peixoto
I have just read in http://kernel.org/doc/Documentation/volatile-considered-harmful.txt
that code like
spin_lock(&the_lock);
do_something_on(&shared_data);
do_something_else_with(&shared_data);
spin_unlock(&the_lock);
doesn't need shared_data to be declared volatile, because the
spin_lock call causes the compiler to reload shared_data from memory.
Within the critical section, the compiler can optimize and load shared
data onto registers. Therefore, declaring shared_data as volatile
serves only to slow things down (assuming that any code that writes to
shared_data locks the_lock beforehand).
So, which functions have this "memory barrier" behavior of spin_lock?
I cannot find this on the internet. In particular, in this code
while (not_ready)
sched_yield();
does not_ready have to be declared as volatile (in my particular
system the code works perfectly without not_ready being volatile, but
I want to know if this is guaranteed) ?
Also, I find it amazing that this code is much faster than using
pthread_cond_signal.
And where can I read about this memory barrier concept? When searching
on the internet, I only found mentions of assembly-level memory
barriers, that is, memory barriers that avoid the CPU from reordering
memory operations across the memory barrier. I did not found anything
about this C-level memory barrier concept.
PS: I have mentioned some POSIX terms in my question, but I believe my
question can be answered with only Standard C knowledge, so I don't
think I am offtopic.
that code like
spin_lock(&the_lock);
do_something_on(&shared_data);
do_something_else_with(&shared_data);
spin_unlock(&the_lock);
doesn't need shared_data to be declared volatile, because the
spin_lock call causes the compiler to reload shared_data from memory.
Within the critical section, the compiler can optimize and load shared
data onto registers. Therefore, declaring shared_data as volatile
serves only to slow things down (assuming that any code that writes to
shared_data locks the_lock beforehand).
So, which functions have this "memory barrier" behavior of spin_lock?
I cannot find this on the internet. In particular, in this code
while (not_ready)
sched_yield();
does not_ready have to be declared as volatile (in my particular
system the code works perfectly without not_ready being volatile, but
I want to know if this is guaranteed) ?
Also, I find it amazing that this code is much faster than using
pthread_cond_signal.
And where can I read about this memory barrier concept? When searching
on the internet, I only found mentions of assembly-level memory
barriers, that is, memory barriers that avoid the CPU from reordering
memory operations across the memory barrier. I did not found anything
about this C-level memory barrier concept.
PS: I have mentioned some POSIX terms in my question, but I believe my
question can be answered with only Standard C knowledge, so I don't
think I am offtopic.