mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 8 | #ifndef SkOncePtr_DEFINED |
| 9 | #define SkOncePtr_DEFINED |
| 10 | |
herb | e6e41a8 | 2015-09-28 11:24:13 -0700 | [diff] [blame] | 11 | #include "../private/SkAtomics.h" |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 12 | #include <memory> |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 13 | |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 14 | template <typename T> class SkBaseOncePtr; |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 15 | |
| 16 | // Use this to create a global static pointer that's intialized exactly once when you call get(). |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 17 | #define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr<type> name; |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 18 | |
| 19 | // Use this for a local or member pointer that's initialized exactly once when you call get(). |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 20 | template <typename T, typename Delete = std::default_delete<T>> |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 21 | class SkOncePtr : SkNoncopyable { |
| 22 | public: |
| 23 | SkOncePtr() { sk_bzero(this, sizeof(*this)); } |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 24 | ~SkOncePtr() { |
| 25 | if (T* ptr = (T*)*this) { |
| 26 | Delete()(ptr); |
| 27 | } |
| 28 | } |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 29 | |
| 30 | template <typename F> |
| 31 | T* get(const F& f) const { |
| 32 | return fOnce.get(f); |
| 33 | } |
| 34 | |
| 35 | operator T*() const { |
| 36 | return (T*)fOnce; |
| 37 | } |
| 38 | |
| 39 | private: |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 40 | SkBaseOncePtr<T> fOnce; |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
mtklein | 9a50174 | 2015-09-09 10:00:22 -0700 | [diff] [blame] | 43 | // If you ask for SkOncePtr<T[]>, we'll clean up with delete[] by default. |
| 44 | template <typename T> |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 45 | class SkOncePtr<T[]> : public SkOncePtr<T, std::default_delete<T[]>> {}; |
mtklein | 9a50174 | 2015-09-09 10:00:22 -0700 | [diff] [blame] | 46 | |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 47 | /* TODO(mtklein): in next CL |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 48 | typedef SkBaseOncePtr<void> SkOnceFlag; |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 49 | #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name |
| 50 | |
| 51 | template <typename F> |
| 52 | inline void SkOnce(SkOnceFlag* once, const F& f) { |
| 53 | once->get([&]{ f(); return (void*)2; }); |
| 54 | } |
| 55 | */ |
| 56 | |
| 57 | // Implementation details below here! No peeking! |
| 58 | |
| 59 | template <typename T> |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 60 | class SkBaseOncePtr { |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 61 | public: |
| 62 | template <typename F> |
| 63 | T* get(const F& f) const { |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 64 | uintptr_t state = sk_atomic_load(&fState, sk_memory_order_acquire); |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 65 | if (state < 2) { |
| 66 | if (state == 0) { |
| 67 | // It looks like no one has tried to create our pointer yet. |
| 68 | // We try to claim that task by atomically swapping our state from '0' to '1'. |
mtklein | 650f9e9 | 2016-04-20 13:49:15 -0700 | [diff] [blame^] | 69 | // See SkOnce.h for why we use an acquire memory order here rather than relaxed. |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 70 | if (sk_atomic_compare_exchange( |
mtklein | 650f9e9 | 2016-04-20 13:49:15 -0700 | [diff] [blame^] | 71 | &fState, &state, (uintptr_t)1, sk_memory_order_acquire, sk_memory_order_acquire)) { |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 72 | // We've claimed it. Create our pointer and store it into fState. |
| 73 | state = (uintptr_t)f(); |
| 74 | SkASSERT(state > 1); |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 75 | sk_atomic_store(&fState, state, sk_memory_order_release); |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 76 | } else { |
| 77 | // Someone else claimed it. |
| 78 | // We fall through to the spin loop just below to wait for them to finish. |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | while (state == 1) { |
| 83 | // State '1' is our busy-but-not-done state. |
| 84 | // Some other thread has claimed the job of creating our pointer. |
| 85 | // We just need to wait for it to finish. |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 86 | state = sk_atomic_load(&fState, sk_memory_order_acquire); |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // We shouldn't be able to get here without having created our pointer. |
| 90 | SkASSERT(state > 1); |
| 91 | } |
| 92 | return (T*)state; |
| 93 | } |
| 94 | |
| 95 | operator T*() const { |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 96 | auto state = sk_atomic_load(&fState, sk_memory_order_acquire); |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 97 | return state < 2 ? nullptr : (T*)state; |
| 98 | // TODO: If state == 1 spin until it's not? |
| 99 | } |
| 100 | |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 101 | // fState == 0 --> we have not created our ptr yet |
| 102 | // fState == 1 --> someone is in the middle of creating our ptr |
| 103 | // else --> (T*)fState is our ptr |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 104 | mutable uintptr_t fState; |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | #endif//SkOncePtr_DEFINED |