Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
ResourceCache.hpp
1#pragma once
2
3#include <atomic>
4#include <cstdint>
5#include <functional>
6#include <mutex>
7#include <type_traits>
8#include <unordered_map>
9#include <utility>
10
11#include <Common/Assert.hpp>
12#include <Common/DeferredDeletionQueue.hpp>
13
14namespace Grindstone::Containers {
15 template<typename Key, typename Value, class Hash = std::hash<Key>>
17 private:
18 struct Entry {
19 Key key;
20 Value value;
21 std::atomic<uint32_t> refs{0};
22 std::once_flag created;
23 };
24
25 public:
26 class Ref {
27 friend class ResourceCache;
28
29 Entry* entry = nullptr;
30
31 public:
32 Ref() = default;
33 explicit Ref(Entry* entry) : entry(entry) {
34 if (entry) {
35 entry->refs.fetch_add(1, std::memory_order_relaxed);
36 }
37 }
38 explicit Ref(std::nullptr_t) : entry(nullptr) {}
39 Ref(const Ref& other) : Ref(other.entry) {}
40 Ref(Ref&& other) noexcept : entry(std::exchange(other.entry, nullptr)) {}
41
42 Ref& operator=(std::nullptr_t other) {
43 if (entry != nullptr) {
44 entry->refs.fetch_sub(1, std::memory_order_acq_rel);
45 entry = nullptr;
46 }
47
48 return *this;
49 }
50
51 Ref& operator=(const Ref& other) {
52 if (other.entry != entry) {
53 if (other.entry != nullptr) {
54 other.entry->refs.fetch_add(1, std::memory_order_relaxed);
55 }
56
57 if (entry != nullptr) {
58 entry->refs.fetch_sub(1, std::memory_order_acq_rel);
59 }
60
61 entry = other.entry;
62 }
63
64 return *this;
65 }
66
67 Ref& operator=(Ref&& other) noexcept {
68 if (other.entry != entry) {
69 if (entry != nullptr) {
70 entry->refs.fetch_sub(1, std::memory_order_acq_rel);
71 }
72
73 entry = std::exchange(other.entry, nullptr);
74 }
75
76 return *this;
77 }
78
79 bool operator==(const Ref& other) const {
80 return entry == other.entry;
81 }
82
83 bool operator!=(const Ref& other) const {
84 return entry != other.entry;
85 }
86
87 bool IsValid() const {
88 return entry != nullptr;
89 }
90
91 ~Ref() {
92 if (entry) {
93 entry->refs.fetch_sub(1, std::memory_order_acq_rel);
94 }
95 }
96 Value operator->() const {
97 if (entry) {
98 return entry->value;
99 }
100
101 return nullptr;
102 }
103
104 Value Get() const {
105 if (entry) {
106 return entry->value;
107 }
108
109 return nullptr;
110 }
111 };
112
113 using Factory = std::function<Value(const Key&)>;
114 using Deleter = std::function<void(Value)>;
115
117 TrimImmediately();
118 GS_ASSERT_WITH_MESSAGE(cache.empty(), "ResourceCache handles outlive cache.");
119 }
120
121 // If key exists in the cache, return it, if not, create it via factory.
122 Ref GetOrCreate(const Key& key) {
123 Ref ref;
124 {
125 std::unique_lock lock(mutex);
126 auto [it, inserted] = cache.try_emplace(key);
127 Entry* entry = &it->second;
128 if (inserted) {
129 entry->key = key;
130 }
131 ref = Ref(entry);
132 }
133
134 std::call_once(ref.entry->created, [&]{ ref.entry->value = constructCallback(key); });
135
136 return ref;
137 }
138
139 // Remove all entries that are unused (where ref count==0).
140 void Trim(Grindstone::DeferredDeletionQueue* deferredDeletionQueue) {
141 std::unique_lock lock(mutex);
142 for (auto it = cache.begin(); it != cache.end();) {
143 if (it->second.refs.load() == 0) {
144 deferredDeletionQueue->PushDeletion(
145 [resource=it->second.value, destroyCallback=this->destroyCallback]() {
146 destroyCallback(resource);
147 }
148 );
149 it = cache.erase(it);
150 } else {
151 ++it;
152 }
153 }
154 }
155
156 // Remove all entries that are unused (where ref count==0).
157 void TrimImmediately() {
158 std::unique_lock lock(mutex);
159 for (auto it = cache.begin(); it != cache.end();) {
160 if (it->second.refs.load() == 0) {
161 destroyCallback(it->second.value);
162 it = cache.erase(it);
163 } else {
164 ++it;
165 }
166 }
167 }
168
169 void SetFactory(Factory factory) { constructCallback = factory; }
170 void SetDeleter(Deleter deleter) { destroyCallback = deleter; }
171
172 protected:
173
174 Factory constructCallback;
175 Deleter destroyCallback;
176
177 std::mutex mutex;
178 std::unordered_map<Key, Entry, Hash> cache;
179 };
180}
Definition ResourceCache.hpp:16
Definition Bitset.hpp:9