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 // |
| 29 // You may optionally pass SkOnce a second function to be called at exit for cle
anup. |
28 | 30 |
29 #include "SkThread.h" | 31 #include "SkThread.h" |
30 #include "SkTypes.h" | 32 #include "SkTypes.h" |
31 | 33 |
32 #define SK_ONCE_INIT { false, { 0, SkDEBUGCODE(0) } } | 34 #define SK_ONCE_INIT { false, { 0, SkDEBUGCODE(0) } } |
33 #define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name = SK_ONCE_INIT | 35 #define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name = SK_ONCE_INIT |
34 | 36 |
35 struct SkOnceFlag; // If manually created, initialize with SkOnceFlag once = SK
_ONCE_INIT | 37 struct SkOnceFlag; // If manually created, initialize with SkOnceFlag once = SK
_ONCE_INIT |
36 | 38 |
37 template <typename Func, typename Arg> | 39 template <typename Func, typename Arg> |
38 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg); | 40 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)() = NULL); |
39 | 41 |
40 // ---------------------- Implementation details below here. -----------------
------------ | 42 // ---------------------- Implementation details below here. -----------------
------------ |
41 | 43 |
42 struct SkOnceFlag { | 44 struct SkOnceFlag { |
43 bool done; | 45 bool done; |
44 SkSpinlock lock; | 46 SkSpinlock lock; |
45 }; | 47 }; |
46 | 48 |
47 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when
refactoring lands. | 49 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when
refactoring lands. |
48 | 50 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 } | 87 } |
86 | 88 |
87 // We've pulled a pretty standard double-checked locking implementation apart | 89 // We've pulled a pretty standard double-checked locking implementation apart |
88 // into its main fast path and a slow path that's called when we suspect the | 90 // into its main fast path and a slow path that's called when we suspect the |
89 // one-time code hasn't run yet. | 91 // one-time code hasn't run yet. |
90 | 92 |
91 // This is the guts of the code, called when we suspect the one-time code hasn't
been run yet. | 93 // This is the guts of the code, called when we suspect the one-time code hasn't
been run yet. |
92 // This should be rarely called, so we separate it from SkOnce and don't mark it
as inline. | 94 // This should be rarely called, so we separate it from SkOnce and don't mark it
as inline. |
93 // (We don't mind if this is an actual function call, but odds are it'll be inli
ned anyway.) | 95 // (We don't mind if this is an actual function call, but odds are it'll be inli
ned anyway.) |
94 template <typename Func, typename Arg> | 96 template <typename Func, typename Arg> |
95 static void sk_once_slow(SkOnceFlag* once, Func f, Arg arg) { | 97 static void sk_once_slow(SkOnceFlag* once, Func f, Arg arg, void (*atExit)()) { |
96 const SkAutoSpinlock lock(&once->lock); | 98 const SkAutoSpinlock lock(&once->lock); |
97 if (!once->done) { | 99 if (!once->done) { |
98 f(arg); | 100 f(arg); |
| 101 if (atExit != NULL) { |
| 102 atexit(atExit); |
| 103 } |
99 // 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 |
100 // done before here---in particular, those done by calling f(arg)---are
observable | 105 // done before here---in particular, those done by calling f(arg)---are
observable |
101 // before the writes after the line, *done = true. | 106 // before the writes after the line, *done = true. |
102 // | 107 // |
103 // 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 |
104 // to and including f(arg), then check in *done=true as a subsequent cha
nge". | 109 // to and including f(arg), then check in *done=true as a subsequent cha
nge". |
105 // | 110 // |
106 // We'll use this in the fast path to make sure f(arg)'s effects are | 111 // We'll use this in the fast path to make sure f(arg)'s effects are |
107 // observable whenever we observe *done == true. | 112 // observable whenever we observe *done == true. |
108 release_barrier(); | 113 release_barrier(); |
(...skipping 11 matching lines...) Expand all Loading... |
120 extern "C" { | 125 extern "C" { |
121 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); |
122 } | 127 } |
123 #define SK_ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__
, mem, desc) | 128 #define SK_ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__
, mem, desc) |
124 #else | 129 #else |
125 #define SK_ANNOTATE_BENIGN_RACE(mem, desc) | 130 #define SK_ANNOTATE_BENIGN_RACE(mem, desc) |
126 #endif | 131 #endif |
127 | 132 |
128 // 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. |
129 template <typename Func, typename Arg> | 134 template <typename Func, typename Arg> |
130 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg) { | 135 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)()) { |
131 SK_ANNOTATE_BENIGN_RACE(&(once->done), "Don't worry TSAN, we're sure this is
safe."); | 136 SK_ANNOTATE_BENIGN_RACE(&(once->done), "Don't worry TSAN, we're sure this is
safe."); |
132 if (!once->done) { | 137 if (!once->done) { |
133 sk_once_slow(once, f, arg); | 138 sk_once_slow(once, f, arg, atExit); |
134 } | 139 } |
135 // 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 |
136 // 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 |
137 // 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. |
138 // | 143 // |
139 // 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 |
140 // commit where we wrote once->done = true". | 145 // commit where we wrote once->done = true". |
141 // | 146 // |
142 // 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 |
143 // 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 |
144 // 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. |
145 acquire_barrier(); | 150 acquire_barrier(); |
146 } | 151 } |
147 | 152 |
148 #undef SK_ANNOTATE_BENIGN_RACE | 153 #undef SK_ANNOTATE_BENIGN_RACE |
149 | 154 |
150 #endif // SkOnce_DEFINED | 155 #endif // SkOnce_DEFINED |
OLD | NEW |