On Sat, Oct 18, 2008 at 06:46:00PM +0200, Paul Davis wrote:
I don't believe this logic is correct.
...
The fact that in the revised version read_ptr is only read once changes
nothing, since only thread ever modifies read_ptr. It doesn't matter how
many times it accessed to do the computation - it will NEVER change its
value during this computation because the computation happens in the
reader thread and the reader thread is the only place where read_ptr is
modified.
It doesn't matter how many times it is read, but it
does matter if it is ever written with a wrong value.
And this *did* happen in the old version at the end of
a read:
rb->read_ptr += n1;
rb->read_ptr &= rb->size_mask;
If a context switch occurs after the first statement,
and read_ptr > size (which can happen), then the writer
would believe there is more space to write than there
actually is.
Same at the other end: an incomplete update of write_ptr
(the addition is executed but not yet the mask) could make
the reader believe there is more to read than there actually
is.
Ciao,
--
FA
Laboratorio di Acustica ed Elettroacustica
Parma, Italia
Lascia la spina, cogli la rosa.