Re: [LAD] a *simple* ring buffer, comments pls?

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Dan Muresan <danmbox@...>
Cc: <linux-audio-dev@...>
Date: Sunday, July 10, 2011 - 9:15 pm

On Sun, Jul 10, 2011 at 06:05:45PM +0300, Dan Muresan wrote:

> Ah. pthread_mutex_lock() / unlock(), as EXTERNAL functions, will never

OK. This is becoming an interesting discussion, so please
allow me to restate clearly the context.

We have a variable 'int xval' that is being modified
by forces unknown to the code we are discussing. This
code is a function f() which uses the value of xval,
but the algorithm implemented by f() requires that
the same value is used at all points where f() uses
xval during a single invocation of that function.

So we have:

extern int xval;

void f(void)
{
int a, b, c, ... x, y, z;

x = xval;

// lots of code using a ... z;
}

Since f() has much more local variables than the CPU
has registers, the compiler could be tempted to reuse
the register used to store x for some other purpose
at some point in the code. It could do that in two ways:

1. Store x on the stack, and read that location when
xval is required again, or

2. Just reuse the register without saving it, and read
the memory location 'xval' again when required.

(2) could make the algorithm fail, because xval could
have changed. So we want to prevent that happening.

The solution I propose is to declare xval volatile.
This forces the compiler to read it just once, as
expressed by the source code. So it either will do (1),
or maybe decide not to trash the register holding x at
all but use another one.

The solution you propose is to protect xval by a mutex.
I invite you to consider the following:

A. If xval is being modified by an interrupt handler
then clearly you can't use the mutex - you can't risk
to block the interrupt handler.

B. From the point of view of the code we are discussing
*there is no difference* between xval being modified by
an interrupt handler, or by another thread. The difference
is completely irrelevant to f(). The only thing that
matters is that xval can change while f() executes.

You would probably accept the 'volatile' if xval is
being written by an interrupt handler. Given (B),
there is no good reason to reject it in either case.

> I see. But as I said, in general the cache coherency problem is worse

On that I absolutely agree - cache coherency is the real
problem, not pipelining. The latter should in fact be
transparent from a language such as C/C++.

Ciao,

--
FA

_______________________________________________
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[LAD] a *simple* ring buffer, comments pls?, James Morris, (Thu Jul 7, 12:10 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Fri Jul 8, 12:51 am)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Fri Jul 8, 11:24 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Fri Jul 8, 1:22 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 5:17 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Gabriel M. Beddingfield, (Fri Jul 8, 6:12 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Arnold Krille, (Fri Jul 8, 7:06 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 7:53 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Fri Jul 8, 8:41 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 9:01 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Arnold Krille, (Fri Jul 8, 8:59 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 9:11 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Fri Jul 8, 7:23 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Mon Jul 11, 6:59 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Mon Jul 11, 8:53 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Tue Jul 12, 5:41 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Kegel, (Fri Jul 8, 7:29 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Fri Jul 8, 8:49 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Sean Bolton, (Fri Jul 8, 9:57 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Fri Jul 8, 11:03 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 11:27 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Sat Jul 9, 1:25 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Sat Jul 9, 2:38 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Sun Jul 10, 3:05 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Sun Jul 10, 9:15 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Mon Jul 11, 12:03 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Mon Jul 11, 8:44 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Kegel, (Sun Jul 10, 10:34 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Sun Jul 10, 9:41 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Sean Bolton, (Mon Jul 11, 1:34 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Sean Bolton, (Mon Jul 11, 1:44 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Mon Jul 11, 7:19 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Mon Jul 11, 8:32 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Mon Jul 11, 8:50 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Tim E. Real, (Mon Jul 11, 9:38 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Robin Gareus, (Mon Jul 11, 10:27 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Arnold Krille, (Mon Jul 11, 10:12 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Robin Gareus, (Mon Jul 11, 10:28 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Mon Jul 11, 10:34 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Mills, (Mon Jul 11, 10:24 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Mon Jul 11, 10:06 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Mon Jul 11, 11:06 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Mon Jul 11, 10:17 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Mon Jul 11, 8:58 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Tue Jul 12, 7:07 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Tue Jul 12, 12:44 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Kai Vehmanen, (Wed Jul 20, 9:25 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Emanuel Rumpf, (Sat Aug 20, 6:24 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Emanuel Rumpf, (Sat Aug 27, 7:21 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Gabriel Beddingfield, (Sat Aug 20, 7:16 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Tue Jul 12, 7:46 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Tue Jul 12, 8:20 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Arnold Krille, (Tue Jul 12, 11:31 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Tue Jul 12, 11:56 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Wed Jul 13, 12:27 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Tue Jul 12, 9:07 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Tue Jul 12, 9:26 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Tue Jul 12, 8:36 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Tue Jul 12, 9:32 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Kegel, (Tue Jul 12, 10:23 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Tue Jul 12, 11:50 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fred Gleason, (Wed Jul 13, 12:08 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Wed Jul 13, 12:25 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Tue Jul 12, 9:37 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Tue Jul 12, 10:00 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Tue Jul 12, 9:16 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Emanuel Rumpf, (Tue Jul 12, 5:20 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Mon Jul 11, 8:50 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, David Olofson, (Mon Jul 11, 8:47 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Kegel, (Fri Jul 8, 9:52 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 6:39 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Fri Jul 8, 12:21 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Fri Jul 8, 12:56 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Gabriel M. Beddingfield, (Fri Jul 8, 1:04 am)
Re: [LAD] a *simple* ring buffer, comments pls?, David Robillard, (Fri Jul 8, 2:23 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Fri Jul 8, 8:45 am)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Fri Jul 8, 8:40 am)