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

Side by Side Diff: base/lazy_instance.cc

Issue 1039823002: Add base::LazyInstance (Closed) Base URL: https://chromium.googlesource.com/chromium/mini_chromium@master
Patch Set: Created 5 years, 9 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/memory/aligned_memory.h » ('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 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 #include "base/lazy_instance.h"
6
7 #include "base/atomicops.h"
8 #include "base/basictypes.h"
9
10 #if defined(OS_POSIX)
11 #include <sched.h>
12 #elif defined(OS_WIN)
13 #include <windows.h>
14 #endif
15
16 namespace {
17
18 void ThreadYield() {
19 #if defined(OS_POSIX)
20 sched_yield();
21 #elif defined(OS_WIN)
22 Sleep(0);
23 #endif
24 }
25
26 } // namespace
27
28 namespace base {
29 namespace internal {
30
31 // TODO(joth): This function could be shared with Singleton, in place of its
32 // WaitForInstance() call.
33 bool NeedsLazyInstance(subtle::AtomicWord* state) {
34 // Try to create the instance, if we're the first, will go from 0 to
35 // kLazyInstanceStateCreating, otherwise we've already been beaten here.
36 // The memory access has no memory ordering as state 0 and
37 // kLazyInstanceStateCreating have no associated data (memory barriers are
38 // all about ordering of memory accesses to *associated* data).
39 if (subtle::NoBarrier_CompareAndSwap(state, 0,
40 kLazyInstanceStateCreating) == 0)
41 // Caller must create instance
42 return true;
43
44 // It's either in the process of being created, or already created. Spin.
45 // The load has acquire memory ordering as a thread which sees
46 // state_ == STATE_CREATED needs to acquire visibility over
47 // the associated data (buf_). Pairing Release_Store is in
48 // CompleteLazyInstance().
49 while (subtle::Acquire_Load(state) == kLazyInstanceStateCreating) {
50 ThreadYield();
51 }
52 // Someone else created the instance.
53 return false;
54 }
55
56 void CompleteLazyInstance(subtle::AtomicWord* state,
57 subtle::AtomicWord new_instance,
58 void* lazy_instance) {
59 // Instance is created, go from CREATING to CREATED.
60 // Releases visibility over private_buf_ to readers. Pairing Acquire_Load's
61 // are in NeedsInstance() and Pointer().
62 subtle::Release_Store(state, new_instance);
63 }
64
65 } // namespace internal
66 } // namespace base
OLDNEW
« no previous file with comments | « base/lazy_instance.h ('k') | base/memory/aligned_memory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698