OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 IOS_WEB_PUBLIC_WEB_THREAD_H_ | 5 #ifndef IOS_WEB_PUBLIC_WEB_THREAD_H_ |
6 #define IOS_WEB_PUBLIC_WEB_THREAD_H_ | 6 #define IOS_WEB_PUBLIC_WEB_THREAD_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 // unregistered by providing a nullptr pointer. | 210 // unregistered by providing a nullptr pointer. |
211 // | 211 // |
212 // If the caller unregisters a delegate before CleanUp has been | 212 // If the caller unregisters a delegate before CleanUp has been |
213 // called, it must perform its own locking to ensure the delegate is | 213 // called, it must perform its own locking to ensure the delegate is |
214 // not deleted while unregistering. | 214 // not deleted while unregistering. |
215 static void SetDelegate(ID identifier, WebThreadDelegate* delegate); | 215 static void SetDelegate(ID identifier, WebThreadDelegate* delegate); |
216 | 216 |
217 // Returns an appropriate error message for when DCHECK_CURRENTLY_ON() fails. | 217 // Returns an appropriate error message for when DCHECK_CURRENTLY_ON() fails. |
218 static std::string GetDCheckCurrentlyOnErrorMessage(ID expected); | 218 static std::string GetDCheckCurrentlyOnErrorMessage(ID expected); |
219 | 219 |
220 // Use these templates in conjunction with RefCountedThreadSafe or scoped_ptr | 220 // Use these templates in conjunction with RefCountedThreadSafe or |
221 // when you want to ensure that an object is deleted on a specific thread. | 221 // std::unique_ptr when you want to ensure that an object is deleted on a |
222 // This is needed when an object can hop between threads | 222 // specific thread. This is needed when an object can hop between threads |
223 // (i.e. IO -> FILE -> IO), and thread switching delays can mean that the | 223 // (i.e. IO -> FILE -> IO), and thread switching delays can mean that the |
224 // final IO tasks executes before the FILE task's stack unwinds. | 224 // final IO tasks executes before the FILE task's stack unwinds. |
225 // This would lead to the object destructing on the FILE thread, which often | 225 // This would lead to the object destructing on the FILE thread, which often |
226 // is not what you want (i.e. to unregister from NotificationService, to | 226 // is not what you want (i.e. to unregister from NotificationService, to |
227 // notify other objects on the creating thread etc). | 227 // notify other objects on the creating thread etc). |
228 template <ID thread> | 228 template <ID thread> |
229 struct DeleteOnThread { | 229 struct DeleteOnThread { |
230 template <typename T> | 230 template <typename T> |
231 static void Destruct(const T* x) { | 231 static void Destruct(const T* x) { |
232 if (CurrentlyOn(thread)) { | 232 if (CurrentlyOn(thread)) { |
(...skipping 17 matching lines...) Expand all Loading... |
250 // : public base::RefCountedThreadSafe< | 250 // : public base::RefCountedThreadSafe< |
251 // Foo, web::WebThread::DeleteOnIOThread> { | 251 // Foo, web::WebThread::DeleteOnIOThread> { |
252 // | 252 // |
253 // ... | 253 // ... |
254 // private: | 254 // private: |
255 // friend struct web::WebThread::DeleteOnThread<web::WebThread::IO>; | 255 // friend struct web::WebThread::DeleteOnThread<web::WebThread::IO>; |
256 // friend class base::DeleteHelper<Foo>; | 256 // friend class base::DeleteHelper<Foo>; |
257 // | 257 // |
258 // ~Foo(); | 258 // ~Foo(); |
259 // | 259 // |
260 // Sample usage with scoped_ptr: | 260 // Sample usage with std::unique_ptr: |
261 // scoped_ptr<Foo, web::WebThread::DeleteOnIOThread> ptr; | 261 // std::unique_ptr<Foo, web::WebThread::DeleteOnIOThread> ptr; |
262 struct DeleteOnUIThread : public DeleteOnThread<UI> {}; | 262 struct DeleteOnUIThread : public DeleteOnThread<UI> {}; |
263 struct DeleteOnIOThread : public DeleteOnThread<IO> {}; | 263 struct DeleteOnIOThread : public DeleteOnThread<IO> {}; |
264 struct DeleteOnFileThread : public DeleteOnThread<FILE> {}; | 264 struct DeleteOnFileThread : public DeleteOnThread<FILE> {}; |
265 struct DeleteOnDBThread : public DeleteOnThread<DB> {}; | 265 struct DeleteOnDBThread : public DeleteOnThread<DB> {}; |
266 | 266 |
267 private: | 267 private: |
268 friend class WebThreadImpl; | 268 friend class WebThreadImpl; |
269 | 269 |
270 WebThread() {} | 270 WebThread() {} |
271 DISALLOW_COPY_AND_ASSIGN(WebThread); | 271 DISALLOW_COPY_AND_ASSIGN(WebThread); |
272 }; | 272 }; |
273 | 273 |
274 } // namespace web | 274 } // namespace web |
275 | 275 |
276 #endif // IOS_WEB_PUBLIC_WEB_THREAD_H_ | 276 #endif // IOS_WEB_PUBLIC_WEB_THREAD_H_ |
OLD | NEW |