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

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

Issue 144953005: TSAN: use somewhat pithier SK_ANNOTATE_UNPROTECTED_READ. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: benh 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
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 10 matching lines...) Expand all
21 // SK_DECLARE_STATIC_ONCE(once); 21 // SK_DECLARE_STATIC_ONCE(once);
22 // SkOnce(&once, set_up_my_singleton, &singleton); 22 // SkOnce(&once, set_up_my_singleton, &singleton);
23 // SkASSERT(NULL != singleton); 23 // SkASSERT(NULL != singleton);
24 // return *singleton; 24 // return *singleton;
25 // } 25 // }
26 // 26 //
27 // OnceTest.cpp also should serve as a few other simple examples. 27 // OnceTest.cpp also should serve as a few other simple examples.
28 // 28 //
29 // You may optionally pass SkOnce a second function to be called at exit for cle anup. 29 // You may optionally pass SkOnce a second function to be called at exit for cle anup.
30 30
31 #include "SkDynamicAnnotations.h"
31 #include "SkThread.h" 32 #include "SkThread.h"
32 #include "SkTypes.h" 33 #include "SkTypes.h"
33 34
34 #define SK_ONCE_INIT { false, { 0, SkDEBUGCODE(0) } } 35 #define SK_ONCE_INIT { false, { 0, SkDEBUGCODE(0) } }
35 #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
36 37
37 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
38 39
39 template <typename Func, typename Arg> 40 template <typename Func, typename Arg>
40 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);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 // In version control terms this is like saying, "check in the work up 109 // In version control terms this is like saying, "check in the work up
109 // to and including f(arg), then check in *done=true as a subsequent cha nge". 110 // to and including f(arg), then check in *done=true as a subsequent cha nge".
110 // 111 //
111 // We'll use this in the fast path to make sure f(arg)'s effects are 112 // We'll use this in the fast path to make sure f(arg)'s effects are
112 // observable whenever we observe *done == true. 113 // observable whenever we observe *done == true.
113 release_barrier(); 114 release_barrier();
114 once->done = true; 115 once->done = true;
115 } 116 }
116 } 117 }
117 118
118 // We nabbed this code from the dynamic_annotations library, and in their honor
119 // we check the same define. If you find yourself wanting more than just
120 // SK_ANNOTATE_BENIGN_RACE, it might make sense to pull that in as a dependency
121 // rather than continue to reproduce it here.
122
123 #if DYNAMIC_ANNOTATIONS_ENABLED
124 // TSAN provides this hook to supress a known-safe apparent race.
125 extern "C" {
126 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co nst char* desc);
127 }
128 #define SK_ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__ , mem, desc)
129 #else
130 #define SK_ANNOTATE_BENIGN_RACE(mem, desc)
131 #endif
132
133 // This is our fast path, called all the time. We do really want it to be inlin ed. 119 // This is our fast path, called all the time. We do really want it to be inlin ed.
134 template <typename Func, typename Arg> 120 template <typename Func, typename Arg>
135 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)()) { 121 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)()) {
136 SK_ANNOTATE_BENIGN_RACE(&(once->done), "Don't worry TSAN, we're sure this is safe."); 122 if (!SK_ANNOTATE_UNPROTECTED_READ(once->done)) {
137 if (!once->done) {
138 sk_once_slow(once, f, arg, atExit); 123 sk_once_slow(once, f, arg, atExit);
139 } 124 }
140 // Also known as a load-load/load-store barrier, this acquire barrier makes 125 // Also known as a load-load/load-store barrier, this acquire barrier makes
141 // sure that anything we read from memory---in particular, memory written by 126 // sure that anything we read from memory---in particular, memory written by
142 // calling f(arg)---is at least as current as the value we read from once->d one. 127 // calling f(arg)---is at least as current as the value we read from once->d one.
143 // 128 //
144 // In version control terms, this is a lot like saying "sync up to the 129 // In version control terms, this is a lot like saying "sync up to the
145 // commit where we wrote once->done = true". 130 // commit where we wrote once->done = true".
146 // 131 //
147 // The release barrier in sk_once_slow guaranteed that once->done = true 132 // The release barrier in sk_once_slow guaranteed that once->done = true
148 // happens after f(arg), so by syncing to once->done = true here we're 133 // happens after f(arg), so by syncing to once->done = true here we're
149 // forcing ourselves to also wait until the effects of f(arg) are readble. 134 // forcing ourselves to also wait until the effects of f(arg) are readble.
150 acquire_barrier(); 135 acquire_barrier();
151 } 136 }
152 137
153 #undef SK_ANNOTATE_BENIGN_RACE 138 #undef SK_ANNOTATE_BENIGN_RACE
154 139
155 #endif // SkOnce_DEFINED 140 #endif // SkOnce_DEFINED
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698