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" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 // Starts the thread and wait for the thread to start and run initialization | 111 // Starts the thread and wait for the thread to start and run initialization |
112 // before returning. It's same as calling Start() and then | 112 // before returning. It's same as calling Start() and then |
113 // WaitUntilThreadStarted(). | 113 // WaitUntilThreadStarted(). |
114 // Note that using this (instead of Start() or StartWithOptions() causes | 114 // Note that using this (instead of Start() or StartWithOptions() causes |
115 // jank on the calling thread, should be used only in testing code. | 115 // jank on the calling thread, should be used only in testing code. |
116 bool StartAndWaitForTesting(); | 116 bool StartAndWaitForTesting(); |
117 | 117 |
118 // Blocks until the thread starts running. Called within StartAndWait(). | 118 // Blocks until the thread starts running. Called within StartAndWait(). |
119 // Note that calling this causes jank on the calling thread, must be used | 119 // Note that calling this causes jank on the calling thread, must be used |
120 // carefully for production code. | 120 // carefully for production code. |
121 bool WaitUntilThreadStarted(); | 121 bool WaitUntilThreadStarted() const; |
122 | 122 |
123 // Signals the thread to exit and returns once the thread has exited. After | 123 // Signals the thread to exit and returns once the thread has exited. After |
124 // this method returns, the Thread object is completely reset and may be used | 124 // this method returns, the Thread object is completely reset and may be used |
125 // as if it were newly constructed (i.e., Start may be called again). | 125 // as if it were newly constructed (i.e., Start may be called again). |
126 // | 126 // |
127 // Stop may be called multiple times and is simply ignored if the thread is | 127 // Stop may be called multiple times and is simply ignored if the thread is |
128 // already stopped. | 128 // already stopped. |
129 // | 129 // |
130 // NOTE: If you are a consumer of Thread, it is not necessary to call this | 130 // NOTE: If you are a consumer of Thread, it is not necessary to call this |
131 // before deleting your Thread objects, as the destructor will do it. | 131 // before deleting your Thread objects, as the destructor will do it. |
(...skipping 31 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 // The thread ID. Should be called after one of Start*() is called. |
174 PlatformThreadId thread_id() const; | 174 PlatformThreadId thread_id() const; |
175 | 175 |
176 // Returns true if the thread has been started, and not yet stopped. | 176 // Returns true if the thread has been started, and not yet stopped. |
177 bool IsRunning() const; | 177 bool IsRunning() const; |
178 | 178 |
179 protected: | 179 protected: |
180 // Called just prior to starting the message loop | 180 // Called just prior to starting the message loop |
181 virtual void Init() {} | 181 virtual void Init() {} |
182 | 182 |
183 // Called to start the message loop | 183 // Called to start the message loop |
(...skipping 21 matching lines...) Expand all Loading... |
205 // PlatformThread::Delegate methods: | 205 // PlatformThread::Delegate methods: |
206 void ThreadMain() override; | 206 void ThreadMain() override; |
207 | 207 |
208 #if defined(OS_WIN) | 208 #if defined(OS_WIN) |
209 // Whether this thread needs to initialize COM, and if so, in what mode. | 209 // Whether this thread needs to initialize COM, and if so, in what mode. |
210 ComStatus com_status_; | 210 ComStatus com_status_; |
211 #endif | 211 #endif |
212 | 212 |
213 // If true, we're in the middle of stopping, and shouldn't access | 213 // If true, we're in the middle of stopping, and shouldn't access |
214 // |message_loop_|. It may non-NULL and invalid. | 214 // |message_loop_|. It may non-NULL and invalid. |
| 215 // Should be written on the thread that creates this thread. Also read data |
| 216 // could be wrong on other threads. |
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 |
| 222 // Protects |running_|. |
| 223 mutable base::Lock running_lock_; |
220 | 224 |
221 // The thread's handle. | 225 // The thread's handle. |
222 PlatformThreadHandle thread_; | 226 PlatformThreadHandle thread_; |
223 mutable base::Lock thread_lock_; // Protects thread_. | 227 |
| 228 // The thread's id once it was started. Ensured by |start_event_|. |
| 229 PlatformThreadId id_; |
224 | 230 |
225 // The thread's message loop. Valid only while the thread is alive. Set | 231 // The thread's message loop. Valid only while the thread is alive. Set |
226 // by the created thread. | 232 // by the created thread. |
227 MessageLoop* message_loop_; | 233 MessageLoop* message_loop_; |
228 | 234 |
229 // Stores Options::timer_slack_ until the message loop has been bound to | 235 // Stores Options::timer_slack_ until the message loop has been bound to |
230 // a thread. | 236 // a thread. |
231 TimerSlack message_loop_timer_slack_; | 237 TimerSlack message_loop_timer_slack_; |
232 | 238 |
233 // The name of the thread. Used for debugging purposes. | 239 // The name of the thread. Used for debugging purposes. |
234 std::string name_; | 240 std::string name_; |
235 | 241 |
236 // Non-null if the thread has successfully started. | 242 // Non-null if the thread has successfully started. |
237 scoped_ptr<WaitableEvent> start_event_; | 243 scoped_ptr<WaitableEvent> start_event_; |
238 | 244 |
239 friend void ThreadQuitHelper(); | 245 friend void ThreadQuitHelper(); |
240 | 246 |
241 DISALLOW_COPY_AND_ASSIGN(Thread); | 247 DISALLOW_COPY_AND_ASSIGN(Thread); |
242 }; | 248 }; |
243 | 249 |
244 } // namespace base | 250 } // namespace base |
245 | 251 |
246 #endif // BASE_THREADING_THREAD_H_ | 252 #endif // BASE_THREADING_THREAD_H_ |
OLD | NEW |