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 @@
namespace blink {
AudioDestinationHandler::AudioDestinationHandler(AudioNode& node)
: AudioHandler(kNodeTypeDestination, node, 0), current_sample_frame_(0) {
: AudioHandler(kNodeTypeDestination, node, 0) {
AddInput();
}
......
......@@ -26,8 +26,8 @@
#ifndef 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/platform/wtf/atomics.h"
namespace blink {
......@@ -54,7 +54,7 @@ class AudioDestinationHandler : public AudioHandler {
virtual void RestartRendering() = 0;
size_t CurrentSampleFrame() const {
return AcquireLoad(&current_sample_frame_);
return current_sample_frame_.load(std::memory_order_acquire);
}
double CurrentTime() const {
......@@ -70,10 +70,15 @@ class AudioDestinationHandler : public AudioHandler {
}
protected:
// The number of sample frames processed by the destination so far.
size_t current_sample_frame_;
void AdvanceCurrentSampleFrame(size_t number_of_frames) {
current_sample_frame_.fetch_add(number_of_frames,
std::memory_order_release);
}
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
// destination ndoe must avoid checking for or accessing the execution
// context.
......
......@@ -35,7 +35,6 @@
#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/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/wtf/atomics.h"
namespace blink {
......@@ -198,8 +197,7 @@ void DefaultAudioDestinationHandler::Render(
Context()->HandlePostRenderTasks(destination_bus);
// Advances the current sample-frame.
size_t new_sample_frame = current_sample_frame_ + number_of_frames;
ReleaseStore(&current_sample_frame_, new_sample_frame);
AdvanceCurrentSampleFrame(number_of_frames);
Context()->UpdateWorkletGlobalScopeOnRenderingThread();
}
......
......@@ -340,8 +340,7 @@ bool OfflineAudioDestinationHandler::RenderIfNotSuspended(
Context()->HandlePostOfflineRenderTasks();
// Advance current sample-frame.
size_t new_sample_frame = current_sample_frame_ + number_of_frames;
ReleaseStore(&current_sample_frame_, new_sample_frame);
AdvanceCurrentSampleFrame(number_of_frames);
Context()->UpdateWorkletGlobalScopeOnRenderingThread();
......
......@@ -29,10 +29,10 @@
#ifndef 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/platform_export.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"
namespace blink {
......@@ -53,9 +53,8 @@ class PLATFORM_EXPORT ReverbInputBuffer {
void Write(const float* source_p, size_t number_of_frames);
// Background threads can call this to check if there's anything to read...
size_t WriteIndex() const { return AcquireLoad(&write_index_); }
void SetWriteIndex(size_t new_index) {
ReleaseStore(&write_index_, new_index);
size_t WriteIndex() const {
return write_index_.load(std::memory_order_acquire);
}
// The individual background threads read here (and hope that they can keep up
......@@ -69,12 +68,16 @@ class PLATFORM_EXPORT ReverbInputBuffer {
void Reset();
private:
void SetWriteIndex(size_t new_index) {
write_index_.store(new_index, std::memory_order_release);
}
AudioFloatArray buffer_;
// |write_index_| can be accessed from several threads. Only use
// the getter and setter to access it atomically. Don't access
// directly!
size_t write_index_;
std::atomic_size_t write_index_;
};
} // 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