1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/core/SkOnce.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,179 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkOnce_DEFINED 1.12 +#define SkOnce_DEFINED 1.13 + 1.14 +// SkOnce.h defines SK_DECLARE_STATIC_ONCE and SkOnce(), which you can use 1.15 +// together to create a threadsafe way to call a function just once. This 1.16 +// is particularly useful for lazy singleton initialization. E.g. 1.17 +// 1.18 +// static void set_up_my_singleton(Singleton** singleton) { 1.19 +// *singleton = new Singleton(...); 1.20 +// } 1.21 +// ... 1.22 +// const Singleton& GetSingleton() { 1.23 +// static Singleton* singleton = NULL; 1.24 +// SK_DECLARE_STATIC_ONCE(once); 1.25 +// SkOnce(&once, set_up_my_singleton, &singleton); 1.26 +// SkASSERT(NULL != singleton); 1.27 +// return *singleton; 1.28 +// } 1.29 +// 1.30 +// OnceTest.cpp also should serve as a few other simple examples. 1.31 +// 1.32 +// You may optionally pass SkOnce a second function to be called at exit for cleanup. 1.33 + 1.34 +#include "SkDynamicAnnotations.h" 1.35 +#include "SkThread.h" 1.36 +#include "SkTypes.h" 1.37 + 1.38 +#define SK_ONCE_INIT { false, { 0, SkDEBUGCODE(0) } } 1.39 +#define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name = SK_ONCE_INIT 1.40 + 1.41 +struct SkOnceFlag; // If manually created, initialize with SkOnceFlag once = SK_ONCE_INIT 1.42 + 1.43 +template <typename Func, typename Arg> 1.44 +inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)() = NULL); 1.45 + 1.46 +// If you've already got a lock and a flag to use, this variant lets you avoid an extra SkOnceFlag. 1.47 +template <typename Lock, typename Func, typename Arg> 1.48 +inline void SkOnce(bool* done, Lock* lock, Func f, Arg arg, void(*atExit)() = NULL); 1.49 + 1.50 +// ---------------------- Implementation details below here. ----------------------------- 1.51 + 1.52 +// This is POD and must be zero-initialized. 1.53 +struct SkSpinlock { 1.54 + void acquire() { 1.55 + SkASSERT(shouldBeZero == 0); 1.56 + // No memory barrier needed, but sk_atomic_cas gives us at least release anyway. 1.57 + while (!sk_atomic_cas(&thisIsPrivate, 0, 1)) { 1.58 + // spin 1.59 + } 1.60 + } 1.61 + 1.62 + void release() { 1.63 + SkASSERT(shouldBeZero == 0); 1.64 + // This requires a release memory barrier before storing, which sk_atomic_cas guarantees. 1.65 + SkAssertResult(sk_atomic_cas(&thisIsPrivate, 1, 0)); 1.66 + } 1.67 + 1.68 + int32_t thisIsPrivate; 1.69 + SkDEBUGCODE(int32_t shouldBeZero;) 1.70 +}; 1.71 + 1.72 +struct SkOnceFlag { 1.73 + bool done; 1.74 + SkSpinlock lock; 1.75 +}; 1.76 + 1.77 +// TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands. 1.78 + 1.79 +#ifdef SK_BUILD_FOR_WIN 1.80 +# include <intrin.h> 1.81 +inline static void compiler_barrier() { 1.82 + _ReadWriteBarrier(); 1.83 +} 1.84 +#else 1.85 +inline static void compiler_barrier() { 1.86 + asm volatile("" : : : "memory"); 1.87 +} 1.88 +#endif 1.89 + 1.90 +inline static void full_barrier_on_arm() { 1.91 +#ifdef SK_CPU_ARM 1.92 +# if SK_ARM_ARCH >= 7 1.93 + asm volatile("dmb" : : : "memory"); 1.94 +# else 1.95 + asm volatile("mcr p15, 0, %0, c7, c10, 5" : : "r" (0) : "memory"); 1.96 +# endif 1.97 +#endif 1.98 +} 1.99 + 1.100 +// On every platform, we issue a compiler barrier to prevent it from reordering 1.101 +// code. That's enough for platforms like x86 where release and acquire 1.102 +// barriers are no-ops. On other platforms we may need to be more careful; 1.103 +// ARM, in particular, needs real code for both acquire and release. We use a 1.104 +// full barrier, which acts as both, because that the finest precision ARM 1.105 +// provides. 1.106 + 1.107 +inline static void release_barrier() { 1.108 + compiler_barrier(); 1.109 + full_barrier_on_arm(); 1.110 +} 1.111 + 1.112 +inline static void acquire_barrier() { 1.113 + compiler_barrier(); 1.114 + full_barrier_on_arm(); 1.115 +} 1.116 + 1.117 +// Works with SkSpinlock or SkMutex. 1.118 +template <typename Lock> 1.119 +class SkAutoLockAcquire { 1.120 +public: 1.121 + explicit SkAutoLockAcquire(Lock* lock) : fLock(lock) { fLock->acquire(); } 1.122 + ~SkAutoLockAcquire() { fLock->release(); } 1.123 +private: 1.124 + Lock* fLock; 1.125 +}; 1.126 + 1.127 +// We've pulled a pretty standard double-checked locking implementation apart 1.128 +// into its main fast path and a slow path that's called when we suspect the 1.129 +// one-time code hasn't run yet. 1.130 + 1.131 +// This is the guts of the code, called when we suspect the one-time code hasn't been run yet. 1.132 +// This should be rarely called, so we separate it from SkOnce and don't mark it as inline. 1.133 +// (We don't mind if this is an actual function call, but odds are it'll be inlined anyway.) 1.134 +template <typename Lock, typename Func, typename Arg> 1.135 +static void sk_once_slow(bool* done, Lock* lock, Func f, Arg arg, void (*atExit)()) { 1.136 + const SkAutoLockAcquire<Lock> locked(lock); 1.137 + if (!*done) { 1.138 + f(arg); 1.139 + if (atExit != NULL) { 1.140 + atexit(atExit); 1.141 + } 1.142 + // Also known as a store-store/load-store barrier, this makes sure that the writes 1.143 + // done before here---in particular, those done by calling f(arg)---are observable 1.144 + // before the writes after the line, *done = true. 1.145 + // 1.146 + // In version control terms this is like saying, "check in the work up 1.147 + // to and including f(arg), then check in *done=true as a subsequent change". 1.148 + // 1.149 + // We'll use this in the fast path to make sure f(arg)'s effects are 1.150 + // observable whenever we observe *done == true. 1.151 + release_barrier(); 1.152 + *done = true; 1.153 + } 1.154 +} 1.155 + 1.156 +// This is our fast path, called all the time. We do really want it to be inlined. 1.157 +template <typename Lock, typename Func, typename Arg> 1.158 +inline void SkOnce(bool* done, Lock* lock, Func f, Arg arg, void(*atExit)()) { 1.159 + if (!SK_ANNOTATE_UNPROTECTED_READ(*done)) { 1.160 + sk_once_slow(done, lock, f, arg, atExit); 1.161 + } 1.162 + // Also known as a load-load/load-store barrier, this acquire barrier makes 1.163 + // sure that anything we read from memory---in particular, memory written by 1.164 + // calling f(arg)---is at least as current as the value we read from once->done. 1.165 + // 1.166 + // In version control terms, this is a lot like saying "sync up to the 1.167 + // commit where we wrote once->done = true". 1.168 + // 1.169 + // The release barrier in sk_once_slow guaranteed that once->done = true 1.170 + // happens after f(arg), so by syncing to once->done = true here we're 1.171 + // forcing ourselves to also wait until the effects of f(arg) are readble. 1.172 + acquire_barrier(); 1.173 +} 1.174 + 1.175 +template <typename Func, typename Arg> 1.176 +inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)()) { 1.177 + return SkOnce(&once->done, &once->lock, f, arg, atExit); 1.178 +} 1.179 + 1.180 +#undef SK_ANNOTATE_BENIGN_RACE 1.181 + 1.182 +#endif // SkOnce_DEFINED