Chromium Code Reviews| Index: base/threading/thread.cc |
| diff --git a/base/threading/thread.cc b/base/threading/thread.cc |
| index 7bff24232e2bf2d66fda4485a4246ebae2e98332..ad229f6f3e31a8908c3d3ff317706c8b10d9f733 100644 |
| --- a/base/threading/thread.cc |
| +++ b/base/threading/thread.cc |
| @@ -62,6 +62,7 @@ Thread::Thread(const std::string& name) |
| stopping_(false), |
| running_(false), |
| thread_(0), |
| + id_(kInvalidThreadId), |
| message_loop_(nullptr), |
| message_loop_timer_slack_(TIMER_SLACK_NONE), |
| name_(name) { |
| @@ -97,25 +98,14 @@ bool Thread::StartWithOptions(const Options& options) { |
| scoped_ptr<MessageLoop> message_loop = MessageLoop::CreateUnbound( |
| type, options.message_pump_factory); |
| message_loop_ = message_loop.get(); |
| - start_event_.reset(new WaitableEvent(false, false)); |
| + start_event_.reset(new WaitableEvent(true, false)); |
| - // Hold the thread_lock_ while starting a new thread, so that we can make sure |
| - // that thread_ is populated before the newly created thread accesses it. |
| - { |
| - AutoLock lock(thread_lock_); |
| - bool created; |
| - if (options.priority == ThreadPriority::NORMAL) { |
| - created = PlatformThread::Create(options.stack_size, this, &thread_); |
| - } else { |
| - created = PlatformThread::CreateWithPriority(options.stack_size, this, |
| - &thread_, options.priority); |
| - } |
| - if (!created) { |
| - DLOG(ERROR) << "failed to create thread"; |
| - message_loop_ = nullptr; |
| - start_event_.reset(); |
| - return false; |
| - } |
| + if (!PlatformThread::CreateWithPriority(options.stack_size, this, |
| + &thread_, options.priority)) { |
| + DLOG(ERROR) << "failed to create thread"; |
| + message_loop_ = nullptr; |
| + start_event_.reset(); |
| + return false; |
| } |
| // The ownership of message_loop is managemed by the newly created thread |
| @@ -165,20 +155,23 @@ void Thread::Stop() { |
| } |
| void Thread::StopSoon() { |
| - // We should only be called on the same thread that started us. |
| - |
| - DCHECK_NE(thread_id(), PlatformThread::CurrentId()); |
| - |
| if (stopping_ || !message_loop_) |
| return; |
| + // Wait for the thread starts running to call thread_id(). |
| + WaitUntilThreadStarted(); |
| + |
| + // We should only be called on the same thread that started us. |
| + DCHECK_NE(thread_id(), PlatformThread::CurrentId()); |
| + |
| stopping_ = true; |
| task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); |
| } |
| PlatformThreadId Thread::thread_id() const { |
| - AutoLock lock(thread_lock_); |
| - return thread_.id(); |
| + AutoLock lock(lock_); |
| + DCHECK_NE(kInvalidThreadId, id_); |
|
kinuko
2015/07/16 00:50:36
Assume that we could fix all the failing tests, ho
Takashi Toyoshima
2015/07/16 05:07:31
Thanks. I'm doing trial and error here https://cod
|
| + return id_; |
| } |
| bool Thread::IsRunning() const { |
| @@ -190,7 +183,7 @@ bool Thread::IsRunning() const { |
| return true; |
| // Otherwise check the running_ flag, which is set to true by the new thread |
| // only while it is inside Run(). |
| - AutoLock lock(running_lock_); |
| + AutoLock lock(lock_); |
| return running_; |
| } |
| @@ -212,6 +205,10 @@ bool Thread::GetThreadWasQuitProperly() { |
| void Thread::ThreadMain() { |
| // Complete the initialization of our Thread object. |
| + { |
| + AutoLock lock(lock_); |
| + id_ = PlatformThread::CurrentId(); |
| + } |
| PlatformThread::SetName(name_.c_str()); |
| ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
| @@ -231,15 +228,11 @@ void Thread::ThreadMain() { |
| } |
| #endif |
| - // Make sure the thread_id() returns current thread. |
| - // (This internally acquires lock against PlatformThread::Create) |
| - DCHECK_EQ(thread_id(), PlatformThread::CurrentId()); |
| - |
| // Let the thread do extra initialization. |
| Init(); |
| { |
| - AutoLock lock(running_lock_); |
| + AutoLock lock(lock_); |
| running_ = true; |
| } |
| @@ -248,7 +241,7 @@ void Thread::ThreadMain() { |
| Run(message_loop_); |
| { |
| - AutoLock lock(running_lock_); |
| + AutoLock lock(lock_); |
| running_ = false; |
| } |