Chromium Code Reviews| Index: base/threading/thread.cc |
| diff --git a/base/threading/thread.cc b/base/threading/thread.cc |
| index 98610a63c09875878ffceca6417d7af16980ca89..f73113a6353929ab42ebd1d5d3e50acc33f001d4 100644 |
| --- a/base/threading/thread.cc |
| +++ b/base/threading/thread.cc |
| @@ -8,6 +8,7 @@ |
| #include "base/bind_helpers.h" |
| #include "base/lazy_instance.h" |
| #include "base/location.h" |
| +#include "base/logging.h" |
| #include "base/run_loop.h" |
| #include "base/synchronization/waitable_event.h" |
| #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| @@ -28,7 +29,7 @@ namespace { |
| // because its Stop method was called. This allows us to catch cases where |
| // MessageLoop::QuitWhenIdle() is called directly, which is unexpected when |
| // using a Thread to setup and run a MessageLoop. |
| -base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool = |
| +base::LazyInstance<base::ThreadLocalBoolean>::Leaky lazy_tls_bool = |
| LAZY_INSTANCE_INITIALIZER; |
| } // namespace |
| @@ -57,6 +58,10 @@ Thread::Thread(const std::string& name) |
| Thread::~Thread() { |
| Stop(); |
| + |
| + // ThreadMain() must have returned before this Thread instance is destroyed |
| + // or use-after-frees could ensue. |
| + DCHECK(!thread_main_exited_.get() || thread_main_exited_->IsSet()); |
|
danakj
2016/07/28 19:07:40
no .get()
gab
2016/07/28 21:40:15
I intentionally added the .get()'s here and below
|
| } |
| bool Thread::Start() { |
| @@ -74,6 +79,8 @@ bool Thread::StartWithOptions(const Options& options) { |
| DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); |
| DCHECK(!message_loop_); |
| DCHECK(!IsRunning()); |
| + DCHECK(!stopping_) << "Starting a non-joinable thread a second time? That's " |
| + << "not allowed!"; |
| #if defined(OS_WIN) |
| DCHECK((com_status_ != STA) || |
| (options.message_loop_type == MessageLoop::TYPE_UI)); |
| @@ -83,6 +90,14 @@ bool Thread::StartWithOptions(const Options& options) { |
| id_event_.Reset(); |
| id_ = kInvalidThreadId; |
| + if (!thread_main_exited_.get()) { |
|
danakj
2016/07/28 19:07:40
no .get()
gab
2016/07/28 21:40:15
Similarly here:
if (!thread_main_exited_)
feels
|
| + thread_main_exited_.reset(new AtomicFlag); |
| + } else { |
| + // It's safe to reset |thread_main_exited_| on sequence when the underlying |
| + // thread isn't running. |
| + thread_main_exited_->UnsafeReset(); |
| + } |
| + |
| SetThreadWasQuitProperly(false); |
| MessageLoop::Type type = options.message_loop_type; |
| @@ -100,8 +115,13 @@ bool Thread::StartWithOptions(const Options& options) { |
| // fixed). |
| { |
| AutoLock lock(thread_lock_); |
| - if (!PlatformThread::CreateWithPriority(options.stack_size, this, &thread_, |
| - options.priority)) { |
| + bool success = |
| + options.joinable |
| + ? PlatformThread::CreateWithPriority(options.stack_size, this, |
| + &thread_, options.priority) |
| + : PlatformThread::CreateNonJoinableWithPriority( |
| + options.stack_size, this, options.priority); |
| + if (!success) { |
| DLOG(ERROR) << "failed to create thread"; |
| message_loop_ = nullptr; |
| return false; |
| @@ -141,11 +161,12 @@ void Thread::Stop() { |
| // DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); |
| AutoLock lock(thread_lock_); |
| + StopSoon(); |
| + |
| + // Can't join if the |thread_| is either already gone or is non-joinable. |
| if (thread_.is_null()) |
| return; |
| - StopSoon(); |
| - |
| // Wait for the thread to exit. |
| // |
| // TODO(darin): Unfortunately, we need to keep |message_loop_| around until |
| @@ -170,6 +191,16 @@ void Thread::StopSoon() { |
| return; |
| stopping_ = true; |
| + |
| + if (using_external_message_loop_) { |
| + // Setting |stopping_| to true above should have been sufficient for this |
| + // thread to be considered "stopped" per it having never set its |running_| |
| + // bit by lack of its own ThreadMain. |
| + DCHECK(!IsRunning()); |
| + message_loop_ = nullptr; |
| + return; |
| + } |
| + |
| task_runner()->PostTask( |
| FROM_HERE, base::Bind(&Thread::ThreadQuitHelper, Unretained(this))); |
| } |
| @@ -219,6 +250,18 @@ bool Thread::GetThreadWasQuitProperly() { |
| return quit_properly; |
| } |
| +void Thread::SetMessageLoop(MessageLoop* message_loop) { |
| + DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); |
| + |
| + // Setting |message_loop_| should suffice for this thread to be considered |
| + // as "running", until Stop() is invoked. |
| + DCHECK(!IsRunning()); |
| + message_loop_ = message_loop; |
| + DCHECK(IsRunning()); |
| + |
| + using_external_message_loop_ = true; |
| +} |
| + |
| void Thread::ThreadMain() { |
| // First, make GetThreadId() available to avoid deadlocks. It could be called |
| // any place in the following thread initialization code. |
| @@ -282,6 +325,8 @@ void Thread::ThreadMain() { |
| // (The message loop is destructed at the end of this block) |
| message_loop_ = nullptr; |
| run_loop_ = nullptr; |
| + |
| + thread_main_exited_->Set(); |
| } |
| void Thread::ThreadQuitHelper() { |