| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 // before it is destructed. | 79 // before it is destructed. |
| 80 ~Thread() override; | 80 ~Thread() override; |
| 81 | 81 |
| 82 #if defined(OS_WIN) | 82 #if defined(OS_WIN) |
| 83 // Causes the thread to initialize COM. This must be called before calling | 83 // Causes the thread to initialize COM. This must be called before calling |
| 84 // Start() or StartWithOptions(). If |use_mta| is false, the thread is also | 84 // Start() or StartWithOptions(). If |use_mta| is false, the thread is also |
| 85 // started with a TYPE_UI message loop. It is an error to call | 85 // started with a TYPE_UI message loop. It is an error to call |
| 86 // init_com_with_mta(false) and then StartWithOptions() with any message loop | 86 // init_com_with_mta(false) and then StartWithOptions() with any message loop |
| 87 // type other than TYPE_UI. | 87 // type other than TYPE_UI. |
| 88 void init_com_with_mta(bool use_mta) { | 88 void init_com_with_mta(bool use_mta) { |
| 89 DCHECK(!start_event_); | 89 DCHECK(!message_loop_); |
| 90 com_status_ = use_mta ? MTA : STA; | 90 com_status_ = use_mta ? MTA : STA; |
| 91 } | 91 } |
| 92 #endif | 92 #endif |
| 93 | 93 |
| 94 // Starts the thread. Returns true if the thread was successfully started; | 94 // Starts the thread. Returns true if the thread was successfully started; |
| 95 // otherwise, returns false. Upon successful return, the message_loop() | 95 // otherwise, returns false. Upon successful return, the message_loop() |
| 96 // getter will return non-null. | 96 // getter will return non-null. |
| 97 // | 97 // |
| 98 // Note: This function can't be called on Windows with the loader lock held; | 98 // Note: This function can't be called on Windows with the loader lock held; |
| 99 // i.e. during a DllMain, global object construction or destruction, atexit() | 99 // i.e. during a DllMain, global object construction or destruction, atexit() |
| (...skipping 11 matching lines...) Expand all 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. |
| 132 // IF YOU ARE A SUBCLASS OF Thread, YOU MUST CALL THIS IN YOUR DESTRUCTOR. | 132 // IF YOU ARE A SUBCLASS OF Thread, YOU MUST CALL THIS IN YOUR DESTRUCTOR. |
| 133 void Stop(); | 133 void Stop(); |
| 134 | 134 |
| 135 // Signals the thread to exit in the near future. | 135 // Signals the thread to exit in the near future. |
| 136 // | 136 // |
| 137 // WARNING: This function is not meant to be commonly used. Use at your own | 137 // WARNING: This function is not meant to be commonly used. Use at your own |
| 138 // risk. Calling this function will cause message_loop() to become invalid in | 138 // risk. Calling this function will cause message_loop() to become invalid in |
| 139 // the near future. This function was created to workaround a specific | 139 // the near future. This function was created to workaround a specific |
| 140 // deadlock on Windows with printer worker thread. In any other case, Stop() | 140 // deadlock on Windows with printer worker thread. In any other case, Stop() |
| 141 // should be used. | 141 // should be used. |
| 142 // | 142 // |
| 143 // StopSoon should not be called multiple times as it is risky to do so. It | 143 // StopSoon should not be called multiple times as it is risky to do so. It |
| 144 // could cause a timing issue in message_loop() access. Call Stop() to reset | 144 // could cause a timing issue in message_loop() access. Call Stop() to reset |
| 145 // the thread object once it is known that the thread has quit. | 145 // the thread object once it is known that the thread has quit. |
| 146 void StopSoon(); | 146 void StopSoon(); |
| 147 | 147 |
| 148 // Returns the message loop for this thread. Use the MessageLoop's | 148 // Returns the message loop for this thread. Use the MessageLoop's |
| 149 // PostTask methods to execute code on the thread. This only returns | 149 // PostTask methods to execute code on the thread. This only returns |
| 150 // non-null after a successful call to Start. After Stop has been called, | 150 // non-null after a successful call to Start. After Stop has been called, |
| 151 // this will return NULL. | 151 // this will return nullptr. |
| 152 // | 152 // |
| 153 // NOTE: You must not call this MessageLoop's Quit method directly. Use | 153 // NOTE: You must not call this MessageLoop's Quit method directly. Use |
| 154 // the Thread's Stop method instead. | 154 // the Thread's Stop method instead. |
| 155 // | 155 // |
| 156 MessageLoop* message_loop() const { return message_loop_; } | 156 MessageLoop* message_loop() const { return message_loop_; } |
| 157 | 157 |
| 158 // Returns a TaskRunner for this thread. Use the TaskRunner's PostTask | 158 // Returns a TaskRunner for this thread. Use the TaskRunner's PostTask |
| 159 // methods to execute code on the thread. Returns NULL if the thread is not | 159 // methods to execute code on the thread. Returns nullptr if the thread is not |
| 160 // running (e.g. before Start or after Stop have been called). Callers can | 160 // running (e.g. before Start or after Stop have been called). Callers can |
| 161 // hold on to this even after the thread is gone; in this situation, attempts | 161 // hold on to this even after the thread is gone; in this situation, attempts |
| 162 // to PostTask() will fail. | 162 // to PostTask() will fail. |
| 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 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 | 209 |
| 210 // PlatformThread::Delegate methods: | 210 // PlatformThread::Delegate methods: |
| 211 void ThreadMain() override; | 211 void ThreadMain() override; |
| 212 | 212 |
| 213 #if defined(OS_WIN) | 213 #if defined(OS_WIN) |
| 214 // 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. |
| 215 ComStatus com_status_; | 215 ComStatus com_status_; |
| 216 #endif | 216 #endif |
| 217 | 217 |
| 218 // 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 |
| 219 // |message_loop_|. It may non-NULL and invalid. | 219 // |message_loop_|. It may non-nullptr and invalid. |
| 220 // Should be written on the thread that created this thread. Also read data |
| 221 // could be wrong on other threads. |
| 220 bool stopping_; | 222 bool stopping_; |
| 221 | 223 |
| 222 // True while inside of Run(). | 224 // True while inside of Run(). |
| 223 bool running_; | 225 bool running_; |
| 224 mutable base::Lock running_lock_; // Protects |running_|. | 226 mutable base::Lock running_lock_; // Protects |running_|. |
| 225 | 227 |
| 226 // The thread's handle. | 228 // The thread's handle. |
| 227 PlatformThreadHandle thread_; | 229 PlatformThreadHandle thread_; |
| 228 mutable base::Lock thread_lock_; // Protects |thread_|. | 230 mutable base::Lock thread_lock_; // Protects |thread_|. |
| 229 | 231 |
| 230 // The thread's id once it has started. | 232 // The thread's id once it has started. |
| 231 PlatformThreadId id_; | 233 PlatformThreadId id_; |
| 232 mutable WaitableEvent id_event_; // Protects |id_|. | 234 mutable WaitableEvent id_event_; // Protects |id_|. |
| 233 | 235 |
| 234 // The thread's message loop. Valid only while the thread is alive. Set | 236 // The thread's message loop. Valid only while the thread is alive. Set |
| 235 // by the created thread. | 237 // by the created thread. |
| 236 MessageLoop* message_loop_; | 238 MessageLoop* message_loop_; |
| 237 | 239 |
| 238 // Stores Options::timer_slack_ until the message loop has been bound to | 240 // Stores Options::timer_slack_ until the message loop has been bound to |
| 239 // a thread. | 241 // a thread. |
| 240 TimerSlack message_loop_timer_slack_; | 242 TimerSlack message_loop_timer_slack_; |
| 241 | 243 |
| 242 // The name of the thread. Used for debugging purposes. | 244 // The name of the thread. Used for debugging purposes. |
| 243 std::string name_; | 245 std::string name_; |
| 244 | 246 |
| 245 // Non-null if the thread has successfully started. | 247 // Signaled when the created thread gets ready to use the message loop. |
| 246 scoped_ptr<WaitableEvent> start_event_; | 248 mutable WaitableEvent start_event_; |
| 247 | 249 |
| 248 friend void ThreadQuitHelper(); | 250 friend void ThreadQuitHelper(); |
| 249 | 251 |
| 250 DISALLOW_COPY_AND_ASSIGN(Thread); | 252 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 251 }; | 253 }; |
| 252 | 254 |
| 253 } // namespace base | 255 } // namespace base |
| 254 | 256 |
| 255 #endif // BASE_THREADING_THREAD_H_ | 257 #endif // BASE_THREADING_THREAD_H_ |
| OLD | NEW |