| OLD | NEW |
| 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 |
| 1 #ifndef SkLazyFnPtr_DEFINED | 8 #ifndef SkLazyFnPtr_DEFINED |
| 2 #define SkLazyFnPtr_DEFINED | 9 #define SkLazyFnPtr_DEFINED |
| 3 | 10 |
| 4 /** Declare a lazily-chosen static function pointer of type F. | 11 /** Declare a lazily-chosen static function pointer of type F. |
| 5 * | 12 * |
| 6 * Example usage: | 13 * Example usage: |
| 7 * | 14 * |
| 8 * typedef int (*FooImpl)(int, int); | 15 * typedef int (*FooImpl)(int, int); |
| 9 * | 16 * |
| 10 * static FooImpl choose_foo() { return ... }; | 17 * static FooImpl choose_foo() { return ... }; |
| 11 * | 18 * |
| 12 * int Foo(int a, int b) { | 19 * int Foo(int a, int b) { |
| 13 * SK_DECLARE_STATIC_LAZY_FN_PTR(FooImpl, choice); | 20 * SK_DECLARE_STATIC_LAZY_FN_PTR(FooImpl, foo, choose_foo); |
| 14 * return choice.get(choose_foo)(a, b); | 21 * return foo.get()(a, b); |
| 15 * } | 22 * } |
| 16 * | 23 * |
| 17 * 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. |
| 18 * 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. |
| 19 * | 26 * |
| 20 * 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. |
| 21 */ | 28 */ |
| 22 #define SK_DECLARE_STATIC_LAZY_FN_PTR(F, name) static Private::SkLazyFnPtr<F> na
me = { NULL } | 29 #define SK_DECLARE_STATIC_LAZY_FN_PTR(F, name, Choose) static Private::SkLazyFnP
tr<F, Choose> name |
| 23 | 30 |
| 24 | 31 |
| 25 // 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. |
| 26 | 33 |
| 27 #include "SkDynamicAnnotations.h" | 34 #include "SkDynamicAnnotations.h" |
| 28 #include "SkThreadPriv.h" | 35 #include "SkThreadPriv.h" |
| 29 | 36 |
| 30 namespace Private { | 37 namespace Private { |
| 31 | 38 |
| 32 // This has no constructor and is link-time initialized, so its members must be
public. | 39 // This has no constructor and must be zero-initialized (the macro above does th
is). |
| 33 template <typename F> | 40 template <typename F, F (*Choose)()> |
| 34 struct SkLazyFnPtr { | 41 class SkLazyFnPtr { |
| 35 F get(F (*choose)()) { | 42 public: |
| 43 F get() { |
| 36 // First, try reading to see if it's already set. | 44 // First, try reading to see if it's already set. |
| 37 F fn = (F)SK_ANNOTATE_UNPROTECTED_READ(fPtr); | 45 F fn = (F)SK_ANNOTATE_UNPROTECTED_READ(fPtr); |
| 38 if (fn != NULL) { | 46 if (fn != NULL) { |
| 39 return fn; | 47 return fn; |
| 40 } | 48 } |
| 41 | 49 |
| 42 // We think it's not already set. | 50 // We think it's not already set. |
| 43 fn = choose(); | 51 fn = Choose(); |
| 44 | 52 |
| 45 // No particular memory barriers needed; we're not guarding anything but
the pointer itself. | 53 // No particular memory barriers needed; we're not guarding anything but
the pointer itself. |
| 46 F prev = (F)sk_atomic_cas(&fPtr, NULL, (void*)fn); | 54 F prev = (F)sk_atomic_cas(&fPtr, NULL, (void*)fn); |
| 47 | 55 |
| 48 // If prev != NULL, someone snuck in and set fPtr concurrently. | 56 // If prev != NULL, someone snuck in and set fPtr concurrently. |
| 49 // If prev == NULL, we did write fn to fPtr. | 57 // If prev == NULL, we did write fn to fPtr. |
| 50 return prev != NULL ? prev : fn; | 58 return prev != NULL ? prev : fn; |
| 51 } | 59 } |
| 52 | 60 |
| 61 private: |
| 53 void* fPtr; | 62 void* fPtr; |
| 54 }; | 63 }; |
| 55 | 64 |
| 56 } // namespace Private | 65 } // namespace Private |
| 57 | 66 |
| 58 #endif//SkLazyFnPtr_DEFINED | 67 #endif//SkLazyFnPtr_DEFINED |
| OLD | NEW |