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

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Arnold Krille <arnold@...>
Cc: <linux-audio-dev@...>
Date: Friday, July 8, 2011 - 7:23 pm

>> > > the one wrinkle in this is that in theory a compiler

Forget about the compiler, the hardware instruction pipeline could
reorder ANYTHING that is unsynchronized. Plus two different threads
might read/write to different cache memories. Here's a paragraph from
the Double Chekced Locking is Broken Declaration
(http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html):

"... processors have their own locally cached copies of memory. On
some processors, unless the processor performs a cache coherence
instruction (e.g., a memory barrier), reads can be performed out of
stale locally cached copies, even if other processors used memory
barriers to force their writes into global memory."

There's in link in that article to how that can happen on an Alpha.

>> > AFAIK nothing fatal can happen if the variables involved

The only thing that volatile accomplishes is to slow down
properly-synchronized programs. volatile is for signal handlers and
interrupt handlers, not for threading.

> read/write are not strict functions, the only thing that counts is if you have

This is not the real problem. Here's where reordering can hurt:

"Without full sync, a non-owned pointer can "appear" to
change before the pointed data is ready (due to cache non-coherence
and access reordering by compiler or hardware). E.g. the consumer
may "see" the write pointer move before its view of the pointed data
is up-to-date, leading to bogus data. Alternatively, the producer
may "see" the read pointer move before the consummer finishes
loading the pointed data; the producer will thus see the
still-in-use data as empty space and may overwrite it, leading to
lost frames."

(From the write-up at the end of my own program, sintvert.c, at
https://github.com/danmbox/sintvert/blob/master/sintvert.c)

This reordering cannot be prevented without proper synchronization. So
my advice to anyone considering this would be to drop volatile and do
proper synchronization at the application level (i.e. semaphores,
since it has to work in real time).

-- Dan
_______________________________________________
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)