| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 SkOnce_DEFINED | 8 #ifndef SkOnce_DEFINED |
| 9 #define SkOnce_DEFINED | 9 #define SkOnce_DEFINED |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // ... | 21 // ... |
| 22 // void EnsureRegistered() { | 22 // void EnsureRegistered() { |
| 23 // SK_DECLARE_STATIC_ONCE(once); | 23 // SK_DECLARE_STATIC_ONCE(once); |
| 24 // SkOnce(&once, register_my_stuff, GetGlobalRegistry()); | 24 // SkOnce(&once, register_my_stuff, GetGlobalRegistry()); |
| 25 // } | 25 // } |
| 26 // | 26 // |
| 27 // No matter how many times you call EnsureRegistered(), register_my_stuff will
be called just once. | 27 // No matter how many times you call EnsureRegistered(), register_my_stuff will
be called just once. |
| 28 // OnceTest.cpp also should serve as a few other simple examples. | 28 // OnceTest.cpp also should serve as a few other simple examples. |
| 29 | 29 |
| 30 #include "SkAtomics.h" | 30 #include "SkAtomics.h" |
| 31 #include "SkSpinlock.h" |
| 31 | 32 |
| 32 // This must be used in a global scope, not in fuction scope or as a class membe
r. | 33 // This must be used in a global scope, not in fuction scope or as a class membe
r. |
| 33 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name | 34 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name |
| 34 | 35 |
| 35 class SkOnceFlag; | 36 class SkOnceFlag; |
| 36 | 37 |
| 37 inline void SkOnce(SkOnceFlag* once, void (*f)()); | 38 inline void SkOnce(SkOnceFlag* once, void (*f)()); |
| 38 | 39 |
| 39 template <typename Arg> | 40 template <typename Arg> |
| 40 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg); | 41 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg); |
| 41 | 42 |
| 42 // If you've already got a lock and a flag to use, this variant lets you avoid a
n extra SkOnceFlag. | 43 // If you've already got a lock and a flag to use, this variant lets you avoid a
n extra SkOnceFlag. |
| 43 template <typename Lock> | 44 template <typename Lock> |
| 44 inline void SkOnce(bool* done, Lock* lock, void (*f)()); | 45 inline void SkOnce(bool* done, Lock* lock, void (*f)()); |
| 45 | 46 |
| 46 template <typename Lock, typename Arg> | 47 template <typename Lock, typename Arg> |
| 47 inline void SkOnce(bool* done, Lock* lock, void (*f)(Arg), Arg arg); | 48 inline void SkOnce(bool* done, Lock* lock, void (*f)(Arg), Arg arg); |
| 48 | 49 |
| 49 // ---------------------- Implementation details below here. -----------------
------------ | 50 // ---------------------- Implementation details below here. -----------------
------------ |
| 50 | 51 |
| 51 // This class has no constructor and must be zero-initialized (the macro above d
oes this). | 52 // This class has no constructor and must be zero-initialized (the macro above d
oes this). |
| 52 class SkOnceFlag { | 53 class SkOnceFlag { |
| 53 public: | 54 public: |
| 54 bool* mutableDone() { return &fDone; } | 55 bool* mutableDone() { return &fDone; } |
| 55 | 56 |
| 56 void acquire() { | 57 void acquire() { fSpinlock.acquire(); } |
| 57 // To act as a mutex, this needs an acquire barrier on success. | 58 void release() { fSpinlock.release(); } |
| 58 // sk_atomic_cas doesn't guarantee this ... | |
| 59 while (!sk_atomic_cas(&fSpinlock, 0, 1)) { | |
| 60 // spin | |
| 61 } | |
| 62 // ... so make sure to issue one of our own. | |
| 63 SkAssertResult(sk_acquire_load(&fSpinlock)); | |
| 64 } | |
| 65 | |
| 66 void release() { | |
| 67 // To act as a mutex, this needs a release barrier. sk_atomic_cas guara
ntees this. | |
| 68 SkAssertResult(sk_atomic_cas(&fSpinlock, 1, 0)); | |
| 69 } | |
| 70 | 59 |
| 71 private: | 60 private: |
| 72 bool fDone; | 61 bool fDone; |
| 73 int32_t fSpinlock; | 62 SkPODSpinlock fSpinlock; |
| 74 }; | 63 }; |
| 75 | 64 |
| 76 // We've pulled a pretty standard double-checked locking implementation apart | 65 // We've pulled a pretty standard double-checked locking implementation apart |
| 77 // into its main fast path and a slow path that's called when we suspect the | 66 // into its main fast path and a slow path that's called when we suspect the |
| 78 // one-time code hasn't run yet. | 67 // one-time code hasn't run yet. |
| 79 | 68 |
| 80 // This is the guts of the code, called when we suspect the one-time code hasn't
been run yet. | 69 // This is the guts of the code, called when we suspect the one-time code hasn't
been run yet. |
| 81 // This should be rarely called, so we separate it from SkOnce and don't mark it
as inline. | 70 // This should be rarely called, so we separate it from SkOnce and don't mark it
as inline. |
| 82 // (We don't mind if this is an actual function call, but odds are it'll be inli
ned anyway.) | 71 // (We don't mind if this is an actual function call, but odds are it'll be inli
ned anyway.) |
| 83 template <typename Lock, typename Arg> | 72 template <typename Lock, typename Arg> |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 inline void SkOnce(SkOnceFlag* once, void (*func)()) { | 130 inline void SkOnce(SkOnceFlag* once, void (*func)()) { |
| 142 return SkOnce(once, sk_once_no_arg_adaptor, func); | 131 return SkOnce(once, sk_once_no_arg_adaptor, func); |
| 143 } | 132 } |
| 144 | 133 |
| 145 template <typename Lock> | 134 template <typename Lock> |
| 146 inline void SkOnce(bool* done, Lock* lock, void (*func)()) { | 135 inline void SkOnce(bool* done, Lock* lock, void (*func)()) { |
| 147 return SkOnce(done, lock, sk_once_no_arg_adaptor, func); | 136 return SkOnce(done, lock, sk_once_no_arg_adaptor, func); |
| 148 } | 137 } |
| 149 | 138 |
| 150 #endif // SkOnce_DEFINED | 139 #endif // SkOnce_DEFINED |
| OLD | NEW |