| 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 PlatformThreadId thread_id() const; | 174 // call. Keeps on returning the same ID even after a Stop() call. The next |
| 175 // Start*() call renews the ID. |
| 176 // |
| 177 // WARNING: This function will block if the thread hasn't started yet. |
| 178 // |
| 179 PlatformThreadId GetThreadId() const; |
| 175 | 180 |
| 176 // Returns true if the thread has been started, and not yet stopped. | 181 // Returns true if the thread has been started, and not yet stopped. |
| 177 bool IsRunning() const; | 182 bool IsRunning() const; |
| 178 | 183 |
| 179 protected: | 184 protected: |
| 180 // Called just prior to starting the message loop | 185 // Called just prior to starting the message loop |
| 181 virtual void Init() {} | 186 virtual void Init() {} |
| 182 | 187 |
| 183 // Called to start the message loop | 188 // Called to start the message loop |
| 184 virtual void Run(MessageLoop* message_loop); | 189 virtual void Run(MessageLoop* message_loop); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 209 // Whether this thread needs to initialize COM, and if so, in what mode. | 214 // Whether this thread needs to initialize COM, and if so, in what mode. |
| 210 ComStatus com_status_; | 215 ComStatus com_status_; |
| 211 #endif | 216 #endif |
| 212 | 217 |
| 213 // If true, we're in the middle of stopping, and shouldn't access | 218 // If true, we're in the middle of stopping, and shouldn't access |
| 214 // |message_loop_|. It may non-NULL and invalid. | 219 // |message_loop_|. It may non-NULL and invalid. |
| 215 bool stopping_; | 220 bool stopping_; |
| 216 | 221 |
| 217 // True while inside of Run(). | 222 // True while inside of Run(). |
| 218 bool running_; | 223 bool running_; |
| 219 mutable base::Lock running_lock_; // Protects running_. | 224 mutable base::Lock running_lock_; // Protects |running_|. |
| 220 | 225 |
| 221 // The thread's handle. | 226 // The thread's handle. |
| 222 PlatformThreadHandle thread_; | 227 PlatformThreadHandle thread_; |
| 223 mutable base::Lock thread_lock_; // Protects thread_. | 228 mutable base::Lock thread_lock_; // Protects |thread_|. |
| 229 |
| 230 // The thread's id once it has started. |
| 231 PlatformThreadId id_; |
| 232 mutable WaitableEvent id_event_; // Protects |id_|. |
| 224 | 233 |
| 225 // The thread's message loop. Valid only while the thread is alive. Set | 234 // The thread's message loop. Valid only while the thread is alive. Set |
| 226 // by the created thread. | 235 // by the created thread. |
| 227 MessageLoop* message_loop_; | 236 MessageLoop* message_loop_; |
| 228 | 237 |
| 229 // Stores Options::timer_slack_ until the message loop has been bound to | 238 // Stores Options::timer_slack_ until the message loop has been bound to |
| 230 // a thread. | 239 // a thread. |
| 231 TimerSlack message_loop_timer_slack_; | 240 TimerSlack message_loop_timer_slack_; |
| 232 | 241 |
| 233 // The name of the thread. Used for debugging purposes. | 242 // The name of the thread. Used for debugging purposes. |
| 234 std::string name_; | 243 std::string name_; |
| 235 | 244 |
| 236 // Non-null if the thread has successfully started. | 245 // Non-null if the thread has successfully started. |
| 237 scoped_ptr<WaitableEvent> start_event_; | 246 scoped_ptr<WaitableEvent> start_event_; |
| 238 | 247 |
| 239 friend void ThreadQuitHelper(); | 248 friend void ThreadQuitHelper(); |
| 240 | 249 |
| 241 DISALLOW_COPY_AND_ASSIGN(Thread); | 250 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 242 }; | 251 }; |
| 243 | 252 |
| 244 } // namespace base | 253 } // namespace base |
| 245 | 254 |
| 246 #endif // BASE_THREADING_THREAD_H_ | 255 #endif // BASE_THREADING_THREAD_H_ |
| OLD | NEW |