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/profiler/scoped_tracker.h" | 10 #include "base/profiler/scoped_tracker.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 LAZY_INSTANCE_INITIALIZER; | 30 LAZY_INSTANCE_INITIALIZER; |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 // This is used to trigger the message loop to exit. | 34 // This is used to trigger the message loop to exit. |
35 void ThreadQuitHelper() { | 35 void ThreadQuitHelper() { |
36 MessageLoop::current()->QuitWhenIdle(); | 36 MessageLoop::current()->QuitWhenIdle(); |
37 Thread::SetThreadWasQuitProperly(true); | 37 Thread::SetThreadWasQuitProperly(true); |
38 } | 38 } |
39 | 39 |
40 // Used to pass data to ThreadMain. This structure is allocated on the stack | |
41 // from within StartWithOptions. | |
42 struct Thread::StartupData { | |
43 // We get away with a const reference here because of how we are allocated. | |
44 const Thread::Options& options; | |
45 | |
46 // Used to synchronize thread startup. | |
47 WaitableEvent event; | |
48 | |
49 explicit StartupData(const Options& opt) | |
50 : options(opt), | |
51 event(false, false) {} | |
52 }; | |
53 | |
54 Thread::Options::Options() | 40 Thread::Options::Options() |
55 : message_loop_type(MessageLoop::TYPE_DEFAULT), | 41 : message_loop_type(MessageLoop::TYPE_DEFAULT), |
56 timer_slack(TIMER_SLACK_NONE), | 42 timer_slack(TIMER_SLACK_NONE), |
57 stack_size(0) { | 43 stack_size(0) { |
58 } | 44 } |
59 | 45 |
60 Thread::Options::Options(MessageLoop::Type type, | 46 Thread::Options::Options(MessageLoop::Type type, |
61 size_t size) | 47 size_t size) |
62 : message_loop_type(type), | 48 : message_loop_type(type), |
63 timer_slack(TIMER_SLACK_NONE), | 49 timer_slack(TIMER_SLACK_NONE), |
64 stack_size(size) { | 50 stack_size(size) { |
65 } | 51 } |
66 | 52 |
67 Thread::Options::~Options() { | 53 Thread::Options::~Options() { |
68 } | 54 } |
69 | 55 |
70 Thread::Thread(const std::string& name) | 56 Thread::Thread(const std::string& name) |
71 : | 57 : |
72 #if defined(OS_WIN) | 58 #if defined(OS_WIN) |
73 com_status_(NONE), | 59 com_status_(NONE), |
74 #endif | 60 #endif |
75 started_(false), | |
76 stopping_(false), | 61 stopping_(false), |
77 running_(false), | 62 running_(false), |
78 startup_data_(NULL), | |
79 thread_(0), | 63 thread_(0), |
80 message_loop_(NULL), | 64 message_loop_(nullptr), |
81 thread_id_(kInvalidThreadId), | 65 message_loop_timer_slack_(TIMER_SLACK_NONE), |
82 name_(name) { | 66 name_(name) { |
83 } | 67 } |
84 | 68 |
85 Thread::~Thread() { | 69 Thread::~Thread() { |
86 Stop(); | 70 Stop(); |
87 } | 71 } |
88 | 72 |
89 bool Thread::Start() { | 73 bool Thread::Start() { |
90 Options options; | 74 Options options; |
91 #if defined(OS_WIN) | 75 #if defined(OS_WIN) |
92 if (com_status_ == STA) | 76 if (com_status_ == STA) |
93 options.message_loop_type = MessageLoop::TYPE_UI; | 77 options.message_loop_type = MessageLoop::TYPE_UI; |
94 #endif | 78 #endif |
95 return StartWithOptions(options); | 79 return StartWithOptions(options); |
96 } | 80 } |
97 | 81 |
98 bool Thread::StartWithOptions(const Options& options) { | 82 bool Thread::StartWithOptions(const Options& options) { |
99 DCHECK(!message_loop_); | 83 DCHECK(!message_loop_); |
100 #if defined(OS_WIN) | 84 #if defined(OS_WIN) |
101 DCHECK((com_status_ != STA) || | 85 DCHECK((com_status_ != STA) || |
102 (options.message_loop_type == MessageLoop::TYPE_UI)); | 86 (options.message_loop_type == MessageLoop::TYPE_UI)); |
103 #endif | 87 #endif |
104 | 88 |
105 SetThreadWasQuitProperly(false); | 89 SetThreadWasQuitProperly(false); |
106 | 90 |
107 StartupData startup_data(options); | 91 MessageLoop::Type type = options.message_loop_type; |
108 startup_data_ = &startup_data; | 92 if (!options.message_pump_factory.is_null()) |
93 type = MessageLoop::TYPE_CUSTOM; | |
109 | 94 |
110 if (!PlatformThread::Create(options.stack_size, this, &thread_)) { | 95 message_loop_timer_slack_ = options.timer_slack; |
111 DLOG(ERROR) << "failed to create thread"; | 96 message_loop_ = new MessageLoop(type, options.message_pump_factory); |
112 startup_data_ = NULL; | 97 |
113 return false; | 98 start_event_.reset(new WaitableEvent(false, false)); |
99 | |
100 // Hold the thread_lock_ while starting a new thread, so that we can make sure | |
101 // that thread_ is populated before the newly created thread accesses it. | |
102 { | |
103 AutoLock lock(thread_lock_); | |
104 if (!PlatformThread::Create(options.stack_size, this, &thread_)) { | |
105 DLOG(ERROR) << "failed to create thread"; | |
106 delete message_loop_; | |
107 message_loop_ = nullptr; | |
108 start_event_.reset(); | |
109 return false; | |
110 } | |
114 } | 111 } |
115 | 112 |
116 // TODO(kinuko): Remove once crbug.com/465458 is solved. | |
117 tracked_objects::ScopedTracker tracking_profile_wait( | |
118 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
119 "465458 base::Thread::StartWithOptions (Wait)")); | |
120 | |
121 // Wait for the thread to start and initialize message_loop_ | |
122 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
123 startup_data.event.Wait(); | |
124 | |
125 // set it to NULL so we don't keep a pointer to some object on the stack. | |
126 startup_data_ = NULL; | |
127 started_ = true; | |
128 | |
129 DCHECK(message_loop_); | 113 DCHECK(message_loop_); |
130 return true; | 114 return true; |
131 } | 115 } |
132 | 116 |
117 bool Thread::StartAndWaitForTesting() { | |
118 bool result = Start(); | |
119 if (!result) | |
120 return false; | |
121 return WaitUntilThreadStarted(); | |
Nico
2015/05/14 04:02:22
This is now just `return Start() && WaitUntilThrea
Takashi Toyoshima
2015/05/14 04:21:50
Nico: You may look the difference between 2 and 3.
kinuko
2015/05/14 08:16:34
The change isn't really meant to change any behavi
| |
122 } | |
123 | |
124 bool Thread::WaitUntilThreadStarted() { | |
125 if (!start_event_) | |
126 return false; | |
127 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
128 start_event_->Wait(); | |
129 return true; | |
130 } | |
131 | |
133 void Thread::Stop() { | 132 void Thread::Stop() { |
134 if (!started_) | 133 if (!start_event_) |
135 return; | 134 return; |
136 | 135 |
137 StopSoon(); | 136 StopSoon(); |
138 | 137 |
139 // Wait for the thread to exit. | 138 // Wait for the thread to exit. |
140 // | 139 // |
141 // TODO(darin): Unfortunately, we need to keep message_loop_ around until | 140 // TODO(darin): Unfortunately, we need to keep message_loop_ around until |
142 // the thread exits. Some consumers are abusing the API. Make them stop. | 141 // the thread exits. Some consumers are abusing the API. Make them stop. |
143 // | 142 // |
144 PlatformThread::Join(thread_); | 143 PlatformThread::Join(thread_); |
145 | 144 |
146 // The thread should NULL message_loop_ on exit. | 145 // The thread should NULL message_loop_ on exit. |
147 DCHECK(!message_loop_); | 146 DCHECK(!message_loop_); |
148 | 147 |
149 // The thread no longer needs to be joined. | 148 // The thread no longer needs to be joined. |
150 started_ = false; | 149 start_event_.reset(); |
151 | 150 |
152 stopping_ = false; | 151 stopping_ = false; |
153 } | 152 } |
154 | 153 |
155 void Thread::StopSoon() { | 154 void Thread::StopSoon() { |
156 // We should only be called on the same thread that started us. | 155 // We should only be called on the same thread that started us. |
157 | 156 |
158 // Reading thread_id_ without a lock can lead to a benign data race | 157 DCHECK_NE(thread_id(), PlatformThread::CurrentId()); |
159 // with ThreadMain, so we annotate it to stay silent under ThreadSanitizer. | |
160 DCHECK_NE(ANNOTATE_UNPROTECTED_READ(thread_id_), PlatformThread::CurrentId()); | |
161 | 158 |
162 if (stopping_ || !message_loop_) | 159 if (stopping_ || !message_loop_) |
163 return; | 160 return; |
164 | 161 |
165 stopping_ = true; | 162 stopping_ = true; |
166 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); | 163 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); |
167 } | 164 } |
168 | 165 |
169 bool Thread::IsRunning() const { | 166 bool Thread::IsRunning() const { |
167 // If the thread's already started (i.e. message_loop_ is non-null) and | |
168 // not yet requested to stop (i.e. stopping_ is false) we can just return | |
169 // true. (Note that stopping_ is touched only on the same thread that | |
170 // starts / started the new thread so we need no locking here.) | |
171 if (message_loop_ && !stopping_) | |
172 return true; | |
173 // Otherwise check the running_ flag, which is set to true by the new thread | |
174 // only while it is inside Run(). | |
175 AutoLock lock(running_lock_); | |
170 return running_; | 176 return running_; |
171 } | 177 } |
172 | 178 |
173 void Thread::SetPriority(ThreadPriority priority) { | 179 void Thread::SetPriority(ThreadPriority priority) { |
174 // The thread must be started (and id known) for this to be | 180 // The thread must be started (and id known) for this to be |
175 // compatible with all platforms. | 181 // compatible with all platforms. |
176 DCHECK_NE(thread_id_, kInvalidThreadId); | 182 DCHECK(message_loop_ != nullptr); |
177 PlatformThread::SetThreadPriority(thread_, priority); | 183 PlatformThread::SetThreadPriority(thread_, priority); |
178 } | 184 } |
179 | 185 |
180 void Thread::Run(MessageLoop* message_loop) { | 186 void Thread::Run(MessageLoop* message_loop) { |
181 message_loop->Run(); | 187 message_loop->Run(); |
182 } | 188 } |
183 | 189 |
184 void Thread::SetThreadWasQuitProperly(bool flag) { | 190 void Thread::SetThreadWasQuitProperly(bool flag) { |
185 lazy_tls_bool.Pointer()->Set(flag); | 191 lazy_tls_bool.Pointer()->Set(flag); |
186 } | 192 } |
187 | 193 |
188 bool Thread::GetThreadWasQuitProperly() { | 194 bool Thread::GetThreadWasQuitProperly() { |
189 bool quit_properly = true; | 195 bool quit_properly = true; |
190 #ifndef NDEBUG | 196 #ifndef NDEBUG |
191 quit_properly = lazy_tls_bool.Pointer()->Get(); | 197 quit_properly = lazy_tls_bool.Pointer()->Get(); |
192 #endif | 198 #endif |
193 return quit_properly; | 199 return quit_properly; |
194 } | 200 } |
195 | 201 |
196 void Thread::ThreadMain() { | 202 void Thread::ThreadMain() { |
197 { | 203 // Complete the initialization of our Thread object. |
198 // The message loop for this thread. | 204 PlatformThread::SetName(name_.c_str()); |
199 // Allocated on the heap to centralize any leak reports at this line. | 205 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
200 scoped_ptr<MessageLoop> message_loop; | |
201 if (!startup_data_->options.message_pump_factory.is_null()) { | |
202 message_loop.reset( | |
203 new MessageLoop(startup_data_->options.message_pump_factory.Run())); | |
204 } else { | |
205 message_loop.reset( | |
206 new MessageLoop(startup_data_->options.message_loop_type)); | |
207 } | |
208 | 206 |
209 // Complete the initialization of our Thread object. | 207 // Lazily initialize the message_loop so that it can run on this thread. |
210 thread_id_ = PlatformThread::CurrentId(); | 208 DCHECK(message_loop_); |
211 PlatformThread::SetName(name_); | 209 scoped_ptr<MessageLoop> message_loop(message_loop_); |
212 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. | 210 message_loop_->BindToCurrentThread(); |
213 message_loop->set_thread_name(name_); | 211 message_loop_->set_thread_name(name_); |
214 message_loop->SetTimerSlack(startup_data_->options.timer_slack); | 212 message_loop_->SetTimerSlack(message_loop_timer_slack_); |
215 message_loop_ = message_loop.get(); | |
216 | 213 |
217 #if defined(OS_WIN) | 214 #if defined(OS_WIN) |
218 scoped_ptr<win::ScopedCOMInitializer> com_initializer; | 215 scoped_ptr<win::ScopedCOMInitializer> com_initializer; |
219 if (com_status_ != NONE) { | 216 if (com_status_ != NONE) { |
220 com_initializer.reset((com_status_ == STA) ? | 217 com_initializer.reset((com_status_ == STA) ? |
221 new win::ScopedCOMInitializer() : | 218 new win::ScopedCOMInitializer() : |
222 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); | 219 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); |
223 } | 220 } |
224 #endif | 221 #endif |
225 | 222 |
226 // Let the thread do extra initialization. | 223 { |
227 // Let's do this before signaling we are started. | 224 // Make sure thread_id() is populated (on the main thread). |
228 Init(); | 225 AutoLock lock(thread_lock_); |
226 DCHECK_EQ(thread_id(), PlatformThread::CurrentId()); | |
227 } | |
229 | 228 |
229 // Let the thread do extra initialization. | |
230 Init(); | |
231 | |
232 { | |
233 AutoLock lock(running_lock_); | |
230 running_ = true; | 234 running_ = true; |
231 startup_data_->event.Signal(); | 235 } |
232 // startup_data_ can't be touched anymore since the starting thread is now | |
233 // unlocked. | |
234 | 236 |
235 Run(message_loop_); | 237 start_event_->Signal(); |
238 | |
239 Run(message_loop_); | |
240 | |
241 { | |
242 AutoLock lock(running_lock_); | |
236 running_ = false; | 243 running_ = false; |
244 } | |
237 | 245 |
238 // Let the thread do extra cleanup. | 246 // Let the thread do extra cleanup. |
239 CleanUp(); | 247 CleanUp(); |
240 | 248 |
241 #if defined(OS_WIN) | 249 #if defined(OS_WIN) |
242 com_initializer.reset(); | 250 com_initializer.reset(); |
243 #endif | 251 #endif |
244 | 252 |
245 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | 253 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
246 DCHECK(GetThreadWasQuitProperly()); | 254 DCHECK(GetThreadWasQuitProperly()); |
247 | 255 |
248 // We can't receive messages anymore. | 256 // We can't receive messages anymore. |
249 message_loop_ = NULL; | 257 // (The message loop is destructed at the end of this block) |
250 } | 258 message_loop_ = NULL; |
251 } | 259 } |
252 | 260 |
253 } // namespace base | 261 } // namespace base |
OLD | NEW |