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 <string> | 8 #include <string> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 // Destroys the thread, stopping it if necessary. | 72 // Destroys the thread, stopping it if necessary. |
73 // | 73 // |
74 // 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 |
75 // guarantee Stop() is explicitly called before the subclass is destroyed). | 75 // guarantee Stop() is explicitly called before the subclass is destroyed). |
76 // 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 |
77 // 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 |
78 // 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 |
79 // before it is destructed. | 79 // before it is destructed. |
80 ~Thread() override; | 80 ~Thread() override; |
81 | 81 |
82 #if defined(OS_WIN) | |
83 // Causes the thread to initialize COM. This must be called before calling | |
84 // Start() or StartWithOptions(). If |use_mta| is false, the thread is also | |
85 // started with a TYPE_UI message loop. It is an error to call | |
86 // init_com_with_mta(false) and then StartWithOptions() with any message loop | |
87 // type other than TYPE_UI. | |
88 void init_com_with_mta(bool use_mta) { | |
89 DCHECK(!start_event_); | |
90 com_status_ = use_mta ? MTA : STA; | |
91 } | |
92 #endif | |
93 | |
94 // Starts the thread. Returns true if the thread was successfully started; | 82 // Starts the thread. Returns true if the thread was successfully started; |
95 // otherwise, returns false. Upon successful return, the message_loop() | 83 // otherwise, returns false. Upon successful return, the message_loop() |
96 // getter will return non-null. | 84 // getter will return non-null. |
97 // | 85 // |
98 // Note: This function can't be called on Windows with the loader lock held; | 86 // Note: This function can't be called on Windows with the loader lock held; |
99 // i.e. during a DllMain, global object construction or destruction, atexit() | 87 // i.e. during a DllMain, global object construction or destruction, atexit() |
100 // callback. | 88 // callback. |
101 bool Start(); | 89 bool Start(); |
102 | 90 |
103 // Starts the thread. Behaves exactly like Start in addition to allow to | 91 // Starts the thread. Behaves exactly like Start in addition to allow to |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 virtual void CleanUp() {} | 190 virtual void CleanUp() {} |
203 | 191 |
204 static void SetThreadWasQuitProperly(bool flag); | 192 static void SetThreadWasQuitProperly(bool flag); |
205 static bool GetThreadWasQuitProperly(); | 193 static bool GetThreadWasQuitProperly(); |
206 | 194 |
207 void set_message_loop(MessageLoop* message_loop) { | 195 void set_message_loop(MessageLoop* message_loop) { |
208 message_loop_ = message_loop; | 196 message_loop_ = message_loop; |
209 } | 197 } |
210 | 198 |
211 private: | 199 private: |
212 #if defined(OS_WIN) | |
213 enum ComStatus { | |
214 NONE, | |
215 STA, | |
216 MTA, | |
217 }; | |
218 #endif | |
219 | |
220 // PlatformThread::Delegate methods: | 200 // PlatformThread::Delegate methods: |
221 void ThreadMain() override; | 201 void ThreadMain() override; |
222 | 202 |
223 #if defined(OS_WIN) | |
224 // Whether this thread needs to initialize COM, and if so, in what mode. | |
225 ComStatus com_status_; | |
226 #endif | |
227 | |
228 // If true, we're in the middle of stopping, and shouldn't access | 203 // If true, we're in the middle of stopping, and shouldn't access |
229 // |message_loop_|. It may non-NULL and invalid. | 204 // |message_loop_|. It may non-NULL and invalid. |
230 bool stopping_; | 205 bool stopping_; |
231 | 206 |
232 // True while inside of Run(). | 207 // True while inside of Run(). |
233 bool running_; | 208 bool running_; |
234 mutable base::Lock running_lock_; // Protects running_. | 209 mutable base::Lock running_lock_; // Protects running_. |
235 | 210 |
236 // The thread's handle. | 211 // The thread's handle. |
237 PlatformThreadHandle thread_; | 212 PlatformThreadHandle thread_; |
(...skipping 14 matching lines...) Expand all Loading... |
252 scoped_ptr<WaitableEvent> start_event_; | 227 scoped_ptr<WaitableEvent> start_event_; |
253 | 228 |
254 friend void ThreadQuitHelper(); | 229 friend void ThreadQuitHelper(); |
255 | 230 |
256 DISALLOW_COPY_AND_ASSIGN(Thread); | 231 DISALLOW_COPY_AND_ASSIGN(Thread); |
257 }; | 232 }; |
258 | 233 |
259 } // namespace base | 234 } // namespace base |
260 | 235 |
261 #endif // BASE_THREADING_THREAD_H_ | 236 #endif // BASE_THREADING_THREAD_H_ |
OLD | NEW |