Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1417)

Unified Diff: Source/core/platform/audio/AudioDSPKernelProcessor.cpp

Issue 23931002: Fix threading races on AudioDSPKernelProcessor::m_kernels (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/core/platform/audio/AudioDSPKernelProcessor.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/platform/audio/AudioDSPKernelProcessor.cpp
diff --git a/Source/core/platform/audio/AudioDSPKernelProcessor.cpp b/Source/core/platform/audio/AudioDSPKernelProcessor.cpp
index 0875464968f0ca5e4b05204984eafa73a8117d15..d284b1c98687ad333123ff176601ac2df1179248 100644
--- a/Source/core/platform/audio/AudioDSPKernelProcessor.cpp
+++ b/Source/core/platform/audio/AudioDSPKernelProcessor.cpp
@@ -35,6 +35,7 @@
#include "core/platform/audio/AudioDSPKernelProcessor.h"
#include "core/platform/audio/AudioDSPKernel.h"
+#include "wtf/MainThread.h"
namespace WebCore {
@@ -50,6 +51,7 @@ void AudioDSPKernelProcessor::initialize()
if (isInitialized())
return;
+ MutexLocker locker(m_processLock);
ASSERT(!m_kernels.size());
// Create processing kernels, one per channel.
@@ -65,6 +67,7 @@ void AudioDSPKernelProcessor::uninitialize()
if (!isInitialized())
return;
+ MutexLocker locker(m_processLock);
m_kernels.clear();
m_initialized = false;
@@ -81,18 +84,26 @@ void AudioDSPKernelProcessor::process(const AudioBus* source, AudioBus* destinat
return;
}
- bool channelCountMatches = source->numberOfChannels() == destination->numberOfChannels() && source->numberOfChannels() == m_kernels.size();
- ASSERT(channelCountMatches);
- if (!channelCountMatches)
- return;
-
- for (unsigned i = 0; i < m_kernels.size(); ++i)
- m_kernels[i]->process(source->channel(i)->data(), destination->channel(i)->mutableData(), framesToProcess);
+ MutexTryLocker tryLocker(m_processLock);
+ if (tryLocker.locked()) {
+ bool channelCountMatches = source->numberOfChannels() == destination->numberOfChannels() && source->numberOfChannels() == m_kernels.size();
+ ASSERT(channelCountMatches);
+ if (!channelCountMatches)
+ return;
+
+ for (unsigned i = 0; i < m_kernels.size(); ++i)
+ m_kernels[i]->process(source->channel(i)->data(), destination->channel(i)->mutableData(), framesToProcess);
+ } else {
+ // Unfortunately, the kernel is being processed by another thread.
+ // See also ConvolverNode::process().
+ destination->zero();
+ }
}
// Resets filter state
void AudioDSPKernelProcessor::reset()
{
+ ASSERT(isMainThread());
if (!isInitialized())
return;
@@ -100,6 +111,7 @@ void AudioDSPKernelProcessor::reset()
// Any processing depending on this value must set it to false at the appropriate time.
m_hasJustReset = true;
+ MutexLocker locker(m_processLock);
for (unsigned i = 0; i < m_kernels.size(); ++i)
m_kernels[i]->reset();
}
@@ -116,14 +128,28 @@ void AudioDSPKernelProcessor::setNumberOfChannels(unsigned numberOfChannels)
double AudioDSPKernelProcessor::tailTime() const
{
- // It is expected that all the kernels have the same tailTime.
- return !m_kernels.isEmpty() ? m_kernels.first()->tailTime() : 0;
+ ASSERT(!isMainThread());
+ MutexTryLocker tryLocker(m_processLock);
+ if (tryLocker.locked()) {
+ // It is expected that all the kernels have the same tailTime.
+ return !m_kernels.isEmpty() ? m_kernels.first()->tailTime() : 0;
+ }
+ // Since we don't want to block the Audio Device thread, we return a large value
+ // instead of trying to acquire the lock.
+ return std::numeric_limits<double>::infinity();
}
double AudioDSPKernelProcessor::latencyTime() const
{
- // It is expected that all the kernels have the same latencyTime.
- return !m_kernels.isEmpty() ? m_kernels.first()->latencyTime() : 0;
+ ASSERT(!isMainThread());
+ MutexTryLocker tryLocker(m_processLock);
+ if (tryLocker.locked()) {
+ // It is expected that all the kernels have the same latencyTime.
+ return !m_kernels.isEmpty() ? m_kernels.first()->latencyTime() : 0;
+ }
+ // Since we don't want to block the Audio Device thread, we return a large value
+ // instead of trying to acquire the lock.
+ return std::numeric_limits<double>::infinity();
}
} // namespace WebCore
« no previous file with comments | « Source/core/platform/audio/AudioDSPKernelProcessor.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698