| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // PLEASE READ: Do you really need a singleton? | 5 // PLEASE READ: Do you really need a singleton? |
| 6 // | 6 // |
| 7 // Singletons make it hard to determine the lifetime of an object, which can | 7 // Singletons make it hard to determine the lifetime of an object, which can |
| 8 // lead to buggy code and spurious crashes. | 8 // lead to buggy code and spurious crashes. |
| 9 // | 9 // |
| 10 // Instead of adding another singleton into the mix, try to identify either: | 10 // Instead of adding another singleton into the mix, try to identify either: |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 56 |
| 57 // Destroys the object. | 57 // Destroys the object. |
| 58 static void Delete(Type* x) { | 58 static void Delete(Type* x) { |
| 59 delete x; | 59 delete x; |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Set to true to automatically register deletion of the object on process | 62 // Set to true to automatically register deletion of the object on process |
| 63 // exit. See below for the required call that makes this happen. | 63 // exit. See below for the required call that makes this happen. |
| 64 static const bool kRegisterAtExit = true; | 64 static const bool kRegisterAtExit = true; |
| 65 | 65 |
| 66 #ifndef NDEBUG |
| 66 // Set to false to disallow access on a non-joinable thread. This is | 67 // Set to false to disallow access on a non-joinable thread. This is |
| 67 // different from kRegisterAtExit because StaticMemorySingletonTraits allows | 68 // different from kRegisterAtExit because StaticMemorySingletonTraits allows |
| 68 // access on non-joinable threads, and gracefully handles this. | 69 // access on non-joinable threads, and gracefully handles this. |
| 69 static const bool kAllowedToAccessOnNonjoinableThread = false; | 70 static const bool kAllowedToAccessOnNonjoinableThread = false; |
| 71 #endif |
| 70 }; | 72 }; |
| 71 | 73 |
| 72 | 74 |
| 73 // Alternate traits for use with the Singleton<Type>. Identical to | 75 // Alternate traits for use with the Singleton<Type>. Identical to |
| 74 // DefaultSingletonTraits except that the Singleton will not be cleaned up | 76 // DefaultSingletonTraits except that the Singleton will not be cleaned up |
| 75 // at exit. | 77 // at exit. |
| 76 template<typename Type> | 78 template<typename Type> |
| 77 struct LeakySingletonTraits : public DefaultSingletonTraits<Type> { | 79 struct LeakySingletonTraits : public DefaultSingletonTraits<Type> { |
| 78 static const bool kRegisterAtExit = false; | 80 static const bool kRegisterAtExit = false; |
| 81 #ifndef NDEBUG |
| 79 static const bool kAllowedToAccessOnNonjoinableThread = true; | 82 static const bool kAllowedToAccessOnNonjoinableThread = true; |
| 83 #endif |
| 80 }; | 84 }; |
| 81 | 85 |
| 82 | 86 |
| 83 // Alternate traits for use with the Singleton<Type>. Allocates memory | 87 // Alternate traits for use with the Singleton<Type>. Allocates memory |
| 84 // for the singleton instance from a static buffer. The singleton will | 88 // for the singleton instance from a static buffer. The singleton will |
| 85 // be cleaned up at exit, but can't be revived after destruction unless | 89 // be cleaned up at exit, but can't be revived after destruction unless |
| 86 // the Resurrect() method is called. | 90 // the Resurrect() method is called. |
| 87 // | 91 // |
| 88 // This is useful for a certain category of things, notably logging and | 92 // This is useful for a certain category of things, notably logging and |
| 89 // tracing, where the singleton instance is of a type carefully constructed to | 93 // tracing, where the singleton instance is of a type carefully constructed to |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 instance_ = 0; | 280 instance_ = 0; |
| 277 } | 281 } |
| 278 static base::subtle::AtomicWord instance_; | 282 static base::subtle::AtomicWord instance_; |
| 279 }; | 283 }; |
| 280 | 284 |
| 281 template <typename Type, typename Traits, typename DifferentiatingType> | 285 template <typename Type, typename Traits, typename DifferentiatingType> |
| 282 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: | 286 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: |
| 283 instance_ = 0; | 287 instance_ = 0; |
| 284 | 288 |
| 285 #endif // BASE_MEMORY_SINGLETON_H_ | 289 #endif // BASE_MEMORY_SINGLETON_H_ |
| OLD | NEW |