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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 static void sk_once_##name##_function(arg) | 44 static void sk_once_##name##_function(arg) |
45 | 45 |
46 // Call this anywhere you need to guarantee that the corresponding DEF_SK_ONCE | 46 // Call this anywhere you need to guarantee that the corresponding DEF_SK_ONCE |
47 // block of code has run. name should match the DEF_SK_ONCE, and here you pass | 47 // block of code has run. name should match the DEF_SK_ONCE, and here you pass |
48 // the actual value of the argument. | 48 // the actual value of the argument. |
49 // E.g | 49 // E.g |
50 // int foo = 0; | 50 // int foo = 0; |
51 // SK_ONCE(my_onetime_setup, &foo); | 51 // SK_ONCE(my_onetime_setup, &foo); |
52 // SkASSERT(5 == foo); | 52 // SkASSERT(5 == foo); |
53 #define SK_ONCE(name, arg) \ | 53 #define SK_ONCE(name, arg) \ |
54 sk_once_impl(&sk_once_##name##_done, &sk_once_##name##_mutex, sk_once_##name
##_function, arg) | 54 sk_once(&sk_once_##name##_done, &sk_once_##name##_mutex, sk_once_##name##_fu
nction, arg) |
55 | 55 |
56 | 56 |
57 // ---------------------- Implementation details below here. -----------------
------------ | 57 // ---------------------- Implementation details below here. -----------------
------------ |
58 | 58 |
59 | 59 |
60 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when
refactoring lands. | 60 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when
refactoring lands. |
61 | 61 |
62 #ifdef SK_BUILD_FOR_WIN | 62 #ifdef SK_BUILD_FOR_WIN |
63 #include <intrin.h> | 63 #include <intrin.h> |
64 inline static void compiler_barrier() { | 64 inline static void compiler_barrier() { |
(...skipping 26 matching lines...) Expand all Loading... |
91 inline static void acquire_barrier() { | 91 inline static void acquire_barrier() { |
92 compiler_barrier(); | 92 compiler_barrier(); |
93 full_barrier_on_arm(); | 93 full_barrier_on_arm(); |
94 } | 94 } |
95 | 95 |
96 // We've pulled a pretty standard double-checked locking implementation apart | 96 // We've pulled a pretty standard double-checked locking implementation apart |
97 // into its main fast path and a slow path that's called when we suspect the | 97 // into its main fast path and a slow path that's called when we suspect the |
98 // one-time code hasn't run yet. | 98 // one-time code hasn't run yet. |
99 | 99 |
100 // This is the guts of the code, called when we suspect the one-time code hasn't
been run yet. | 100 // This is the guts of the code, called when we suspect the one-time code hasn't
been run yet. |
101 // This should be rarely called, so we separate it from sk_once_impl and don't m
ark it as inline. | 101 // This should be rarely called, so we separate it from sk_once and don't mark i
t as inline. |
102 // (We don't mind if this is an actual function call, but odds are it'll be inli
ned anyway.) | 102 // (We don't mind if this is an actual function call, but odds are it'll be inli
ned anyway.) |
103 template <typename Arg> | 103 template <typename Arg> |
104 static void sk_once_impl_slow(bool* done, SkBaseMutex* mutex, void (*once)(Arg),
Arg arg) { | 104 static void sk_once_slow(bool* done, SkBaseMutex* mutex, void (*once)(Arg), Arg
arg) { |
105 const SkAutoMutexAcquire lock(*mutex); | 105 const SkAutoMutexAcquire lock(*mutex); |
106 if (!*done) { | 106 if (!*done) { |
107 once(arg); | 107 once(arg); |
108 // Also known as a store-store/load-store barrier, this makes sure that
the writes | 108 // Also known as a store-store/load-store barrier, this makes sure that
the writes |
109 // done before here---in particular, those done by calling once(arg)---a
re observable | 109 // done before here---in particular, those done by calling once(arg)---a
re observable |
110 // before the writes after the line, *done = true. | 110 // before the writes after the line, *done = true. |
111 // | 111 // |
112 // In version control terms this is like saying, "check in the work up | 112 // In version control terms this is like saying, "check in the work up |
113 // to and including once(arg), then check in *done=true as a subsequent
change". | 113 // to and including once(arg), then check in *done=true as a subsequent
change". |
114 // | 114 // |
(...skipping 14 matching lines...) Expand all Loading... |
129 extern "C" { | 129 extern "C" { |
130 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co
nst char* desc); | 130 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co
nst char* desc); |
131 } | 131 } |
132 #define ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__, m
em, desc) | 132 #define ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__, m
em, desc) |
133 #else | 133 #else |
134 #define ANNOTATE_BENIGN_RACE(mem, desc) | 134 #define ANNOTATE_BENIGN_RACE(mem, desc) |
135 #endif | 135 #endif |
136 | 136 |
137 // This is our fast path, called all the time. We do really want it to be inlin
ed. | 137 // This is our fast path, called all the time. We do really want it to be inlin
ed. |
138 template <typename Arg> | 138 template <typename Arg> |
139 inline static void sk_once_impl(bool* done, SkBaseMutex* mutex, void (*once)(Arg
), Arg arg) { | 139 inline static void sk_once(bool* done, SkBaseMutex* mutex, void (*once)(Arg), Ar
g arg) { |
140 ANNOTATE_BENIGN_RACE(done, "Don't worry TSAN, we're sure this is safe."); | 140 ANNOTATE_BENIGN_RACE(done, "Don't worry TSAN, we're sure this is safe."); |
141 if (!*done) { | 141 if (!*done) { |
142 sk_once_impl_slow(done, mutex, once, arg); | 142 sk_once_slow(done, mutex, once, arg); |
143 } | 143 } |
144 // Also known as a load-load/load-store barrier, this acquire barrier makes | 144 // Also known as a load-load/load-store barrier, this acquire barrier makes |
145 // sure that anything we read from memory---in particular, memory written by | 145 // sure that anything we read from memory---in particular, memory written by |
146 // calling once(arg)---is at least as current as the value we read from done
. | 146 // calling once(arg)---is at least as current as the value we read from done
. |
147 // | 147 // |
148 // In version control terms, this is a lot like saying "sync up to the | 148 // In version control terms, this is a lot like saying "sync up to the |
149 // commit where we wrote *done = true". | 149 // commit where we wrote *done = true". |
150 // | 150 // |
151 // The release barrier in sk_once_impl_slow guaranteed that *done = true | 151 // The release barrier in sk_once_slow guaranteed that *done = true |
152 // happens after once(arg), so by syncing to *done = true here we're | 152 // happens after once(arg), so by syncing to *done = true here we're |
153 // forcing ourselves to also wait until the effects of once(arg) are readble
. | 153 // forcing ourselves to also wait until the effects of once(arg) are readble
. |
154 acquire_barrier(); | 154 acquire_barrier(); |
155 } | 155 } |
156 | 156 |
157 #undef ANNOTATE_BENIGN_RACE | 157 #undef ANNOTATE_BENIGN_RACE |
158 | 158 |
159 | 159 |
160 #endif // SkOnce_DEFINED | 160 #endif // SkOnce_DEFINED |
OLD | NEW |