[LAD] [LAU] Simple, easy multithreaded circular buffer library for Linux?

Fons Adriaensen fons at kokkinizita.net
Sun Oct 19 09:13:58 UTC 2008

On Sun, Oct 19, 2008 at 09:55:54AM +0200, Paul Davis wrote:

>    1) fons' design, which never actually wraps readptr or writeptr, but
>       masks the address used to access the data buffer
>    2) removing the intermediate state's visibility
> i admit to preferring (2) even though i know that with a 64 bit index,
> not wrapping the ptrs is not really a problem.

Another advantage of 1 is that it has no problem with
a completely full buffer, and this doesn't even require
any special code. If access is always in 2^N blocks,
it can store one block more.

> i think that the simpler code:
>        ptr = (ptr + incr) & mask;
> is subject to potential compiler and/or processor "optimization" that
> might reduce it back to the problem case of two ops without an
> intermediate load/store location.

You mean _with_ and intermediate store (I hope) ?

Adding a intermediate store would not really be an
'optimisation'... I doubt if any compiler would do
it. But you can prevent it from doing so, see below.

> the volatile declaration ought to  prevent the compiler from
> doing this

It does. Declaring a var volatile means that the compiler MUST
assume that reading or writing it can have side effects, and
that therefore it MUST access the var exactly as specified. For
the new code this means that only the correct value will ever
be written, for the old it forced the compiler to write the
wrong one.



Laboratorio di Acustica ed Elettroacustica
Parma, Italia

Lascia la spina, cogli la rosa.

More information about the Linux-audio-dev mailing list