Chromium Code Reviews| Index: base/threading/thread.cc |
| diff --git a/base/threading/thread.cc b/base/threading/thread.cc |
| index ea5b1747e3836a8092517b19e8197d1cf3debb62..6e7dcfa86ac1b828b352172c287dcf848dd53148 100644 |
| --- a/base/threading/thread.cc |
| +++ b/base/threading/thread.cc |
| @@ -36,20 +36,6 @@ void ThreadQuitHelper() { |
| Thread::SetThreadWasQuitProperly(true); |
| } |
| -// Used to pass data to ThreadMain. This structure is allocated on the stack |
| -// from within StartWithOptions. |
| -struct Thread::StartupData { |
| - // We get away with a const reference here because of how we are allocated. |
| - const Thread::Options& options; |
| - |
| - // Used to synchronize thread startup. |
| - WaitableEvent event; |
| - |
| - explicit StartupData(const Options& opt) |
| - : options(opt), |
| - event(false, false) {} |
| -}; |
| - |
| Thread::Options::Options() |
| : message_loop_type(MessageLoop::TYPE_DEFAULT), |
| timer_slack(TIMER_SLACK_NONE), |
| @@ -71,13 +57,10 @@ Thread::Thread(const std::string& name) |
| #if defined(OS_WIN) |
| com_status_(NONE), |
| #endif |
| - started_(false), |
| stopping_(false), |
| running_(false), |
| - startup_data_(NULL), |
| thread_(0), |
| - message_loop_(NULL), |
| - thread_id_(kInvalidThreadId), |
| + message_loop_(nullptr), |
| name_(name) { |
| } |
| @@ -108,34 +91,49 @@ bool Thread::StartWithOptions(const Options& options) { |
| SetThreadWasQuitProperly(false); |
| - StartupData startup_data(options); |
| - startup_data_ = &startup_data; |
| + scoped_ptr<MessageLoop::LazyInitOptions> message_loop_options( |
| + new MessageLoop::LazyInitOptions); |
| + message_loop_options->message_loop_type = options.message_loop_type; |
| + message_loop_options->message_pump_factory = options.message_pump_factory; |
| + message_loop_options->timer_slack = options.timer_slack; |
| + message_loop_ = MessageLoop::CreateForLazyInit(message_loop_options.Pass()); |
| + |
| + start_event_.reset(new WaitableEvent(false, false)); |
| if (!PlatformThread::Create(options.stack_size, this, &thread_)) { |
| DLOG(ERROR) << "failed to create thread"; |
| - startup_data_ = NULL; |
| + delete message_loop_; |
| + message_loop_ = NULL; |
| + start_event_.reset(); |
| return false; |
| } |
| - // TODO(eroman): Remove once crbug.com/465458 is solved. |
| - tracked_objects::ScopedTracker tracking_profile_wait( |
| - FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| - "465458 base::Thread::StartWithOptions (Wait)")); |
| - |
| - // Wait for the thread to start and initialize message_loop_ |
| - base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| - startup_data.event.Wait(); |
| + DCHECK(message_loop_); |
| + return true; |
| +} |
| - // set it to NULL so we don't keep a pointer to some object on the stack. |
| - startup_data_ = NULL; |
| - started_ = true; |
| +bool Thread::StartAndWait() { |
| + bool result = Start(); |
| + if (!result) |
| + return result; |
| + WaitUntilThreadStarted(); |
| + return result; |
| +} |
| - DCHECK(message_loop_); |
| +bool Thread::WaitUntilThreadStarted() { |
| + if (!start_event_) |
| + return false; |
| + // TODO(kinuko): Remove once crbug.com/465458 is solved. |
| + tracked_objects::ScopedTracker tracking_profile( |
| + FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| + "465458 base::Thread::WaitUntilThreadStarted()")); |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + start_event_->Wait(); |
| return true; |
| } |
| void Thread::Stop() { |
| - if (!started_) |
| + if (!start_event_) |
| return; |
| StopSoon(); |
| @@ -151,7 +149,7 @@ void Thread::Stop() { |
| DCHECK(!message_loop_); |
| // The thread no longer needs to be joined. |
| - started_ = false; |
| + start_event_.reset(); |
| stopping_ = false; |
| } |
| @@ -159,9 +157,7 @@ void Thread::Stop() { |
| void Thread::StopSoon() { |
| // We should only be called on the same thread that started us. |
| - // Reading thread_id_ without a lock can lead to a benign data race |
| - // with ThreadMain, so we annotate it to stay silent under ThreadSanitizer. |
| - DCHECK_NE(ANNOTATE_UNPROTECTED_READ(thread_id_), PlatformThread::CurrentId()); |
| + DCHECK_NE(thread_id(), PlatformThread::CurrentId()); |
| if (stopping_ || !message_loop_) |
| return; |
| @@ -171,13 +167,15 @@ void Thread::StopSoon() { |
| } |
| bool Thread::IsRunning() const { |
| - return running_; |
| + if (stopping_) |
|
rvargas (doing something else)
2015/03/26 02:19:27
Now I see... there's a giant lock in Stop (Join),
kinuko
2015/04/13 02:02:59
Not only for testing but also in production code (
|
| + return IsInRunLoop(); |
| + return !!start_event_; |
| } |
| void Thread::SetPriority(ThreadPriority priority) { |
| // The thread must be started (and id known) for this to be |
| // compatible with all platforms. |
| - DCHECK_NE(thread_id_, kInvalidThreadId); |
| + DCHECK(!!start_event_); |
|
rvargas (doing something else)
2015/03/26 02:19:27
nit: shouldn't need !! (scoped_ptr is testable)
kinuko
2015/04/13 02:02:59
Done.
|
| PlatformThread::SetThreadPriority(thread_, priority); |
| } |
| @@ -199,24 +197,16 @@ bool Thread::GetThreadWasQuitProperly() { |
| void Thread::ThreadMain() { |
| { |
|
rvargas (doing something else)
2015/03/26 02:19:27
nit: remove this
kinuko
2015/04/13 02:03:00
Done.
|
| - // The message loop for this thread. |
| - // Allocated on the heap to centralize any leak reports at this line. |
| - scoped_ptr<MessageLoop> message_loop; |
| - if (!startup_data_->options.message_pump_factory.is_null()) { |
| - message_loop.reset( |
| - new MessageLoop(startup_data_->options.message_pump_factory.Run())); |
| - } else { |
| - message_loop.reset( |
| - new MessageLoop(startup_data_->options.message_loop_type)); |
| - } |
| - |
| // Complete the initialization of our Thread object. |
| - thread_id_ = PlatformThread::CurrentId(); |
| + DCHECK_EQ(thread_id(), PlatformThread::CurrentId()); |
| PlatformThread::SetName(name_.c_str()); |
| ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
| - message_loop->set_thread_name(name_); |
| - message_loop->SetTimerSlack(startup_data_->options.timer_slack); |
| - message_loop_ = message_loop.get(); |
| + |
| + // Lazily initialize the message_loop so that it can run on this thread. |
| + DCHECK(message_loop_); |
| + scoped_ptr<MessageLoop> message_loop(message_loop_); |
|
rvargas (doing something else)
2015/03/26 02:19:28
Is this intentional?
kinuko
2015/04/13 02:03:00
Sort of. (This needs to be freed at the end of mai
|
| + message_loop_->set_thread_name(name_); |
| + message_loop_->LazyInit(); |
| #if defined(OS_WIN) |
| scoped_ptr<win::ScopedCOMInitializer> com_initializer; |
| @@ -228,16 +218,21 @@ void Thread::ThreadMain() { |
| #endif |
| // Let the thread do extra initialization. |
| - // Let's do this before signaling we are started. |
| Init(); |
| - running_ = true; |
| - startup_data_->event.Signal(); |
| - // startup_data_ can't be touched anymore since the starting thread is now |
| - // unlocked. |
| + { |
| + AutoLock lock(lock_); |
| + running_ = true; |
| + } |
| + |
| + start_event_->Signal(); |
| Run(message_loop_); |
| - running_ = false; |
| + |
| + { |
| + AutoLock lock(lock_); |
| + running_ = false; |
| + } |
| // Let the thread do extra cleanup. |
| CleanUp(); |
| @@ -254,4 +249,9 @@ void Thread::ThreadMain() { |
| } |
| } |
| +bool Thread::IsInRunLoop() const { |
| + base::AutoLock lock(lock_); |
| + return running_; |
| +} |
| + |
| } // namespace base |