Bug 1682938 - fixup ReverbInputBuffer's writeIndex. r=karlt
authorAlexis Beingessner <a.beingessner@gmail.com>
Mon, 21 Dec 2020 17:10:29 +0000
changeset 561474 798d7b63c7111c67291e49306e5eb07891136962
parent 561473 e7bfdae0223e647eeb8795e0c25180b3014d2d36
child 561475 242e995f5bf0d6f3446ee89381f1be3b651157be
push id133220
push userrvandermeulen@mozilla.com
push dateMon, 21 Dec 2020 17:17:08 +0000
treeherderautoland@798d7b63c711 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskarlt
bugs1682938
milestone86.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1682938 - fixup ReverbInputBuffer's writeIndex. r=karlt Differential Revision: https://phabricator.services.mozilla.com/D100024
dom/media/webaudio/blink/ReverbInputBuffer.cpp
dom/media/webaudio/blink/ReverbInputBuffer.h
--- a/dom/media/webaudio/blink/ReverbInputBuffer.cpp
+++ b/dom/media/webaudio/blink/ReverbInputBuffer.cpp
@@ -34,28 +34,29 @@ using namespace mozilla;
 namespace WebCore {
 
 ReverbInputBuffer::ReverbInputBuffer(size_t length) : m_writeIndex(0) {
   m_buffer.SetLength(length);
   PodZero(m_buffer.Elements(), length);
 }
 
 void ReverbInputBuffer::write(const float* sourceP, size_t numberOfFrames) {
+  // m_writeIndex is atomic and checked by other threads, so only touch
+  // it at the start and end.
   size_t bufferLength = m_buffer.Length();
-  bool isCopySafe = m_writeIndex + numberOfFrames <= bufferLength;
-  MOZ_ASSERT(isCopySafe);
-  if (!isCopySafe) return;
+  size_t index = m_writeIndex;
+  size_t newIndex = index + numberOfFrames;
 
-  memcpy(m_buffer.Elements() + m_writeIndex, sourceP,
-         sizeof(float) * numberOfFrames);
+  MOZ_RELEASE_ASSERT(newIndex <= bufferLength);
+
+  memcpy(m_buffer.Elements() + index, sourceP, sizeof(float) * numberOfFrames);
 
-  m_writeIndex += numberOfFrames;
-  MOZ_ASSERT(m_writeIndex <= bufferLength);
-
-  if (m_writeIndex >= bufferLength) m_writeIndex = 0;
+  if (newIndex >= bufferLength) {
+    m_writeIndex = 0;
+  }
 }
 
 float* ReverbInputBuffer::directReadFrom(int* readIndex,
                                          size_t numberOfFrames) {
   size_t bufferLength = m_buffer.Length();
   bool isPointerGood = readIndex && *readIndex >= 0 &&
                        *readIndex + numberOfFrames <= bufferLength;
   MOZ_ASSERT(isPointerGood);
--- a/dom/media/webaudio/blink/ReverbInputBuffer.h
+++ b/dom/media/webaudio/blink/ReverbInputBuffer.h
@@ -25,16 +25,17 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #ifndef ReverbInputBuffer_h
 #define ReverbInputBuffer_h
 
 #include "nsTArray.h"
+#include "mozilla/Atomics.h"
 #include "mozilla/MemoryReporting.h"
 
 namespace WebCore {
 
 // ReverbInputBuffer is used to buffer input samples for deferred processing by
 // the background threads.
 class ReverbInputBuffer {
  public:
@@ -59,14 +60,14 @@ class ReverbInputBuffer {
   void reset();
 
   size_t sizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
     return m_buffer.ShallowSizeOfExcludingThis(aMallocSizeOf);
   }
 
  private:
   nsTArray<float> m_buffer;
-  size_t m_writeIndex;
+  mozilla::Atomic<size_t, mozilla::ReleaseAcquire> m_writeIndex;
 };
 
 }  // namespace WebCore
 
 #endif  // ReverbInputBuffer_h