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: |
11 // a) An existing singleton that can manage your object's lifetime | 11 // a) An existing singleton that can manage your object's lifetime |
12 // b) Locations where you can deterministically create the object and pass | 12 // b) Locations where you can deterministically create the object and pass |
13 // into other objects | 13 // into other objects |
14 // | 14 // |
15 // If you absolutely need a singleton, please keep them as trivial as possible | 15 // If you absolutely need a singleton, please keep them as trivial as possible |
16 // and ideally a leaf dependency. Singletons get problematic when they attempt | 16 // and ideally a leaf dependency. Singletons get problematic when they attempt |
17 // to do too much in their destructor or have circular dependencies. | 17 // to do too much in their destructor or have circular dependencies. |
18 | 18 |
19 #ifndef BASE_MEMORY_SINGLETON_H_ | 19 #ifndef BASE_MEMORY_SINGLETON_H_ |
20 #define BASE_MEMORY_SINGLETON_H_ | 20 #define BASE_MEMORY_SINGLETON_H_ |
21 #pragma once | 21 #pragma once |
22 | 22 |
23 #include "base/at_exit.h" | 23 #include "base/at_exit.h" |
24 #include "base/atomicops.h" | 24 #include "base/atomicops.h" |
25 #include "base/base_export.h" | 25 #include "base/base_export.h" |
26 #include "base/memory/aligned_memory.h" | |
26 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 27 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
27 #include "base/threading/thread_restrictions.h" | 28 #include "base/threading/thread_restrictions.h" |
28 | 29 |
29 namespace base { | 30 namespace base { |
30 namespace internal { | 31 namespace internal { |
31 | 32 |
32 // Our AtomicWord doubles as a spinlock, where a value of | 33 // Our AtomicWord doubles as a spinlock, where a value of |
33 // kBeingCreatedMarker means the spinlock is being held for creation. | 34 // kBeingCreatedMarker means the spinlock is being held for creation. |
34 static const subtle::AtomicWord kBeingCreatedMarker = 1; | 35 static const subtle::AtomicWord kBeingCreatedMarker = 1; |
35 | 36 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 // a callback the system will invoke when logging levels change. Note that | 100 // a callback the system will invoke when logging levels change. Note that |
100 // this is also used in e.g. Chrome Frame, where you have to allow for the | 101 // this is also used in e.g. Chrome Frame, where you have to allow for the |
101 // possibility of loading briefly into someone else's process space, and | 102 // possibility of loading briefly into someone else's process space, and |
102 // so leaking is not an option, as that would sabotage the state of your host | 103 // so leaking is not an option, as that would sabotage the state of your host |
103 // process once you've unloaded. | 104 // process once you've unloaded. |
104 template <typename Type> | 105 template <typename Type> |
105 struct StaticMemorySingletonTraits { | 106 struct StaticMemorySingletonTraits { |
106 // WARNING: User has to deal with get() in the singleton class | 107 // WARNING: User has to deal with get() in the singleton class |
107 // this is traits for returning NULL. | 108 // this is traits for returning NULL. |
108 static Type* New() { | 109 static Type* New() { |
110 // Only constructs once and returns pointer; otherwise returns NULL. | |
109 if (base::subtle::NoBarrier_AtomicExchange(&dead_, 1)) | 111 if (base::subtle::NoBarrier_AtomicExchange(&dead_, 1)) |
Sigurður Ásgeirsson
2012/02/22 15:31:32
I couldn't find a discussion of this anywhere in o
Jeffrey Yasskin (google)
2012/02/22 18:38:42
Both writes and reads can be reordered in either d
| |
110 return NULL; | 112 return NULL; |
111 Type* ptr = reinterpret_cast<Type*>(buffer_); | |
112 | 113 |
113 // We are protected by a memory barrier. | 114 return new(buffer_.data()) Type; |
Jeffrey Yasskin (google)
2012/02/22 18:38:42
Since you removed the ()s after "Type", this won't
jbates
2012/02/22 19:37:15
Done.
| |
114 new(ptr) Type(); | |
115 return ptr; | |
116 } | 115 } |
117 | 116 |
118 static void Delete(Type* p) { | 117 static void Delete(Type* p) { |
119 base::subtle::NoBarrier_Store(&dead_, 1); | |
jbates
2012/02/21 23:39:04
dead_ is already set to 1 at the beginning of New(
| |
120 base::subtle::MemoryBarrier(); | |
121 if (p != NULL) | 118 if (p != NULL) |
122 p->Type::~Type(); | 119 p->Type::~Type(); |
123 } | 120 } |
124 | 121 |
125 static const bool kRegisterAtExit = true; | 122 static const bool kRegisterAtExit = true; |
126 static const bool kAllowedToAccessOnNonjoinableThread = true; | 123 static const bool kAllowedToAccessOnNonjoinableThread = true; |
127 | 124 |
128 // Exposed for unittesting. | 125 // Exposed for unittesting. |
129 static void Resurrect() { | 126 static void Resurrect() { |
130 base::subtle::NoBarrier_Store(&dead_, 0); | 127 base::subtle::NoBarrier_Store(&dead_, 0); |
131 } | 128 } |
132 | 129 |
133 private: | 130 private: |
134 static const size_t kBufferSize = (sizeof(Type) + | 131 static base::AlignedMemory<Type> buffer_; |
135 sizeof(intptr_t) - 1) / sizeof(intptr_t); | |
136 static intptr_t buffer_[kBufferSize]; | |
137 | |
138 // Signal the object was already deleted, so it is not revived. | 132 // Signal the object was already deleted, so it is not revived. |
139 static base::subtle::Atomic32 dead_; | 133 static base::subtle::Atomic32 dead_; |
140 }; | 134 }; |
141 | 135 |
142 template <typename Type> intptr_t | 136 template <typename Type> base::AlignedMemory<Type> |
143 StaticMemorySingletonTraits<Type>::buffer_[kBufferSize]; | 137 StaticMemorySingletonTraits<Type>::buffer_; |
144 template <typename Type> base::subtle::Atomic32 | 138 template <typename Type> base::subtle::Atomic32 |
145 StaticMemorySingletonTraits<Type>::dead_ = 0; | 139 StaticMemorySingletonTraits<Type>::dead_ = 0; |
146 | 140 |
147 // The Singleton<Type, Traits, DifferentiatingType> class manages a single | 141 // The Singleton<Type, Traits, DifferentiatingType> class manages a single |
148 // instance of Type which will be created on first use and will be destroyed at | 142 // instance of Type which will be created on first use and will be destroyed at |
149 // normal process exit). The Trait::Delete function will not be called on | 143 // normal process exit). The Trait::Delete function will not be called on |
150 // abnormal process exit. | 144 // abnormal process exit. |
151 // | 145 // |
152 // DifferentiatingType is used as a key to differentiate two different | 146 // DifferentiatingType is used as a key to differentiate two different |
153 // singletons having the same memory allocation functions but serving a | 147 // singletons having the same memory allocation functions but serving a |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
282 instance_ = 0; | 276 instance_ = 0; |
283 } | 277 } |
284 static base::subtle::AtomicWord instance_; | 278 static base::subtle::AtomicWord instance_; |
285 }; | 279 }; |
286 | 280 |
287 template <typename Type, typename Traits, typename DifferentiatingType> | 281 template <typename Type, typename Traits, typename DifferentiatingType> |
288 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: | 282 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: |
289 instance_ = 0; | 283 instance_ = 0; |
290 | 284 |
291 #endif // BASE_MEMORY_SINGLETON_H_ | 285 #endif // BASE_MEMORY_SINGLETON_H_ |
OLD | NEW |