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

Side by Side Diff: base/lazy_instance.h

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

Powered by Google App Engine
This is Rietveld 408576698