Commit 75c63f8b authored by Max Morin's avatar Max Morin Committed by Commit Bot

Fix InitializeOnAudioThread race.

It may race with ShutdownOnAudioThread when running the audio service
out of process.

Bug: 854816
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win_optional_gpu_tests_rel
Change-Id: I2ccd4254f4c4f0f70ffb2180c3a586e35ce1baed
Reviewed-on: https://chromium-review.googlesource.com/1109683Reviewed-by: default avatarHenrik Grunell <grunell@chromium.org>
Commit-Queue: Max Morin <maxmorin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570029}
parent 398625c5
......@@ -504,15 +504,15 @@ AudioManagerMac::AudioManagerMac(std::unique_ptr<AudioThread> audio_thread,
: AudioManagerBase(std::move(audio_thread), audio_log_factory),
current_sample_rate_(0),
current_output_device_(kAudioDeviceUnknown),
in_shutdown_(false) {
in_shutdown_(false),
weak_ptr_factory_(this) {
SetMaxOutputStreamsAllowed(kMaxOutputStreams);
// Task must be posted last to avoid races from handing out "this" to the
// audio thread. Always PostTask even if we're on the right thread since
// AudioManager creation is on the startup path and this may be slow.
// PostTask since AudioManager creation may be on the startup path and this
// may be slow.
GetTaskRunner()->PostTask(
FROM_HERE, base::Bind(&AudioManagerMac::InitializeOnAudioThread,
base::Unretained(this)));
weak_ptr_factory_.GetWeakPtr()));
}
AudioManagerMac::~AudioManagerMac() = default;
......
......@@ -16,6 +16,7 @@
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "media/audio/audio_manager_base.h"
#include "media/audio/mac/audio_device_listener_mac.h"
......@@ -215,6 +216,8 @@ class MEDIA_EXPORT AudioManagerMac : public AudioManagerBase {
// Core Audio APIs are not executed during shutdown.
bool in_shutdown_;
base::WeakPtrFactory<AudioManagerMac> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(AudioManagerMac);
};
......
......@@ -92,12 +92,20 @@ AudioManagerWin::AudioManagerWin(std::unique_ptr<AudioThread> audio_thread,
SetMaxOutputStreamsAllowed(kMaxOutputStreams);
// WARNING: This is executed on the UI loop, do not add any code here which
// loads libraries or attempts to call out into the OS. Instead add such code
// to the InitializeOnAudioThread() method below.
// WARNING: This may be executed on the UI loop, do not add any code here
// which loads libraries or attempts to call out into the OS. Instead add
// such code to the InitializeOnAudioThread() method below.
// In case we are already on the audio thread (i.e. when running out of
// process audio), don't post.
if (GetTaskRunner()->BelongsToCurrentThread()) {
this->InitializeOnAudioThread();
return;
}
// Task must be posted last to avoid races from handing out "this" to the
// audio thread.
// audio thread. Unretained is safe since we join the audio thread before
// destructing |this|.
GetTaskRunner()->PostTask(
FROM_HERE, base::Bind(&AudioManagerWin::InitializeOnAudioThread,
base::Unretained(this)));
......
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