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

Unified Diff: third_party/WebKit/Source/platform/audio/HRTFDatabaseLoader.cpp

Issue 2154473003: Simplify locking in HRTFDatabaseLoader (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comments. Created 4 years, 5 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 | « third_party/WebKit/Source/platform/audio/HRTFDatabaseLoader.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/platform/audio/HRTFDatabaseLoader.cpp
diff --git a/third_party/WebKit/Source/platform/audio/HRTFDatabaseLoader.cpp b/third_party/WebKit/Source/platform/audio/HRTFDatabaseLoader.cpp
index 223293a7ed3a8adaab4ace0899f7ef82ce3db39c..cef7e6d1691ca93bb168cc4114dc159be16f0ed2 100644
--- a/third_party/WebKit/Source/platform/audio/HRTFDatabaseLoader.cpp
+++ b/third_party/WebKit/Source/platform/audio/HRTFDatabaseLoader.cpp
@@ -39,6 +39,8 @@ namespace blink {
using LoaderMap = HashMap<double, HRTFDatabaseLoader*>;
+// loaderMap() returns the static hash map that contains the mapping between the
+// sample rate and the corresponding HRTF database.
static LoaderMap& loaderMap()
{
DEFINE_STATIC_LOCAL(LoaderMap*, map, (new LoaderMap));
@@ -76,33 +78,44 @@ HRTFDatabaseLoader::~HRTFDatabaseLoader()
void HRTFDatabaseLoader::loadTask()
{
- ASSERT(!isMainThread());
-
- {
- MutexLocker locker(m_lock);
- if (!m_hrtfDatabase) {
- // Load the default HRTF database.
- m_hrtfDatabase = HRTFDatabase::create(m_databaseSampleRate);
- }
- }
+ DCHECK(!isMainThread());
+ DCHECK(!m_hrtfDatabase);
+
+ // Protect access to m_hrtfDatabase, which can be accessed from the audio
+ // thread.
+ MutexLocker locker(m_lock);
+ // Load the default HRTF database.
+ m_hrtfDatabase = HRTFDatabase::create(m_databaseSampleRate);
}
void HRTFDatabaseLoader::loadAsynchronously()
{
ASSERT(isMainThread());
- MutexLocker locker(m_lock);
- if (!m_hrtfDatabase && !m_thread) {
- // Start the asynchronous database loading process.
- m_thread = wrapUnique(Platform::current()->createThread("HRTF database loader"));
- // TODO(alexclarke): Should this be posted as a loading task?
- m_thread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, crossThreadBind(&HRTFDatabaseLoader::loadTask, crossThreadUnretained(this)));
- }
+ // m_hrtfDatabase and m_thread should both be unset because this should be a
+ // new HRTFDatabaseLoader object that was just created by
+ // createAndLoadAsynchronouslyIfNecessary and because we haven't started
+ // loadTask yet for this object.
+ DCHECK(!m_hrtfDatabase);
+ DCHECK(!m_thread);
+
+ // Start the asynchronous database loading process.
+ m_thread = wrapUnique(Platform::current()->createThread("HRTF database loader"));
+ // TODO(alexclarke): Should this be posted as a loading task?
+ m_thread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, crossThreadBind(&HRTFDatabaseLoader::loadTask, crossThreadUnretained(this)));
}
-bool HRTFDatabaseLoader::isLoaded()
+HRTFDatabase* HRTFDatabaseLoader::database()
{
- MutexLocker locker(m_lock);
+ DCHECK(!isMainThread());
+
+ // Seeing that this is only called from the audio thread, we can't block.
+ // It's ok to return nullptr if we can't get the lock.
+ MutexTryLocker tryLocker(m_lock);
+
+ if (!tryLocker.locked())
+ return nullptr;
+
return m_hrtfDatabase.get();
}
« no previous file with comments | « third_party/WebKit/Source/platform/audio/HRTFDatabaseLoader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698