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_THREAD_H_ | 5 #ifndef BASE_THREADING_THREAD_H_ |
6 #define BASE_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/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
14 | 14 |
| 15 #if defined(OS_WIN) |
| 16 #include "base/memory/scoped_ptr.h" |
| 17 #endif |
| 18 |
15 namespace base { | 19 namespace base { |
16 | 20 |
| 21 #if defined(OS_WIN) |
| 22 namespace win { |
| 23 class ScopedCOMInitializer; |
| 24 } |
| 25 #endif |
| 26 |
17 // A simple thread abstraction that establishes a MessageLoop on a new thread. | 27 // A simple thread abstraction that establishes a MessageLoop on a new thread. |
18 // The consumer uses the MessageLoop of the thread to cause code to execute on | 28 // The consumer uses the MessageLoop of the thread to cause code to execute on |
19 // the thread. When this object is destroyed the thread is terminated. All | 29 // the thread. When this object is destroyed the thread is terminated. All |
20 // pending tasks queued on the thread's message loop will run to completion | 30 // pending tasks queued on the thread's message loop will run to completion |
21 // before the thread is terminated. | 31 // before the thread is terminated. |
22 // | 32 // |
23 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). | 33 // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). |
24 // | 34 // |
25 // After the thread is stopped, the destruction sequence is: | 35 // After the thread is stopped, the destruction sequence is: |
26 // | 36 // |
(...skipping 23 matching lines...) Expand all Loading... |
50 // Destroys the thread, stopping it if necessary. | 60 // Destroys the thread, stopping it if necessary. |
51 // | 61 // |
52 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or | 62 // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or |
53 // guarantee Stop() is explicitly called before the subclass is destroyed). | 63 // guarantee Stop() is explicitly called before the subclass is destroyed). |
54 // This is required to avoid a data race between the destructor modifying the | 64 // This is required to avoid a data race between the destructor modifying the |
55 // vtable, and the thread's ThreadMain calling the virtual method Run(). It | 65 // vtable, and the thread's ThreadMain calling the virtual method Run(). It |
56 // also ensures that the CleanUp() virtual method is called on the subclass | 66 // also ensures that the CleanUp() virtual method is called on the subclass |
57 // before it is destructed. | 67 // before it is destructed. |
58 virtual ~Thread(); | 68 virtual ~Thread(); |
59 | 69 |
| 70 #if defined(OS_WIN) |
| 71 // Causes the thread to initialize COM. This must be called before calling |
| 72 // Start() or StartWithOptions(). If |use_mta| is false, the thread is also |
| 73 // started with a TYPE_UI message loop. It is an error to call |
| 74 // init_com_with_mta(false) and then StartWithOptions() with any message loop |
| 75 // type other than TYPE_UI. |
| 76 void init_com_with_mta(bool use_mta) { |
| 77 DCHECK(!started_); |
| 78 com_status_ = use_mta ? MTA : STA; |
| 79 } |
| 80 #endif |
| 81 |
60 // Starts the thread. Returns true if the thread was successfully started; | 82 // Starts the thread. Returns true if the thread was successfully started; |
61 // otherwise, returns false. Upon successful return, the message_loop() | 83 // otherwise, returns false. Upon successful return, the message_loop() |
62 // getter will return non-null. | 84 // getter will return non-null. |
63 // | 85 // |
64 // 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; |
65 // i.e. during a DllMain, global object construction or destruction, atexit() | 87 // i.e. during a DllMain, global object construction or destruction, atexit() |
66 // callback. | 88 // callback. |
67 bool Start(); | 89 bool Start(); |
68 | 90 |
69 // 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 virtual void CleanUp() {} | 163 virtual void CleanUp() {} |
142 | 164 |
143 static void SetThreadWasQuitProperly(bool flag); | 165 static void SetThreadWasQuitProperly(bool flag); |
144 static bool GetThreadWasQuitProperly(); | 166 static bool GetThreadWasQuitProperly(); |
145 | 167 |
146 void set_message_loop(MessageLoop* message_loop) { | 168 void set_message_loop(MessageLoop* message_loop) { |
147 message_loop_ = message_loop; | 169 message_loop_ = message_loop; |
148 } | 170 } |
149 | 171 |
150 private: | 172 private: |
151 bool thread_was_started() const { return started_; } | 173 #if defined(OS_WIN) |
| 174 enum ComStatus { |
| 175 NONE, |
| 176 STA, |
| 177 MTA, |
| 178 }; |
| 179 ComStatus com_status_; |
| 180 scoped_ptr<win::ScopedCOMInitializer> com_initializer_; |
| 181 #endif |
152 | 182 |
153 // PlatformThread::Delegate methods: | 183 // PlatformThread::Delegate methods: |
154 virtual void ThreadMain() OVERRIDE; | 184 virtual void ThreadMain() OVERRIDE; |
155 | 185 |
156 // Whether we successfully started the thread. | 186 // Whether we successfully started the thread. |
157 bool started_; | 187 bool started_; |
158 | 188 |
159 // If true, we're in the middle of stopping, and shouldn't access | 189 // If true, we're in the middle of stopping, and shouldn't access |
160 // |message_loop_|. It may non-NULL and invalid. | 190 // |message_loop_|. It may non-NULL and invalid. |
161 bool stopping_; | 191 bool stopping_; |
(...skipping 18 matching lines...) Expand all Loading... |
180 // The name of the thread. Used for debugging purposes. | 210 // The name of the thread. Used for debugging purposes. |
181 std::string name_; | 211 std::string name_; |
182 | 212 |
183 friend void ThreadQuitHelper(); | 213 friend void ThreadQuitHelper(); |
184 | 214 |
185 DISALLOW_COPY_AND_ASSIGN(Thread); | 215 DISALLOW_COPY_AND_ASSIGN(Thread); |
186 }; | 216 }; |
187 | 217 |
188 } // namespace base | 218 } // namespace base |
189 | 219 |
190 #endif // BASE_THREAD_H_ | 220 #endif // BASE_THREADING_THREAD_H_ |
OLD | NEW |