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 |
34 | 16 struct SkOnceFlag { |
35 // Pass a unique name (at least in this scope) for name, and a type and name | 17 SkBaseMutex mutex; |
36 // for arg (as if writing a function declaration). | 18 bool done; |
37 // E.g. | 19 }; |
38 // DEF_SK_ONCE(my_onetime_setup, int* foo) { | 20 #define SK_DECLARE_STATIC_ONCE(name) \ |
39 // *foo += 5; | 21 static SkOnceFlag name = { { PTHREAD_MUTEX_INITIALIZER }, false } |
bungeman-skia
2013/10/22 21:06:11
This doesn't work on Windows?
mtklein
2013/10/22 21:08:24
Does SK_DECLARE_STATIC_MUTEX not work on Windows e
mtklein
2013/10/22 21:22:08
Figure this is the way to go now? How does the mu
| |
40 // } | |
41 #define DEF_SK_ONCE(name, arg) \ | |
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 | 22 |
57 // ---------------------- Implementation details below here. ----------------- ------------ | 23 // ---------------------- Implementation details below here. ----------------- ------------ |
58 | 24 |
59 | 25 |
60 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands. | 26 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands. |
61 | 27 |
62 #ifdef SK_BUILD_FOR_WIN | 28 #ifdef SK_BUILD_FOR_WIN |
63 #include <intrin.h> | 29 #include <intrin.h> |
64 inline static void compiler_barrier() { | 30 inline static void compiler_barrier() { |
65 _ReadWriteBarrier(); | 31 _ReadWriteBarrier(); |
(...skipping 25 matching lines...) Expand all Loading... | |
91 inline static void acquire_barrier() { | 57 inline static void acquire_barrier() { |
92 compiler_barrier(); | 58 compiler_barrier(); |
93 full_barrier_on_arm(); | 59 full_barrier_on_arm(); |
94 } | 60 } |
95 | 61 |
96 // We've pulled a pretty standard double-checked locking implementation apart | 62 // 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 | 63 // into its main fast path and a slow path that's called when we suspect the |
98 // one-time code hasn't run yet. | 64 // one-time code hasn't run yet. |
99 | 65 |
100 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet. | 66 // 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. | 67 // 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.) | 68 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.) |
103 template <typename Arg> | 69 template <typename Arg> |
104 static void sk_once_slow(bool* done, SkBaseMutex* mutex, void (*once)(Arg), Arg arg) { | 70 static void sk_once_slow(SkOnceFlag* once, void (*f)(Arg), Arg arg) { |
105 const SkAutoMutexAcquire lock(*mutex); | 71 const SkAutoMutexAcquire lock(once->mutex); |
106 if (!*done) { | 72 if (!once->done) { |
107 once(arg); | 73 f(arg); |
108 // Also known as a store-store/load-store barrier, this makes sure that the writes | 74 // 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 | 75 // done before here---in particular, those done by calling once(arg)---a re observable |
110 // before the writes after the line, *done = true. | 76 // before the writes after the line, *done = true. |
111 // | 77 // |
112 // In version control terms this is like saying, "check in the work up | 78 // 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". | 79 // to and including once(arg), then check in *done=true as a subsequent change". |
114 // | 80 // |
115 // We'll use this in the fast path to make sure once(arg)'s effects are | 81 // We'll use this in the fast path to make sure once(arg)'s effects are |
116 // observable whenever we observe *done == true. | 82 // observable whenever we observe *done == true. |
117 release_barrier(); | 83 release_barrier(); |
118 *done = true; | 84 once->done = true; |
119 } | 85 } |
120 } | 86 } |
121 | 87 |
122 // We nabbed this code from the dynamic_annotations library, and in their honor | 88 // 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 | 89 // 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 | 90 // ANNOTATE_BENIGN_RACE, it might make sense to pull that in as a dependency |
125 // rather than continue to reproduce it here. | 91 // rather than continue to reproduce it here. |
126 | 92 |
127 #if DYNAMIC_ANNOTATIONS_ENABLED | 93 #if DYNAMIC_ANNOTATIONS_ENABLED |
128 // TSAN provides this hook to supress a known-safe apparent race. | 94 // TSAN provides this hook to supress a known-safe apparent race. |
129 extern "C" { | 95 extern "C" { |
130 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co nst char* desc); | 96 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co nst char* desc); |
131 } | 97 } |
132 #define ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__, m em, desc) | 98 #define ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__, m em, desc) |
133 #else | 99 #else |
134 #define ANNOTATE_BENIGN_RACE(mem, desc) | 100 #define ANNOTATE_BENIGN_RACE(mem, desc) |
135 #endif | 101 #endif |
136 | 102 |
137 // This is our fast path, called all the time. We do really want it to be inlin ed. | 103 // This is our fast path, called all the time. We do really want it to be inlin ed. |
138 template <typename Arg> | 104 template <typename Arg> |
139 inline static void sk_once(bool* done, SkBaseMutex* mutex, void (*once)(Arg), Ar g arg) { | 105 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."); | 106 ANNOTATE_BENIGN_RACE(once->done, "Don't worry TSAN, we're sure this is safe. "); |
141 if (!*done) { | 107 if (!once->done) { |
142 sk_once_slow(done, mutex, once, arg); | 108 sk_once_slow(once, f, arg); |
143 } | 109 } |
144 // Also known as a load-load/load-store barrier, this acquire barrier makes | 110 // 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 | 111 // 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 . | 112 // calling f(arg)---is at least as current as the value we read from once->d one. |
147 // | 113 // |
148 // In version control terms, this is a lot like saying "sync up to the | 114 // In version control terms, this is a lot like saying "sync up to the |
149 // commit where we wrote *done = true". | 115 // commit where we wrote once->done = true". |
150 // | 116 // |
151 // The release barrier in sk_once_slow guaranteed that *done = true | 117 // 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 | 118 // 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 . | 119 // forcing ourselves to also wait until the effects of f(arg) are readble. |
154 acquire_barrier(); | 120 acquire_barrier(); |
155 } | 121 } |
156 | 122 |
157 #undef ANNOTATE_BENIGN_RACE | 123 #undef ANNOTATE_BENIGN_RACE |
158 | 124 |
159 | |
160 #endif // SkOnce_DEFINED | 125 #endif // SkOnce_DEFINED |
OLD | NEW |