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 | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 14 | // 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] | 15 | #define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr<type> name; |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 16 | |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 17 | template <typename T> |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 18 | class SkBaseOncePtr { |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 19 | public: |
| 20 | template <typename F> |
| 21 | T* get(const F& f) const { |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 22 | uintptr_t state = sk_atomic_load(&fState, sk_memory_order_acquire); |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 23 | if (state < 2) { |
| 24 | if (state == 0) { |
| 25 | // It looks like no one has tried to create our pointer yet. |
| 26 | // 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] | 27 | // 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] | 28 | if (sk_atomic_compare_exchange( |
mtklein | 650f9e9 | 2016-04-20 13:49:15 -0700 | [diff] [blame] | 29 | &fState, &state, (uintptr_t)1, sk_memory_order_acquire, sk_memory_order_acquire)) { |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 30 | // We've claimed it. Create our pointer and store it into fState. |
| 31 | state = (uintptr_t)f(); |
| 32 | SkASSERT(state > 1); |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 33 | sk_atomic_store(&fState, state, sk_memory_order_release); |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 34 | } else { |
| 35 | // Someone else claimed it. |
| 36 | // We fall through to the spin loop just below to wait for them to finish. |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | while (state == 1) { |
| 41 | // State '1' is our busy-but-not-done state. |
| 42 | // Some other thread has claimed the job of creating our pointer. |
| 43 | // We just need to wait for it to finish. |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 44 | state = sk_atomic_load(&fState, sk_memory_order_acquire); |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // We shouldn't be able to get here without having created our pointer. |
| 48 | SkASSERT(state > 1); |
| 49 | } |
| 50 | return (T*)state; |
| 51 | } |
| 52 | |
| 53 | operator T*() const { |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 54 | auto state = sk_atomic_load(&fState, sk_memory_order_acquire); |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 55 | return state < 2 ? nullptr : (T*)state; |
| 56 | // TODO: If state == 1 spin until it's not? |
| 57 | } |
| 58 | |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 59 | // fState == 0 --> we have not created our ptr yet |
| 60 | // fState == 1 --> someone is in the middle of creating our ptr |
| 61 | // else --> (T*)fState is our ptr |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 62 | mutable uintptr_t fState; |
mtklein | 714a710 | 2015-09-04 10:26:27 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | #endif//SkOncePtr_DEFINED |