2015-09-04 17:26:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkOncePtr_DEFINED
|
|
|
|
#define SkOncePtr_DEFINED
|
|
|
|
|
2015-09-28 18:24:13 +00:00
|
|
|
#include "../private/SkAtomics.h"
|
2016-01-25 03:49:24 +00:00
|
|
|
#include "SkUniquePtr.h"
|
2015-09-04 17:26:27 +00:00
|
|
|
|
2015-09-09 16:09:53 +00:00
|
|
|
template <typename T> class SkBaseOncePtr;
|
2015-09-04 17:26:27 +00:00
|
|
|
|
|
|
|
// Use this to create a global static pointer that's intialized exactly once when you call get().
|
2015-09-24 14:34:49 +00:00
|
|
|
#define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr<type> name;
|
2015-09-04 17:26:27 +00:00
|
|
|
|
|
|
|
// Use this for a local or member pointer that's initialized exactly once when you call get().
|
2016-01-25 03:49:24 +00:00
|
|
|
template <typename T, typename Delete = skstd::default_delete<T>>
|
2015-09-04 17:26:27 +00:00
|
|
|
class SkOncePtr : SkNoncopyable {
|
|
|
|
public:
|
|
|
|
SkOncePtr() { sk_bzero(this, sizeof(*this)); }
|
2015-09-09 16:09:53 +00:00
|
|
|
~SkOncePtr() {
|
|
|
|
if (T* ptr = (T*)*this) {
|
|
|
|
Delete()(ptr);
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 17:26:27 +00:00
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
T* get(const F& f) const {
|
|
|
|
return fOnce.get(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T*() const {
|
|
|
|
return (T*)fOnce;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-09-09 16:09:53 +00:00
|
|
|
SkBaseOncePtr<T> fOnce;
|
2015-09-04 17:26:27 +00:00
|
|
|
};
|
|
|
|
|
2015-09-09 17:00:22 +00:00
|
|
|
// If you ask for SkOncePtr<T[]>, we'll clean up with delete[] by default.
|
|
|
|
template <typename T>
|
2016-01-25 03:49:24 +00:00
|
|
|
class SkOncePtr<T[]> : public SkOncePtr<T, skstd::default_delete<T[]>> {};
|
2015-09-09 17:00:22 +00:00
|
|
|
|
2015-09-04 17:26:27 +00:00
|
|
|
/* TODO(mtklein): in next CL
|
2015-09-09 16:09:53 +00:00
|
|
|
typedef SkBaseOncePtr<void> SkOnceFlag;
|
2015-09-04 17:26:27 +00:00
|
|
|
#define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
inline void SkOnce(SkOnceFlag* once, const F& f) {
|
|
|
|
once->get([&]{ f(); return (void*)2; });
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Implementation details below here! No peeking!
|
|
|
|
|
|
|
|
template <typename T>
|
2015-09-09 16:09:53 +00:00
|
|
|
class SkBaseOncePtr {
|
2015-09-04 17:26:27 +00:00
|
|
|
public:
|
|
|
|
template <typename F>
|
|
|
|
T* get(const F& f) const {
|
2015-09-24 14:34:49 +00:00
|
|
|
uintptr_t state = sk_atomic_load(&fState, sk_memory_order_acquire);
|
2015-09-04 17:26:27 +00:00
|
|
|
if (state < 2) {
|
|
|
|
if (state == 0) {
|
|
|
|
// It looks like no one has tried to create our pointer yet.
|
|
|
|
// We try to claim that task by atomically swapping our state from '0' to '1'.
|
2015-09-24 14:34:49 +00:00
|
|
|
if (sk_atomic_compare_exchange(
|
|
|
|
&fState, &state, (uintptr_t)1, sk_memory_order_relaxed, sk_memory_order_relaxed)) {
|
2015-09-04 17:26:27 +00:00
|
|
|
// We've claimed it. Create our pointer and store it into fState.
|
|
|
|
state = (uintptr_t)f();
|
|
|
|
SkASSERT(state > 1);
|
2015-09-24 14:34:49 +00:00
|
|
|
sk_atomic_store(&fState, state, sk_memory_order_release);
|
2015-09-04 17:26:27 +00:00
|
|
|
} else {
|
|
|
|
// Someone else claimed it.
|
|
|
|
// We fall through to the spin loop just below to wait for them to finish.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (state == 1) {
|
|
|
|
// State '1' is our busy-but-not-done state.
|
|
|
|
// Some other thread has claimed the job of creating our pointer.
|
|
|
|
// We just need to wait for it to finish.
|
2015-09-24 14:34:49 +00:00
|
|
|
state = sk_atomic_load(&fState, sk_memory_order_acquire);
|
2015-09-04 17:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We shouldn't be able to get here without having created our pointer.
|
|
|
|
SkASSERT(state > 1);
|
|
|
|
}
|
|
|
|
return (T*)state;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T*() const {
|
2015-09-24 14:34:49 +00:00
|
|
|
auto state = sk_atomic_load(&fState, sk_memory_order_acquire);
|
2015-09-04 17:26:27 +00:00
|
|
|
return state < 2 ? nullptr : (T*)state;
|
|
|
|
// TODO: If state == 1 spin until it's not?
|
|
|
|
}
|
|
|
|
|
|
|
|
// fState == 0 --> we have not created our ptr yet
|
|
|
|
// fState == 1 --> someone is in the middle of creating our ptr
|
|
|
|
// else --> (T*)fState is our ptr
|
2015-09-24 14:34:49 +00:00
|
|
|
mutable uintptr_t fState;
|
2015-09-04 17:26:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif//SkOncePtr_DEFINED
|