Skip to content

Commit

Permalink
[M90-LTS] Protect HRTF database loader thread from access by differen…
Browse files Browse the repository at this point in the history
…t threads

This patch add a new mutex locker around the HRTF database loader
thread to ensure the safe exclusive access of the loader thread
and the HRTF database.

(cherry picked from commit 6811e85)

Bug: 1233564
Change-Id: Ie12b99ffe520d3747e34af387a37637a10aab38a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3068260
Auto-Submit: Hongchan Choi <hongchan@chromium.org>
Commit-Queue: Kentaro Hara <haraken@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#908269}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3097764
Reviewed-by: Artem Sumaneev <asumaneev@google.com>
Owners-Override: Artem Sumaneev <asumaneev@google.com>
Commit-Queue: Roger Felipe Zanoni da Silva <rzanoni@google.com>
Cr-Commit-Position: refs/branch-heads/4430@{#1568}
Cr-Branched-From: e5ce7dc-refs/heads/master@{#857950}
  • Loading branch information
hoch authored and Chromium LUCI CQ committed Aug 20, 2021
1 parent 186264a commit ba7a4d0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void HRTFDatabaseLoader::LoadTask() {
void HRTFDatabaseLoader::LoadAsynchronously() {
DCHECK(IsMainThread());

MutexLocker locker(lock_);

// 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
Expand Down Expand Up @@ -122,6 +124,10 @@ void HRTFDatabaseLoader::CleanupTask(base::WaitableEvent* sync) {
}

void HRTFDatabaseLoader::WaitForLoaderThreadCompletion() {
// We can lock this because this is called from either the main thread or
// the offline audio rendering thread.
MutexLocker locker(lock_);

if (!thread_)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class PLATFORM_EXPORT HRTFDatabaseLoader final
// must be called from the audio thread.
bool IsLoaded() { return Database(); }

// waitForLoaderThreadCompletion() may be called more than once and is
// thread-safe.
// May be called from both main and audio thread, and also can be called more
// than once.
void WaitForLoaderThreadCompletion();

// Returns the database or nullptr if the database doesn't yet exist. Must
Expand All @@ -87,11 +87,10 @@ class PLATFORM_EXPORT HRTFDatabaseLoader final
void LoadTask();
void CleanupTask(base::WaitableEvent*);

// Holding a m_lock is required when accessing m_hrtfDatabase since we access
// it from multiple threads.
// |lock_| MUST be held when accessing |hrtf_database_| or |thread_| because
// it can be accessed by multiple threads (e.g multiple AudioContexts).
Mutex lock_;
std::unique_ptr<HRTFDatabase> hrtf_database_;

std::unique_ptr<Thread> thread_;

float database_sample_rate_;
Expand Down

0 comments on commit ba7a4d0

Please sign in to comment.