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

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

Issue 996763002: Clean up SkDynamicAnnotations. (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: review 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 | « include/core/SkLazyPtr.h ('k') | include/core/SkThreadPriv.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
11 // Before trying SkOnce, see if SkLazyPtr or SkLazyFnPtr will work for you. 11 // Before trying SkOnce, see if SkLazyPtr or SkLazyFnPtr will work for you.
12 // They're smaller and faster, if slightly less versatile. 12 // They're smaller and faster, if slightly less versatile.
13 13
14 14
15 // SkOnce.h defines SK_DECLARE_STATIC_ONCE and SkOnce(), which you can use 15 // SkOnce.h defines SK_DECLARE_STATIC_ONCE and SkOnce(), which you can use
16 // together to create a threadsafe way to call a function just once. E.g. 16 // together to create a threadsafe way to call a function just once. E.g.
17 // 17 //
18 // static void register_my_stuff(GlobalRegistry* registry) { 18 // static void register_my_stuff(GlobalRegistry* registry) {
19 // registry->register(...); 19 // registry->register(...);
20 // } 20 // }
21 // ... 21 // ...
22 // void EnsureRegistered() { 22 // void EnsureRegistered() {
23 // SK_DECLARE_STATIC_ONCE(once); 23 // SK_DECLARE_STATIC_ONCE(once);
24 // SkOnce(&once, register_my_stuff, GetGlobalRegistry()); 24 // SkOnce(&once, register_my_stuff, GetGlobalRegistry());
25 // } 25 // }
26 // 26 //
27 // No matter how many times you call EnsureRegistered(), register_my_stuff will be called just once. 27 // No matter how many times you call EnsureRegistered(), register_my_stuff will be called just once.
28 // OnceTest.cpp also should serve as a few other simple examples. 28 // OnceTest.cpp also should serve as a few other simple examples.
29 29
30 #include "SkDynamicAnnotations.h" 30 #include "SkAtomics.h"
31 #include "SkThread.h"
32 #include "SkTypes.h"
33 31
34 // This must be used in a global scope, not in fuction scope or as a class membe r. 32 // This must be used in a global scope, not in fuction scope or as a class membe r.
35 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name 33 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name
36 34
37 class SkOnceFlag; 35 class SkOnceFlag;
38 36
39 inline void SkOnce(SkOnceFlag* once, void (*f)()); 37 inline void SkOnce(SkOnceFlag* once, void (*f)());
40 38
41 template <typename Arg> 39 template <typename Arg>
42 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg); 40 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 // We've pulled a pretty standard double-checked locking implementation apart 76 // We've pulled a pretty standard double-checked locking implementation apart
79 // into its main fast path and a slow path that's called when we suspect the 77 // into its main fast path and a slow path that's called when we suspect the
80 // one-time code hasn't run yet. 78 // one-time code hasn't run yet.
81 79
82 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet. 80 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet.
83 // This should be rarely called, so we separate it from SkOnce and don't mark it as inline. 81 // This should be rarely called, so we separate it from SkOnce and don't mark it as inline.
84 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.) 82 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.)
85 template <typename Lock, typename Arg> 83 template <typename Lock, typename Arg>
86 static void sk_once_slow(bool* done, Lock* lock, void (*f)(Arg), Arg arg) { 84 static void sk_once_slow(bool* done, Lock* lock, void (*f)(Arg), Arg arg) {
87 lock->acquire(); 85 lock->acquire();
88 if (!*done) { 86 if (!sk_atomic_load(done, sk_memory_order_relaxed)) {
89 f(arg); 87 f(arg);
90 // Also known as a store-store/load-store barrier, this makes sure that the writes 88 // Also known as a store-store/load-store barrier, this makes sure that the writes
91 // done before here---in particular, those done by calling f(arg)---are observable 89 // done before here---in particular, those done by calling f(arg)---are observable
92 // before the writes after the line, *done = true. 90 // before the writes after the line, *done = true.
93 // 91 //
94 // In version control terms this is like saying, "check in the work up 92 // In version control terms this is like saying, "check in the work up
95 // to and including f(arg), then check in *done=true as a subsequent cha nge". 93 // to and including f(arg), then check in *done=true as a subsequent cha nge".
96 // 94 //
97 // We'll use this in the fast path to make sure f(arg)'s effects are 95 // We'll use this in the fast path to make sure f(arg)'s effects are
98 // observable whenever we observe *done == true. 96 // observable whenever we observe *done == true.
99 sk_release_store(done, true); 97 sk_release_store(done, true);
100 } 98 }
101 lock->release(); 99 lock->release();
102 } 100 }
103 101
104 // This is our fast path, called all the time. We do really want it to be inlin ed. 102 // This is our fast path, called all the time. We do really want it to be inlin ed.
105 template <typename Lock, typename Arg> 103 template <typename Lock, typename Arg>
106 inline void SkOnce(bool* done, Lock* lock, void (*f)(Arg), Arg arg) { 104 inline void SkOnce(bool* done, Lock* lock, void (*f)(Arg), Arg arg) {
107 if (!SK_ANNOTATE_UNPROTECTED_READ(*done)) { 105 // When *done == true:
106 // Also known as a load-load/load-store barrier, this acquire barrier make s
107 // sure that anything we read from memory---in particular, memory written by
108 // calling f(arg)---is at least as current as the value we read from done.
109 //
110 // In version control terms, this is a lot like saying "sync up to the
111 // commit where we wrote done = true".
112 //
113 // The release barrier in sk_once_slow guaranteed that done = true
114 // happens after f(arg), so by syncing to done = true here we're
115 // forcing ourselves to also wait until the effects of f(arg) are readble.
116 //
117 // When *done == false:
118 // We'll try to call f(arg) in sk_once_slow.
119 // If we get the lock, great, we call f(arg), release true into done, and drop the lock.
120 // If we race and don't get the lock first, we'll wait for the first guy t o finish.
121 // Then lock acquire() will give us at least an acquire memory barrier to get the same
122 // effect as the acquire load in the *done == true fast case. We'll see * done is true,
123 // then just drop the lock and return.
124 if (!sk_atomic_load(done, sk_memory_order_acquire)) {
108 sk_once_slow(done, lock, f, arg); 125 sk_once_slow(done, lock, f, arg);
109 } 126 }
110 // Also known as a load-load/load-store barrier, this acquire barrier makes
111 // sure that anything we read from memory---in particular, memory written by
112 // calling f(arg)---is at least as current as the value we read from done.
113 //
114 // In version control terms, this is a lot like saying "sync up to the
115 // commit where we wrote done = true".
116 //
117 // The release barrier in sk_once_slow guaranteed that done = true
118 // happens after f(arg), so by syncing to done = true here we're
119 // forcing ourselves to also wait until the effects of f(arg) are readble.
120 SkAssertResult(sk_acquire_load(done));
121 } 127 }
122 128
123 template <typename Arg> 129 template <typename Arg>
124 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg) { 130 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg) {
125 return SkOnce(once->mutableDone(), once, f, arg); 131 return SkOnce(once->mutableDone(), once, f, arg);
126 } 132 }
127 133
128 // Calls its argument. 134 // Calls its argument.
129 // This lets us use functions that take no arguments with SkOnce methods above. 135 // This lets us use functions that take no arguments with SkOnce methods above.
130 // (We pass _this_ as the function and the no-arg function as its argument. Cut e eh?) 136 // (We pass _this_ as the function and the no-arg function as its argument. Cut e eh?)
131 static void sk_once_no_arg_adaptor(void (*f)()) { 137 static void sk_once_no_arg_adaptor(void (*f)()) {
132 f(); 138 f();
133 } 139 }
134 140
135 inline void SkOnce(SkOnceFlag* once, void (*func)()) { 141 inline void SkOnce(SkOnceFlag* once, void (*func)()) {
136 return SkOnce(once, sk_once_no_arg_adaptor, func); 142 return SkOnce(once, sk_once_no_arg_adaptor, func);
137 } 143 }
138 144
139 template <typename Lock> 145 template <typename Lock>
140 inline void SkOnce(bool* done, Lock* lock, void (*func)()) { 146 inline void SkOnce(bool* done, Lock* lock, void (*func)()) {
141 return SkOnce(done, lock, sk_once_no_arg_adaptor, func); 147 return SkOnce(done, lock, sk_once_no_arg_adaptor, func);
142 } 148 }
143 149
144 #endif // SkOnce_DEFINED 150 #endif // SkOnce_DEFINED
OLDNEW
« no previous file with comments | « include/core/SkLazyPtr.h ('k') | include/core/SkThreadPriv.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698