| 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" | |
| 17 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
| 18 | 17 |
| 19 namespace base { | 18 namespace base { |
| 20 | 19 |
| 21 class MessagePump; | 20 class MessagePump; |
| 22 class WaitableEvent; | |
| 23 | 21 |
| 24 // A simple thread abstraction that establishes a MessageLoop on a new thread. | 22 // 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 | 23 // 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 | 24 // 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 | 25 // pending tasks queued on the thread's message loop will run to completion |
| 28 // before the thread is terminated. | 26 // before the thread is terminated. |
| 29 // | 27 // |
| 30 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). | 28 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). |
| 31 // | 29 // |
| 32 // After the thread is stopped, the destruction sequence is: | 30 // After the thread is stopped, the destruction sequence is: |
| 33 // | 31 // |
| 34 // (1) Thread::CleanUp() | 32 // (1) Thread::CleanUp() |
| 35 // (2) MessageLoop::~MessageLoop | 33 // (2) MessageLoop::~MessageLoop |
| 36 // (3.b) MessageLoop::DestructionObserver::WillDestroyCurrentMessageLoop | 34 // (3.b) MessageLoop::DestructionObserver::WillDestroyCurrentMessageLoop |
| 37 class BASE_EXPORT Thread : PlatformThread::Delegate { | 35 class BASE_EXPORT Thread : PlatformThread::Delegate { |
| 38 public: | 36 public: |
| 39 struct BASE_EXPORT Options { | 37 struct BASE_EXPORT Options { |
| 40 typedef Callback<scoped_ptr<MessagePump>()> MessagePumpFactory; | 38 typedef Callback<scoped_ptr<MessagePump>()> MessagePumpFactory; |
| 41 | 39 |
| 42 Options(); | 40 Options(); |
| 43 Options(MessageLoop::Type type, size_t size); | 41 Options(MessageLoop::Type type, size_t size); |
| 44 ~Options(); | 42 ~Options(); |
| 45 | 43 |
| 46 // Specifies the type of message loop that will be allocated on the thread. | 44 // Specifies the type of message loop that will be allocated on the thread. |
| 47 // This is ignored if message_pump_factory.is_null() is false. | 45 // This is ignored if message_pump_factory.is_null() is false. |
| 48 MessageLoop::Type message_loop_type; | 46 MessageLoop::Type message_loop_type; |
| 49 | 47 |
| 50 // Specifies timer slack for thread message loop. | 48 // Specify timer slack for thread message loop. |
| 51 TimerSlack timer_slack; | 49 TimerSlack timer_slack; |
| 52 | 50 |
| 53 // Used to create the MessagePump for the MessageLoop. The callback is Run() | 51 // Used to create the MessagePump for the MessageLoop. The callback is Run() |
| 54 // on the thread. If message_pump_factory.is_null(), then a MessagePump | 52 // on the thread. If message_pump_factory.is_null(), then a MessagePump |
| 55 // appropriate for |message_loop_type| is created. Setting this forces the | 53 // appropriate for |message_loop_type| is created. Setting this forces the |
| 56 // MessageLoop::Type to TYPE_CUSTOM. | 54 // MessageLoop::Type to TYPE_CUSTOM. |
| 57 MessagePumpFactory message_pump_factory; | 55 MessagePumpFactory message_pump_factory; |
| 58 | 56 |
| 59 // Specifies the maximum stack size that the thread is allowed to use. | 57 // Specifies the maximum stack size that the thread is allowed to use. |
| 60 // This does not necessarily correspond to the thread's initial stack size. | 58 // This does not necessarily correspond to the thread's initial stack size. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 76 // before it is destructed. | 74 // before it is destructed. |
| 77 ~Thread() override; | 75 ~Thread() override; |
| 78 | 76 |
| 79 #if defined(OS_WIN) | 77 #if defined(OS_WIN) |
| 80 // Causes the thread to initialize COM. This must be called before calling | 78 // Causes the thread to initialize COM. This must be called before calling |
| 81 // Start() or StartWithOptions(). If |use_mta| is false, the thread is also | 79 // Start() or StartWithOptions(). If |use_mta| is false, the thread is also |
| 82 // started with a TYPE_UI message loop. It is an error to call | 80 // started with a TYPE_UI message loop. It is an error to call |
| 83 // init_com_with_mta(false) and then StartWithOptions() with any message loop | 81 // init_com_with_mta(false) and then StartWithOptions() with any message loop |
| 84 // type other than TYPE_UI. | 82 // type other than TYPE_UI. |
| 85 void init_com_with_mta(bool use_mta) { | 83 void init_com_with_mta(bool use_mta) { |
| 86 DCHECK(!start_event_); | 84 DCHECK(!started_); |
| 87 com_status_ = use_mta ? MTA : STA; | 85 com_status_ = use_mta ? MTA : STA; |
| 88 } | 86 } |
| 89 #endif | 87 #endif |
| 90 | 88 |
| 91 // Starts the thread. Returns true if the thread was successfully started; | 89 // Starts the thread. Returns true if the thread was successfully started; |
| 92 // otherwise, returns false. Upon successful return, the message_loop() | 90 // otherwise, returns false. Upon successful return, the message_loop() |
| 93 // getter will return non-null. | 91 // getter will return non-null. |
| 94 // | 92 // |
| 95 // Note: This function can't be called on Windows with the loader lock held; | 93 // Note: This function can't be called on Windows with the loader lock held; |
| 96 // i.e. during a DllMain, global object construction or destruction, atexit() | 94 // i.e. during a DllMain, global object construction or destruction, atexit() |
| 97 // callback. | 95 // callback. |
| 98 bool Start(); | 96 bool Start(); |
| 99 | 97 |
| 100 // Starts the thread. Behaves exactly like Start in addition to allow to | 98 // Starts the thread. Behaves exactly like Start in addition to allow to |
| 101 // override the default options. | 99 // override the default options. |
| 102 // | 100 // |
| 103 // Note: This function can't be called on Windows with the loader lock held; | 101 // Note: This function can't be called on Windows with the loader lock held; |
| 104 // i.e. during a DllMain, global object construction or destruction, atexit() | 102 // i.e. during a DllMain, global object construction or destruction, atexit() |
| 105 // callback. | 103 // callback. |
| 106 bool StartWithOptions(const Options& options); | 104 bool StartWithOptions(const Options& options); |
| 107 | 105 |
| 108 // Starts the thread and wait for the thread to start and run initialization | |
| 109 // before returning. It's same as calling Start() and then | |
| 110 // WaitUntilThreadStarted(). | |
| 111 // Note that using this (instead of Start() or StartWithOptions() causes | |
| 112 // jank on the calling thread, should be used only in testing code. | |
| 113 bool StartAndWaitForTesting(); | |
| 114 | |
| 115 // Blocks until the thread starts running. Called within StartAndWait(). | |
| 116 // Note that calling this causes jank on the calling thread, must be used | |
| 117 // carefully for production code. | |
| 118 bool WaitUntilThreadStarted(); | |
| 119 | |
| 120 // Signals the thread to exit and returns once the thread has exited. After | 106 // Signals the thread to exit and returns once the thread has exited. After |
| 121 // this method returns, the Thread object is completely reset and may be used | 107 // this method returns, the Thread object is completely reset and may be used |
| 122 // as if it were newly constructed (i.e., Start may be called again). | 108 // as if it were newly constructed (i.e., Start may be called again). |
| 123 // | 109 // |
| 124 // Stop may be called multiple times and is simply ignored if the thread is | 110 // Stop may be called multiple times and is simply ignored if the thread is |
| 125 // already stopped. | 111 // already stopped. |
| 126 // | 112 // |
| 127 // NOTE: If you are a consumer of Thread, it is not necessary to call this | 113 // NOTE: If you are a consumer of Thread, it is not necessary to call this |
| 128 // before deleting your Thread objects, as the destructor will do it. | 114 // before deleting your Thread objects, as the destructor will do it. |
| 129 // IF YOU ARE A SUBCLASS OF Thread, YOU MUST CALL THIS IN YOUR DESTRUCTOR. | 115 // IF YOU ARE A SUBCLASS OF Thread, YOU MUST CALL THIS IN YOUR DESTRUCTOR. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 return message_loop_->task_runner(); | 159 return message_loop_->task_runner(); |
| 174 } | 160 } |
| 175 | 161 |
| 176 // Returns the name of this thread (for display in debugger too). | 162 // Returns the name of this thread (for display in debugger too). |
| 177 const std::string& thread_name() const { return name_; } | 163 const std::string& thread_name() const { return name_; } |
| 178 | 164 |
| 179 // The native thread handle. | 165 // The native thread handle. |
| 180 PlatformThreadHandle thread_handle() { return thread_; } | 166 PlatformThreadHandle thread_handle() { return thread_; } |
| 181 | 167 |
| 182 // The thread ID. | 168 // The thread ID. |
| 183 PlatformThreadId thread_id() const { return thread_.id(); } | 169 PlatformThreadId thread_id() const { return thread_id_; } |
| 184 | 170 |
| 185 // Returns true if the thread has been started, and not yet stopped. | 171 // Returns true if the thread has been started, and not yet stopped. |
| 186 bool IsRunning() const; | 172 bool IsRunning() const; |
| 187 | 173 |
| 188 // Sets the thread priority. The thread must already be started. | 174 // Sets the thread priority. The thread must already be started. |
| 189 void SetPriority(ThreadPriority priority); | 175 void SetPriority(ThreadPriority priority); |
| 190 | 176 |
| 191 protected: | 177 protected: |
| 192 // Called just prior to starting the message loop | 178 // Called just prior to starting the message loop |
| 193 virtual void Init() {} | 179 virtual void Init() {} |
| (...skipping 21 matching lines...) Expand all Loading... |
| 215 #endif | 201 #endif |
| 216 | 202 |
| 217 // PlatformThread::Delegate methods: | 203 // PlatformThread::Delegate methods: |
| 218 void ThreadMain() override; | 204 void ThreadMain() override; |
| 219 | 205 |
| 220 #if defined(OS_WIN) | 206 #if defined(OS_WIN) |
| 221 // Whether this thread needs to initialize COM, and if so, in what mode. | 207 // Whether this thread needs to initialize COM, and if so, in what mode. |
| 222 ComStatus com_status_; | 208 ComStatus com_status_; |
| 223 #endif | 209 #endif |
| 224 | 210 |
| 211 // Whether we successfully started the thread. |
| 212 bool started_; |
| 213 |
| 225 // If true, we're in the middle of stopping, and shouldn't access | 214 // If true, we're in the middle of stopping, and shouldn't access |
| 226 // |message_loop_|. It may non-NULL and invalid. | 215 // |message_loop_|. It may non-NULL and invalid. |
| 227 bool stopping_; | 216 bool stopping_; |
| 228 | 217 |
| 229 // True while inside of Run(). | 218 // True while inside of Run(). |
| 230 bool running_; | 219 bool running_; |
| 231 mutable base::Lock lock_; // Protects running_. | 220 |
| 221 // Used to pass data to ThreadMain. |
| 222 struct StartupData; |
| 223 StartupData* startup_data_; |
| 232 | 224 |
| 233 // The thread's handle. | 225 // The thread's handle. |
| 234 PlatformThreadHandle thread_; | 226 PlatformThreadHandle thread_; |
| 235 | 227 |
| 236 // The thread's message loop. Valid only while the thread is alive. Set | 228 // The thread's message loop. Valid only while the thread is alive. Set |
| 237 // by the created thread. | 229 // by the created thread. |
| 238 MessageLoop* message_loop_; | 230 MessageLoop* message_loop_; |
| 239 | 231 |
| 240 // Stores Options::timer_slack_ until the message loop has been bound to | 232 // Our thread's ID. |
| 241 // a thread. | 233 PlatformThreadId thread_id_; |
| 242 TimerSlack message_loop_timer_slack_; | |
| 243 | 234 |
| 244 // The name of the thread. Used for debugging purposes. | 235 // The name of the thread. Used for debugging purposes. |
| 245 std::string name_; | 236 std::string name_; |
| 246 | 237 |
| 247 // Non-null if the thread has successfully started. | |
| 248 scoped_ptr<WaitableEvent> start_event_; | |
| 249 | |
| 250 friend void ThreadQuitHelper(); | 238 friend void ThreadQuitHelper(); |
| 251 | 239 |
| 252 DISALLOW_COPY_AND_ASSIGN(Thread); | 240 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 253 }; | 241 }; |
| 254 | 242 |
| 255 } // namespace base | 243 } // namespace base |
| 256 | 244 |
| 257 #endif // BASE_THREADING_THREAD_H_ | 245 #endif // BASE_THREADING_THREAD_H_ |
| OLD | NEW |