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

Side by Side Diff: base/lazy_instance.h

Issue 8465010: Minor perf and size optimization - don't do asserts in release build (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month 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 | « no previous file | base/memory/singleton.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // The LazyInstance<Type, Traits> class manages a single instance of Type, 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 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 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 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() 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. 10 // and Pointer() will always return the same, completely initialized instance.
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 // Declaring a destructor (even if it's empty) will cause MSVC to register a 126 // Declaring a destructor (even if it's empty) will cause MSVC to register a
127 // static initializer to register the empty destructor with atexit(). 127 // static initializer to register the empty destructor with atexit().
128 // Refer to the destructor-related comment in LazyInstanceHelper. 128 // Refer to the destructor-related comment in LazyInstanceHelper.
129 // ~LazyInstance() {} 129 // ~LazyInstance() {}
130 130
131 Type& Get() { 131 Type& Get() {
132 return *Pointer(); 132 return *Pointer();
133 } 133 }
134 134
135 Type* Pointer() { 135 Type* Pointer() {
136 #ifndef NDEBUG
137 // Avoid making TLS lookup on release builds.
136 if (!Traits::kAllowedToAccessOnNonjoinableThread) 138 if (!Traits::kAllowedToAccessOnNonjoinableThread)
137 base::ThreadRestrictions::AssertSingletonAllowed(); 139 base::ThreadRestrictions::AssertSingletonAllowed();
140 #endif
138 141
139 // We will hopefully have fast access when the instance is already created. 142 // We will hopefully have fast access when the instance is already created.
140 // Since a thread sees state_ != STATE_CREATED at most once, 143 // Since a thread sees state_ != STATE_CREATED at most once,
141 // the load is taken out of NeedsInstance() as a fast-path. 144 // the load is taken out of NeedsInstance() as a fast-path.
142 // The load has acquire memory ordering as a thread which sees 145 // The load has acquire memory ordering as a thread which sees
143 // state_ == STATE_CREATED needs to acquire visibility over 146 // state_ == STATE_CREATED needs to acquire visibility over
144 // the associated data (buf_). Pairing Release_Store is in 147 // the associated data (buf_). Pairing Release_Store is in
145 // CompleteInstance(). 148 // CompleteInstance().
146 if ((base::subtle::Acquire_Load(&state_) != STATE_CREATED) && 149 if ((base::subtle::Acquire_Load(&state_) != STATE_CREATED) &&
147 NeedsInstance()) { 150 NeedsInstance()) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 189
187 Type *instance_; 190 Type *instance_;
188 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. 191 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance.
189 192
190 DISALLOW_COPY_AND_ASSIGN(LazyInstance); 193 DISALLOW_COPY_AND_ASSIGN(LazyInstance);
191 }; 194 };
192 195
193 } // namespace base 196 } // namespace base
194 197
195 #endif // BASE_LAZY_INSTANCE_H_ 198 #endif // BASE_LAZY_INSTANCE_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/singleton.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698