blob: 261f9a7ef4a9b871bb2d62ec9fdfef3d436aaf35 [file] [log] [blame]
mtklein714a7102015-09-04 10:26:27 -07001/*
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
herbe6e41a82015-09-28 11:24:13 -070011#include "../private/SkAtomics.h"
mtklein1138be42016-01-24 19:49:24 -080012#include "SkUniquePtr.h"
mtklein714a7102015-09-04 10:26:27 -070013
mtklein6c59d802015-09-09 09:09:53 -070014template <typename T> class SkBaseOncePtr;
mtklein714a7102015-09-04 10:26:27 -070015
16// Use this to create a global static pointer that's intialized exactly once when you call get().
herb7f0a3d72015-09-24 07:34:49 -070017#define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr<type> name;
mtklein714a7102015-09-04 10:26:27 -070018
19// Use this for a local or member pointer that's initialized exactly once when you call get().
mtklein1138be42016-01-24 19:49:24 -080020template <typename T, typename Delete = skstd::default_delete<T>>
mtklein714a7102015-09-04 10:26:27 -070021class SkOncePtr : SkNoncopyable {
22public:
23 SkOncePtr() { sk_bzero(this, sizeof(*this)); }
mtklein6c59d802015-09-09 09:09:53 -070024 ~SkOncePtr() {
25 if (T* ptr = (T*)*this) {
26 Delete()(ptr);
27 }
28 }
mtklein714a7102015-09-04 10:26:27 -070029
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
39private:
mtklein6c59d802015-09-09 09:09:53 -070040 SkBaseOncePtr<T> fOnce;
mtklein714a7102015-09-04 10:26:27 -070041};
42
mtklein9a501742015-09-09 10:00:22 -070043// If you ask for SkOncePtr<T[]>, we'll clean up with delete[] by default.
44template <typename T>
mtklein1138be42016-01-24 19:49:24 -080045class SkOncePtr<T[]> : public SkOncePtr<T, skstd::default_delete<T[]>> {};
mtklein9a501742015-09-09 10:00:22 -070046
mtklein714a7102015-09-04 10:26:27 -070047/* TODO(mtklein): in next CL
mtklein6c59d802015-09-09 09:09:53 -070048typedef SkBaseOncePtr<void> SkOnceFlag;
mtklein714a7102015-09-04 10:26:27 -070049#define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name
50
51template <typename F>
52inline 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
59template <typename T>
mtklein6c59d802015-09-09 09:09:53 -070060class SkBaseOncePtr {
mtklein714a7102015-09-04 10:26:27 -070061public:
62 template <typename F>
63 T* get(const F& f) const {
herb7f0a3d72015-09-24 07:34:49 -070064 uintptr_t state = sk_atomic_load(&fState, sk_memory_order_acquire);
mtklein714a7102015-09-04 10:26:27 -070065 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'.
herb7f0a3d72015-09-24 07:34:49 -070069 if (sk_atomic_compare_exchange(
70 &fState, &state, (uintptr_t)1, sk_memory_order_relaxed, sk_memory_order_relaxed)) {
mtklein714a7102015-09-04 10:26:27 -070071 // We've claimed it. Create our pointer and store it into fState.
72 state = (uintptr_t)f();
73 SkASSERT(state > 1);
herb7f0a3d72015-09-24 07:34:49 -070074 sk_atomic_store(&fState, state, sk_memory_order_release);
mtklein714a7102015-09-04 10:26:27 -070075 } else {
76 // Someone else claimed it.
77 // We fall through to the spin loop just below to wait for them to finish.
78 }
79 }
80
81 while (state == 1) {
82 // State '1' is our busy-but-not-done state.
83 // Some other thread has claimed the job of creating our pointer.
84 // We just need to wait for it to finish.
herb7f0a3d72015-09-24 07:34:49 -070085 state = sk_atomic_load(&fState, sk_memory_order_acquire);
mtklein714a7102015-09-04 10:26:27 -070086 }
87
88 // We shouldn't be able to get here without having created our pointer.
89 SkASSERT(state > 1);
90 }
91 return (T*)state;
92 }
93
94 operator T*() const {
herb7f0a3d72015-09-24 07:34:49 -070095 auto state = sk_atomic_load(&fState, sk_memory_order_acquire);
mtklein714a7102015-09-04 10:26:27 -070096 return state < 2 ? nullptr : (T*)state;
97 // TODO: If state == 1 spin until it's not?
98 }
99
mtklein714a7102015-09-04 10:26:27 -0700100 // fState == 0 --> we have not created our ptr yet
101 // fState == 1 --> someone is in the middle of creating our ptr
102 // else --> (T*)fState is our ptr
herb7f0a3d72015-09-24 07:34:49 -0700103 mutable uintptr_t fState;
mtklein714a7102015-09-04 10:26:27 -0700104};
105
106#endif//SkOncePtr_DEFINED