OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2014 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #ifndef SkLazyPtr_DEFINED | |
9 #define SkLazyPtr_DEFINED | |
10 | |
11 /** Declare a lazily-chosen static pointer (or array of pointers) of type T. | |
12 * | |
13 * Example usage: | |
14 * | |
15 * Foo* GetSingletonFoo() { | |
16 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton); // Created with new, destro
yed with delete. | |
17 * return singleton.get(); | |
18 * } | |
19 * | |
20 * These macros take an optional T* (*Create)() and void (*Destroy)(T*) at the
end. | |
21 * If not given, we'll use new and delete. | |
22 * These options are most useful when T doesn't have a public constructor or de
structor. | |
23 * Create comes first, so you may use a custom Create with a default Destroy, b
ut not vice versa. | |
24 * | |
25 * Foo* CustomCreate() { return ...; } | |
26 * void CustomDestroy(Foo* ptr) { ... } | |
27 * Foo* GetSingletonFooWithCustomCleanup() { | |
28 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton, CustomCreate, CustomDestroy); | |
29 * return singleton.get(); | |
30 * } | |
31 * | |
32 * If you have a bunch of related static pointers of the same type, you can | |
33 * declare an array of lazy pointers together, and we'll pass the index to Crea
te(). | |
34 * | |
35 * Foo* CreateFoo(int i) { return ...; } | |
36 * Foo* GetCachedFoo(Foo::Enum enumVal) { | |
37 * SK_DECLARE_STATIC_LAZY_PTR_ARRAY(Foo, Foo::kEnumCount, cachedFoos, Creat
eFoo); | |
38 * return cachedFoos[enumVal]; | |
39 * } | |
40 * | |
41 * | |
42 * You can think of SK_DECLARE_STATIC_LAZY_PTR as a cheaper specialization of | |
43 * SkOnce. There is no mutex or extra storage used past the pointer itself. | |
44 * | |
45 * We may call Create more than once, but all threads will see the same pointer | |
46 * returned from get(). Any extra calls to Create will be cleaned up. | |
47 * | |
48 * These macros must be used in a global scope, not in function scope or as a c
lass member. | |
49 */ | |
50 | |
51 #define SK_DECLARE_STATIC_LAZY_PTR(T, name, ...) \ | |
52 namespace {} static Private::SkStaticLazyPtr<T, ##__VA_ARGS__> name | |
53 | |
54 #define SK_DECLARE_STATIC_LAZY_PTR_ARRAY(T, name, N, ...) \ | |
55 namespace {} static Private::SkStaticLazyPtrArray<T, N, ##__VA_ARGS__> name | |
56 | |
57 // namespace {} forces these macros to only be legal in global scopes. Chrome h
as thread-safety | |
58 // problems with them in function-local statics because it uses -fno-threadsafe-
statics, and even | |
59 // in builds with threadsafe statics, those threadsafe statics are just unnecess
ary overhead. | |
60 | |
61 // Everything below here is private implementation details. Don't touch, don't
even look. | |
62 | |
63 #include "SkAtomics.h" | |
64 | |
65 // See FIXME below. | |
66 class SkFontConfigInterfaceDirect; | |
67 | |
68 namespace Private { | |
69 | |
70 // Set *dst to ptr if *dst is NULL. Returns value of *dst, destroying ptr if no
t swapped in. | |
71 // Issues acquire memory barrier on failure, release on success. | |
72 template <typename P, void (*Destroy)(P)> | |
73 static P try_cas(P* dst, P ptr) { | |
74 P prev = NULL; | |
75 if (sk_atomic_compare_exchange(dst, &prev, ptr, | |
76 sk_memory_order_release/*on success*/, | |
77 sk_memory_order_acquire/*on failure*/)) { | |
78 // We need a release barrier before returning ptr. The compare_exchange
provides it. | |
79 SkASSERT(!prev); | |
80 return ptr; | |
81 } else { | |
82 Destroy(ptr); | |
83 // We need an acquire barrier before returning prev. The compare_exchan
ge provided it. | |
84 SkASSERT(prev); | |
85 return prev; | |
86 } | |
87 } | |
88 | |
89 template <typename T> | |
90 T* sk_new() { | |
91 return new T; | |
92 } | |
93 template <typename T> | |
94 void sk_delete(T* ptr) { | |
95 delete ptr; | |
96 } | |
97 | |
98 // We're basing these implementations here on this article: | |
99 // http://preshing.com/20140709/the-purpose-of-memory_order_consume-in-cpp11/ | |
100 // | |
101 // Because the users of SkLazyPtr and SkLazyPtrArray will read the pointers | |
102 // _through_ our atomically set pointer, there is a data dependency between our | |
103 // atomic and the guarded data, and so we only need writer-releases / | |
104 // reader-consumes memory pairing rather than the more general write-releases / | |
105 // reader-acquires convention. | |
106 // | |
107 // This is nice, because a consume load is free on all our platforms: x86, | |
108 // ARM, MIPS. In contrast, an acquire load issues a memory barrier on non-x86. | |
109 | |
110 template <typename T> | |
111 T consume_load(T* ptr) { | |
112 #if defined(THREAD_SANITIZER) | |
113 // TSAN gets anxious if we don't tell it what we're actually doing, a consum
e load. | |
114 return sk_atomic_load(ptr, sk_memory_order_consume); | |
115 #else | |
116 // All current compilers blindly upgrade consume memory order to acquire mem
ory order. | |
117 // For our purposes, though, no memory barrier is required, so we lie and us
e relaxed. | |
118 return sk_atomic_load(ptr, sk_memory_order_relaxed); | |
119 #endif | |
120 } | |
121 | |
122 // This has no constructor and must be zero-initalized (the macro above does thi
s). | |
123 template <typename T, T* (*Create)() = sk_new<T>, void (*Destroy)(T*) = sk_delet
e<T> > | |
124 class SkStaticLazyPtr { | |
125 public: | |
126 T* get() { | |
127 // If fPtr has already been filled, we need a consume barrier when loadi
ng it. | |
128 // If not, we need a release barrier when setting it. try_cas will do t
hat. | |
129 T* ptr = consume_load(&fPtr); | |
130 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create()); | |
131 } | |
132 | |
133 private: | |
134 T* fPtr; | |
135 }; | |
136 | |
137 template <typename T> | |
138 T* sk_new_arg(int i) { | |
139 return new T(i); | |
140 } | |
141 | |
142 // This has no constructor and must be zero-initalized (the macro above does thi
s). | |
143 template <typename T, int N, T* (*Create)(int) = sk_new_arg<T>, void (*Destroy)(
T*) = sk_delete<T> > | |
144 class SkStaticLazyPtrArray { | |
145 public: | |
146 T* operator[](int i) { | |
147 SkASSERT(i >= 0 && i < N); | |
148 // If fPtr has already been filled, we need an consume barrier when load
ing it. | |
149 // If not, we need a release barrier when setting it. try_cas will do t
hat. | |
150 T* ptr = consume_load(&fArray[i]); | |
151 return ptr ? ptr : try_cas<T*, Destroy>(&fArray[i], Create(i)); | |
152 } | |
153 | |
154 private: | |
155 T* fArray[N]; | |
156 }; | |
157 | |
158 } // namespace Private | |
159 | |
160 // This version is suitable for use as a class member. | |
161 // It's much the same as above except: | |
162 // - it has a constructor to zero itself; | |
163 // - it has a destructor to clean up; | |
164 // - get() calls SkNew(T) to create the pointer; | |
165 // - get(functor) calls functor to create the pointer. | |
166 template <typename T, void (*Destroy)(T*) = Private::sk_delete<T> > | |
167 class SkLazyPtr : SkNoncopyable { | |
168 public: | |
169 SkLazyPtr() : fPtr(NULL) {} | |
170 ~SkLazyPtr() { if (fPtr) { Destroy((T*)fPtr); } } | |
171 | |
172 T* get() const { | |
173 T* ptr = Private::consume_load(&fPtr); | |
174 return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, new T); | |
175 } | |
176 | |
177 template <typename Create> | |
178 T* get(const Create& create) const { | |
179 T* ptr = Private::consume_load(&fPtr); | |
180 return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, create()); | |
181 } | |
182 | |
183 private: | |
184 mutable T* fPtr; | |
185 }; | |
186 | |
187 | |
188 #endif//SkLazyPtr_DEFINED | |
OLD | NEW |