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

Side by Side Diff: include/core/SkOnce.h

Issue 155963003: SkOnce in is_lcd_supported instead of hand rolled double-checked locking. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: style Created 6 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 | « no previous file | include/core/SkThread.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 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkOnce_DEFINED 8 #ifndef SkOnce_DEFINED
9 #define SkOnce_DEFINED 9 #define SkOnce_DEFINED
10 10
(...skipping 22 matching lines...) Expand all
33 #include "SkTypes.h" 33 #include "SkTypes.h"
34 34
35 #define SK_ONCE_INIT { false, { 0, SkDEBUGCODE(0) } } 35 #define SK_ONCE_INIT { false, { 0, SkDEBUGCODE(0) } }
36 #define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name = SK_ONCE_INIT 36 #define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name = SK_ONCE_INIT
37 37
38 struct SkOnceFlag; // If manually created, initialize with SkOnceFlag once = SK _ONCE_INIT 38 struct SkOnceFlag; // If manually created, initialize with SkOnceFlag once = SK _ONCE_INIT
39 39
40 template <typename Func, typename Arg> 40 template <typename Func, typename Arg>
41 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)() = NULL); 41 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)() = NULL);
42 42
43 // If you've already got a lock and a flag to use, this variant lets you avoid a n extra SKOnceFlag.
bungeman-skia 2014/02/05 19:12:58 Lowercase the 'K'? ('SkOnceFlag')
44 template <typename Lock, typename Func, typename Arg>
45 inline void SkOnce(bool* done, Lock* lock, Func f, Arg arg, void(*atExit)() = NU LL);
46
43 // ---------------------- Implementation details below here. ----------------- ------------ 47 // ---------------------- Implementation details below here. ----------------- ------------
44 48
49 // This is POD and must be zero-initialized.
50 struct SkSpinlock {
51 void acquire() {
52 SkASSERT(shouldBeZero == 0);
53 // No memory barrier needed, but sk_atomic_cas gives us at least release anyway.
54 while (!sk_atomic_cas(&thisIsPrivate, 0, 1)) {
55 // spin
56 }
57 }
58
59 void release() {
60 SkASSERT(shouldBeZero == 0);
61 // This requires a release memory barrier before storing, which sk_atomi c_cas guarantees.
62 SkAssertResult(sk_atomic_cas(&thisIsPrivate, 1, 0));
63 }
64
65 int32_t thisIsPrivate;
66 SkDEBUGCODE(int32_t shouldBeZero;)
67 };
68
45 struct SkOnceFlag { 69 struct SkOnceFlag {
46 bool done; 70 bool done;
47 SkSpinlock lock; 71 SkSpinlock lock;
48 }; 72 };
49 73
50 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands. 74 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands.
51 75
52 #ifdef SK_BUILD_FOR_WIN 76 #ifdef SK_BUILD_FOR_WIN
53 # include <intrin.h> 77 # include <intrin.h>
54 inline static void compiler_barrier() { 78 inline static void compiler_barrier() {
(...skipping 25 matching lines...) Expand all
80 inline static void release_barrier() { 104 inline static void release_barrier() {
81 compiler_barrier(); 105 compiler_barrier();
82 full_barrier_on_arm(); 106 full_barrier_on_arm();
83 } 107 }
84 108
85 inline static void acquire_barrier() { 109 inline static void acquire_barrier() {
86 compiler_barrier(); 110 compiler_barrier();
87 full_barrier_on_arm(); 111 full_barrier_on_arm();
88 } 112 }
89 113
114 // Works with SkSpinlock or SkMutex.
115 template <typename Lock>
116 class SkAutoLockAcquire {
117 public:
118 explicit SkAutoLockAcquire(Lock* lock) : fLock(lock) { fLock->acquire(); }
119 ~SkAutoLockAcquire() { fLock->release(); }
120 private:
121 Lock* fLock;
122 };
123
90 // We've pulled a pretty standard double-checked locking implementation apart 124 // We've pulled a pretty standard double-checked locking implementation apart
91 // into its main fast path and a slow path that's called when we suspect the 125 // into its main fast path and a slow path that's called when we suspect the
92 // one-time code hasn't run yet. 126 // one-time code hasn't run yet.
93 127
94 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet. 128 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet.
95 // This should be rarely called, so we separate it from SkOnce and don't mark it as inline. 129 // This should be rarely called, so we separate it from SkOnce and don't mark it as inline.
96 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.) 130 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.)
97 template <typename Func, typename Arg> 131 template <typename Lock, typename Func, typename Arg>
98 static void sk_once_slow(SkOnceFlag* once, Func f, Arg arg, void (*atExit)()) { 132 static void sk_once_slow(bool* done, Lock* lock, Func f, Arg arg, void (*atExit) ()) {
99 const SkAutoSpinlock lock(&once->lock); 133 const SkAutoLockAcquire<Lock> locked(lock);
100 if (!once->done) { 134 if (!*done) {
101 f(arg); 135 f(arg);
102 if (atExit != NULL) { 136 if (atExit != NULL) {
103 atexit(atExit); 137 atexit(atExit);
104 } 138 }
105 // Also known as a store-store/load-store barrier, this makes sure that the writes 139 // Also known as a store-store/load-store barrier, this makes sure that the writes
106 // done before here---in particular, those done by calling f(arg)---are observable 140 // done before here---in particular, those done by calling f(arg)---are observable
107 // before the writes after the line, *done = true. 141 // before the writes after the line, *done = true.
108 // 142 //
109 // In version control terms this is like saying, "check in the work up 143 // In version control terms this is like saying, "check in the work up
110 // to and including f(arg), then check in *done=true as a subsequent cha nge". 144 // to and including f(arg), then check in *done=true as a subsequent cha nge".
111 // 145 //
112 // We'll use this in the fast path to make sure f(arg)'s effects are 146 // We'll use this in the fast path to make sure f(arg)'s effects are
113 // observable whenever we observe *done == true. 147 // observable whenever we observe *done == true.
114 release_barrier(); 148 release_barrier();
115 once->done = true; 149 *done = true;
116 } 150 }
117 } 151 }
118 152
119 // This is our fast path, called all the time. We do really want it to be inlin ed. 153 // This is our fast path, called all the time. We do really want it to be inlin ed.
120 template <typename Func, typename Arg> 154 template <typename Lock, typename Func, typename Arg>
121 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)()) { 155 inline void SkOnce(bool* done, Lock* lock, Func f, Arg arg, void(*atExit)()) {
122 if (!SK_ANNOTATE_UNPROTECTED_READ(once->done)) { 156 if (!SK_ANNOTATE_UNPROTECTED_READ(*done)) {
123 sk_once_slow(once, f, arg, atExit); 157 sk_once_slow(done, lock, f, arg, atExit);
124 } 158 }
125 // Also known as a load-load/load-store barrier, this acquire barrier makes 159 // Also known as a load-load/load-store barrier, this acquire barrier makes
126 // sure that anything we read from memory---in particular, memory written by 160 // sure that anything we read from memory---in particular, memory written by
127 // calling f(arg)---is at least as current as the value we read from once->d one. 161 // calling f(arg)---is at least as current as the value we read from once->d one.
128 // 162 //
129 // In version control terms, this is a lot like saying "sync up to the 163 // In version control terms, this is a lot like saying "sync up to the
130 // commit where we wrote once->done = true". 164 // commit where we wrote once->done = true".
131 // 165 //
132 // The release barrier in sk_once_slow guaranteed that once->done = true 166 // The release barrier in sk_once_slow guaranteed that once->done = true
133 // happens after f(arg), so by syncing to once->done = true here we're 167 // happens after f(arg), so by syncing to once->done = true here we're
134 // forcing ourselves to also wait until the effects of f(arg) are readble. 168 // forcing ourselves to also wait until the effects of f(arg) are readble.
135 acquire_barrier(); 169 acquire_barrier();
136 } 170 }
137 171
172 template <typename Func, typename Arg>
173 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)()) {
174 return SkOnce(&once->done, &once->lock, f, arg, atExit);
175 }
176
138 #undef SK_ANNOTATE_BENIGN_RACE 177 #undef SK_ANNOTATE_BENIGN_RACE
139 178
140 #endif // SkOnce_DEFINED 179 #endif // SkOnce_DEFINED
OLDNEW
« no previous file with comments | « no previous file | include/core/SkThread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698