<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 4, 2016 at 1:00 PM,  <span dir="ltr"><<a href="mailto:linux-audio-dev-request@lists.linuxaudio.org" target="_blank">linux-audio-dev-request@lists.linuxaudio.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Send Linux-audio-dev mailing list submissions to<br>
        <a href="mailto:linux-audio-dev@lists.linuxaudio.org">linux-audio-dev@lists.linuxaudio.org</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
        <a href="http://lists.linuxaudio.org/listinfo/linux-audio-dev" rel="noreferrer" target="_blank">http://lists.linuxaudio.org/listinfo/linux-audio-dev</a><br>
or, via email, send a message with subject or body 'help' to<br>
        <a href="mailto:linux-audio-dev-request@lists.linuxaudio.org">linux-audio-dev-request@lists.linuxaudio.org</a><br>
<br>
You can reach the person managing the list at<br>
        <a href="mailto:linux-audio-dev-owner@lists.linuxaudio.org">linux-audio-dev-owner@lists.linuxaudio.org</a><br>
<br>
When replying, please edit your Subject line so it is more specific<br>
than "Re: Contents of Linux-audio-dev digest..."<br>
<br>
<br>
Today's Topics:<br>
<br>
   1. Re: Code reordering (Sebastian Gesemann)<br>
<br>
<br>
----------------------------------------------------------------------<br>
<br>
Message: 1<br>
Date: Fri, 4 Mar 2016 10:16:02 +0100<br>
From: Sebastian Gesemann <<a href="mailto:s.gesemann@gmail.com">s.gesemann@gmail.com</a>><br>
To: Jonathan Brickman <<a href="mailto:jeb@ponderworthy.com">jeb@ponderworthy.com</a>><br>
Cc: Linux Audio Developers <<a href="mailto:linux-audio-dev@lists.linuxaudio.org">linux-audio-dev@lists.linuxaudio.org</a>><br>
Subject: Re: [LAD] Code reordering<br>
Message-ID:<br>
        <<a href="mailto:CAGdQazeKzU1ZOyHrnZLoQnXgm2ca4yZ36%2B-%2BxT1MupZMfjTpPg@mail.gmail.com">CAGdQazeKzU1ZOyHrnZLoQnXgm2ca4yZ36+-+xT1MupZMfjTpPg@mail.gmail.com</a>><br>
