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 two macros, DEF_SK_ONCE and SK_ONCE. | 11 // TODO(mtklein): update |
12 // You can use these macros together to create a threadsafe block of code that | |
13 // runs at most once, no matter how many times you call it. This is | |
14 // particularly useful for lazy singleton initialization. E.g. | |
15 // | |
16 // DEF_SK_ONCE(set_up_my_singleton, SingletonType* singleton) { | |
17 // // Code in this block will run at most once. | |
18 // *singleton = new Singleton(...); | |
19 // } | |
20 // ... | |
21 // const Singleton& getSingleton() { | |
22 // static Singleton* singleton = NULL; | |
23 // // Always call SK_ONCE. It's very cheap to call after the first time. | |
24 // SK_ONCE(set_up_my_singleton, singleton); | |
25 // SkASSERT(NULL != singleton); | |
26 // return *singleton; | |
27 // } | |
28 // | |
29 // OnceTest.cpp also should serve as another simple example. | |
30 | 12 |
31 #include "SkThread.h" | 13 #include "SkThread.h" |
32 #include "SkTypes.h" | 14 #include "SkTypes.h" |
33 | 15 |
16 struct SkOnceFlag { | |
17 bool done; | |
18 SkBaseMutex mutex; | |
19 }; | |
34 | 20 |
35 // Pass a unique name (at least in this scope) for name, and a type and name | 21 #ifdef SK_USE_POSIX_THREADS |
36 // for arg (as if writing a function declaration). | 22 #define SK_DECLARE_STATIC_ONCE(name) \ |
37 // E.g. | 23 static SkOnceFlag name = { false, { PTHREAD_MUTEX_INITIALIZER } } |
38 // DEF_SK_ONCE(my_onetime_setup, int* foo) { | 24 #else |
39 // *foo += 5; | 25 #define SK_DECLARE_STATIC_ONCE(name) \ |
40 // } | 26 static SkOnceFlag name = { false } |
bungeman-skia
2013/10/22 21:38:59
There is no call to InitializeCriticalSection here
| |
41 #define DEF_SK_ONCE(name, arg) \ | 27 #endif |
42 static bool sk_once_##name##_done = false; \ | |
43 SK_DECLARE_STATIC_MUTEX(sk_once_##name##_mutex); \ | |
44 static void sk_once_##name##_function(arg) | |
45 | |
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 | |
48 // the actual value of the argument. | |
49 // E.g | |
50 // int foo = 0; | |
51 // SK_ONCE(my_onetime_setup, &foo); | |
52 // SkASSERT(5 == foo); | |
53 #define SK_ONCE(name, arg) \ | |
54 sk_once(&sk_once_##name##_done, &sk_once_##name##_mutex, sk_once_##name##_fu nction, arg) | |
55 | |
56 | 28 |
57 // ---------------------- Implementation details below here. ----------------- ------------ | 29 // ---------------------- Implementation details below here. ----------------- ------------ |
58 | 30 |
59 | 31 |
60 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands. | 32 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands. |
61 | 33 |
62 #ifdef SK_BUILD_FOR_WIN | 34 #ifdef SK_BUILD_FOR_WIN |
63 #include <intrin.h> | 35 #include <intrin.h> |
64 inline static void compiler_barrier() { | 36 inline static void compiler_barrier() { |
65 _ReadWriteBarrier(); | 37 _ReadWriteBarrier(); |
(...skipping 25 matching lines...) Expand all Loading... | |
91 inline static void acquire_barrier() { | 63 inline static void acquire_barrier() { |
92 compiler_barrier(); | 64 compiler_barrier(); |
93 full_barrier_on_arm(); | 65 full_barrier_on_arm(); |
94 } | 66 } |
95 | 67 |
96 // We've pulled a pretty standard double-checked locking implementation apart | 68 // 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 | 69 // into its main fast path and a slow path that's called when we suspect the |
98 // one-time code hasn't run yet. | 70 // one-time code hasn't run yet. |
99 | 71 |
100 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet. | 72 // 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 and don't mark i t as inline. | 73 // This should be rarely called, so we separate it from SkOnce and don't mark it as inline. |
102 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.) | 74 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.) |
103 template <typename Arg> | 75 template <typename Arg> |
104 static void sk_once_slow(bool* done, SkBaseMutex* mutex, void (*once)(Arg), Arg arg) { | 76 static void sk_once_slow(SkOnceFlag* once, void (*f)(Arg), Arg arg) { |
105 const SkAutoMutexAcquire lock(*mutex); | 77 const SkAutoMutexAcquire lock(once->mutex); |
106 if (!*done) { | 78 if (!once->done) { |
107 once(arg); | 79 f(arg); |
108 // Also known as a store-store/load-store barrier, this makes sure that the writes | 80 // 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 | 81 // done before here---in particular, those done by calling once(arg)---a re observable |
110 // before the writes after the line, *done = true. | 82 // before the writes after the line, *done = true. |
111 // | 83 // |
112 // In version control terms this is like saying, "check in the work up | 84 // 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". | 85 // to and including once(arg), then check in *done=true as a subsequent change". |
114 // | 86 // |
115 // We'll use this in the fast path to make sure once(arg)'s effects are | 87 // We'll use this in the fast path to make sure once(arg)'s effects are |
116 // observable whenever we observe *done == true. | 88 // observable whenever we observe *done == true. |
117 release_barrier(); | 89 release_barrier(); |
118 *done = true; | 90 once->done = true; |
119 } | 91 } |
120 } | 92 } |
121 | 93 |
122 // We nabbed this code from the dynamic_annotations library, and in their honor | 94 // We nabbed this code from the dynamic_annotations library, and in their honor |
123 // we check the same define. If you find yourself wanting more than just | 95 // we check the same define. If you find yourself wanting more than just |
124 // ANNOTATE_BENIGN_RACE, it might make sense to pull that in as a dependency | 96 // ANNOTATE_BENIGN_RACE, it might make sense to pull that in as a dependency |
125 // rather than continue to reproduce it here. | 97 // rather than continue to reproduce it here. |
126 | 98 |
127 #if DYNAMIC_ANNOTATIONS_ENABLED | 99 #if DYNAMIC_ANNOTATIONS_ENABLED |
128 // TSAN provides this hook to supress a known-safe apparent race. | 100 // TSAN provides this hook to supress a known-safe apparent race. |
129 extern "C" { | 101 extern "C" { |
130 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co nst char* desc); | 102 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co nst char* desc); |
131 } | 103 } |
132 #define ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__, m em, desc) | 104 #define ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__, m em, desc) |
133 #else | 105 #else |
134 #define ANNOTATE_BENIGN_RACE(mem, desc) | 106 #define ANNOTATE_BENIGN_RACE(mem, desc) |
135 #endif | 107 #endif |
136 | 108 |
137 // This is our fast path, called all the time. We do really want it to be inlin ed. | 109 // This is our fast path, called all the time. We do really want it to be inlin ed. |
138 template <typename Arg> | 110 template <typename Arg> |
139 inline static void sk_once(bool* done, SkBaseMutex* mutex, void (*once)(Arg), Ar g arg) { | 111 inline static void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg) { |
140 ANNOTATE_BENIGN_RACE(done, "Don't worry TSAN, we're sure this is safe."); | 112 ANNOTATE_BENIGN_RACE(once->done, "Don't worry TSAN, we're sure this is safe. "); |
141 if (!*done) { | 113 if (!once->done) { |
142 sk_once_slow(done, mutex, once, arg); | 114 sk_once_slow(once, f, arg); |
143 } | 115 } |
144 // Also known as a load-load/load-store barrier, this acquire barrier makes | 116 // 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 | 117 // 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 . | 118 // calling f(arg)---is at least as current as the value we read from once->d one. |
147 // | 119 // |
148 // In version control terms, this is a lot like saying "sync up to the | 120 // In version control terms, this is a lot like saying "sync up to the |
149 // commit where we wrote *done = true". | 121 // commit where we wrote once->done = true". |
150 // | 122 // |
151 // The release barrier in sk_once_slow guaranteed that *done = true | 123 // The release barrier in sk_once_slow guaranteed that once->done = true |
152 // happens after once(arg), so by syncing to *done = true here we're | 124 // happens after f(arg), so by syncing to once->done = true here we're |
153 // forcing ourselves to also wait until the effects of once(arg) are readble . | 125 // forcing ourselves to also wait until the effects of f(arg) are readble. |
154 acquire_barrier(); | 126 acquire_barrier(); |
155 } | 127 } |
156 | 128 |
157 #undef ANNOTATE_BENIGN_RACE | 129 #undef ANNOTATE_BENIGN_RACE |
158 | 130 |
159 | |
160 #endif // SkOnce_DEFINED | 131 #endif // SkOnce_DEFINED |
OLD | NEW |