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

Unified Diff: base/lazy_instance.h

Issue 8393002: Remove static function pointer from LazyInstance (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 9 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/lazy_instance.h
diff --git a/base/lazy_instance.h b/base/lazy_instance.h
index 147133546e6f31dc7c70ef335b4d0c2e5f753c74..496fc1f0404948986de277bfe7b472827dce9cc6 100644
--- a/base/lazy_instance.h
+++ b/base/lazy_instance.h
@@ -49,6 +49,7 @@ namespace base {
template <typename Type>
struct DefaultLazyInstanceTraits {
+ static const bool kRegisterOnExit = true;
static const bool kAllowedToAccessOnNonjoinableThread = false;
static Type* New(void* instance) {
@@ -60,30 +61,24 @@ struct DefaultLazyInstanceTraits {
// The parenthesis is very important here to force POD type initialization.
return new (instance) Type();
}
- static void Delete(void* instance) {
+ static void Delete(Type* instance) {
// Explicitly call the destructor.
- reinterpret_cast<Type*>(instance)->~Type();
+ instance->~Type();
}
};
template <typename Type>
struct LeakyLazyInstanceTraits {
+ static const bool kRegisterOnExit = false;
static const bool kAllowedToAccessOnNonjoinableThread = true;
static Type* New(void* instance) {
return DefaultLazyInstanceTraits<Type>::New(instance);
}
- // Rather than define an empty Delete function, we make Delete itself
- // a null pointer. This allows us to completely sidestep registering
- // this object with an AtExitManager, which allows you to use
- // LeakyLazyInstanceTraits in contexts where you don't have an
- // AtExitManager.
- static void (*Delete)(void* instance);
+ static void Delete(Type* instance) {
+ }
};
-template <typename Type>
-void (*LeakyLazyInstanceTraits<Type>::Delete)(void* instance) = NULL;
-
// We pull out some of the functionality into a non-templated base, so that we
// can implement the more complicated pieces out of line in the .cc file.
class BASE_EXPORT LazyInstanceHelper {
@@ -152,9 +147,7 @@ class LazyInstance : public LazyInstanceHelper {
NeedsInstance()) {
// Create the instance in the space provided by |buf_|.
instance_ = Traits::New(buf_);
- // Traits::Delete will be null for LeakyLazyInstanceTraits
- void (*dtor)(void*) = Traits::Delete;
- CompleteInstance(this, (dtor == NULL) ? NULL : OnExit);
+ CompleteInstance(this, Traits::kRegisterOnExit ? OnExit : NULL);
}
// This annotation helps race detectors recognize correct lock-less
@@ -181,7 +174,7 @@ class LazyInstance : public LazyInstanceHelper {
private:
// Adapter function for use with AtExit. This should be called single
- // threaded, so don't use atomic operations.
joth 2011/10/25 14:22:57 this comment looked incorrect, as it is using an a
willchan no longer on Chromium 2011/10/25 15:57:25 I think what's happening here is we *shouldn't* ne
+ // threaded, so don't synchronize across threads.
// Calling OnExit while the instance is in use by other threads is a mistake.
static void OnExit(void* lazy_instance) {
LazyInstance<Type, Traits>* me =
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698