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