Chromium Code Reviews| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/base_export.h" | 13 #include "base/base_export.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/message_loop/timer_slack.h" | 17 #include "base/message_loop/timer_slack.h" |
| 18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 20 #include "base/synchronization/waitable_event.h" | 20 #include "base/synchronization/waitable_event.h" |
| 21 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
| 22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 23 | 23 |
| 24 namespace base { | 24 namespace base { |
| 25 | 25 |
| 26 class MessagePump; | 26 class MessagePump; |
| 27 class RunLoop; | |
| 27 | 28 |
| 28 // A simple thread abstraction that establishes a MessageLoop on a new thread. | 29 // A simple thread abstraction that establishes a MessageLoop on a new thread. |
| 29 // The consumer uses the MessageLoop of the thread to cause code to execute on | 30 // The consumer uses the MessageLoop of the thread to cause code to execute on |
| 30 // the thread. When this object is destroyed the thread is terminated. All | 31 // the thread. When this object is destroyed the thread is terminated. All |
| 31 // pending tasks queued on the thread's message loop will run to completion | 32 // pending tasks queued on the thread's message loop will run to completion |
| 32 // before the thread is terminated. | 33 // before the thread is terminated. |
| 33 // | 34 // |
| 34 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). | 35 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). |
| 35 // | 36 // |
| 36 // After the thread is stopped, the destruction sequence is: | 37 // After the thread is stopped, the destruction sequence is: |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 // | 184 // |
| 184 PlatformThreadId GetThreadId() const; | 185 PlatformThreadId GetThreadId() const; |
| 185 | 186 |
| 186 // Returns true if the thread has been started, and not yet stopped. | 187 // Returns true if the thread has been started, and not yet stopped. |
| 187 bool IsRunning() const; | 188 bool IsRunning() const; |
| 188 | 189 |
| 189 protected: | 190 protected: |
| 190 // Called just prior to starting the message loop | 191 // Called just prior to starting the message loop |
| 191 virtual void Init() {} | 192 virtual void Init() {} |
| 192 | 193 |
| 193 // Called to start the message loop | 194 // Called to start the run loop |
| 194 virtual void Run(MessageLoop* message_loop); | 195 virtual void Run(RunLoop* run_loop); |
| 195 | 196 |
| 196 // Called just after the message loop ends | 197 // Called just after the message loop ends |
| 197 virtual void CleanUp() {} | 198 virtual void CleanUp() {} |
| 198 | 199 |
| 199 static void SetThreadWasQuitProperly(bool flag); | 200 static void SetThreadWasQuitProperly(bool flag); |
| 200 static bool GetThreadWasQuitProperly(); | 201 static bool GetThreadWasQuitProperly(); |
| 201 | 202 |
| 202 void set_message_loop(MessageLoop* message_loop) { | 203 void set_message_loop(MessageLoop* message_loop) { |
| 203 message_loop_ = message_loop; | 204 message_loop_ = message_loop; |
| 204 } | 205 } |
| 205 | 206 |
| 206 private: | 207 private: |
| 207 #if defined(OS_WIN) | 208 #if defined(OS_WIN) |
| 208 enum ComStatus { | 209 enum ComStatus { |
| 209 NONE, | 210 NONE, |
| 210 STA, | 211 STA, |
| 211 MTA, | 212 MTA, |
| 212 }; | 213 }; |
| 213 #endif | 214 #endif |
| 214 | 215 |
| 215 // PlatformThread::Delegate methods: | 216 // PlatformThread::Delegate methods: |
| 216 void ThreadMain() override; | 217 void ThreadMain() override; |
| 217 | 218 |
| 219 void ThreadQuitHelper(); | |
| 220 | |
| 218 #if defined(OS_WIN) | 221 #if defined(OS_WIN) |
| 219 // Whether this thread needs to initialize COM, and if so, in what mode. | 222 // Whether this thread needs to initialize COM, and if so, in what mode. |
| 220 ComStatus com_status_; | 223 ComStatus com_status_; |
| 221 #endif | 224 #endif |
| 222 | 225 |
| 223 // If true, we're in the middle of stopping, and shouldn't access | 226 // If true, we're in the middle of stopping, and shouldn't access |
| 224 // |message_loop_|. It may non-nullptr and invalid. | 227 // |message_loop_|. It may non-nullptr and invalid. |
| 225 // Should be written on the thread that created this thread. Also read data | 228 // Should be written on the thread that created this thread. Also read data |
| 226 // could be wrong on other threads. | 229 // could be wrong on other threads. |
| 227 bool stopping_; | 230 bool stopping_; |
| 228 | 231 |
| 229 // True while inside of Run(). | 232 // True while inside of Run(). |
| 230 bool running_; | 233 bool running_; |
| 231 mutable base::Lock running_lock_; // Protects |running_|. | 234 mutable base::Lock running_lock_; // Protects |running_|. |
| 232 | 235 |
| 233 // The thread's handle. | 236 // The thread's handle. |
| 234 PlatformThreadHandle thread_; | 237 PlatformThreadHandle thread_; |
| 235 mutable base::Lock thread_lock_; // Protects |thread_|. | 238 mutable base::Lock thread_lock_; // Protects |thread_|. |
| 236 | 239 |
| 237 // The thread's id once it has started. | 240 // The thread's id once it has started. |
| 238 PlatformThreadId id_; | 241 PlatformThreadId id_; |
| 239 mutable WaitableEvent id_event_; // Protects |id_|. | 242 mutable WaitableEvent id_event_; // Protects |id_|. |
| 240 | 243 |
| 241 // The thread's message loop. Valid only while the thread is alive. Set | 244 // The thread's MessageLoop and RunLoop. Valid only while the thread is alive. |
| 242 // by the created thread. | 245 // Set by the created thread. |
| 243 MessageLoop* message_loop_; | 246 MessageLoop* message_loop_; |
| 247 RunLoop* run_loop_; | |
|
gab
2016/07/21 19:10:51
Note: this turns out to not be relevant in this sp
| |
| 244 | 248 |
| 245 // Stores Options::timer_slack_ until the message loop has been bound to | 249 // Stores Options::timer_slack_ until the message loop has been bound to |
| 246 // a thread. | 250 // a thread. |
| 247 TimerSlack message_loop_timer_slack_; | 251 TimerSlack message_loop_timer_slack_; |
| 248 | 252 |
| 249 // The name of the thread. Used for debugging purposes. | 253 // The name of the thread. Used for debugging purposes. |
| 250 std::string name_; | 254 std::string name_; |
| 251 | 255 |
| 252 // Signaled when the created thread gets ready to use the message loop. | 256 // Signaled when the created thread gets ready to use the message loop. |
| 253 mutable WaitableEvent start_event_; | 257 mutable WaitableEvent start_event_; |
| 254 | 258 |
| 255 friend void ThreadQuitHelper(); | |
| 256 | |
| 257 DISALLOW_COPY_AND_ASSIGN(Thread); | 259 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 258 }; | 260 }; |
| 259 | 261 |
| 260 } // namespace base | 262 } // namespace base |
| 261 | 263 |
| 262 #endif // BASE_THREADING_THREAD_H_ | 264 #endif // BASE_THREADING_THREAD_H_ |
| OLD | NEW |