Commit 98eec82a authored by Jeremy Roman's avatar Jeremy Roman Committed by Commit Bot

webaudio: Use std::atomic for atomic operations in AudioDestinationHandler and ReverbInputBuffer.

Bug: 736037
Change-Id: I88d3a6a314fbbf83ca90dd0293b9370f8453ad22
Reviewed-on: https://chromium-review.googlesource.com/c/1348956Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Jeremy Roman <jbroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611424}
parent bfd2a9f6
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
namespace blink { namespace blink {
AudioDestinationHandler::AudioDestinationHandler(AudioNode& node) AudioDestinationHandler::AudioDestinationHandler(AudioNode& node)
: AudioHandler(kNodeTypeDestination, node, 0), current_sample_frame_(0) { : AudioHandler(kNodeTypeDestination, node, 0) {
AddInput(); AddInput();
} }
......
...@@ -26,8 +26,8 @@ ...@@ -26,8 +26,8 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_DESTINATION_NODE_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_DESTINATION_NODE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_DESTINATION_NODE_H_ #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_DESTINATION_NODE_H_
#include <atomic>
#include "third_party/blink/renderer/modules/webaudio/audio_node.h" #include "third_party/blink/renderer/modules/webaudio/audio_node.h"
#include "third_party/blink/renderer/platform/wtf/atomics.h"
namespace blink { namespace blink {
...@@ -54,7 +54,7 @@ class AudioDestinationHandler : public AudioHandler { ...@@ -54,7 +54,7 @@ class AudioDestinationHandler : public AudioHandler {
virtual void RestartRendering() = 0; virtual void RestartRendering() = 0;
size_t CurrentSampleFrame() const { size_t CurrentSampleFrame() const {
return AcquireLoad(&current_sample_frame_); return current_sample_frame_.load(std::memory_order_acquire);
} }
double CurrentTime() const { double CurrentTime() const {
...@@ -70,10 +70,15 @@ class AudioDestinationHandler : public AudioHandler { ...@@ -70,10 +70,15 @@ class AudioDestinationHandler : public AudioHandler {
} }
protected: protected:
// The number of sample frames processed by the destination so far. void AdvanceCurrentSampleFrame(size_t number_of_frames) {
size_t current_sample_frame_; current_sample_frame_.fetch_add(number_of_frames,
std::memory_order_release);
}
private: private:
// The number of sample frames processed by the destination so far.
std::atomic_size_t current_sample_frame_{0};
// True if the execution context is being destroyed. If this is true, the // True if the execution context is being destroyed. If this is true, the
// destination ndoe must avoid checking for or accessing the execution // destination ndoe must avoid checking for or accessing the execution
// context. // context.
......
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
#include "third_party/blink/renderer/platform/audio/audio_utilities.h" #include "third_party/blink/renderer/platform/audio/audio_utilities.h"
#include "third_party/blink/renderer/platform/audio/denormal_disabler.h" #include "third_party/blink/renderer/platform/audio/denormal_disabler.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h" #include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/wtf/atomics.h"
namespace blink { namespace blink {
...@@ -198,8 +197,7 @@ void DefaultAudioDestinationHandler::Render( ...@@ -198,8 +197,7 @@ void DefaultAudioDestinationHandler::Render(
Context()->HandlePostRenderTasks(destination_bus); Context()->HandlePostRenderTasks(destination_bus);
// Advances the current sample-frame. // Advances the current sample-frame.
size_t new_sample_frame = current_sample_frame_ + number_of_frames; AdvanceCurrentSampleFrame(number_of_frames);
ReleaseStore(&current_sample_frame_, new_sample_frame);
Context()->UpdateWorkletGlobalScopeOnRenderingThread(); Context()->UpdateWorkletGlobalScopeOnRenderingThread();
} }
......
...@@ -340,8 +340,7 @@ bool OfflineAudioDestinationHandler::RenderIfNotSuspended( ...@@ -340,8 +340,7 @@ bool OfflineAudioDestinationHandler::RenderIfNotSuspended(
Context()->HandlePostOfflineRenderTasks(); Context()->HandlePostOfflineRenderTasks();
// Advance current sample-frame. // Advance current sample-frame.
size_t new_sample_frame = current_sample_frame_ + number_of_frames; AdvanceCurrentSampleFrame(number_of_frames);
ReleaseStore(&current_sample_frame_, new_sample_frame);
Context()->UpdateWorkletGlobalScopeOnRenderingThread(); Context()->UpdateWorkletGlobalScopeOnRenderingThread();
......
...@@ -29,10 +29,10 @@ ...@@ -29,10 +29,10 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_AUDIO_REVERB_INPUT_BUFFER_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_AUDIO_REVERB_INPUT_BUFFER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_AUDIO_REVERB_INPUT_BUFFER_H_ #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_AUDIO_REVERB_INPUT_BUFFER_H_
#include <atomic>
#include "third_party/blink/renderer/platform/audio/audio_array.h" #include "third_party/blink/renderer/platform/audio/audio_array.h"
#include "third_party/blink/renderer/platform/platform_export.h" #include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h" #include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/atomics.h"
#include "third_party/blink/renderer/platform/wtf/noncopyable.h" #include "third_party/blink/renderer/platform/wtf/noncopyable.h"
namespace blink { namespace blink {
...@@ -53,9 +53,8 @@ class PLATFORM_EXPORT ReverbInputBuffer { ...@@ -53,9 +53,8 @@ class PLATFORM_EXPORT ReverbInputBuffer {
void Write(const float* source_p, size_t number_of_frames); void Write(const float* source_p, size_t number_of_frames);
// Background threads can call this to check if there's anything to read... // Background threads can call this to check if there's anything to read...
size_t WriteIndex() const { return AcquireLoad(&write_index_); } size_t WriteIndex() const {
void SetWriteIndex(size_t new_index) { return write_index_.load(std::memory_order_acquire);
ReleaseStore(&write_index_, new_index);
} }
// The individual background threads read here (and hope that they can keep up // The individual background threads read here (and hope that they can keep up
...@@ -69,12 +68,16 @@ class PLATFORM_EXPORT ReverbInputBuffer { ...@@ -69,12 +68,16 @@ class PLATFORM_EXPORT ReverbInputBuffer {
void Reset(); void Reset();
private: private:
void SetWriteIndex(size_t new_index) {
write_index_.store(new_index, std::memory_order_release);
}
AudioFloatArray buffer_; AudioFloatArray buffer_;
// |write_index_| can be accessed from several threads. Only use // |write_index_| can be accessed from several threads. Only use
// the getter and setter to access it atomically. Don't access // the getter and setter to access it atomically. Don't access
// directly! // directly!
size_t write_index_; std::atomic_size_t write_index_;
}; };
} // namespace blink } // namespace blink
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment