blob: 921c6a6534b83de2f95a97e836f5d71dba61bc2f [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"
mtklein5f939ab2016-03-16 10:28:35 -070012#include <memory>
mtklein714a7102015-09-04 10:26:27 -070013
mtklein714a7102015-09-04 10:26:27 -070014// Use this to create a global static pointer that's intialized exactly once when you call get().
herb7f0a3d72015-09-24 07:34:49 -070015#define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr<type> name;
mtklein714a7102015-09-04 10:26:27 -070016
mtklein714a7102015-09-04 10:26:27 -070017template <typename T>
mtklein6c59d802015-09-09 09:09:53 -070018class SkBaseOncePtr {
mtklein714a7102015-09-04 10:26:27 -070019public:
20 template <typename F>
21 T* get(const F& f) const {
herb7f0a3d72015-09-24 07:34:49 -070022 uintptr_t state = sk_atomic_load(&fState, sk_memory_order_acquire);
mtklein714a7102015-09-04 10:26:27 -070023 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'.
mtklein650f9e92016-04-20 13:49:15 -070027 // See SkOnce.h for why we use an acquire memory order here rather than relaxed.
herb7f0a3d72015-09-24 07:34:49 -070028 if (sk_atomic_compare_exchange(
mtklein650f9e92016-04-20 13:49:15 -070029 &fState, &state, (uintptr_t)1, sk_memory_order_acquire, sk_memory_order_acquire)) {
mtklein714a7102015-09-04 10:26:27 -070030 // We've claimed it. Create our pointer and store it into fState.
31 state = (uintptr_t)f();
32 SkASSERT(state > 1);
herb7f0a3d72015-09-24 07:34:49 -070033 sk_atomic_store(&fState, state, sk_memory_order_release);
mtklein714a7102015-09-04 10:26:27 -070034 } 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.
herb7f0a3d72015-09-24 07:34:49 -070044 state = sk_atomic_load(&fState, sk_memory_order_acquire);
mtklein714a7102015-09-04 10:26:27 -070045 }
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 {
herb7f0a3d72015-09-24 07:34:49 -070054 auto state = sk_atomic_load(&fState, sk_memory_order_acquire);
mtklein714a7102015-09-04 10:26:27 -070055 return state < 2 ? nullptr : (T*)state;
56 // TODO: If state == 1 spin until it's not?
57 }
58
mtklein714a7102015-09-04 10:26:27 -070059 // 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
herb7f0a3d72015-09-24 07:34:49 -070062 mutable uintptr_t fState;
mtklein714a7102015-09-04 10:26:27 -070063};
64
65#endif//SkOncePtr_DEFINED