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/sequence_checker.h" | 18 #include "base/sequence_checker.h" |
19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/synchronization/atomic_flag.h" |
20 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
21 #include "base/synchronization/waitable_event.h" | 22 #include "base/synchronization/waitable_event.h" |
22 #include "base/threading/platform_thread.h" | 23 #include "base/threading/platform_thread.h" |
23 #include "build/build_config.h" | 24 #include "build/build_config.h" |
24 | 25 |
25 namespace base { | 26 namespace base { |
26 | 27 |
27 class MessagePump; | 28 class MessagePump; |
28 class RunLoop; | 29 class RunLoop; |
29 | 30 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 // MessageLoop::Type to TYPE_CUSTOM. | 68 // MessageLoop::Type to TYPE_CUSTOM. |
68 MessagePumpFactory message_pump_factory; | 69 MessagePumpFactory message_pump_factory; |
69 | 70 |
70 // Specifies the maximum stack size that the thread is allowed to use. | 71 // Specifies the maximum stack size that the thread is allowed to use. |
71 // This does not necessarily correspond to the thread's initial stack size. | 72 // This does not necessarily correspond to the thread's initial stack size. |
72 // A value of 0 indicates that the default maximum should be used. | 73 // A value of 0 indicates that the default maximum should be used. |
73 size_t stack_size = 0; | 74 size_t stack_size = 0; |
74 | 75 |
75 // Specifies the initial thread priority. | 76 // Specifies the initial thread priority. |
76 ThreadPriority priority = ThreadPriority::NORMAL; | 77 ThreadPriority priority = ThreadPriority::NORMAL; |
| 78 |
| 79 // If false, the thread will not be joined on destruction. This is intended |
| 80 // for threads that want TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN |
| 81 // semantics. Stop() will not be synchronous and will instead merely have |
| 82 // StopSoon() semantics on such threads. Thread instances can only be |
| 83 // destroyed after their ThreadMain() returns (which typically means that |
| 84 // non-joinable instances must be leaked). |
| 85 // TODO(gab): allow non-joinable instances to be deleted without causing |
| 86 // user-after-frees (proposal @ https://crbug.com/629139#c14) |
| 87 bool joinable = true; |
77 }; | 88 }; |
78 | 89 |
79 // Constructor. | 90 // Constructor. |
80 // name is a display string to identify the thread. | 91 // name is a display string to identify the thread. |
81 explicit Thread(const std::string& name); | 92 explicit Thread(const std::string& name); |
82 | 93 |
83 // Destroys the thread, stopping it if necessary. | 94 // Destroys the thread, stopping it if necessary. |
84 // | 95 // |
85 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or | 96 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or |
86 // guarantee Stop() is explicitly called before the subclass is destroyed). | 97 // guarantee Stop() is explicitly called before the subclass is destroyed). |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 // WaitUntilThreadStarted(). | 135 // WaitUntilThreadStarted(). |
125 // Note that using this (instead of Start() or StartWithOptions() causes | 136 // Note that using this (instead of Start() or StartWithOptions() causes |
126 // jank on the calling thread, should be used only in testing code. | 137 // jank on the calling thread, should be used only in testing code. |
127 bool StartAndWaitForTesting(); | 138 bool StartAndWaitForTesting(); |
128 | 139 |
129 // Blocks until the thread starts running. Called within StartAndWait(). | 140 // Blocks until the thread starts running. Called within StartAndWait(). |
130 // Note that calling this causes jank on the calling thread, must be used | 141 // Note that calling this causes jank on the calling thread, must be used |
131 // carefully for production code. | 142 // carefully for production code. |
132 bool WaitUntilThreadStarted() const; | 143 bool WaitUntilThreadStarted() const; |
133 | 144 |
134 // Signals the thread to exit and returns once the thread has exited. After | 145 // Signals the thread to exit and returns once the thread has exited (or right |
135 // this method returns, the Thread object is completely reset and may be used | 146 // away if the thread is non-joinable). For joinable threads only: after this |
136 // as if it were newly constructed (i.e., Start may be called again). | 147 // method returns, the Thread object is completely reset and may be used as if |
| 148 // it were newly constructed (i.e., Start may be called again) -- non-joinable |
| 149 // threads are not re-usable. |
137 // | 150 // |
138 // Stop may be called multiple times and is simply ignored if the thread is | 151 // Stop may be called multiple times and is simply ignored if the thread is |
139 // already stopped. | 152 // already stopped or currently stopping. |
140 // | 153 // |
141 // NOTE: If you are a consumer of Thread, it is not necessary to call this | 154 // NOTE: If you are a consumer of Thread, it is not necessary to call this |
142 // before deleting your Thread objects, as the destructor will do it. | 155 // before deleting your Thread objects, as the destructor will do it. |
143 // IF YOU ARE A SUBCLASS OF Thread, YOU MUST CALL THIS IN YOUR DESTRUCTOR. | 156 // IF YOU ARE A SUBCLASS OF Thread, YOU MUST CALL THIS IN YOUR DESTRUCTOR. |
144 void Stop(); | 157 void Stop(); |
145 | 158 |
146 // Signals the thread to exit in the near future. | 159 // Signals the thread to exit in the near future. |
147 // | 160 // |
148 // WARNING: This function is not meant to be commonly used. Use at your own | 161 // WARNING: This function is not meant to be commonly used. Use at your own |
149 // risk. Calling this function will cause message_loop() to become invalid in | 162 // risk. Calling this function will cause message_loop() to become invalid in |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 | 234 |
222 // Called to start the run loop | 235 // Called to start the run loop |
223 virtual void Run(RunLoop* run_loop); | 236 virtual void Run(RunLoop* run_loop); |
224 | 237 |
225 // Called just after the message loop ends | 238 // Called just after the message loop ends |
226 virtual void CleanUp() {} | 239 virtual void CleanUp() {} |
227 | 240 |
228 static void SetThreadWasQuitProperly(bool flag); | 241 static void SetThreadWasQuitProperly(bool flag); |
229 static bool GetThreadWasQuitProperly(); | 242 static bool GetThreadWasQuitProperly(); |
230 | 243 |
231 void set_message_loop(MessageLoop* message_loop) { | 244 // Bind this Thread to an existing MessageLoop instead of starting a new one. |
232 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); | 245 void SetMessageLoop(MessageLoop* message_loop); |
233 message_loop_ = message_loop; | |
234 } | |
235 | 246 |
236 private: | 247 private: |
237 #if defined(OS_WIN) | 248 #if defined(OS_WIN) |
238 enum ComStatus { | 249 enum ComStatus { |
239 NONE, | 250 NONE, |
240 STA, | 251 STA, |
241 MTA, | 252 MTA, |
242 }; | 253 }; |
243 #endif | 254 #endif |
244 | 255 |
(...skipping 23 matching lines...) Expand all Loading... |
268 | 279 |
269 // The thread's id once it has started. | 280 // The thread's id once it has started. |
270 PlatformThreadId id_ = kInvalidThreadId; | 281 PlatformThreadId id_ = kInvalidThreadId; |
271 mutable WaitableEvent id_event_; // Protects |id_|. | 282 mutable WaitableEvent id_event_; // Protects |id_|. |
272 | 283 |
273 // The thread's MessageLoop and RunLoop. Valid only while the thread is alive. | 284 // The thread's MessageLoop and RunLoop. Valid only while the thread is alive. |
274 // Set by the created thread. | 285 // Set by the created thread. |
275 MessageLoop* message_loop_ = nullptr; | 286 MessageLoop* message_loop_ = nullptr; |
276 RunLoop* run_loop_ = nullptr; | 287 RunLoop* run_loop_ = nullptr; |
277 | 288 |
| 289 // True only if |message_loop_| was externally provided by |SetMessageLoop()| |
| 290 // in which case this Thread has no underlying |thread_| and should merely |
| 291 // drop |message_loop_| on Stop(). |
| 292 bool using_external_message_loop_ = false; |
| 293 |
278 // Stores Options::timer_slack_ until the message loop has been bound to | 294 // Stores Options::timer_slack_ until the message loop has been bound to |
279 // a thread. | 295 // a thread. |
280 TimerSlack message_loop_timer_slack_ = TIMER_SLACK_NONE; | 296 TimerSlack message_loop_timer_slack_ = TIMER_SLACK_NONE; |
281 | 297 |
282 // The name of the thread. Used for debugging purposes. | 298 // The name of the thread. Used for debugging purposes. |
283 const std::string name_; | 299 const std::string name_; |
284 | 300 |
285 // Signaled when the created thread gets ready to use the message loop. | 301 // Signaled when the created thread gets ready to use the message loop. |
286 mutable WaitableEvent start_event_; | 302 mutable WaitableEvent start_event_; |
287 | 303 |
288 // This class is not thread-safe, use this to verify access from the owning | 304 // This class is not thread-safe, use this to verify access from the owning |
289 // sequence of the Thread. | 305 // sequence of the Thread. |
290 SequenceChecker owning_sequence_checker_; | 306 SequenceChecker owning_sequence_checker_; |
291 | 307 |
| 308 // Set when ThreadMain() returns. The pointer itself is null until the thread |
| 309 // is started for the first time. so a Thread()/Start()/Stop()/Start()/... |
| 310 // sequence will result in null/false/true/false/... state for |
| 311 // |thread_main_exited_|. |
| 312 std::unique_ptr<AtomicFlag> thread_main_exited_; |
| 313 |
292 DISALLOW_COPY_AND_ASSIGN(Thread); | 314 DISALLOW_COPY_AND_ASSIGN(Thread); |
293 }; | 315 }; |
294 | 316 |
295 } // namespace base | 317 } // namespace base |
296 | 318 |
297 #endif // BASE_THREADING_THREAD_H_ | 319 #endif // BASE_THREADING_THREAD_H_ |
OLD | NEW |