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