Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(874)

Unified Diff: base/memory/singleton.h

Issue 1308823002: Move Singleton and related structs to namespace base (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ToT Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/logging_win.cc ('k') | base/memory/singleton_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/singleton.h
diff --git a/base/memory/singleton.h b/base/memory/singleton.h
index e50bdc05f34eeecefc293529e7eeab0e9da0bbde..731969900222c116d7f7733299e636d34e759acb 100644
--- a/base/memory/singleton.h
+++ b/base/memory/singleton.h
@@ -36,10 +36,10 @@ static const subtle::AtomicWord kBeingCreatedMarker = 1;
// we can implement the more complicated pieces out of line in the .cc file.
BASE_EXPORT subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance);
+class DeleteTraceLogForTesting;
+
} // namespace internal
-} // namespace base
-// TODO(joth): Move more of this file into namespace base
// Default traits for Singleton<Type>. Calls operator new and operator delete on
// the object. Registers automatic deletion at process exit.
@@ -110,7 +110,7 @@ struct StaticMemorySingletonTraits {
// this is traits for returning NULL.
static Type* New() {
// Only constructs once and returns pointer; otherwise returns NULL.
- if (base::subtle::NoBarrier_AtomicExchange(&dead_, 1))
+ if (subtle::NoBarrier_AtomicExchange(&dead_, 1))
return NULL;
return new(buffer_.void_data()) Type();
@@ -125,20 +125,19 @@ struct StaticMemorySingletonTraits {
static const bool kAllowedToAccessOnNonjoinableThread = true;
// Exposed for unittesting.
- static void Resurrect() {
- base::subtle::NoBarrier_Store(&dead_, 0);
- }
+ static void Resurrect() { subtle::NoBarrier_Store(&dead_, 0); }
private:
- static base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> buffer_;
+ static AlignedMemory<sizeof(Type), ALIGNOF(Type)> buffer_;
// Signal the object was already deleted, so it is not revived.
- static base::subtle::Atomic32 dead_;
+ static subtle::Atomic32 dead_;
};
-template <typename Type> base::AlignedMemory<sizeof(Type), ALIGNOF(Type)>
+template <typename Type>
+AlignedMemory<sizeof(Type), ALIGNOF(Type)>
StaticMemorySingletonTraits<Type>::buffer_;
-template <typename Type> base::subtle::Atomic32
- StaticMemorySingletonTraits<Type>::dead_ = 0;
+template <typename Type>
+subtle::Atomic32 StaticMemorySingletonTraits<Type>::dead_ = 0;
// The Singleton<Type, Traits, DifferentiatingType> class manages a single
// instance of Type which will be created on first use and will be destroyed at
@@ -190,7 +189,7 @@ template <typename Type> base::subtle::Atomic32
// RAE = kRegisterAtExit
//
// On every platform, if Traits::RAE is true, the singleton will be destroyed at
-// process exit. More precisely it uses base::AtExitManager which requires an
+// process exit. More precisely it uses AtExitManager which requires an
// object of this type to be instantiated. AtExitManager mimics the semantics
// of atexit() such as LIFO order but under Windows is safer to call. For more
// information see at_exit.h.
@@ -209,6 +208,7 @@ template <typename Type> base::subtle::Atomic32
// (b) Your factory function must never throw an exception. This class is not
// exception-safe.
//
+
template <typename Type,
typename Traits = DefaultSingletonTraits<Type>,
typename DifferentiatingType = Type>
@@ -219,7 +219,7 @@ class Singleton {
friend Type* Type::GetInstance();
// Allow TraceLog tests to test tracing after OnExit.
- friend class DeleteTraceLogForTesting;
+ friend class internal::DeleteTraceLogForTesting;
// This class is safe to be constructed and copy-constructed since it has no
// member.
@@ -229,36 +229,36 @@ class Singleton {
#ifndef NDEBUG
// Avoid making TLS lookup on release builds.
if (!Traits::kAllowedToAccessOnNonjoinableThread)
- base::ThreadRestrictions::AssertSingletonAllowed();
+ ThreadRestrictions::AssertSingletonAllowed();
#endif
// The load has acquire memory ordering as the thread which reads the
// instance_ pointer must acquire visibility over the singleton data.
- base::subtle::AtomicWord value = base::subtle::Acquire_Load(&instance_);
- if (value != 0 && value != base::internal::kBeingCreatedMarker) {
+ subtle::AtomicWord value = subtle::Acquire_Load(&instance_);
+ if (value != 0 && value != internal::kBeingCreatedMarker) {
return reinterpret_cast<Type*>(value);
}
// Object isn't created yet, maybe we will get to create it, let's try...
- if (base::subtle::Acquire_CompareAndSwap(
- &instance_, 0, base::internal::kBeingCreatedMarker) == 0) {
+ if (subtle::Acquire_CompareAndSwap(&instance_, 0,
+ internal::kBeingCreatedMarker) == 0) {
// instance_ was NULL and is now kBeingCreatedMarker. Only one thread
// will ever get here. Threads might be spinning on us, and they will
// stop right after we do this store.
Type* newval = Traits::New();
// Releases the visibility over instance_ to the readers.
- base::subtle::Release_Store(
- &instance_, reinterpret_cast<base::subtle::AtomicWord>(newval));
+ subtle::Release_Store(&instance_,
+ reinterpret_cast<subtle::AtomicWord>(newval));
if (newval != NULL && Traits::kRegisterAtExit)
- base::AtExitManager::RegisterCallback(OnExit, NULL);
+ AtExitManager::RegisterCallback(OnExit, NULL);
return newval;
}
// We hit a race. Wait for the other thread to complete it.
- value = base::internal::WaitForInstance(&instance_);
+ value = internal::WaitForInstance(&instance_);
return reinterpret_cast<Type*>(value);
}
@@ -269,15 +269,15 @@ class Singleton {
static void OnExit(void* /*unused*/) {
// AtExit should only ever be register after the singleton instance was
// created. We should only ever get here with a valid instance_ pointer.
- Traits::Delete(
- reinterpret_cast<Type*>(base::subtle::NoBarrier_Load(&instance_)));
+ Traits::Delete(reinterpret_cast<Type*>(subtle::NoBarrier_Load(&instance_)));
instance_ = 0;
}
- static base::subtle::AtomicWord instance_;
+ static subtle::AtomicWord instance_;
};
template <typename Type, typename Traits, typename DifferentiatingType>
-base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>::
- instance_ = 0;
+subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>::instance_ = 0;
+
+} // namespace base
#endif // BASE_MEMORY_SINGLETON_H_
« no previous file with comments | « base/logging_win.cc ('k') | base/memory/singleton_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698