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

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: Init() => InstallMessageLoop() -- Init was already used on Thread::... 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
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>
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 // MessageLoop::Type to TYPE_CUSTOM. 67 // MessageLoop::Type to TYPE_CUSTOM.
68 MessagePumpFactory message_pump_factory; 68 MessagePumpFactory message_pump_factory;
69 69
70 // 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.
71 // 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.
72 // 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.
73 size_t stack_size = 0; 73 size_t stack_size = 0;
74 74
75 // Specifies the initial thread priority. 75 // Specifies the initial thread priority.
76 ThreadPriority priority = ThreadPriority::NORMAL; 76 ThreadPriority priority = ThreadPriority::NORMAL;
77
78 // If false, the thread will not be joined on destruction. This is intended
79 // for threads that want TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN
80 // semantics. Stop() will not be synchronous and will instead merely have
81 // StopSoon() semantics on such threads.
82 bool joinable = true;
77 }; 83 };
78 84
79 // Constructor. 85 // Constructor.
80 // name is a display string to identify the thread. 86 // name is a display string to identify the thread.
81 explicit Thread(const std::string& name); 87 explicit Thread(const std::string& name);
82 88
83 // Destroys the thread, stopping it if necessary. 89 // Destroys the thread, stopping it if necessary.
84 // 90 //
85 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or 91 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or
86 // guarantee Stop() is explicitly called before the subclass is destroyed). 92 // guarantee Stop() is explicitly called before the subclass is destroyed).
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 // WaitUntilThreadStarted(). 130 // WaitUntilThreadStarted().
125 // Note that using this (instead of Start() or StartWithOptions() causes 131 // Note that using this (instead of Start() or StartWithOptions() causes
126 // jank on the calling thread, should be used only in testing code. 132 // jank on the calling thread, should be used only in testing code.
127 bool StartAndWaitForTesting(); 133 bool StartAndWaitForTesting();
128 134
129 // Blocks until the thread starts running. Called within StartAndWait(). 135 // Blocks until the thread starts running. Called within StartAndWait().
130 // Note that calling this causes jank on the calling thread, must be used 136 // Note that calling this causes jank on the calling thread, must be used
131 // carefully for production code. 137 // carefully for production code.
132 bool WaitUntilThreadStarted() const; 138 bool WaitUntilThreadStarted() const;
133 139
134 // Signals the thread to exit and returns once the thread has exited. After 140 // 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 141 // 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). 142 // method returns, the Thread object is completely reset and may be used as if
143 // it were newly constructed (i.e., Start may be called again) -- non-joinable
144 // threads are not re-usable.
137 // 145 //
138 // Stop may be called multiple times and is simply ignored if the thread is 146 // Stop may be called multiple times and is simply ignored if the thread is
139 // already stopped. 147 // already stopped or currently stopping.
140 // 148 //
141 // NOTE: If you are a consumer of Thread, it is not necessary to call this 149 // 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. 150 // 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. 151 // IF YOU ARE A SUBCLASS OF Thread, YOU MUST CALL THIS IN YOUR DESTRUCTOR.
144 void Stop(); 152 void Stop();
145 153
146 // Signals the thread to exit in the near future. 154 // Signals the thread to exit in the near future.
147 // 155 //
148 // WARNING: This function is not meant to be commonly used. Use at your own 156 // 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 157 // 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 229
222 // Called to start the run loop 230 // Called to start the run loop
223 virtual void Run(RunLoop* run_loop); 231 virtual void Run(RunLoop* run_loop);
224 232
225 // Called just after the message loop ends 233 // Called just after the message loop ends
226 virtual void CleanUp() {} 234 virtual void CleanUp() {}
227 235
228 static void SetThreadWasQuitProperly(bool flag); 236 static void SetThreadWasQuitProperly(bool flag);
229 static bool GetThreadWasQuitProperly(); 237 static bool GetThreadWasQuitProperly();
230 238
231 void set_message_loop(MessageLoop* message_loop) { 239 // Bind this Thread to an existing MessageLoop instead of starting a new one.
232 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); 240 void SetMessageLoop(MessageLoop* message_loop);
233 message_loop_ = message_loop;
234 }
235 241
236 private: 242 private:
237 #if defined(OS_WIN) 243 #if defined(OS_WIN)
238 enum ComStatus { 244 enum ComStatus {
239 NONE, 245 NONE,
240 STA, 246 STA,
241 MTA, 247 MTA,
242 }; 248 };
243 #endif 249 #endif
244 250
(...skipping 23 matching lines...) Expand all
268 274
269 // The thread's id once it has started. 275 // The thread's id once it has started.
270 PlatformThreadId id_ = kInvalidThreadId; 276 PlatformThreadId id_ = kInvalidThreadId;
271 mutable WaitableEvent id_event_; // Protects |id_|. 277 mutable WaitableEvent id_event_; // Protects |id_|.
272 278
273 // The thread's MessageLoop and RunLoop. Valid only while the thread is alive. 279 // The thread's MessageLoop and RunLoop. Valid only while the thread is alive.
274 // Set by the created thread. 280 // Set by the created thread.
275 MessageLoop* message_loop_ = nullptr; 281 MessageLoop* message_loop_ = nullptr;
276 RunLoop* run_loop_ = nullptr; 282 RunLoop* run_loop_ = nullptr;
277 283
284 // True only if |message_loop_| was externally provided by |SetMessageLoop()|
285 // in which case this Thread has no underlying |thread_| and should merely
286 // drop |message_loop_| on Stop().
287 bool using_external_message_loop_ = false;
288
278 // Stores Options::timer_slack_ until the message loop has been bound to 289 // Stores Options::timer_slack_ until the message loop has been bound to
279 // a thread. 290 // a thread.
280 TimerSlack message_loop_timer_slack_ = TIMER_SLACK_NONE; 291 TimerSlack message_loop_timer_slack_ = TIMER_SLACK_NONE;
281 292
282 // The name of the thread. Used for debugging purposes. 293 // The name of the thread. Used for debugging purposes.
283 const std::string name_; 294 const std::string name_;
284 295
285 // Signaled when the created thread gets ready to use the message loop. 296 // Signaled when the created thread gets ready to use the message loop.
286 mutable WaitableEvent start_event_; 297 mutable WaitableEvent start_event_;
287 298
288 // This class is not thread-safe, use this to verify access from the owning 299 // This class is not thread-safe, use this to verify access from the owning
289 // sequence of the Thread. 300 // sequence of the Thread.
290 SequenceChecker owning_sequence_checker_; 301 SequenceChecker owning_sequence_checker_;
291 302
292 DISALLOW_COPY_AND_ASSIGN(Thread); 303 DISALLOW_COPY_AND_ASSIGN(Thread);
293 }; 304 };
294 305
295 } // namespace base 306 } // namespace base
296 307
297 #endif // BASE_THREADING_THREAD_H_ 308 #endif // BASE_THREADING_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698