OLD | NEW |
---|---|
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 Loading... | |
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 (!*done) { |
Alexander Potapenko
2015/03/11 09:09:44
For the sake of uniformity this needs to be an ato
mtklein
2015/03/11 15:31:20
Done.
| |
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 if (!sk_atomic_load(done, sk_memory_order_relaxed)) { |
108 sk_once_slow(done, lock, f, arg); | 106 sk_once_slow(done, lock, f, arg); |
109 } | 107 } |
110 // Also known as a load-load/load-store barrier, this acquire barrier makes | 108 // 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 | 109 // 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. | 110 // calling f(arg)---is at least as current as the value we read from done. |
113 // | 111 // |
114 // In version control terms, this is a lot like saying "sync up to the | 112 // In version control terms, this is a lot like saying "sync up to the |
115 // commit where we wrote done = true". | 113 // commit where we wrote done = true". |
116 // | 114 // |
117 // The release barrier in sk_once_slow guaranteed that done = true | 115 // 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 | 116 // 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. | 117 // forcing ourselves to also wait until the effects of f(arg) are readble. |
120 SkAssertResult(sk_acquire_load(done)); | 118 SkAssertResult(sk_acquire_load(done)); |
Alexander Potapenko
2015/03/11 09:09:44
|done| is always true at this point, so it looks l
mtklein
2015/03/11 15:31:20
Done. I think we were worried about the case when
| |
121 } | 119 } |
122 | 120 |
123 template <typename Arg> | 121 template <typename Arg> |
124 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg) { | 122 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg) { |
125 return SkOnce(once->mutableDone(), once, f, arg); | 123 return SkOnce(once->mutableDone(), once, f, arg); |
126 } | 124 } |
127 | 125 |
128 // Calls its argument. | 126 // Calls its argument. |
129 // This lets us use functions that take no arguments with SkOnce methods above. | 127 // 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?) | 128 // (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)()) { | 129 static void sk_once_no_arg_adaptor(void (*f)()) { |
132 f(); | 130 f(); |
133 } | 131 } |
134 | 132 |
135 inline void SkOnce(SkOnceFlag* once, void (*func)()) { | 133 inline void SkOnce(SkOnceFlag* once, void (*func)()) { |
136 return SkOnce(once, sk_once_no_arg_adaptor, func); | 134 return SkOnce(once, sk_once_no_arg_adaptor, func); |
137 } | 135 } |
138 | 136 |
139 template <typename Lock> | 137 template <typename Lock> |
140 inline void SkOnce(bool* done, Lock* lock, void (*func)()) { | 138 inline void SkOnce(bool* done, Lock* lock, void (*func)()) { |
141 return SkOnce(done, lock, sk_once_no_arg_adaptor, func); | 139 return SkOnce(done, lock, sk_once_no_arg_adaptor, func); |
142 } | 140 } |
143 | 141 |
144 #endif // SkOnce_DEFINED | 142 #endif // SkOnce_DEFINED |
OLD | NEW |