| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 SkOncePtr_DEFINED | 8 #ifndef SkOncePtr_DEFINED |
| 9 #define SkOncePtr_DEFINED | 9 #define SkOncePtr_DEFINED |
| 10 | 10 |
| 11 #include "SkAtomics.h" | 11 #include "SkAtomics.h" |
| 12 #include "SkUniquePtr.h" | |
| 13 | 12 |
| 14 template <typename T> class SkBaseOncePtr; | 13 template <typename T> class SkStaticOnce; |
| 15 | 14 |
| 16 // Use this to create a global static pointer that's intialized exactly once whe
n you call get(). | 15 // Use this to create a global static pointer that's intialized exactly once whe
n you call get(). |
| 17 #define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr
<type> name | 16 #define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkStaticOnce<
type> name |
| 18 | 17 |
| 19 // Use this for a local or member pointer that's initialized exactly once when y
ou call get(). | 18 // Use this for a local or member pointer that's initialized exactly once when y
ou call get(). |
| 20 template <typename T, typename Delete = skstd::default_delete<T>> | 19 template <typename T> |
| 21 class SkOncePtr : SkNoncopyable { | 20 class SkOncePtr : SkNoncopyable { |
| 22 public: | 21 public: |
| 23 SkOncePtr() { sk_bzero(this, sizeof(*this)); } | 22 SkOncePtr() { sk_bzero(this, sizeof(*this)); } |
| 24 ~SkOncePtr() { | 23 |
| 25 if (T* ptr = (T*)*this) { | 24 // SkOncePtr does not have a destructor and does not clean up the pointer.
But you may, e.g. |
| 26 Delete()(ptr); | 25 // delete (T*)fOncePtr; |
| 27 } | 26 // SkSafeUnref((T*)fOncePtr); |
| 28 } | 27 // etc. |
| 29 | 28 |
| 30 template <typename F> | 29 template <typename F> |
| 31 T* get(const F& f) const { | 30 T* get(const F& f) const { |
| 32 return fOnce.get(f); | 31 return fOnce.get(f); |
| 33 } | 32 } |
| 34 | 33 |
| 35 operator T*() const { | 34 operator T*() const { |
| 36 return (T*)fOnce; | 35 return (T*)fOnce; |
| 37 } | 36 } |
| 38 | 37 |
| 39 private: | 38 private: |
| 40 SkBaseOncePtr<T> fOnce; | 39 SkStaticOnce<T> fOnce; |
| 41 }; | 40 }; |
| 42 | 41 |
| 43 /* TODO(mtklein): in next CL | 42 /* TODO(mtklein): in next CL |
| 44 typedef SkBaseOncePtr<void> SkOnceFlag; | 43 typedef SkStaticOnce<void> SkOnceFlag; |
| 45 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name | 44 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name |
| 46 | 45 |
| 47 template <typename F> | 46 template <typename F> |
| 48 inline void SkOnce(SkOnceFlag* once, const F& f) { | 47 inline void SkOnce(SkOnceFlag* once, const F& f) { |
| 49 once->get([&]{ f(); return (void*)2; }); | 48 once->get([&]{ f(); return (void*)2; }); |
| 50 } | 49 } |
| 51 */ | 50 */ |
| 52 | 51 |
| 53 // Implementation details below here! No peeking! | 52 // Implementation details below here! No peeking! |
| 54 | 53 |
| 55 template <typename T> | 54 template <typename T> |
| 56 class SkBaseOncePtr { | 55 class SkStaticOnce { |
| 57 public: | 56 public: |
| 58 template <typename F> | 57 template <typename F> |
| 59 T* get(const F& f) const { | 58 T* get(const F& f) const { |
| 60 uintptr_t state = fState.load(sk_memory_order_acquire); | 59 uintptr_t state = fState.load(sk_memory_order_acquire); |
| 61 if (state < 2) { | 60 if (state < 2) { |
| 62 if (state == 0) { | 61 if (state == 0) { |
| 63 // It looks like no one has tried to create our pointer yet. | 62 // It looks like no one has tried to create our pointer yet. |
| 64 // We try to claim that task by atomically swapping our state fr
om '0' to '1'. | 63 // We try to claim that task by atomically swapping our state fr
om '0' to '1'. |
| 65 if (fState.compare_exchange(&state, 1, sk_memory_order_relaxed, | 64 if (fState.compare_exchange(&state, 1, sk_memory_order_relaxed, |
| 66 sk_memory_order_relaxed))
{ | 65 sk_memory_order_relaxed))
{ |
| (...skipping 27 matching lines...) Expand all Loading... |
| 94 } | 93 } |
| 95 | 94 |
| 96 private: | 95 private: |
| 97 // fState == 0 --> we have not created our ptr yet | 96 // fState == 0 --> we have not created our ptr yet |
| 98 // fState == 1 --> someone is in the middle of creating our ptr | 97 // fState == 1 --> someone is in the middle of creating our ptr |
| 99 // else --> (T*)fState is our ptr | 98 // else --> (T*)fState is our ptr |
| 100 mutable SkAtomic<uintptr_t> fState; | 99 mutable SkAtomic<uintptr_t> fState; |
| 101 }; | 100 }; |
| 102 | 101 |
| 103 #endif//SkOncePtr_DEFINED | 102 #endif//SkOncePtr_DEFINED |
| OLD | NEW |