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

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

Issue 2135413003: Add |joinable| to Thread::Options (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ignore SetMessageLoop(nullptr) for now -- added TODO Created 4 years, 4 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 | « base/threading/platform_thread_win.cc ('k') | base/threading/thread.cc » ('j') | no next file with comments »
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/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
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. Non-joinable threads can't be joined (must be leaked and
82 // can't be destroyed or Stop()'ed).
83 // TODO(gab): allow non-joinable instances to be deleted without causing
84 // user-after-frees (proposal @ https://crbug.com/629139#c14)
85 bool joinable = true;
77 }; 86 };
78 87
79 // Constructor. 88 // Constructor.
80 // name is a display string to identify the thread. 89 // name is a display string to identify the thread.
81 explicit Thread(const std::string& name); 90 explicit Thread(const std::string& name);
82 91
83 // Destroys the thread, stopping it if necessary. 92 // Destroys the thread, stopping it if necessary.
84 // 93 //
85 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or 94 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or
86 // guarantee Stop() is explicitly called before the subclass is destroyed). 95 // guarantee Stop() is explicitly called before the subclass is destroyed).
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 // WaitUntilThreadStarted(). 133 // WaitUntilThreadStarted().
125 // Note that using this (instead of Start() or StartWithOptions() causes 134 // Note that using this (instead of Start() or StartWithOptions() causes
126 // jank on the calling thread, should be used only in testing code. 135 // jank on the calling thread, should be used only in testing code.
127 bool StartAndWaitForTesting(); 136 bool StartAndWaitForTesting();
128 137
129 // Blocks until the thread starts running. Called within StartAndWait(). 138 // Blocks until the thread starts running. Called within StartAndWait().
130 // Note that calling this causes jank on the calling thread, must be used 139 // Note that calling this causes jank on the calling thread, must be used
131 // carefully for production code. 140 // carefully for production code.
132 bool WaitUntilThreadStarted() const; 141 bool WaitUntilThreadStarted() const;
133 142
134 // Signals the thread to exit and returns once the thread has exited. After 143 // Signals the thread to exit and returns once the thread has exited. The
135 // this method returns, the Thread object is completely reset and may be used 144 // Thread object is completely reset and may be used as if it were newly
136 // as if it were newly constructed (i.e., Start may be called again). 145 // constructed (i.e., Start may be called again). Can only be called if
146 // |joinable_|.
137 // 147 //
138 // Stop may be called multiple times and is simply ignored if the thread is 148 // Stop may be called multiple times and is simply ignored if the thread is
139 // already stopped. 149 // already stopped or currently stopping.
140 // 150 //
141 // NOTE: If you are a consumer of Thread, it is not necessary to call this 151 // 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. 152 // 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. 153 // IF YOU ARE A SUBCLASS OF Thread, YOU MUST CALL THIS IN YOUR DESTRUCTOR.
144 void Stop(); 154 void Stop();
145 155
146 // Signals the thread to exit in the near future. 156 // Signals the thread to exit in the near future.
147 // 157 //
148 // WARNING: This function is not meant to be commonly used. Use at your own 158 // 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 159 // risk. Calling this function will cause message_loop() to become invalid in
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 231
222 // Called to start the run loop 232 // Called to start the run loop
223 virtual void Run(RunLoop* run_loop); 233 virtual void Run(RunLoop* run_loop);
224 234
225 // Called just after the message loop ends 235 // Called just after the message loop ends
226 virtual void CleanUp() {} 236 virtual void CleanUp() {}
227 237
228 static void SetThreadWasQuitProperly(bool flag); 238 static void SetThreadWasQuitProperly(bool flag);
229 static bool GetThreadWasQuitProperly(); 239 static bool GetThreadWasQuitProperly();
230 240
231 void set_message_loop(MessageLoop* message_loop) { 241 // Bind this Thread to an existing MessageLoop instead of starting a new one.
232 DCHECK(owning_sequence_checker_.CalledOnValidSequence()); 242 void SetMessageLoop(MessageLoop* message_loop);
233 message_loop_ = message_loop;
234 }
235 243
236 private: 244 private:
237 #if defined(OS_WIN) 245 #if defined(OS_WIN)
238 enum ComStatus { 246 enum ComStatus {
239 NONE, 247 NONE,
240 STA, 248 STA,
241 MTA, 249 MTA,
242 }; 250 };
243 #endif 251 #endif
244 252
245 // PlatformThread::Delegate methods: 253 // PlatformThread::Delegate methods:
246 void ThreadMain() override; 254 void ThreadMain() override;
247 255
248 void ThreadQuitHelper(); 256 void ThreadQuitHelper();
249 257
250 #if defined(OS_WIN) 258 #if defined(OS_WIN)
251 // Whether this thread needs to initialize COM, and if so, in what mode. 259 // Whether this thread needs to initialize COM, and if so, in what mode.
252 ComStatus com_status_ = NONE; 260 ComStatus com_status_ = NONE;
253 #endif 261 #endif
254 262
263 // Mirrors the Options::joinable field used to start this thread. Verified
264 // on Stop() -- non-joinable threads can't be joined (must be leaked).
265 bool joinable_ = true;
266
255 // If true, we're in the middle of stopping, and shouldn't access 267 // If true, we're in the middle of stopping, and shouldn't access
256 // |message_loop_|. It may non-nullptr and invalid. 268 // |message_loop_|. It may non-nullptr and invalid.
257 // Should be written on the thread that created this thread. Also read data 269 // Should be written on the thread that created this thread. Also read data
258 // could be wrong on other threads. 270 // could be wrong on other threads.
259 bool stopping_ = false; 271 bool stopping_ = false;
260 272
261 // True while inside of Run(). 273 // True while inside of Run().
262 bool running_ = false; 274 bool running_ = false;
263 mutable base::Lock running_lock_; // Protects |running_|. 275 mutable base::Lock running_lock_; // Protects |running_|.
264 276
265 // The thread's handle. 277 // The thread's handle.
266 PlatformThreadHandle thread_; 278 PlatformThreadHandle thread_;
267 mutable base::Lock thread_lock_; // Protects |thread_|. 279 mutable base::Lock thread_lock_; // Protects |thread_|.
268 280
269 // The thread's id once it has started. 281 // The thread's id once it has started.
270 PlatformThreadId id_ = kInvalidThreadId; 282 PlatformThreadId id_ = kInvalidThreadId;
271 mutable WaitableEvent id_event_; // Protects |id_|. 283 mutable WaitableEvent id_event_; // Protects |id_|.
272 284
273 // The thread's MessageLoop and RunLoop. Valid only while the thread is alive. 285 // The thread's MessageLoop and RunLoop. Valid only while the thread is alive.
274 // Set by the created thread. 286 // Set by the created thread.
275 MessageLoop* message_loop_ = nullptr; 287 MessageLoop* message_loop_ = nullptr;
276 RunLoop* run_loop_ = nullptr; 288 RunLoop* run_loop_ = nullptr;
277 289
290 // True only if |message_loop_| was externally provided by |SetMessageLoop()|
291 // in which case this Thread has no underlying |thread_| and should merely
292 // drop |message_loop_| on Stop().
293 bool using_external_message_loop_ = false;
294
278 // Stores Options::timer_slack_ until the message loop has been bound to 295 // Stores Options::timer_slack_ until the message loop has been bound to
279 // a thread. 296 // a thread.
280 TimerSlack message_loop_timer_slack_ = TIMER_SLACK_NONE; 297 TimerSlack message_loop_timer_slack_ = TIMER_SLACK_NONE;
281 298
282 // The name of the thread. Used for debugging purposes. 299 // The name of the thread. Used for debugging purposes.
283 const std::string name_; 300 const std::string name_;
284 301
285 // Signaled when the created thread gets ready to use the message loop. 302 // Signaled when the created thread gets ready to use the message loop.
286 mutable WaitableEvent start_event_; 303 mutable WaitableEvent start_event_;
287 304
288 // This class is not thread-safe, use this to verify access from the owning 305 // This class is not thread-safe, use this to verify access from the owning
289 // sequence of the Thread. 306 // sequence of the Thread.
290 SequenceChecker owning_sequence_checker_; 307 SequenceChecker owning_sequence_checker_;
291 308
292 DISALLOW_COPY_AND_ASSIGN(Thread); 309 DISALLOW_COPY_AND_ASSIGN(Thread);
293 }; 310 };
294 311
295 } // namespace base 312 } // namespace base
296 313
297 #endif // BASE_THREADING_THREAD_H_ 314 #endif // BASE_THREADING_THREAD_H_
OLDNEW
« no previous file with comments | « base/threading/platform_thread_win.cc ('k') | base/threading/thread.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698