Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(470)

Side by Side Diff: base/threading/thread.h

Issue 2145463002: Modernize base::Thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Disable check in IsRunning() per crbug/629139 and add extra logging. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/threading/thread.cc » ('j') | base/threading/thread.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/single_thread_task_runner.h" 19 #include "base/single_thread_task_runner.h"
19 #include "base/synchronization/lock.h" 20 #include "base/synchronization/lock.h"
20 #include "base/synchronization/waitable_event.h" 21 #include "base/synchronization/waitable_event.h"
21 #include "base/threading/platform_thread.h" 22 #include "base/threading/platform_thread.h"
22 #include "build/build_config.h" 23 #include "build/build_config.h"
23 24
24 namespace base { 25 namespace base {
25 26
26 class MessagePump; 27 class MessagePump;
27 class RunLoop; 28 class RunLoop;
28 29
29 // A simple thread abstraction that establishes a MessageLoop on a new thread. 30 // A simple thread abstraction that establishes a MessageLoop on a new thread.
30 // The consumer uses the MessageLoop of the thread to cause code to execute on 31 // The consumer uses the MessageLoop of the thread to cause code to execute on
31 // the thread. When this object is destroyed the thread is terminated. All 32 // the thread. When this object is destroyed the thread is terminated. All
32 // pending tasks queued on the thread's message loop will run to completion 33 // pending tasks queued on the thread's message loop will run to completion
33 // before the thread is terminated. 34 // before the thread is terminated.
34 // 35 //
35 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). 36 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread().
36 // 37 //
37 // After the thread is stopped, the destruction sequence is: 38 // After the thread is stopped, the destruction sequence is:
38 // 39 //
39 // (1) Thread::CleanUp() 40 // (1) Thread::CleanUp()
40 // (2) MessageLoop::~MessageLoop 41 // (2) MessageLoop::~MessageLoop
41 // (3.b) MessageLoop::DestructionObserver::WillDestroyCurrentMessageLoop 42 // (3.b) MessageLoop::DestructionObserver::WillDestroyCurrentMessageLoop
43 //
44 // This API is not thread-safe: unless indicated otherwise it's methods are only
45 // valid from the owning sequence (which is the one from which Start() is
46 // invoked, should it differ from the one on which it was constructed).
42 class BASE_EXPORT Thread : PlatformThread::Delegate { 47 class BASE_EXPORT Thread : PlatformThread::Delegate {
43 public: 48 public:
44 struct BASE_EXPORT Options { 49 struct BASE_EXPORT Options {
45 typedef Callback<std::unique_ptr<MessagePump>()> MessagePumpFactory; 50 typedef Callback<std::unique_ptr<MessagePump>()> MessagePumpFactory;
46 51
47 Options(); 52 Options();
48 Options(MessageLoop::Type type, size_t size); 53 Options(MessageLoop::Type type, size_t size);
49 Options(const Options& other); 54 Options(const Options& other);
50 ~Options(); 55 ~Options();
51 56
52 // Specifies the type of message loop that will be allocated on the thread. 57 // Specifies the type of message loop that will be allocated on the thread.
53 // This is ignored if message_pump_factory.is_null() is false. 58 // This is ignored if message_pump_factory.is_null() is false.
54 MessageLoop::Type message_loop_type; 59 MessageLoop::Type message_loop_type = MessageLoop::TYPE_DEFAULT;
55 60
56 // Specifies timer slack for thread message loop. 61 // Specifies timer slack for thread message loop.
57 TimerSlack timer_slack; 62 TimerSlack timer_slack = TIMER_SLACK_NONE;
58 63
59 // Used to create the MessagePump for the MessageLoop. The callback is Run() 64 // Used to create the MessagePump for the MessageLoop. The callback is Run()
60 // on the thread. If message_pump_factory.is_null(), then a MessagePump 65 // on the thread. If message_pump_factory.is_null(), then a MessagePump
61 // appropriate for |message_loop_type| is created. Setting this forces the 66 // appropriate for |message_loop_type| is created. Setting this forces the
62 // MessageLoop::Type to TYPE_CUSTOM. 67 // MessageLoop::Type to TYPE_CUSTOM.
63 MessagePumpFactory message_pump_factory; 68 MessagePumpFactory message_pump_factory;
64 69
65 // Specifies the maximum stack size that the thread is allowed to use. 70 // Specifies the maximum stack size that the thread is allowed to use.
66 // This does not necessarily correspond to the thread's initial stack size. 71 // This does not necessarily correspond to the thread's initial stack size.
67 // A value of 0 indicates that the default maximum should be used. 72 // A value of 0 indicates that the default maximum should be used.
68 size_t stack_size; 73 size_t stack_size = 0;
69 74
70 // Specifies the initial thread priority. 75 // Specifies the initial thread priority.
71 ThreadPriority priority; 76 ThreadPriority priority = ThreadPriority::NORMAL;
72 }; 77 };
73 78
74 // Constructor. 79 // Constructor.
75 // name is a display string to identify the thread. 80 // name is a display string to identify the thread.
76 explicit Thread(const std::string& name); 81 explicit Thread(const std::string& name);
77 82
78 // Destroys the thread, stopping it if necessary. 83 // Destroys the thread, stopping it if necessary.
79 // 84 //
80 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or 85 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or
81 // guarantee Stop() is explicitly called before the subclass is destroyed). 86 // guarantee Stop() is explicitly called before the subclass is destroyed).
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 void StopSoon(); 157 void StopSoon();
153 158
154 // Returns the message loop for this thread. Use the MessageLoop's 159 // Returns the message loop for this thread. Use the MessageLoop's
155 // PostTask methods to execute code on the thread. This only returns 160 // PostTask methods to execute code on the thread. This only returns
156 // non-null after a successful call to Start. After Stop has been called, 161 // non-null after a successful call to Start. After Stop has been called,
157 // this will return nullptr. 162 // this will return nullptr.
158 // 163 //
159 // NOTE: You must not call this MessageLoop's Quit method directly. Use 164 // NOTE: You must not call this MessageLoop's Quit method directly. Use
160 // the Thread's Stop method instead. 165 // the Thread's Stop method instead.
161 // 166 //
162 MessageLoop* message_loop() const { return message_loop_; } 167 // In addition to this Thread's owner, this can also safely be called from the
danakj 2016/07/21 19:43:26 "Thread's owning sequence" sounds more strictly co
gab 2016/07/22 16:02:55 Done.
168 // underlying thread itself.
169 MessageLoop* message_loop() const {
170 // This class doesn't provide synchronization around |message_loop_| and as
171 // such only the owner should access it (and the underlying thread which
172 // never sees it before it's set). In practice, many callers are coming from
173 // unrelated threads but provide their own implicit (e.g. memory barriers
174 // from task posting) or explicit (e.g. locks) synchronization making the
175 // access of |message_loop_| safe... Changing all of those callers is
176 // unfeasible; instead verify that they can reliably see
177 // |message_loop_ != nullptr| without synchronization as a proof that their
178 // external synchronization catches the unsynchronized effects of Start().
179 DCHECK(sequence_checker_.CalledOnValidSequencedThread() ||
180 id_ == PlatformThread::CurrentId() || message_loop_)
181 << id_ << " vs " << PlatformThread::CurrentId();
182 return message_loop_;
183 }
163 184
164 // Returns a TaskRunner for this thread. Use the TaskRunner's PostTask 185 // Returns a TaskRunner for this thread. Use the TaskRunner's PostTask
165 // methods to execute code on the thread. Returns nullptr if the thread is not 186 // methods to execute code on the thread. Returns nullptr if the thread is not
166 // running (e.g. before Start or after Stop have been called). Callers can 187 // running (e.g. before Start or after Stop have been called). Callers can
167 // hold on to this even after the thread is gone; in this situation, attempts 188 // hold on to this even after the thread is gone; in this situation, attempts
168 // to PostTask() will fail. 189 // to PostTask() will fail.
190 //
191 // In addition to this Thread's owner, this can also safely be called from the
danakj 2016/07/21 19:43:26 ditto "owner"
gab 2016/07/22 16:02:55 Done.
192 // underlying thread itself.
169 scoped_refptr<SingleThreadTaskRunner> task_runner() const { 193 scoped_refptr<SingleThreadTaskRunner> task_runner() const {
194 // Ref. comment on |message_loop()| DCHECK.
danakj 2016/07/21 19:43:26 "Refer to the DCHECK and comment inside |message_l
gab 2016/07/22 16:02:55 Done.
195 DCHECK(sequence_checker_.CalledOnValidSequencedThread() ||
196 id_ == PlatformThread::CurrentId() || message_loop_)
197 << id_ << " vs " << PlatformThread::CurrentId();
170 return message_loop_ ? message_loop_->task_runner() : nullptr; 198 return message_loop_ ? message_loop_->task_runner() : nullptr;
171 } 199 }
172 200
173 // Returns the name of this thread (for display in debugger too). 201 // Returns the name of this thread (for display in debugger too).
174 const std::string& thread_name() const { return name_; } 202 const std::string& thread_name() const { return name_; }
175 203
176 // Returns the thread ID. Should not be called before the first Start*() 204 // Returns the thread ID. Should not be called before the first Start*()
177 // call. Keeps on returning the same ID even after a Stop() call. The next 205 // call. Keeps on returning the same ID even after a Stop() call. The next
178 // Start*() call renews the ID. 206 // Start*() call renews the ID.
179 // 207 //
180 // WARNING: This function will block if the thread hasn't started yet. 208 // WARNING: This function will block if the thread hasn't started yet.
181 // 209 //
210 // This method is thread-safe.
182 PlatformThreadId GetThreadId() const; 211 PlatformThreadId GetThreadId() const;
183 212
184 // Returns true if the thread has been started, and not yet stopped. 213 // Returns true if the thread has been started, and not yet stopped.
185 bool IsRunning() const; 214 bool IsRunning() const;
186 215
187 protected: 216 protected:
188 // Called just prior to starting the message loop 217 // Called just prior to starting the message loop
189 virtual void Init() {} 218 virtual void Init() {}
190 219
191 // Called to start the run loop 220 // Called to start the run loop
192 virtual void Run(RunLoop* run_loop); 221 virtual void Run(RunLoop* run_loop);
193 222
194 // Called just after the message loop ends 223 // Called just after the message loop ends
195 virtual void CleanUp() {} 224 virtual void CleanUp() {}
196 225
197 static void SetThreadWasQuitProperly(bool flag); 226 static void SetThreadWasQuitProperly(bool flag);
198 static bool GetThreadWasQuitProperly(); 227 static bool GetThreadWasQuitProperly();
199 228
200 void set_message_loop(MessageLoop* message_loop) { 229 void set_message_loop(MessageLoop* message_loop) {
230 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
201 message_loop_ = message_loop; 231 message_loop_ = message_loop;
202 } 232 }
203 233
204 private: 234 private:
205 #if defined(OS_WIN) 235 #if defined(OS_WIN)
206 enum ComStatus { 236 enum ComStatus {
207 NONE, 237 NONE,
208 STA, 238 STA,
209 MTA, 239 MTA,
210 }; 240 };
211 #endif 241 #endif
212 242
213 // PlatformThread::Delegate methods: 243 // PlatformThread::Delegate methods:
214 void ThreadMain() override; 244 void ThreadMain() override;
215 245
216 void ThreadQuitHelper(); 246 void ThreadQuitHelper();
217 247
218 #if defined(OS_WIN) 248 #if defined(OS_WIN)
219 // Whether this thread needs to initialize COM, and if so, in what mode. 249 // Whether this thread needs to initialize COM, and if so, in what mode.
220 ComStatus com_status_; 250 ComStatus com_status_ = NONE;
221 #endif 251 #endif
222 252
223 // If true, we're in the middle of stopping, and shouldn't access 253 // If true, we're in the middle of stopping, and shouldn't access
224 // |message_loop_|. It may non-nullptr and invalid. 254 // |message_loop_|. It may non-nullptr and invalid.
225 // Should be written on the thread that created this thread. Also read data 255 // Should be written on the thread that created this thread. Also read data
226 // could be wrong on other threads. 256 // could be wrong on other threads.
227 bool stopping_; 257 bool stopping_ = false;
228 258
229 // True while inside of Run(). 259 // True while inside of Run().
230 bool running_; 260 bool running_ = false;
231 mutable base::Lock running_lock_; // Protects |running_|. 261 mutable base::Lock running_lock_; // Protects |running_|.
232 262
233 // The thread's handle. 263 // The thread's handle.
234 PlatformThreadHandle thread_; 264 PlatformThreadHandle thread_;
235 mutable base::Lock thread_lock_; // Protects |thread_|.
236 265
237 // The thread's id once it has started. 266 // The thread's id once it has started.
238 PlatformThreadId id_; 267 PlatformThreadId id_ = kInvalidThreadId;
239 mutable WaitableEvent id_event_; // Protects |id_|. 268 mutable WaitableEvent id_event_; // Protects |id_|.
240 269
241 // The thread's MessageLoop and RunLoop. Valid only while the thread is alive. 270 // The thread's MessageLoop and RunLoop. Valid only while the thread is alive.
242 // Set by the created thread. 271 // Set by the created thread.
243 MessageLoop* message_loop_; 272 MessageLoop* message_loop_ = nullptr;
244 RunLoop* run_loop_; 273 RunLoop* run_loop_ = nullptr;
245 274
246 // Stores Options::timer_slack_ until the message loop has been bound to 275 // Stores Options::timer_slack_ until the message loop has been bound to
247 // a thread. 276 // a thread.
248 TimerSlack message_loop_timer_slack_; 277 TimerSlack message_loop_timer_slack_ = TIMER_SLACK_NONE;
249 278
250 // The name of the thread. Used for debugging purposes. 279 // The name of the thread. Used for debugging purposes.
251 std::string name_; 280 const std::string name_;
danakj 2016/07/21 19:43:26 Yay const
gab 2016/07/22 16:02:55 :-)
252 281
253 // Signaled when the created thread gets ready to use the message loop. 282 // Signaled when the created thread gets ready to use the message loop.
254 mutable WaitableEvent start_event_; 283 mutable WaitableEvent start_event_;
255 284
285 // This class is not thread-safe.
danakj 2016/07/21 19:43:26 ...thread-safe, use this to verify access from the
286 SequenceChecker sequence_checker_;
danakj 2016/07/21 19:43:26 nit: maybe call this owning_sequence_checker_? or
gab 2016/07/22 16:02:55 Done.
287
256 DISALLOW_COPY_AND_ASSIGN(Thread); 288 DISALLOW_COPY_AND_ASSIGN(Thread);
257 }; 289 };
258 290
259 } // namespace base 291 } // namespace base
260 292
261 #endif // BASE_THREADING_THREAD_H_ 293 #endif // BASE_THREADING_THREAD_H_
OLDNEW
« no previous file with comments | « no previous file | base/threading/thread.cc » ('j') | base/threading/thread.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698