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

Unified Diff: base/lazy_instance.cc

Issue 8491043: Allow linker initialization of lazy instance (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: thakis comment, renamed LAZY_INSTANCE_INITIALIZER 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 side-by-side diff with in-line comments
Download patch
Index: base/lazy_instance.cc
diff --git a/base/lazy_instance.cc b/base/lazy_instance.cc
index 1be34883a77b24563f2e8633a1d9729baa01533f..8e3e47f3169667a369946063eeb356cd16feaf14 100644
--- a/base/lazy_instance.cc
+++ b/base/lazy_instance.cc
@@ -11,15 +11,18 @@
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
namespace base {
+namespace internal {
-bool LazyInstanceHelper::NeedsInstance() {
+// TODO(joth): This function could be shared with Singlton, in place of its
M-A Ruel 2011/11/11 21:34:06 Singleton
joth 2011/11/14 16:58:23 Done.
+// WaitForInstance() call.
+bool NeedsLazyInstance(base::subtle::AtomicWord* state) {
// Try to create the instance, if we're the first, will go from EMPTY
// to CREATING, otherwise we've already been beaten here.
// The memory access has no memory ordering as STATE_EMPTY and STATE_CREATING
// has no associated data (memory barriers are all about ordering
// of memory accesses to *associated* data).
if (base::subtle::NoBarrier_CompareAndSwap(
- &state_, STATE_EMPTY, STATE_CREATING) == STATE_EMPTY)
+ state, 0, internal::kLazyInstanceStateCreating) == 0)
// Caller must create instance
return true;
@@ -28,28 +31,31 @@ bool LazyInstanceHelper::NeedsInstance() {
// state_ == STATE_CREATED needs to acquire visibility over
// the associated data (buf_). Pairing Release_Store is in
// CompleteInstance().
- while (base::subtle::Acquire_Load(&state_) != STATE_CREATED)
+ while (base::subtle::Acquire_Load(state) ==
+ internal::kLazyInstanceStateCreating) {
PlatformThread::YieldCurrentThread();
-
+ }
// Someone else created the instance.
return false;
}
-void LazyInstanceHelper::CompleteInstance(void* instance, void (*dtor)(void*)) {
+void CompleteLazyInstance(base::subtle::AtomicWord* state,
+ base::subtle::AtomicWord new_instance,
+ void* lazy_instance,
+ void (*dtor)(void*)) {
// See the comment to the corresponding HAPPENS_AFTER in Pointer().
- ANNOTATE_HAPPENS_BEFORE(&state_);
+ ANNOTATE_HAPPENS_BEFORE(state);
// Instance is created, go from CREATING to CREATED.
- // Releases visibility over buf_ to readers. Pairing Acquire_Load's are in
- // NeedsInstance() and Pointer().
- base::subtle::Release_Store(&state_, STATE_CREATED);
+ // Releases visibility over private_buf_ to readers. Pairing Acquire_Load's
+ // are in NeedsInstance() and Pointer().
+ base::subtle::Release_Store(state, new_instance);
// Make sure that the lazily instantiated object will get destroyed at exit.
if (dtor)
- base::AtExitManager::RegisterCallback(dtor, instance);
+ base::AtExitManager::RegisterCallback(dtor, lazy_instance);
}
+} // namespace internal
} // namespace base
M-A Ruel 2011/11/11 21:34:06 remove too
joth 2011/11/14 16:58:23 Done.
-
-
« base/lazy_instance.h ('K') | « base/lazy_instance.h ('k') | base/lazy_instance_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698