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

Side by Side Diff: base/lazy_instance.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « base/lazy_instance.h ('k') | base/lazy_instance_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/lazy_instance.h"
6
7 #include "base/at_exit.h"
8 #include "base/atomicops.h"
9 #include "base/basictypes.h"
10 #include "base/threading/platform_thread.h"
11
12 namespace base {
13 namespace internal {
14
15 // TODO(joth): This function could be shared with Singleton, in place of its
16 // WaitForInstance() call.
17 bool NeedsLazyInstance(subtle::AtomicWord* state) {
18 // Try to create the instance, if we're the first, will go from 0 to
19 // kLazyInstanceStateCreating, otherwise we've already been beaten here.
20 // The memory access has no memory ordering as state 0 and
21 // kLazyInstanceStateCreating have no associated data (memory barriers are
22 // all about ordering of memory accesses to *associated* data).
23 if (subtle::NoBarrier_CompareAndSwap(state, 0,
24 kLazyInstanceStateCreating) == 0)
25 // Caller must create instance
26 return true;
27
28 // It's either in the process of being created, or already created. Spin.
29 // The load has acquire memory ordering as a thread which sees
30 // state_ == STATE_CREATED needs to acquire visibility over
31 // the associated data (buf_). Pairing Release_Store is in
32 // CompleteLazyInstance().
33 while (subtle::Acquire_Load(state) == kLazyInstanceStateCreating) {
34 PlatformThread::YieldCurrentThread();
35 }
36 // Someone else created the instance.
37 return false;
38 }
39
40 void CompleteLazyInstance(subtle::AtomicWord* state,
41 subtle::AtomicWord new_instance,
42 void* lazy_instance,
43 void (*dtor)(void*)) {
44 // Instance is created, go from CREATING to CREATED.
45 // Releases visibility over private_buf_ to readers. Pairing Acquire_Load's
46 // are in NeedsInstance() and Pointer().
47 subtle::Release_Store(state, new_instance);
48
49 // Make sure that the lazily instantiated object will get destroyed at exit.
50 if (dtor)
51 AtExitManager::RegisterCallback(dtor, lazy_instance);
52 }
53
54 } // namespace internal
55 } // namespace base
OLDNEW
« no previous file with comments | « 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