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 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // | 105 // |
106 // We'll use this in the fast path to make sure f(arg)'s effects are | 106 // We'll use this in the fast path to make sure f(arg)'s effects are |
107 // observable whenever we observe *done == true. | 107 // observable whenever we observe *done == true. |
108 release_barrier(); | 108 release_barrier(); |
109 once->done = true; | 109 once->done = true; |
110 } | 110 } |
111 } | 111 } |
112 | 112 |
113 // We nabbed this code from the dynamic_annotations library, and in their honor | 113 // We nabbed this code from the dynamic_annotations library, and in their honor |
114 // we check the same define. If you find yourself wanting more than just | 114 // we check the same define. If you find yourself wanting more than just |
115 // ANNOTATE_BENIGN_RACE, it might make sense to pull that in as a dependency | 115 // SK_ANNOTATE_BENIGN_RACE, it might make sense to pull that in as a dependency |
116 // rather than continue to reproduce it here. | 116 // rather than continue to reproduce it here. |
117 | 117 |
118 #if DYNAMIC_ANNOTATIONS_ENABLED | 118 #if DYNAMIC_ANNOTATIONS_ENABLED |
119 // TSAN provides this hook to supress a known-safe apparent race. | 119 // TSAN provides this hook to supress a known-safe apparent race. |
120 extern "C" { | 120 extern "C" { |
121 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co
nst char* desc); | 121 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co
nst char* desc); |
122 } | 122 } |
123 #define ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__, m
em, desc) | 123 #define SK_ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__
, mem, desc) |
124 #else | 124 #else |
125 #define ANNOTATE_BENIGN_RACE(mem, desc) | 125 #define SK_ANNOTATE_BENIGN_RACE(mem, desc) |
126 #endif | 126 #endif |
127 | 127 |
128 // This is our fast path, called all the time. We do really want it to be inlin
ed. | 128 // This is our fast path, called all the time. We do really want it to be inlin
ed. |
129 template <typename Func, typename Arg> | 129 template <typename Func, typename Arg> |
130 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg) { | 130 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg) { |
131 ANNOTATE_BENIGN_RACE(&(once->done), "Don't worry TSAN, we're sure this is sa
fe."); | 131 SK_ANNOTATE_BENIGN_RACE(&(once->done), "Don't worry TSAN, we're sure this is
safe."); |
132 if (!once->done) { | 132 if (!once->done) { |
133 sk_once_slow(once, f, arg); | 133 sk_once_slow(once, f, arg); |
134 } | 134 } |
135 // Also known as a load-load/load-store barrier, this acquire barrier makes | 135 // Also known as a load-load/load-store barrier, this acquire barrier makes |
136 // sure that anything we read from memory---in particular, memory written by | 136 // sure that anything we read from memory---in particular, memory written by |
137 // calling f(arg)---is at least as current as the value we read from once->d
one. | 137 // calling f(arg)---is at least as current as the value we read from once->d
one. |
138 // | 138 // |
139 // In version control terms, this is a lot like saying "sync up to the | 139 // In version control terms, this is a lot like saying "sync up to the |
140 // commit where we wrote once->done = true". | 140 // commit where we wrote once->done = true". |
141 // | 141 // |
142 // The release barrier in sk_once_slow guaranteed that once->done = true | 142 // The release barrier in sk_once_slow guaranteed that once->done = true |
143 // happens after f(arg), so by syncing to once->done = true here we're | 143 // happens after f(arg), so by syncing to once->done = true here we're |
144 // forcing ourselves to also wait until the effects of f(arg) are readble. | 144 // forcing ourselves to also wait until the effects of f(arg) are readble. |
145 acquire_barrier(); | 145 acquire_barrier(); |
146 } | 146 } |
147 | 147 |
148 #undef ANNOTATE_BENIGN_RACE | 148 #undef SK_ANNOTATE_BENIGN_RACE |
149 | 149 |
150 #endif // SkOnce_DEFINED | 150 #endif // SkOnce_DEFINED |
OLD | NEW |