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

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

Issue 1180693002: Update from https://crrev.com/333737 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 6 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/simple_thread.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 <string> 8 #include <string>
9 9
10 #include "base/base_export.h" 10 #include "base/base_export.h"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/message_loop/timer_slack.h" 14 #include "base/message_loop/timer_slack.h"
15 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
16 #include "base/synchronization/lock.h"
16 #include "base/threading/platform_thread.h" 17 #include "base/threading/platform_thread.h"
17 18
18 namespace base { 19 namespace base {
19 20
20 class MessagePump; 21 class MessagePump;
22 class WaitableEvent;
21 23
22 // A simple thread abstraction that establishes a MessageLoop on a new thread. 24 // A simple thread abstraction that establishes a MessageLoop on a new thread.
23 // The consumer uses the MessageLoop of the thread to cause code to execute on 25 // The consumer uses the MessageLoop of the thread to cause code to execute on
24 // the thread. When this object is destroyed the thread is terminated. All 26 // the thread. When this object is destroyed the thread is terminated. All
25 // pending tasks queued on the thread's message loop will run to completion 27 // pending tasks queued on the thread's message loop will run to completion
26 // before the thread is terminated. 28 // before the thread is terminated.
27 // 29 //
28 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). 30 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread().
29 // 31 //
30 // After the thread is stopped, the destruction sequence is: 32 // After the thread is stopped, the destruction sequence is:
31 // 33 //
32 // (1) Thread::CleanUp() 34 // (1) Thread::CleanUp()
33 // (2) MessageLoop::~MessageLoop 35 // (2) MessageLoop::~MessageLoop
34 // (3.b) MessageLoop::DestructionObserver::WillDestroyCurrentMessageLoop 36 // (3.b) MessageLoop::DestructionObserver::WillDestroyCurrentMessageLoop
35 class BASE_EXPORT Thread : PlatformThread::Delegate { 37 class BASE_EXPORT Thread : PlatformThread::Delegate {
36 public: 38 public:
37 struct BASE_EXPORT Options { 39 struct BASE_EXPORT Options {
38 typedef Callback<scoped_ptr<MessagePump>()> MessagePumpFactory; 40 typedef Callback<scoped_ptr<MessagePump>()> MessagePumpFactory;
39 41
40 Options(); 42 Options();
41 Options(MessageLoop::Type type, size_t size); 43 Options(MessageLoop::Type type, size_t size);
42 ~Options(); 44 ~Options();
43 45
44 // Specifies the type of message loop that will be allocated on the thread. 46 // Specifies the type of message loop that will be allocated on the thread.
45 // This is ignored if message_pump_factory.is_null() is false. 47 // This is ignored if message_pump_factory.is_null() is false.
46 MessageLoop::Type message_loop_type; 48 MessageLoop::Type message_loop_type;
47 49
48 // Specify timer slack for thread message loop. 50 // Specifies timer slack for thread message loop.
49 TimerSlack timer_slack; 51 TimerSlack timer_slack;
50 52
51 // Used to create the MessagePump for the MessageLoop. The callback is Run() 53 // Used to create the MessagePump for the MessageLoop. The callback is Run()
52 // on the thread. If message_pump_factory.is_null(), then a MessagePump 54 // on the thread. If message_pump_factory.is_null(), then a MessagePump
53 // appropriate for |message_loop_type| is created. Setting this forces the 55 // appropriate for |message_loop_type| is created. Setting this forces the
54 // MessageLoop::Type to TYPE_CUSTOM. 56 // MessageLoop::Type to TYPE_CUSTOM.
55 MessagePumpFactory message_pump_factory; 57 MessagePumpFactory message_pump_factory;
56 58
57 // Specifies the maximum stack size that the thread is allowed to use. 59 // Specifies the maximum stack size that the thread is allowed to use.
58 // This does not necessarily correspond to the thread's initial stack size. 60 // This does not necessarily correspond to the thread's initial stack size.
59 // A value of 0 indicates that the default maximum should be used. 61 // A value of 0 indicates that the default maximum should be used.
60 size_t stack_size; 62 size_t stack_size;
63
64 // Specifies the initial thread priority.
65 ThreadPriority priority;
61 }; 66 };
62 67
63 // Constructor. 68 // Constructor.
64 // name is a display string to identify the thread. 69 // name is a display string to identify the thread.
65 explicit Thread(const std::string& name); 70 explicit Thread(const std::string& name);
66 71
67 // Destroys the thread, stopping it if necessary. 72 // Destroys the thread, stopping it if necessary.
68 // 73 //
69 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or 74 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or
70 // guarantee Stop() is explicitly called before the subclass is destroyed). 75 // guarantee Stop() is explicitly called before the subclass is destroyed).
71 // This is required to avoid a data race between the destructor modifying the 76 // This is required to avoid a data race between the destructor modifying the
72 // vtable, and the thread's ThreadMain calling the virtual method Run(). It 77 // vtable, and the thread's ThreadMain calling the virtual method Run(). It
73 // also ensures that the CleanUp() virtual method is called on the subclass 78 // also ensures that the CleanUp() virtual method is called on the subclass
74 // before it is destructed. 79 // before it is destructed.
75 ~Thread() override; 80 ~Thread() override;
76 81
77 #if defined(OS_WIN) 82 #if defined(OS_WIN)
78 // 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
79 // 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
80 // 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
81 // 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
82 // type other than TYPE_UI. 87 // type other than TYPE_UI.
83 void init_com_with_mta(bool use_mta) { 88 void init_com_with_mta(bool use_mta) {
84 DCHECK(!started_); 89 DCHECK(!start_event_);
85 com_status_ = use_mta ? MTA : STA; 90 com_status_ = use_mta ? MTA : STA;
86 } 91 }
87 #endif 92 #endif
88 93
89 // Starts the thread. Returns true if the thread was successfully started; 94 // Starts the thread. Returns true if the thread was successfully started;
90 // otherwise, returns false. Upon successful return, the message_loop() 95 // otherwise, returns false. Upon successful return, the message_loop()
91 // getter will return non-null. 96 // getter will return non-null.
92 // 97 //
93 // 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;
94 // i.e. during a DllMain, global object construction or destruction, atexit() 99 // i.e. during a DllMain, global object construction or destruction, atexit()
95 // callback. 100 // callback.
96 bool Start(); 101 bool Start();
97 102
98 // Starts the thread. Behaves exactly like Start in addition to allow to 103 // Starts the thread. Behaves exactly like Start in addition to allow to
99 // override the default options. 104 // override the default options.
100 // 105 //
101 // Note: This function can't be called on Windows with the loader lock held; 106 // Note: This function can't be called on Windows with the loader lock held;
102 // i.e. during a DllMain, global object construction or destruction, atexit() 107 // i.e. during a DllMain, global object construction or destruction, atexit()
103 // callback. 108 // callback.
104 bool StartWithOptions(const Options& options); 109 bool StartWithOptions(const Options& options);
105 110
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
113 // WaitUntilThreadStarted().
114 // Note that using this (instead of Start() or StartWithOptions() causes
115 // jank on the calling thread, should be used only in testing code.
116 bool StartAndWaitForTesting();
117
118 // Blocks until the thread starts running. Called within StartAndWait().
119 // Note that calling this causes jank on the calling thread, must be used
120 // carefully for production code.
121 bool WaitUntilThreadStarted();
122
106 // 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
107 // 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
108 // 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).
109 // 126 //
110 // 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
111 // already stopped. 128 // already stopped.
112 // 129 //
113 // 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
114 // before deleting your Thread objects, as the destructor will do it. 131 // before deleting your Thread objects, as the destructor will do it.
115 // 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.
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 return message_loop_->task_runner(); 176 return message_loop_->task_runner();
160 } 177 }
161 178
162 // Returns the name of this thread (for display in debugger too). 179 // Returns the name of this thread (for display in debugger too).
163 const std::string& thread_name() const { return name_; } 180 const std::string& thread_name() const { return name_; }
164 181
165 // The native thread handle. 182 // The native thread handle.
166 PlatformThreadHandle thread_handle() { return thread_; } 183 PlatformThreadHandle thread_handle() { return thread_; }
167 184
168 // The thread ID. 185 // The thread ID.
169 PlatformThreadId thread_id() const { return thread_id_; } 186 PlatformThreadId thread_id() const;
170 187
171 // Returns true if the thread has been started, and not yet stopped. 188 // Returns true if the thread has been started, and not yet stopped.
172 bool IsRunning() const; 189 bool IsRunning() const;
173 190
174 // Sets the thread priority. The thread must already be started. 191 // Sets the thread priority. The thread must already be started.
175 void SetPriority(ThreadPriority priority); 192 void SetPriority(ThreadPriority priority);
176 193
177 protected: 194 protected:
178 // Called just prior to starting the message loop 195 // Called just prior to starting the message loop
179 virtual void Init() {} 196 virtual void Init() {}
(...skipping 21 matching lines...) Expand all
201 #endif 218 #endif
202 219
203 // PlatformThread::Delegate methods: 220 // PlatformThread::Delegate methods:
204 void ThreadMain() override; 221 void ThreadMain() override;
205 222
206 #if defined(OS_WIN) 223 #if defined(OS_WIN)
207 // Whether this thread needs to initialize COM, and if so, in what mode. 224 // Whether this thread needs to initialize COM, and if so, in what mode.
208 ComStatus com_status_; 225 ComStatus com_status_;
209 #endif 226 #endif
210 227
211 // Whether we successfully started the thread.
212 bool started_;
213
214 // If true, we're in the middle of stopping, and shouldn't access 228 // If true, we're in the middle of stopping, and shouldn't access
215 // |message_loop_|. It may non-NULL and invalid. 229 // |message_loop_|. It may non-NULL and invalid.
216 bool stopping_; 230 bool stopping_;
217 231
218 // True while inside of Run(). 232 // True while inside of Run().
219 bool running_; 233 bool running_;
220 234 mutable base::Lock running_lock_; // Protects running_.
221 // Used to pass data to ThreadMain.
222 struct StartupData;
223 StartupData* startup_data_;
224 235
225 // The thread's handle. 236 // The thread's handle.
226 PlatformThreadHandle thread_; 237 PlatformThreadHandle thread_;
238 mutable base::Lock thread_lock_; // Protects thread_.
227 239
228 // The thread's message loop. Valid only while the thread is alive. Set 240 // The thread's message loop. Valid only while the thread is alive. Set
229 // by the created thread. 241 // by the created thread.
230 MessageLoop* message_loop_; 242 MessageLoop* message_loop_;
231 243
232 // Our thread's ID. 244 // Stores Options::timer_slack_ until the message loop has been bound to
233 PlatformThreadId thread_id_; 245 // a thread.
246 TimerSlack message_loop_timer_slack_;
234 247
235 // The name of the thread. Used for debugging purposes. 248 // The name of the thread. Used for debugging purposes.
236 std::string name_; 249 std::string name_;
237 250
251 // Non-null if the thread has successfully started.
252 scoped_ptr<WaitableEvent> start_event_;
253
238 friend void ThreadQuitHelper(); 254 friend void ThreadQuitHelper();
239 255
240 DISALLOW_COPY_AND_ASSIGN(Thread); 256 DISALLOW_COPY_AND_ASSIGN(Thread);
241 }; 257 };
242 258
243 } // namespace base 259 } // namespace base
244 260
245 #endif // BASE_THREADING_THREAD_H_ 261 #endif // BASE_THREADING_THREAD_H_
OLDNEW
« no previous file with comments | « base/threading/simple_thread.cc ('k') | base/threading/thread.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698