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 #ifndef BASE_THREADING_THREAD_H_ | 5 #ifndef BASE_THREADING_THREAD_H_ |
6 #define BASE_THREADING_THREAD_H_ | 6 #define BASE_THREADING_THREAD_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/message_loop/timer_slack.h" | 14 #include "base/message_loop/timer_slack.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 #include "base/synchronization/waitable_event.h" | |
17 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 | 21 |
21 class MessagePump; | 22 class MessagePump; |
22 class WaitableEvent; | |
23 | 23 |
24 // A simple thread abstraction that establishes a MessageLoop on a new thread. | 24 // A simple thread abstraction that establishes a MessageLoop on a new thread. |
25 // The consumer uses the MessageLoop of the thread to cause code to execute on | 25 // The consumer uses the MessageLoop of the thread to cause code to execute on |
26 // the thread. When this object is destroyed the thread is terminated. All | 26 // the thread. When this object is destroyed the thread is terminated. All |
27 // pending tasks queued on the thread's message loop will run to completion | 27 // pending tasks queued on the thread's message loop will run to completion |
28 // before the thread is terminated. | 28 // before the thread is terminated. |
29 // | 29 // |
30 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). | 30 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). |
31 // | 31 // |
32 // After the thread is stopped, the destruction sequence is: | 32 // After the thread is stopped, the destruction sequence is: |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 scoped_refptr<SingleThreadTaskRunner> task_runner() const { | 163 scoped_refptr<SingleThreadTaskRunner> task_runner() const { |
164 return message_loop_ ? message_loop_->task_runner() : nullptr; | 164 return message_loop_ ? message_loop_->task_runner() : nullptr; |
165 } | 165 } |
166 | 166 |
167 // Returns the name of this thread (for display in debugger too). | 167 // Returns the name of this thread (for display in debugger too). |
168 const std::string& thread_name() const { return name_; } | 168 const std::string& thread_name() const { return name_; } |
169 | 169 |
170 // The native thread handle. | 170 // The native thread handle. |
171 PlatformThreadHandle thread_handle() { return thread_; } | 171 PlatformThreadHandle thread_handle() { return thread_; } |
172 | 172 |
173 // The thread ID. | 173 // Returns the thread ID. Should not be called before the first Start*() |
174 // call. Keeps on returning the same ID even after a Stop() call. The next | |
Takashi Toyoshima
2015/07/17 13:20:22
Note: many tests rely on this behavior to hold the
| |
175 // Start*() call renews the ID. | |
174 PlatformThreadId thread_id() const; | 176 PlatformThreadId thread_id() const; |
175 | 177 |
176 // Returns true if the thread has been started, and not yet stopped. | 178 // Returns true if the thread has been started, and not yet stopped. |
177 bool IsRunning() const; | 179 bool IsRunning() const; |
178 | 180 |
179 protected: | 181 protected: |
180 // Called just prior to starting the message loop | 182 // Called just prior to starting the message loop |
181 virtual void Init() {} | 183 virtual void Init() {} |
182 | 184 |
183 // Called to start the message loop | 185 // Called to start the message loop |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 bool stopping_; | 217 bool stopping_; |
216 | 218 |
217 // True while inside of Run(). | 219 // True while inside of Run(). |
218 bool running_; | 220 bool running_; |
219 mutable base::Lock running_lock_; // Protects running_. | 221 mutable base::Lock running_lock_; // Protects running_. |
220 | 222 |
221 // The thread's handle. | 223 // The thread's handle. |
222 PlatformThreadHandle thread_; | 224 PlatformThreadHandle thread_; |
223 mutable base::Lock thread_lock_; // Protects thread_. | 225 mutable base::Lock thread_lock_; // Protects thread_. |
224 | 226 |
227 // The thread's id once it was started. | |
Lei Zhang
2015/07/20 21:27:32
was -> has
Takashi Toyoshima
2015/07/21 16:05:37
Done.
| |
228 PlatformThreadId id_; | |
229 | |
Lei Zhang
2015/07/20 21:27:32
nit: no line separator to better indicate relation
Takashi Toyoshima
2015/07/21 16:05:37
Done.
| |
230 mutable WaitableEvent id_event_; // Protects id_. | |
231 | |
225 // The thread's message loop. Valid only while the thread is alive. Set | 232 // The thread's message loop. Valid only while the thread is alive. Set |
226 // by the created thread. | 233 // by the created thread. |
227 MessageLoop* message_loop_; | 234 MessageLoop* message_loop_; |
228 | 235 |
229 // Stores Options::timer_slack_ until the message loop has been bound to | 236 // Stores Options::timer_slack_ until the message loop has been bound to |
230 // a thread. | 237 // a thread. |
231 TimerSlack message_loop_timer_slack_; | 238 TimerSlack message_loop_timer_slack_; |
232 | 239 |
233 // The name of the thread. Used for debugging purposes. | 240 // The name of the thread. Used for debugging purposes. |
234 std::string name_; | 241 std::string name_; |
235 | 242 |
236 // Non-null if the thread has successfully started. | 243 // Non-null if the thread has successfully started. |
237 scoped_ptr<WaitableEvent> start_event_; | 244 scoped_ptr<WaitableEvent> start_event_; |
Takashi Toyoshima
2015/07/17 13:20:22
start_event_ should not be scoped_ptr too, but I d
| |
238 | 245 |
239 friend void ThreadQuitHelper(); | 246 friend void ThreadQuitHelper(); |
240 | 247 |
241 DISALLOW_COPY_AND_ASSIGN(Thread); | 248 DISALLOW_COPY_AND_ASSIGN(Thread); |
242 }; | 249 }; |
243 | 250 |
244 } // namespace base | 251 } // namespace base |
245 | 252 |
246 #endif // BASE_THREADING_THREAD_H_ | 253 #endif // BASE_THREADING_THREAD_H_ |
OLD | NEW |