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 24 matching lines...) Expand all Loading... |
35 | 35 |
36 namespace Private { | 36 namespace Private { |
37 | 37 |
38 // 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). |
39 template <typename F, F (*Choose)()> | 39 template <typename F, F (*Choose)()> |
40 class SkLazyFnPtr { | 40 class SkLazyFnPtr { |
41 public: | 41 public: |
42 F get() { | 42 F get() { |
43 // First, try reading to see if it's already set. | 43 // First, try reading to see if it's already set. |
44 F fn = (F)sk_atomic_load(&fPtr, sk_memory_order_relaxed); | 44 F fn = (F)sk_atomic_load(&fPtr, sk_memory_order_relaxed); |
45 if (fn != NULL) { | 45 if (fn != nullptr) { |
46 return fn; | 46 return fn; |
47 } | 47 } |
48 | 48 |
49 // We think it's not already set. | 49 // We think it's not already set. |
50 fn = Choose(); | 50 fn = Choose(); |
51 | 51 |
52 // 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. |
53 F prev = (F)sk_atomic_cas(&fPtr, NULL, (void*)fn); | 53 F prev = (F)sk_atomic_cas(&fPtr, nullptr, (void*)fn); |
54 | 54 |
55 // If prev != NULL, someone snuck in and set fPtr concurrently. | 55 // If prev != nullptr, someone snuck in and set fPtr concurrently. |
56 // If prev == NULL, we did write fn to fPtr. | 56 // If prev == nullptr, we did write fn to fPtr. |
57 return prev != NULL ? prev : fn; | 57 return prev != nullptr ? prev : fn; |
58 } | 58 } |
59 | 59 |
60 private: | 60 private: |
61 void* fPtr; | 61 void* fPtr; |
62 }; | 62 }; |
63 | 63 |
64 } // namespace Private | 64 } // namespace Private |
65 | 65 |
66 #endif//SkLazyFnPtr_DEFINED | 66 #endif//SkLazyFnPtr_DEFINED |
OLD | NEW |