1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file. 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 5ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#ifndef BASE_MEMORY_SINGLETON_H_ 6ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#define BASE_MEMORY_SINGLETON_H_ 73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/at_exit.h" 10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/atomicops.h" 11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/third_party/dynamic_annotations/dynamic_annotations.h" 123f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#include "base/threading/platform_thread.h" 133f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#include "base/threading/thread_restrictions.h" 14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Default traits for Singleton<Type>. Calls operator new and operator delete on 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the object. Registers automatic deletion at process exit. 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Overload if you need arguments or another memory allocation function. 18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttemplate<typename Type> 19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct DefaultSingletonTraits { 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Allocates the object. 21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static Type* New() { 22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // The parenthesis is very important here; it forces POD type 23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // initialization. 24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return new Type(); 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Destroys the object. 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static void Delete(Type* x) { 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott delete x; 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Set to true to automatically register deletion of the object on process 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // exit. See below for the required call that makes this happen. 34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static const bool kRegisterAtExit = true; 35201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch 36201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch // Set to false to disallow access on a non-joinable thread. This is 37201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch // different from kRegisterAtExit because StaticMemorySingletonTraits allows 38201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch // access on non-joinable threads, and gracefully handles this. 39201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch static const bool kAllowedToAccessOnNonjoinableThread = false; 40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Alternate traits for use with the Singleton<Type>. Identical to 44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// DefaultSingletonTraits except that the Singleton will not be cleaned up 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// at exit. 46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttemplate<typename Type> 47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct LeakySingletonTraits : public DefaultSingletonTraits<Type> { 48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static const bool kRegisterAtExit = false; 49201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch static const bool kAllowedToAccessOnNonjoinableThread = true; 50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Alternate traits for use with the Singleton<Type>. Allocates memory 54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// for the singleton instance from a static buffer. The singleton will 55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// be cleaned up at exit, but can't be revived after destruction unless 56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// the Resurrect() method is called. 57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// This is useful for a certain category of things, notably logging and 59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// tracing, where the singleton instance is of a type carefully constructed to 60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// be safe to access post-destruction. 61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// In logging and tracing you'll typically get stray calls at odd times, like 62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// during static destruction, thread teardown and the like, and there's a 63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// termination race on the heap-based singleton - e.g. if one thread calls 64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// get(), but then another thread initiates AtExit processing, the first thread 65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// may call into an object residing in unallocated memory. If the instance is 66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// allocated from the data segment, then this is survivable. 67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// The destructor is to deallocate system resources, in this case to unregister 69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// a callback the system will invoke when logging levels change. Note that 70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// this is also used in e.g. Chrome Frame, where you have to allow for the 71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// possibility of loading briefly into someone else's process space, and 72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// so leaking is not an option, as that would sabotage the state of your host 73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// process once you've unloaded. 74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochtemplate <typename Type> 75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochstruct StaticMemorySingletonTraits { 76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // WARNING: User has to deal with get() in the singleton class 77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // this is traits for returning NULL. 78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static Type* New() { 79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (base::subtle::NoBarrier_AtomicExchange(&dead_, 1)) 80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return NULL; 81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch Type* ptr = reinterpret_cast<Type*>(buffer_); 82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // We are protected by a memory barrier. 84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch new(ptr) Type(); 85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return ptr; 86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static void Delete(Type* p) { 89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch base::subtle::NoBarrier_Store(&dead_, 1); 90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch base::subtle::MemoryBarrier(); 91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (p != NULL) 92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch p->Type::~Type(); 93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static const bool kRegisterAtExit = true; 96201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch static const bool kAllowedToAccessOnNonjoinableThread = true; 97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 98c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Exposed for unittesting. 99c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static void Resurrect() { 100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch base::subtle::NoBarrier_Store(&dead_, 0); 101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private: 104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static const size_t kBufferSize = (sizeof(Type) + 105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch sizeof(intptr_t) - 1) / sizeof(intptr_t); 106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static intptr_t buffer_[kBufferSize]; 107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Signal the object was already deleted, so it is not revived. 109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static base::subtle::Atomic32 dead_; 110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 111c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochtemplate <typename Type> intptr_t 113c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch StaticMemorySingletonTraits<Type>::buffer_[kBufferSize]; 114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochtemplate <typename Type> base::subtle::Atomic32 115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch StaticMemorySingletonTraits<Type>::dead_ = 0; 116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The Singleton<Type, Traits, DifferentiatingType> class manages a single 118c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// instance of Type which will be created on first use and will be destroyed at 119c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// normal process exit). The Trait::Delete function will not be called on 120c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// abnormal process exit. 121c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 122c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// DifferentiatingType is used as a key to differentiate two different 123c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// singletons having the same memory allocation functions but serving a 124c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// different purpose. This is mainly used for Locks serving different purposes. 125c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 12621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// Example usage: 12721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// 12821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// In your header: 129ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// #include "base/memory/singleton.h" 13021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// class FooClass { 13121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// public: 13221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// static FooClass* GetInstance(); <-- See comment below on this. 13321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// void Bar() { ... } 13421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// private: 13521d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// FooClass() { ... } 13621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// friend struct DefaultSingletonTraits<FooClass>; 13721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// 13821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// DISALLOW_COPY_AND_ASSIGN(FooClass); 13921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// }; 14021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// 14121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// In your source file: 14221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// FooClass* FooClass::GetInstance() { 14321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// return Singleton<FooClass>::get(); 14421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// } 14521d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// 14621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// And to call methods on FooClass: 14721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// FooClass::GetInstance()->Bar(); 14821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// 14921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// NOTE: The method accessing Singleton<T>::get() has to be named as GetInstance 15021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// and it is important that FooClass::GetInstance() is not inlined in the 15121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// header. This makes sure that when source files from multiple targets include 15221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// this header they don't end up with different copies of the inlined code 15321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// creating multiple copies of the singleton. 154c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 155c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Singleton<> has no non-static members and doesn't need to actually be 15621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// instantiated. 157c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 158c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This class is itself thread-safe. The underlying Type must of course be 159c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// thread-safe if you want to use it concurrently. Two parameters may be tuned 160c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// depending on the user's requirements. 161c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 162c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Glossary: 163c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// RAE = kRegisterAtExit 164c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 165c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// On every platform, if Traits::RAE is true, the singleton will be destroyed at 166c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// process exit. More precisely it uses base::AtExitManager which requires an 167c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// object of this type to be instantiated. AtExitManager mimics the semantics 168c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// of atexit() such as LIFO order but under Windows is safer to call. For more 169c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// information see at_exit.h. 170c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 171c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If Traits::RAE is false, the singleton will not be freed at process exit, 172c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// thus the singleton will be leaked if it is ever accessed. Traits::RAE 173c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// shouldn't be false unless absolutely necessary. Remember that the heap where 174c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the object is allocated may be destroyed by the CRT anyway. 175c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 176c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Caveats: 177c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// (a) Every call to get(), operator->() and operator*() incurs some overhead 178c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// (16ns on my P4/2.8GHz) to check whether the object has already been 179c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// initialized. You may wish to cache the result of get(); it will not 180c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// change. 181c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 182c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// (b) Your factory function must never throw an exception. This class is not 183c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// exception-safe. 184c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 185c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttemplate <typename Type, 186c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott typename Traits = DefaultSingletonTraits<Type>, 187c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott typename DifferentiatingType = Type> 188c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass Singleton { 18921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen private: 19021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // Classes using the Singleton<T> pattern should declare a GetInstance() 19121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // method and call Singleton::get() from within that. 19221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen friend Type* Type::GetInstance(); 19321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen 194c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // This class is safe to be constructed and copy-constructed since it has no 195c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // member. 196c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 197c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Return a pointer to the one true instance of the class. 198c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static Type* get() { 199201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch if (!Traits::kAllowedToAccessOnNonjoinableThread) 200201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch base::ThreadRestrictions::AssertSingletonAllowed(); 201201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch 202c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Our AtomicWord doubles as a spinlock, where a value of 203c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // kBeingCreatedMarker means the spinlock is being held for creation. 204c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static const base::subtle::AtomicWord kBeingCreatedMarker = 1; 205c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 206c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::subtle::AtomicWord value = base::subtle::NoBarrier_Load(&instance_); 207c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (value != 0 && value != kBeingCreatedMarker) { 208c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // See the corresponding HAPPENS_BEFORE below. 209c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ANNOTATE_HAPPENS_AFTER(&instance_); 210c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return reinterpret_cast<Type*>(value); 211c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 212c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 213c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Object isn't created yet, maybe we will get to create it, let's try... 214c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (base::subtle::Acquire_CompareAndSwap(&instance_, 215c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 0, 216c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott kBeingCreatedMarker) == 0) { 217c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // instance_ was NULL and is now kBeingCreatedMarker. Only one thread 218c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // will ever get here. Threads might be spinning on us, and they will 219c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // stop right after we do this store. 220c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott Type* newval = Traits::New(); 221c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 222c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // This annotation helps race detectors recognize correct lock-less 223c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // synchronization between different threads calling get(). 224c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // See the corresponding HAPPENS_AFTER below and above. 225c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ANNOTATE_HAPPENS_BEFORE(&instance_); 226c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::subtle::Release_Store( 227c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott &instance_, reinterpret_cast<base::subtle::AtomicWord>(newval)); 228c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 229c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (newval != NULL && Traits::kRegisterAtExit) 230c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::AtExitManager::RegisterCallback(OnExit, NULL); 231c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 232c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return newval; 233c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 234c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 235c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // We hit a race. Another thread beat us and either: 236c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // - Has the object in BeingCreated state 237c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // - Already has the object created... 238c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // We know value != NULL. It could be kBeingCreatedMarker, or a valid ptr. 239c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Unless your constructor can be very time consuming, it is very unlikely 240c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // to hit this race. When it does, we just spin and yield the thread until 241c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // the object has been created. 242c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott while (true) { 243c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott value = base::subtle::NoBarrier_Load(&instance_); 244c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (value != kBeingCreatedMarker) 245c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott break; 2463f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen base::PlatformThread::YieldCurrentThread(); 247c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 248c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 249c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // See the corresponding HAPPENS_BEFORE above. 250c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ANNOTATE_HAPPENS_AFTER(&instance_); 251c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return reinterpret_cast<Type*>(value); 252c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 253c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 254c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Adapter function for use with AtExit(). This should be called single 2554a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch // threaded, so don't use atomic operations. 2564a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch // Calling OnExit while singleton is in use by other threads is a mistake. 257dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen static void OnExit(void* /*unused*/) { 258c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // AtExit should only ever be register after the singleton instance was 259c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // created. We should only ever get here with a valid instance_ pointer. 2604a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch Traits::Delete( 2614a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch reinterpret_cast<Type*>(base::subtle::NoBarrier_Load(&instance_))); 2624a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch instance_ = 0; 263c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 264c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static base::subtle::AtomicWord instance_; 265c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 266c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 267c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttemplate <typename Type, typename Traits, typename DifferentiatingType> 268c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbase::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: 269c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott instance_ = 0; 270c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 271ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#endif // BASE_MEMORY_SINGLETON_H_ 272