ipc/chromium/src/base/lazy_instance.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 // The LazyInstance<Type, Traits> class manages a single instance of Type,
michael@0 6 // which will be lazily created on the first time it's accessed. This class is
michael@0 7 // useful for places you would normally use a function-level static, but you
michael@0 8 // need to have guaranteed thread-safety. The Type constructor will only ever
michael@0 9 // be called once, even if two threads are racing to create the object. Get()
michael@0 10 // and Pointer() will always return the same, completely initialized instance.
michael@0 11 // When the instance is constructed it is registered with AtExitManager. The
michael@0 12 // destructor will be called on program exit.
michael@0 13 //
michael@0 14 // LazyInstance is completely thread safe, assuming that you create it safely.
michael@0 15 // The class was designed to be POD initialized, so it shouldn't require a
michael@0 16 // static constructor. It really only makes sense to declare a LazyInstance as
michael@0 17 // a global variable using the base::LinkerInitialized constructor.
michael@0 18 //
michael@0 19 // LazyInstance is similar to Singleton, except it does not have the singleton
michael@0 20 // property. You can have multiple LazyInstance's of the same type, and each
michael@0 21 // will manage a unique instance. It also preallocates the space for Type, as
michael@0 22 // to avoid allocating the Type instance on the heap. This may help with the
michael@0 23 // performance of creating the instance, and reducing heap fragmentation. This
michael@0 24 // requires that Type be a complete type so we can determine the size.
michael@0 25 //
michael@0 26 // Example usage:
michael@0 27 // static LazyInstance<MyClass> my_instance(base::LINKER_INITIALIZED);
michael@0 28 // void SomeMethod() {
michael@0 29 // my_instance.Get().SomeMethod(); // MyClass::SomeMethod()
michael@0 30 //
michael@0 31 // MyClass* ptr = my_instance.Pointer();
michael@0 32 // ptr->DoDoDo(); // MyClass::DoDoDo
michael@0 33 // }
michael@0 34
michael@0 35 #ifndef BASE_LAZY_INSTANCE_H_
michael@0 36 #define BASE_LAZY_INSTANCE_H_
michael@0 37
michael@0 38 #include "base/atomicops.h"
michael@0 39 #include "base/basictypes.h"
michael@0 40
michael@0 41 namespace base {
michael@0 42
michael@0 43 template <typename Type>
michael@0 44 struct DefaultLazyInstanceTraits {
michael@0 45 static void New(void* instance) {
michael@0 46 // Use placement new to initialize our instance in our preallocated space.
michael@0 47 // The parenthesis is very important here to force POD type initialization.
michael@0 48 new (instance) Type();
michael@0 49 }
michael@0 50 static void Delete(void* instance) {
michael@0 51 // Explicitly call the destructor.
michael@0 52 reinterpret_cast<Type*>(instance)->~Type();
michael@0 53 }
michael@0 54 };
michael@0 55
michael@0 56 // We pull out some of the functionality into a non-templated base, so that we
michael@0 57 // can implement the more complicated pieces out of line in the .cc file.
michael@0 58 class LazyInstanceHelper {
michael@0 59 protected:
michael@0 60 enum {
michael@0 61 STATE_EMPTY = 0,
michael@0 62 STATE_CREATING = 1,
michael@0 63 STATE_CREATED = 2
michael@0 64 };
michael@0 65
michael@0 66 explicit LazyInstanceHelper(LinkerInitialized x) { /* state_ is 0 */ }
michael@0 67 // Declaring a destructor (even if it's empty) will cause MSVC to register a
michael@0 68 // static initializer to register the empty destructor with atexit().
michael@0 69
michael@0 70 // Make sure that instance is created, creating or waiting for it to be
michael@0 71 // created if neccessary. Constructs with |ctor| in the space provided by
michael@0 72 // |instance| and registers dtor for destruction at program exit.
michael@0 73 void EnsureInstance(void* instance, void (*ctor)(void*), void (*dtor)(void*));
michael@0 74
michael@0 75 base::subtle::Atomic32 state_;
michael@0 76
michael@0 77 private:
michael@0 78 DISALLOW_COPY_AND_ASSIGN(LazyInstanceHelper);
michael@0 79 };
michael@0 80
michael@0 81 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> >
michael@0 82 class LazyInstance : public LazyInstanceHelper {
michael@0 83 public:
michael@0 84 explicit LazyInstance(LinkerInitialized x) : LazyInstanceHelper(x) { }
michael@0 85 // Declaring a destructor (even if it's empty) will cause MSVC to register a
michael@0 86 // static initializer to register the empty destructor with atexit().
michael@0 87
michael@0 88 Type& Get() {
michael@0 89 return *Pointer();
michael@0 90 }
michael@0 91
michael@0 92 Type* Pointer() {
michael@0 93 Type* instance = reinterpret_cast<Type*>(&buf_);
michael@0 94
michael@0 95 // We will hopefully have fast access when the instance is already created.
michael@0 96 if (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED)
michael@0 97 EnsureInstance(instance, Traits::New, Traits::Delete);
michael@0 98
michael@0 99 return instance;
michael@0 100 }
michael@0 101
michael@0 102 private:
michael@0 103 int8_t buf_[sizeof(Type)]; // Preallocate the space for the Type instance.
michael@0 104
michael@0 105 DISALLOW_COPY_AND_ASSIGN(LazyInstance);
michael@0 106 };
michael@0 107
michael@0 108 } // namespace base
michael@0 109
michael@0 110 #endif // BASE_LAZY_INSTANCE_H_

mercurial