| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/threading/thread.h" | 5 #include "base/threading/thread.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 (options.message_loop_type == MessageLoop::TYPE_UI)); | 87 (options.message_loop_type == MessageLoop::TYPE_UI)); |
| 88 #endif | 88 #endif |
| 89 | 89 |
| 90 SetThreadWasQuitProperly(false); | 90 SetThreadWasQuitProperly(false); |
| 91 | 91 |
| 92 MessageLoop::Type type = options.message_loop_type; | 92 MessageLoop::Type type = options.message_loop_type; |
| 93 if (!options.message_pump_factory.is_null()) | 93 if (!options.message_pump_factory.is_null()) |
| 94 type = MessageLoop::TYPE_CUSTOM; | 94 type = MessageLoop::TYPE_CUSTOM; |
| 95 | 95 |
| 96 message_loop_timer_slack_ = options.timer_slack; | 96 message_loop_timer_slack_ = options.timer_slack; |
| 97 message_loop_ = new MessageLoop(type, options.message_pump_factory); | 97 scoped_ptr<MessageLoop> message_loop = |
| 98 | 98 MessageLoop::CreateUnbound(type, options.message_pump_factory); |
| 99 message_loop_ = message_loop.get(); |
| 99 start_event_.reset(new WaitableEvent(false, false)); | 100 start_event_.reset(new WaitableEvent(false, false)); |
| 100 | 101 |
| 101 // Hold the thread_lock_ while starting a new thread, so that we can make sure | 102 // Hold the thread_lock_ while starting a new thread, so that we can make sure |
| 102 // that thread_ is populated before the newly created thread accesses it. | 103 // that thread_ is populated before the newly created thread accesses it. |
| 103 { | 104 { |
| 104 AutoLock lock(thread_lock_); | 105 AutoLock lock(thread_lock_); |
| 105 bool created; | 106 bool created; |
| 106 if (options.priority == ThreadPriority::NORMAL) { | 107 if (options.priority == ThreadPriority::NORMAL) { |
| 107 created = PlatformThread::Create(options.stack_size, this, &thread_); | 108 created = PlatformThread::Create(options.stack_size, this, &thread_); |
| 108 } else { | 109 } else { |
| 109 created = PlatformThread::CreateWithPriority(options.stack_size, this, | 110 created = PlatformThread::CreateWithPriority(options.stack_size, this, |
| 110 &thread_, options.priority); | 111 &thread_, options.priority); |
| 111 } | 112 } |
| 112 if (!created) { | 113 if (!created) { |
| 113 DLOG(ERROR) << "failed to create thread"; | 114 DLOG(ERROR) << "failed to create thread"; |
| 114 delete message_loop_; | |
| 115 message_loop_ = nullptr; | 115 message_loop_ = nullptr; |
| 116 start_event_.reset(); | 116 start_event_.reset(); |
| 117 return false; | 117 return false; |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 | 120 |
| 121 // The ownership of message_loop is managemed by the newly created thread |
| 122 // within the ThreadMain. |
| 123 ignore_result(message_loop.release()); |
| 124 |
| 121 DCHECK(message_loop_); | 125 DCHECK(message_loop_); |
| 122 return true; | 126 return true; |
| 123 } | 127 } |
| 124 | 128 |
| 125 bool Thread::StartAndWaitForTesting() { | 129 bool Thread::StartAndWaitForTesting() { |
| 126 bool result = Start(); | 130 bool result = Start(); |
| 127 if (!result) | 131 if (!result) |
| 128 return false; | 132 return false; |
| 129 WaitUntilThreadStarted(); | 133 WaitUntilThreadStarted(); |
| 130 return true; | 134 return true; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 // true. (Note that stopping_ is touched only on the same thread that | 187 // true. (Note that stopping_ is touched only on the same thread that |
| 184 // starts / started the new thread so we need no locking here.) | 188 // starts / started the new thread so we need no locking here.) |
| 185 if (message_loop_ && !stopping_) | 189 if (message_loop_ && !stopping_) |
| 186 return true; | 190 return true; |
| 187 // Otherwise check the running_ flag, which is set to true by the new thread | 191 // Otherwise check the running_ flag, which is set to true by the new thread |
| 188 // only while it is inside Run(). | 192 // only while it is inside Run(). |
| 189 AutoLock lock(running_lock_); | 193 AutoLock lock(running_lock_); |
| 190 return running_; | 194 return running_; |
| 191 } | 195 } |
| 192 | 196 |
| 193 void Thread::SetPriority(ThreadPriority priority) { | |
| 194 // The thread must be started (and id known) for this to be | |
| 195 // compatible with all platforms. | |
| 196 DCHECK(message_loop_ != nullptr); | |
| 197 PlatformThread::SetThreadPriority(thread_, priority); | |
| 198 } | |
| 199 | |
| 200 void Thread::Run(MessageLoop* message_loop) { | 197 void Thread::Run(MessageLoop* message_loop) { |
| 201 message_loop->Run(); | 198 message_loop->Run(); |
| 202 } | 199 } |
| 203 | 200 |
| 204 void Thread::SetThreadWasQuitProperly(bool flag) { | 201 void Thread::SetThreadWasQuitProperly(bool flag) { |
| 205 lazy_tls_bool.Pointer()->Set(flag); | 202 lazy_tls_bool.Pointer()->Set(flag); |
| 206 } | 203 } |
| 207 | 204 |
| 208 bool Thread::GetThreadWasQuitProperly() { | 205 bool Thread::GetThreadWasQuitProperly() { |
| 209 bool quit_properly = true; | 206 bool quit_properly = true; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 | 261 |
| 265 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | 262 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
| 266 DCHECK(GetThreadWasQuitProperly()); | 263 DCHECK(GetThreadWasQuitProperly()); |
| 267 | 264 |
| 268 // We can't receive messages anymore. | 265 // We can't receive messages anymore. |
| 269 // (The message loop is destructed at the end of this block) | 266 // (The message loop is destructed at the end of this block) |
| 270 message_loop_ = NULL; | 267 message_loop_ = NULL; |
| 271 } | 268 } |
| 272 | 269 |
| 273 } // namespace base | 270 } // namespace base |
| OLD | NEW |