diff options
author | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-05-28 21:43:59 +0000 |
---|---|---|
committer | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-05-28 21:43:59 +0000 |
commit | 81496fb21637cc8d2a2b45a790e0f9d6d6f769c4 (patch) | |
tree | 00e25ec3280a138d3964bc794f379f5dd5f0f915 | |
parent | 940aa237bb3acdeb23bec7bd416b4aecabb51103 (diff) |
Spin off just SkLazyFnPtr from 305513002.
The memory barrier in SkOnce is a perf regression for sk_mem{set,cpy} in
SkUtils on ARM. We can do a lot better for function pointers.
BUG=skia:
Committed: http://code.google.com/p/skia/source/detail?r=14929
R=bungeman@google.com, mtklein@google.com
Author: mtklein@chromium.org
Review URL: https://codereview.chromium.org/305753002
git-svn-id: http://skia.googlecode.com/svn/trunk@14938 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | src/core/SkLazyFnPtr.h | 58 | ||||
-rw-r--r-- | src/core/SkThreadPriv.h | 23 | ||||
-rw-r--r-- | src/core/SkUtils.cpp | 50 | ||||
-rw-r--r-- | src/ports/SkAtomics_sync.h | 6 | ||||
-rw-r--r-- | src/ports/SkAtomics_win.h | 4 |
5 files changed, 107 insertions, 34 deletions
diff --git a/src/core/SkLazyFnPtr.h b/src/core/SkLazyFnPtr.h new file mode 100644 index 0000000000..94594b4aeb --- /dev/null +++ b/src/core/SkLazyFnPtr.h @@ -0,0 +1,58 @@ +#ifndef SkLazyFnPtr_DEFINED +#define SkLazyFnPtr_DEFINED + +/** Declare a lazily-chosen static function pointer of type F. + * + * Example usage: + * + * typedef int (*FooImpl)(int, int); + * + * static FooImpl choose_foo() { return ... }; + * + * int Foo(int a, int b) { + * SK_DECLARE_STATIC_LAZY_FN_PTR(FooImpl, choice); + * return choice.get(choose_foo)(a, b); + * } + * + * You can think of SK_DECLARE_STATIC_LAZY_FN_PTR as a cheaper specialization of SkOnce. + * There is no mutex, and in the fast path, no memory barriers are issued. + * + * This must be used in a global or function scope, not as a class member. + */ +#define SK_DECLARE_STATIC_LAZY_FN_PTR(F, name) static Private::SkLazyFnPtr<F> name = { NULL } + + +// Everything below here is private implementation details. Don't touch, don't even look. + +#include "SkDynamicAnnotations.h" +#include "SkThreadPriv.h" + +namespace Private { + +// This has no constructor and is link-time initialized, so its members must be public. +template <typename F> +struct SkLazyFnPtr { + F get(F (*choose)()) { + // First, try reading to see if it's already set. + F fn = (F)SK_ANNOTATE_UNPROTECTED_READ(fPtr); + if (fn != NULL) { + return fn; + } + + // We think it's not already set. + fn = choose(); + + // No particular memory barriers needed; we're not guarding anything but the pointer itself. + F prev = (F)sk_atomic_cas(&fPtr, NULL, (void*)fn); + + // If prev != NULL, someone snuck in and set fPtr concurrently. + // If prev == NULL, we did write fn to fPtr. + return prev != NULL ? prev : fn; + } + + void* fPtr; +}; + +} // namespace Private + +#endif//SkLazyFnPtr_DEFINED diff --git a/src/core/SkThreadPriv.h b/src/core/SkThreadPriv.h new file mode 100644 index 0000000000..a972921606 --- /dev/null +++ b/src/core/SkThreadPriv.h @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkThreadPriv_DEFINED +#define SkThreadPriv_DEFINED + +#include "SkTypes.h" + +// SK_ATOMICS_PLATFORM_H must provide inline implementations for the following declarations. + +/** Atomic compare and set, for pointers. + * If *addr == before, set *addr to after. Always returns previous value of *addr. + * This must act as a compiler barrier. + */ +static void* sk_atomic_cas(void** addr, void* before, void* after); + +#include SK_ATOMICS_PLATFORM_H + +#endif//SkThreadPriv_DEFINED diff --git a/src/core/SkUtils.cpp b/src/core/SkUtils.cpp index ca18e0cb2d..591a198c65 100644 --- a/src/core/SkUtils.cpp +++ b/src/core/SkUtils.cpp @@ -8,7 +8,7 @@ #include "SkUtils.h" -#include "SkOnce.h" +#include "SkLazyFnPtr.h" #if 0 #define assign_16_longs(dst, value) \ @@ -113,52 +113,34 @@ static void sk_memcpy32_portable(uint32_t dst[], const uint32_t src[], int count memcpy(dst, src, count * sizeof(uint32_t)); } -static void choose_memset16(SkMemset16Proc* proc) { - *proc = SkMemset16GetPlatformProc(); - if (NULL == *proc) { - *proc = &sk_memset16_portable; - } +static SkMemset16Proc choose_memset16() { + SkMemset16Proc proc = SkMemset16GetPlatformProc(); + return proc ? proc : sk_memset16_portable; } void sk_memset16(uint16_t dst[], uint16_t value, int count) { - SK_DECLARE_STATIC_ONCE(once); - static SkMemset16Proc proc = NULL; - SkOnce(&once, choose_memset16, &proc); - SkASSERT(proc != NULL); - - return proc(dst, value, count); + SK_DECLARE_STATIC_LAZY_FN_PTR(SkMemset16Proc, choice); + return choice.get(choose_memset16)(dst, value, count); } -static void choose_memset32(SkMemset32Proc* proc) { - *proc = SkMemset32GetPlatformProc(); - if (NULL == *proc) { - *proc = &sk_memset32_portable; - } +static SkMemset32Proc choose_memset32() { + SkMemset32Proc proc = SkMemset32GetPlatformProc(); + return proc ? proc : sk_memset32_portable; } void sk_memset32(uint32_t dst[], uint32_t value, int count) { - SK_DECLARE_STATIC_ONCE(once); - static SkMemset32Proc proc = NULL; - SkOnce(&once, choose_memset32, &proc); - SkASSERT(proc != NULL); - - return proc(dst, value, count); + SK_DECLARE_STATIC_LAZY_FN_PTR(SkMemset32Proc, choice); + return choice.get(choose_memset32)(dst, value, count); } -static void choose_memcpy32(SkMemcpy32Proc* proc) { - *proc = SkMemcpy32GetPlatformProc(); - if (NULL == *proc) { - *proc = &sk_memcpy32_portable; - } +static SkMemcpy32Proc choose_memcpy32() { + SkMemcpy32Proc proc = SkMemcpy32GetPlatformProc(); + return proc ? proc : sk_memcpy32_portable; } void sk_memcpy32(uint32_t dst[], const uint32_t src[], int count) { - SK_DECLARE_STATIC_ONCE(once); - static SkMemcpy32Proc proc = NULL; - SkOnce(&once, choose_memcpy32, &proc); - SkASSERT(proc != NULL); - - return proc(dst, src, count); + SK_DECLARE_STATIC_LAZY_FN_PTR(SkMemcpy32Proc, choice); + return choice.get(choose_memcpy32)(dst, src, count); } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/ports/SkAtomics_sync.h b/src/ports/SkAtomics_sync.h index 45ba63f305..b0d17527f0 100644 --- a/src/ports/SkAtomics_sync.h +++ b/src/ports/SkAtomics_sync.h @@ -32,6 +32,12 @@ static inline __attribute__((always_inline)) bool sk_atomic_cas(int32_t* addr, return __sync_bool_compare_and_swap(addr, before, after); } +static inline __attribute__((always_inline)) void* sk_atomic_cas(void** addr, + void* before, + void* after) { + return __sync_val_compare_and_swap(addr, before, after); +} + static inline __attribute__((always_inline)) void sk_membar_acquire__after_atomic_conditional_inc() { } #endif diff --git a/src/ports/SkAtomics_win.h b/src/ports/SkAtomics_win.h index 7454d66055..16923947ef 100644 --- a/src/ports/SkAtomics_win.h +++ b/src/ports/SkAtomics_win.h @@ -40,6 +40,10 @@ static inline bool sk_atomic_cas(int32_t* addr, int32_t before, int32_t after) { return _InterlockedCompareExchange(reinterpret_cast<long*>(addr), after, before) == before; } +static inline void* sk_atomic_cas(void** addr, void* before, void* after) { + return InterlockedCompareExchangePointer(addr, after, before); +} + static inline void sk_membar_acquire__after_atomic_conditional_inc() { } #endif |