OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 SkLazyFnPtr_DEFINED | 8 #ifndef SkLazyFnPtr_DEFINED |
9 #define SkLazyFnPtr_DEFINED | 9 #define SkLazyFnPtr_DEFINED |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... |
24 * You can think of SK_DECLARE_STATIC_LAZY_FN_PTR as a cheaper specialization o
f SkOnce. | 24 * You can think of SK_DECLARE_STATIC_LAZY_FN_PTR as a cheaper specialization o
f SkOnce. |
25 * There is no mutex, and in the fast path, no memory barriers are issued. | 25 * There is no mutex, and in the fast path, no memory barriers are issued. |
26 * | 26 * |
27 * This must be used in a global or function scope, not as a class member. | 27 * This must be used in a global or function scope, not as a class member. |
28 */ | 28 */ |
29 #define SK_DECLARE_STATIC_LAZY_FN_PTR(F, name, Choose) static Private::SkLazyFnP
tr<F, Choose> name | 29 #define SK_DECLARE_STATIC_LAZY_FN_PTR(F, name, Choose) static Private::SkLazyFnP
tr<F, Choose> name |
30 | 30 |
31 | 31 |
32 // Everything below here is private implementation details. Don't touch, don't
even look. | 32 // Everything below here is private implementation details. Don't touch, don't
even look. |
33 | 33 |
34 #include "SkDynamicAnnotations.h" | 34 #include "SkAtomics.h" |
35 #include "SkThreadPriv.h" | |
36 | 35 |
37 namespace Private { | 36 namespace Private { |
38 | 37 |
39 // This has no constructor and must be zero-initialized (the macro above does th
is). | 38 // This has no constructor and must be zero-initialized (the macro above does th
is). |
40 template <typename F, F (*Choose)()> | 39 template <typename F, F (*Choose)()> |
41 class SkLazyFnPtr { | 40 class SkLazyFnPtr { |
42 public: | 41 public: |
43 F get() { | 42 F get() { |
44 // First, try reading to see if it's already set. | 43 // First, try reading to see if it's already set. |
45 F fn = (F)SK_ANNOTATE_UNPROTECTED_READ(fPtr); | 44 F fn = (F)sk_atomic_load(&fPtr, sk_memory_order_relaxed); |
46 if (fn != NULL) { | 45 if (fn != NULL) { |
47 return fn; | 46 return fn; |
48 } | 47 } |
49 | 48 |
50 // We think it's not already set. | 49 // We think it's not already set. |
51 fn = Choose(); | 50 fn = Choose(); |
52 | 51 |
53 // No particular memory barriers needed; we're not guarding anything but
the pointer itself. | 52 // No particular memory barriers needed; we're not guarding anything but
the pointer itself. |
54 F prev = (F)sk_atomic_cas(&fPtr, NULL, (void*)fn); | 53 F prev = (F)sk_atomic_cas(&fPtr, NULL, (void*)fn); |
55 | 54 |
56 // If prev != NULL, someone snuck in and set fPtr concurrently. | 55 // If prev != NULL, someone snuck in and set fPtr concurrently. |
57 // If prev == NULL, we did write fn to fPtr. | 56 // If prev == NULL, we did write fn to fPtr. |
58 return prev != NULL ? prev : fn; | 57 return prev != NULL ? prev : fn; |
59 } | 58 } |
60 | 59 |
61 private: | 60 private: |
62 void* fPtr; | 61 void* fPtr; |
63 }; | 62 }; |
64 | 63 |
65 } // namespace Private | 64 } // namespace Private |
66 | 65 |
67 #endif//SkLazyFnPtr_DEFINED | 66 #endif//SkLazyFnPtr_DEFINED |
OLD | NEW |