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 // SkOnce.h defines SK_DECLARE_STATIC_ONCE and SkOnce(), which you can use | 11 // SkOnce.h defines SK_DECLARE_STATIC_ONCE and SkOnce(), which you can use |
12 // together to create a threadsafe way to call a function just once. This | 12 // together to create a threadsafe way to call a function just once. This |
13 // is particularly useful for lazy singleton initialization. E.g. | 13 // is particularly useful for lazy singleton initialization. E.g. |
14 // | 14 // |
15 // static void set_up_my_singleton(Singleton** singleton) { | 15 // static void set_up_my_singleton(Singleton** singleton) { |
16 // *singleton = new Singleton(...); | 16 // *singleton = new Singleton(...); |
17 // } | 17 // } |
18 // ... | 18 // ... |
19 // const Singleton& GetSingleton() { | 19 // const Singleton& GetSingleton() { |
20 // static Singleton* singleton = NULL; | 20 // static Singleton* singleton = NULL; |
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 #include "SkThread.h" | 29 #include "SkThread.h" |
30 #include "SkTypes.h" | 30 #include "SkTypes.h" |
31 | 31 |
32 #ifdef SK_USE_POSIX_THREADS | 32 #ifdef SK_USE_POSIX_THREADS |
33 #define SK_DECLARE_STATIC_ONCE(name) \ | 33 # define SK_ONCE_INIT { false, { PTHREAD_MUTEX_INITIALIZER } } |
34 static SkOnceFlag name = { false, { PTHREAD_MUTEX_INITIALIZER } } | |
35 #else | 34 #else |
36 #define SK_DECLARE_STATIC_ONCE(name) \ | 35 # define SK_ONCE_INIT { false, SkBaseMutex() } |
37 static SkOnceFlag name = { false, SkBaseMutex() } | |
38 #endif | 36 #endif |
39 | 37 |
40 struct SkOnceFlag; | 38 #define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name = SK_ONCE_INIT |
41 | 39 |
42 template <typename Arg> | 40 struct SkOnceFlag; // If manually created, initialize with SkOnceFlag once = SK
_ONCE_INIT |
43 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg); | 41 |
| 42 template <typename Func, typename Arg> |
| 43 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg); |
44 | 44 |
45 // ---------------------- Implementation details below here. -----------------
------------ | 45 // ---------------------- Implementation details below here. -----------------
------------ |
46 | 46 |
47 struct SkOnceFlag { | 47 struct SkOnceFlag { |
48 bool done; | 48 bool done; |
49 SkBaseMutex mutex; | 49 SkBaseMutex mutex; |
50 }; | 50 }; |
51 | 51 |
52 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when
refactoring lands. | 52 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when
refactoring lands. |
53 | 53 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 full_barrier_on_arm(); | 89 full_barrier_on_arm(); |
90 } | 90 } |
91 | 91 |
92 // We've pulled a pretty standard double-checked locking implementation apart | 92 // We've pulled a pretty standard double-checked locking implementation apart |
93 // into its main fast path and a slow path that's called when we suspect the | 93 // into its main fast path and a slow path that's called when we suspect the |
94 // one-time code hasn't run yet. | 94 // one-time code hasn't run yet. |
95 | 95 |
96 // This is the guts of the code, called when we suspect the one-time code hasn't
been run yet. | 96 // This is the guts of the code, called when we suspect the one-time code hasn't
been run yet. |
97 // This should be rarely called, so we separate it from SkOnce and don't mark it
as inline. | 97 // This should be rarely called, so we separate it from SkOnce and don't mark it
as inline. |
98 // (We don't mind if this is an actual function call, but odds are it'll be inli
ned anyway.) | 98 // (We don't mind if this is an actual function call, but odds are it'll be inli
ned anyway.) |
99 template <typename Arg> | 99 template <typename Func, typename Arg> |
100 static void sk_once_slow(SkOnceFlag* once, void (*f)(Arg), Arg arg) { | 100 static void sk_once_slow(SkOnceFlag* once, Func f, Arg arg) { |
101 const SkAutoMutexAcquire lock(once->mutex); | 101 const SkAutoMutexAcquire lock(once->mutex); |
102 if (!once->done) { | 102 if (!once->done) { |
103 f(arg); | 103 f(arg); |
104 // Also known as a store-store/load-store barrier, this makes sure that
the writes | 104 // Also known as a store-store/load-store barrier, this makes sure that
the writes |
105 // done before here---in particular, those done by calling once(arg)---a
re observable | 105 // done before here---in particular, those done by calling f(arg)---are
observable |
106 // before the writes after the line, *done = true. | 106 // before the writes after the line, *done = true. |
107 // | 107 // |
108 // In version control terms this is like saying, "check in the work up | 108 // In version control terms this is like saying, "check in the work up |
109 // to and including once(arg), then check in *done=true as a subsequent
change". | 109 // to and including f(arg), then check in *done=true as a subsequent cha
nge". |
110 // | 110 // |
111 // We'll use this in the fast path to make sure once(arg)'s effects are | 111 // We'll use this in the fast path to make sure f(arg)'s effects are |
112 // observable whenever we observe *done == true. | 112 // observable whenever we observe *done == true. |
113 release_barrier(); | 113 release_barrier(); |
114 once->done = true; | 114 once->done = true; |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 // We nabbed this code from the dynamic_annotations library, and in their honor | 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 | 119 // we check the same define. If you find yourself wanting more than just |
120 // ANNOTATE_BENIGN_RACE, it might make sense to pull that in as a dependency | 120 // ANNOTATE_BENIGN_RACE, it might make sense to pull that in as a dependency |
121 // rather than continue to reproduce it here. | 121 // rather than continue to reproduce it here. |
122 | 122 |
123 #if DYNAMIC_ANNOTATIONS_ENABLED | 123 #if DYNAMIC_ANNOTATIONS_ENABLED |
124 // TSAN provides this hook to supress a known-safe apparent race. | 124 // TSAN provides this hook to supress a known-safe apparent race. |
125 extern "C" { | 125 extern "C" { |
126 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co
nst char* desc); | 126 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co
nst char* desc); |
127 } | 127 } |
128 #define ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__, m
em, desc) | 128 #define ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__, m
em, desc) |
129 #else | 129 #else |
130 #define ANNOTATE_BENIGN_RACE(mem, desc) | 130 #define ANNOTATE_BENIGN_RACE(mem, desc) |
131 #endif | 131 #endif |
132 | 132 |
133 // This is our fast path, called all the time. We do really want it to be inlin
ed. | 133 // This is our fast path, called all the time. We do really want it to be inlin
ed. |
134 template <typename Arg> | 134 template <typename Func, typename Arg> |
135 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg) { | 135 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg) { |
136 ANNOTATE_BENIGN_RACE(&(once->done), "Don't worry TSAN, we're sure this is sa
fe."); | 136 ANNOTATE_BENIGN_RACE(&(once->done), "Don't worry TSAN, we're sure this is sa
fe."); |
137 if (!once->done) { | 137 if (!once->done) { |
138 sk_once_slow(once, f, arg); | 138 sk_once_slow(once, f, arg); |
139 } | 139 } |
140 // Also known as a load-load/load-store barrier, this acquire barrier makes | 140 // 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 | 141 // 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. | 142 // calling f(arg)---is at least as current as the value we read from once->d
one. |
143 // | 143 // |
144 // In version control terms, this is a lot like saying "sync up to the | 144 // In version control terms, this is a lot like saying "sync up to the |
145 // commit where we wrote once->done = true". | 145 // commit where we wrote once->done = true". |
146 // | 146 // |
147 // The release barrier in sk_once_slow guaranteed that once->done = true | 147 // 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 | 148 // 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. | 149 // forcing ourselves to also wait until the effects of f(arg) are readble. |
150 acquire_barrier(); | 150 acquire_barrier(); |
151 } | 151 } |
152 | 152 |
153 #undef ANNOTATE_BENIGN_RACE | 153 #undef ANNOTATE_BENIGN_RACE |
154 | 154 |
155 #endif // SkOnce_DEFINED | 155 #endif // SkOnce_DEFINED |
OLD | NEW |