| 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 CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ | 5 #ifndef CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ |
| 6 #define CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ | 6 #define CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 // Sets the delegate for the specified BrowserThread. | 222 // Sets the delegate for the specified BrowserThread. |
| 223 // | 223 // |
| 224 // Only one delegate may be registered at a time. Delegates may be | 224 // Only one delegate may be registered at a time. Delegates may be |
| 225 // unregistered by providing a NULL pointer. | 225 // unregistered by providing a NULL pointer. |
| 226 // | 226 // |
| 227 // If the caller unregisters a delegate before CleanUp has been | 227 // If the caller unregisters a delegate before CleanUp has been |
| 228 // called, it must perform its own locking to ensure the delegate is | 228 // called, it must perform its own locking to ensure the delegate is |
| 229 // not deleted while unregistering. | 229 // not deleted while unregistering. |
| 230 static void SetDelegate(ID identifier, BrowserThreadDelegate* delegate); | 230 static void SetDelegate(ID identifier, BrowserThreadDelegate* delegate); |
| 231 | 231 |
| 232 // Use these templates in conjuction with RefCountedThreadSafe when you want | 232 // Use these templates in conjunction with RefCountedThreadSafe or scoped_ptr |
| 233 // to ensure that an object is deleted on a specific thread. This is needed | 233 // when you want to ensure that an object is deleted on a specific thread. |
| 234 // when an object can hop between threads (i.e. IO -> FILE -> IO), and thread | 234 // This is needed when an object can hop between threads |
| 235 // switching delays can mean that the final IO tasks executes before the FILE | 235 // (i.e. IO -> FILE -> IO), and thread switching delays can mean that the |
| 236 // task's stack unwinds. This would lead to the object destructing on the | 236 // final IO tasks executes before the FILE task's stack unwinds. |
| 237 // FILE thread, which often is not what you want (i.e. to unregister from | 237 // This would lead to the object destructing on the FILE thread, which often |
| 238 // NotificationService, to notify other objects on the creating thread etc). | 238 // is not what you want (i.e. to unregister from NotificationService, to |
| 239 // notify other objects on the creating thread etc). |
| 239 template<ID thread> | 240 template<ID thread> |
| 240 struct DeleteOnThread { | 241 struct DeleteOnThread { |
| 241 template<typename T> | 242 template<typename T> |
| 242 static void Destruct(const T* x) { | 243 static void Destruct(const T* x) { |
| 243 if (CurrentlyOn(thread)) { | 244 if (CurrentlyOn(thread)) { |
| 244 delete x; | 245 delete x; |
| 245 } else { | 246 } else { |
| 246 if (!DeleteSoon(thread, FROM_HERE, x)) { | 247 if (!DeleteSoon(thread, FROM_HERE, x)) { |
| 247 #if defined(UNIT_TEST) | 248 #if defined(UNIT_TEST) |
| 248 // Only logged under unit testing because leaks at shutdown | 249 // Only logged under unit testing because leaks at shutdown |
| 249 // are acceptable under normal circumstances. | 250 // are acceptable under normal circumstances. |
| 250 LOG(ERROR) << "DeleteSoon failed on thread " << thread; | 251 LOG(ERROR) << "DeleteSoon failed on thread " << thread; |
| 251 #endif // UNIT_TEST | 252 #endif // UNIT_TEST |
| 252 } | 253 } |
| 253 } | 254 } |
| 254 } | 255 } |
| 256 template <typename T> |
| 257 inline void operator()(T* ptr) const { |
| 258 enum { type_must_be_complete = sizeof(T) }; |
| 259 Destruct(ptr); |
| 260 } |
| 255 }; | 261 }; |
| 256 | 262 |
| 257 // Sample usage: | 263 // Sample usage with RefCountedThreadSafe: |
| 258 // class Foo | 264 // class Foo |
| 259 // : public base::RefCountedThreadSafe< | 265 // : public base::RefCountedThreadSafe< |
| 260 // Foo, BrowserThread::DeleteOnIOThread> { | 266 // Foo, BrowserThread::DeleteOnIOThread> { |
| 261 // | 267 // |
| 262 // ... | 268 // ... |
| 263 // private: | 269 // private: |
| 264 // friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; | 270 // friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; |
| 265 // friend class base::DeleteHelper<Foo>; | 271 // friend class base::DeleteHelper<Foo>; |
| 266 // | 272 // |
| 267 // ~Foo(); | 273 // ~Foo(); |
| 274 // |
| 275 // Sample usage with scoped_ptr: |
| 276 // scoped_ptr<Foo, BrowserThread::DeleteOnIOThread> ptr; |
| 268 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; | 277 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; |
| 269 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; | 278 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; |
| 270 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; | 279 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; |
| 271 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; | 280 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; |
| 272 | 281 |
| 273 // Returns an appropriate error message for when DCHECK_CURRENTLY_ON() fails. | 282 // Returns an appropriate error message for when DCHECK_CURRENTLY_ON() fails. |
| 274 static std::string GetDCheckCurrentlyOnErrorMessage(ID expected); | 283 static std::string GetDCheckCurrentlyOnErrorMessage(ID expected); |
| 275 | 284 |
| 276 private: | 285 private: |
| 277 friend class BrowserThreadImpl; | 286 friend class BrowserThreadImpl; |
| 278 | 287 |
| 279 BrowserThread() {} | 288 BrowserThread() {} |
| 280 DISALLOW_COPY_AND_ASSIGN(BrowserThread); | 289 DISALLOW_COPY_AND_ASSIGN(BrowserThread); |
| 281 }; | 290 }; |
| 282 | 291 |
| 283 } // namespace content | 292 } // namespace content |
| 284 | 293 |
| 285 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ | 294 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ |
| OLD | NEW |