Content-Type: text/plain; charset=UTF-8<br>
<br>
On Wed, Mar 2, 2016 at 5:55 PM, Jonathan Brickman <<a href="mailto:jeb@ponderworthy.com">jeb@ponderworthy.com</a>> wrote:<br>
> On 3/1/2016 11:40 AM, Paul Davis wrote:<br>
><br>
> > the JACK implementation relies on two things to work:<br>
> ><br>
> >    * pointer and integer operations are (weakly) atomic on all platforms<br>
> > that JACK runs on<br>
> >    * code reordering will either not happen or will be prevented by the<br>
> > compiler<br>
><br>
> Does #2 mean that -O3 should always be avoided when compiling JACK clients?<br>
<br>
As I said, I consider JACK's ringbuffer implementation to be broken.<br>
According to the C11/C++11 memory model there is nothing in the code<br>
that prevents reordering the update to write_ptr and the update to<br>
*buf in jack_ringbuffer_write. The use of volatile only makes sure<br>
that read/write accesses to the volatile variables are not reordered<br>
or "optimized out" by caching. Specificaly, a volatile write is not a<br>
release barrier. It does not constrain reordering with respect to<br>
other memory locations (*buf). This makes the access to the buffer's<br>
content unordered and invokes undefined behaviour.<br>
<br>
Having said that, if you can be sure that the compiler does not<br>
reorder this (by checking the assembly code, for example) then you<br>
will be fine on an x86/x64 platform because this platform makes an<br>
extra guarantee: writes in one thread are never seen out of order from<br>
another thread's perspective.<br>
<br></blockquote><div><br></div><div>You are right. There was even a discussion about how broken it was</div><div>in 2008, and it was fixed, at least in practice.</div><div><a href="http://lists.linuxaudio.org/pipermail/linux-audio-user/2008-October/056000.html">http://lists.linuxaudio.org/pipermail/linux-audio-user/2008-October/056000.html</a><br></div><div><br></div><div>Theoretically (and not unlikely also in practice), it seems to be still broken.</div><div>This can also confirmed by compiling with -fsanitize=thread:</div><div><br></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">WARNING: ThreadSanitizer: data race (pid=24978)
</span><br>Write of size 8 at 0x7d0c0000efd8 by thread T2:
<br>#0 jack_ringbuffer_write jack/ringbuffer.c:247 (test-int-array-jack+0x000000401a30)
<br>#1 writer_start /home/ksvalast/rbtest/test-int-array.c:85 (test-int-array-jack+0x000000400f4d)
<br>#2 <null> <null> (libtsan.so.0+0x0000000235b9)
<br>
<br>Previous read of size 8 at 0x7d0c0000efd8 by thread T1:
<br>#0 jack_ringbuffer_read_space jack/ringbuffer.c:108 (test-int-array-jack+0x000000401346)
<br>#1 reader_start /home/ksvalast/rbtest/test-int-array.c:48 (test-int-array-jack+0x000000400df2)
<br>#2 <null> <null> (libtsan.so.0+0x0000000235b9)
<br>
<br>Location is heap block of size 48 at 0x7d0c0000efd0 allocated by main thread:
<br>#0 malloc <null> (libtsan.so.0+0x000000025993)
<br>#1 jack_ringbuffer_create jack/ringbuffer.c:41 (test-int-array-jack+0x0000004010ab)
<br>#2 main /home/ksvalast/rbtest/test-int-array.c:102 (test-int-array-jack+0x00000040100c)
<br>
<br>Thread T2 (tid=24981, running) created by main thread at:
<br>#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:105 (test-int-array-jack+0x000000401056)
<br>
<br>Thread T1 (tid=24980, running) created by main thread at:
<br>#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:104 (test-int-array-jack+0x00000040103b)
<br>
<br>SUMMARY: ThreadSanitizer: data race jack/ringbuffer.c:247 jack_ringbuffer_write
<br>==================
<br>==================
<br>WARNING: ThreadSanitizer: data race (pid=24978)
<br>Read of size 8 at 0x7d500000fe00 by thread T1:
<br>#0 memcpy <null> (libtsan.so.0+0x00000002666a)
<br>#1 jack_ringbuffer_read jack/ringbuffer.c:166 (test-int-array-jack+0x0000004015fc)
<br>#2 reader_start /home/ksvalast/rbtest/test-int-array.c:50 (test-int-array-jack+0x000000400e22)
<br>#3 <null> <null> (libtsan.so.0+0x0000000235b9)
<br>
<br>Previous write of size 8 at 0x7d500000fe00 by thread T2:
<br>#0 memcpy <null> (libtsan.so.0+0x00000002666a)
<br>#1 jack_ringbuffer_write jack/ringbuffer.c:246 (test-int-array-jack+0x0000004019e5)
<br>#2 writer_start /home/ksvalast/rbtest/test-int-array.c:85 (test-int-array-jack+0x000000400f4d)
<br>#3 <null> <null> (libtsan.so.0+0x0000000235b9)
<br>
<br>Location is heap block of size 512 at 0x7d500000fe00 allocated by main thread:
<br>#0 malloc <null> (libtsan.so.0+0x000000025993)
<br>#1 jack_ringbuffer_create jack/ringbuffer.c:52 (test-int-array-jack+0x0000004011c8)
<br>#2 main /home/ksvalast/rbtest/test-int-array.c:102 (test-int-array-jack+0x00000040100c)
<br>
<br>Thread T1 (tid=24980, running) created by main thread at:
<br>#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:104 (test-int-array-jack+0x00000040103b)
<br>
<br>Thread T2 (tid=24981, running) created by main thread at:
<br>#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:105 (test-int-array-jack+0x000000401056)<br>
<br></span></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">SUMMARY: ThreadSanitizer: data race ??:0 __interceptor_memcpy
</span><br>==================
<br>==================
<br>WARNING: ThreadSanitizer: data race (pid=24978)
<br>Write of size 8 at 0x7d0c0000efe0 by thread T1:
<br>#0 jack_ringbuffer_read jack/ringbuffer.c:167 (test-int-array-jack+0x000000401647)
<br>#1 reader_start /home/ksvalast/rbtest/test-int-array.c:50 (test-int-array-jack+0x000000400e22)
<br>#2 <null> <null> (libtsan.so.0+0x0000000235b9)
<br>
<br>Previous read of size 8 at 0x7d0c0000efe0 by thread T2:
<br>#0 jack_ringbuffer_write_space jack/ringbuffer.c:128 (test-int-array-jack+0x00000040141a)
<br>#1 writer_start /home/ksvalast/rbtest/test-int-array.c:83 (test-int-array-jack+0x000000400f1d)
<br>#2 <null> <null> (libtsan.so.0+0x0000000235b9)
<br>
<br>Location is heap block of size 48 at 0x7d0c0000efd0 allocated by main thread:
<br>#0 malloc <null> (libtsan.so.0+0x000000025993)
<br>#1 jack_ringbuffer_create jack/ringbuffer.c:41 (test-int-array-jack+0x0000004010ab)
<br>#2 main /home/ksvalast/rbtest/test-int-array.c:102 (test-int-array-jack+0x00000040100c)
<br>
<br>Thread T1 (tid=24980, running) created by main thread at:
<br>#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:104 (test-int-array-jack+0x00000040103b)
<br>
<br>Thread T2 (tid=24981, running) created by main thread at:
<br>#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:105 (test-int-array-jack+0x000000401056)
<br>
<br>SUMMARY: ThreadSanitizer: data race jack/ringbuffer.c:167 jack_ringbuffer_read
<br>==================<br>
<br></span></div></div></div></div>