15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PLEASE READ: Do you really need a singleton? 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Singletons make it hard to determine the lifetime of an object, which can 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// lead to buggy code and spurious crashes. 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Instead of adding another singleton into the mix, try to identify either: 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a) An existing singleton that can manage your object's lifetime 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// b) Locations where you can deterministically create the object and pass 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// into other objects 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If you absolutely need a singleton, please keep them as trivial as possible 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and ideally a leaf dependency. Singletons get problematic when they attempt 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to do too much in their destructor or have circular dependencies. 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_MEMORY_SINGLETON_H_ 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_MEMORY_SINGLETON_H_ 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/at_exit.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/atomicops.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/aligned_memory.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/third_party/dynamic_annotations/dynamic_annotations.h" 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread_restrictions.h" 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace internal { 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Our AtomicWord doubles as a spinlock, where a value of 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// kBeingCreatedMarker means the spinlock is being held for creation. 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const subtle::AtomicWord kBeingCreatedMarker = 1; 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We pull out some of the functionality into a non-templated function, so that 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// we can implement the more complicated pieces out of line in the .cc file. 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance); 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace internal 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace base 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(joth): Move more of this file into namespace base 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Default traits for Singleton<Type>. Calls operator new and operator delete on 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the object. Registers automatic deletion at process exit. 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Overload if you need arguments or another memory allocation function. 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<typename Type> 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct DefaultSingletonTraits { 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Allocates the object. 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static Type* New() { 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The parenthesis is very important here; it forces POD type 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // initialization. 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return new Type(); 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Destroys the object. 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void Delete(Type* x) { 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delete x; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Set to true to automatically register deletion of the object on process 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // exit. See below for the required call that makes this happen. 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static const bool kRegisterAtExit = true; 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef NDEBUG 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Set to false to disallow access on a non-joinable thread. This is 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // different from kRegisterAtExit because StaticMemorySingletonTraits allows 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // access on non-joinable threads, and gracefully handles this. 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static const bool kAllowedToAccessOnNonjoinableThread = false; 71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Alternate traits for use with the Singleton<Type>. Identical to 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DefaultSingletonTraits except that the Singleton will not be cleaned up 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// at exit. 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<typename Type> 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct LeakySingletonTraits : public DefaultSingletonTraits<Type> { 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static const bool kRegisterAtExit = false; 81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef NDEBUG 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static const bool kAllowedToAccessOnNonjoinableThread = true; 83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Alternate traits for use with the Singleton<Type>. Allocates memory 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for the singleton instance from a static buffer. The singleton will 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be cleaned up at exit, but can't be revived after destruction unless 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the Resurrect() method is called. 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is useful for a certain category of things, notably logging and 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// tracing, where the singleton instance is of a type carefully constructed to 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be safe to access post-destruction. 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In logging and tracing you'll typically get stray calls at odd times, like 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// during static destruction, thread teardown and the like, and there's a 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// termination race on the heap-based singleton - e.g. if one thread calls 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// get(), but then another thread initiates AtExit processing, the first thread 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// may call into an object residing in unallocated memory. If the instance is 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// allocated from the data segment, then this is survivable. 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The destructor is to deallocate system resources, in this case to unregister 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a callback the system will invoke when logging levels change. Note that 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this is also used in e.g. Chrome Frame, where you have to allow for the 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// possibility of loading briefly into someone else's process space, and 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// so leaking is not an option, as that would sabotage the state of your host 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// process once you've unloaded. 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename Type> 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct StaticMemorySingletonTraits { 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // WARNING: User has to deal with get() in the singleton class 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // this is traits for returning NULL. 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static Type* New() { 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Only constructs once and returns pointer; otherwise returns NULL. 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (base::subtle::NoBarrier_AtomicExchange(&dead_, 1)) 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return NULL; 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return new(buffer_.void_data()) Type(); 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void Delete(Type* p) { 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (p != NULL) 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) p->Type::~Type(); 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static const bool kRegisterAtExit = true; 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static const bool kAllowedToAccessOnNonjoinableThread = true; 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Exposed for unittesting. 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void Resurrect() { 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::subtle::NoBarrier_Store(&dead_, 0); 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> buffer_; 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Signal the object was already deleted, so it is not revived. 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static base::subtle::Atomic32 dead_; 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename Type> base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) StaticMemorySingletonTraits<Type>::buffer_; 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename Type> base::subtle::Atomic32 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) StaticMemorySingletonTraits<Type>::dead_ = 0; 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The Singleton<Type, Traits, DifferentiatingType> class manages a single 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// instance of Type which will be created on first use and will be destroyed at 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// normal process exit). The Trait::Delete function will not be called on 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// abnormal process exit. 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DifferentiatingType is used as a key to differentiate two different 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// singletons having the same memory allocation functions but serving a 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// different purpose. This is mainly used for Locks serving different purposes. 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example usage: 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In your header: 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// template <typename T> struct DefaultSingletonTraits; 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// class FooClass { 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// public: 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static FooClass* GetInstance(); <-- See comment below on this. 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// void Bar() { ... } 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// private: 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FooClass() { ... } 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// friend struct DefaultSingletonTraits<FooClass>; 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DISALLOW_COPY_AND_ASSIGN(FooClass); 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// }; 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In your source file: 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// #include "base/memory/singleton.h" 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FooClass* FooClass::GetInstance() { 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// return Singleton<FooClass>::get(); 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// } 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// And to call methods on FooClass: 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FooClass::GetInstance()->Bar(); 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NOTE: The method accessing Singleton<T>::get() has to be named as GetInstance 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and it is important that FooClass::GetInstance() is not inlined in the 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// header. This makes sure that when source files from multiple targets include 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this header they don't end up with different copies of the inlined code 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// creating multiple copies of the singleton. 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Singleton<> has no non-static members and doesn't need to actually be 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// instantiated. 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class is itself thread-safe. The underlying Type must of course be 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// thread-safe if you want to use it concurrently. Two parameters may be tuned 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// depending on the user's requirements. 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Glossary: 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// RAE = kRegisterAtExit 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// On every platform, if Traits::RAE is true, the singleton will be destroyed at 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// process exit. More precisely it uses base::AtExitManager which requires an 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// object of this type to be instantiated. AtExitManager mimics the semantics 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of atexit() such as LIFO order but under Windows is safer to call. For more 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// information see at_exit.h. 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If Traits::RAE is false, the singleton will not be freed at process exit, 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// thus the singleton will be leaked if it is ever accessed. Traits::RAE 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// shouldn't be false unless absolutely necessary. Remember that the heap where 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the object is allocated may be destroyed by the CRT anyway. 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Caveats: 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (a) Every call to get(), operator->() and operator*() incurs some overhead 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (16ns on my P4/2.8GHz) to check whether the object has already been 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// initialized. You may wish to cache the result of get(); it will not 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// change. 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (b) Your factory function must never throw an exception. This class is not 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// exception-safe. 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename Type, 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typename Traits = DefaultSingletonTraits<Type>, 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typename DifferentiatingType = Type> 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Singleton { 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Classes using the Singleton<T> pattern should declare a GetInstance() 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // method and call Singleton::get() from within that. 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) friend Type* Type::GetInstance(); 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Allow TraceLog tests to test tracing after OnExit. 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) friend class DeleteTraceLogForTesting; 2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This class is safe to be constructed and copy-constructed since it has no 2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // member. 2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Return a pointer to the one true instance of the class. 2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static Type* get() { 2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NDEBUG 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Avoid making TLS lookup on release builds. 2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!Traits::kAllowedToAccessOnNonjoinableThread) 2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::ThreadRestrictions::AssertSingletonAllowed(); 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // The load has acquire memory ordering as the thread which reads the 237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // instance_ pointer must acquire visibility over the singleton data. 238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::subtle::AtomicWord value = base::subtle::Acquire_Load(&instance_); 2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (value != 0 && value != base::internal::kBeingCreatedMarker) { 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // See the corresponding HAPPENS_BEFORE below. 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ANNOTATE_HAPPENS_AFTER(&instance_); 2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return reinterpret_cast<Type*>(value); 2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Object isn't created yet, maybe we will get to create it, let's try... 2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (base::subtle::Acquire_CompareAndSwap( 2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) &instance_, 0, base::internal::kBeingCreatedMarker) == 0) { 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // instance_ was NULL and is now kBeingCreatedMarker. Only one thread 2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // will ever get here. Threads might be spinning on us, and they will 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // stop right after we do this store. 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Type* newval = Traits::New(); 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This annotation helps race detectors recognize correct lock-less 2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // synchronization between different threads calling get(). 2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // See the corresponding HAPPENS_AFTER below and above. 2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ANNOTATE_HAPPENS_BEFORE(&instance_); 257a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Releases the visibility over instance_ to the readers. 2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::subtle::Release_Store( 2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) &instance_, reinterpret_cast<base::subtle::AtomicWord>(newval)); 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (newval != NULL && Traits::kRegisterAtExit) 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::AtExitManager::RegisterCallback(OnExit, NULL); 2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return newval; 2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We hit a race. Wait for the other thread to complete it. 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) value = base::internal::WaitForInstance(&instance_); 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // See the corresponding HAPPENS_BEFORE above. 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ANNOTATE_HAPPENS_AFTER(&instance_); 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return reinterpret_cast<Type*>(value); 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Adapter function for use with AtExit(). This should be called single 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // threaded, so don't use atomic operations. 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Calling OnExit while singleton is in use by other threads is a mistake. 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void OnExit(void* /*unused*/) { 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // AtExit should only ever be register after the singleton instance was 2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // created. We should only ever get here with a valid instance_ pointer. 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Traits::Delete( 2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) reinterpret_cast<Type*>(base::subtle::NoBarrier_Load(&instance_))); 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) instance_ = 0; 2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static base::subtle::AtomicWord instance_; 2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename Type, typename Traits, typename DifferentiatingType> 2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: 2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) instance_ = 0; 2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // BASE_MEMORY_SINGLETON_H_ 293