Blender V5.3
BLI_concurrent_map.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifdef WITH_TBB
10/* Quiet top level deprecation message, unrelated to API usage here. */
11# include <tbb/concurrent_hash_map.h>
12#else
13# include "BLI_mutex.hh"
14# include "BLI_set.hh"
15#endif
16
17#include "BLI_hash.hh"
18#include "BLI_hash_tables.hh"
19
20namespace blender {
21
37template<typename Key,
38 typename Value,
39 typename Hash = DefaultHash<Key>,
40 typename IsEqual = DefaultEquality<Key>>
42 public:
44
45 /* Sometimes TBB requires the value to be constructible. */
46 static_assert(std::is_copy_constructible_v<Value>);
47
48#ifdef WITH_TBB
49 private:
50 struct Hasher {
51 template<typename T> size_t hash(const T &value) const
52 {
53 return Hash{}(value);
54 }
55
56 template<typename T1, typename T2> bool equal(const T1 &a, const T2 &b) const
57 {
58 return IsEqual{}(a, b);
59 }
60 };
61
62 using TBBMap = tbb::concurrent_hash_map<Key, Value, Hasher>;
63 TBBMap map_;
64
65 public:
66 using MutableAccessor = typename TBBMap::accessor;
67 using ConstAccessor = typename TBBMap::const_accessor;
68
76 bool lookup(MutableAccessor &accessor, const Key &key)
77 {
78 return map_.find(accessor, key);
79 }
80
85 bool lookup(ConstAccessor &accessor, const Key &key)
86 {
87 return map_.find(accessor, key);
88 }
89
94 bool add(MutableAccessor &accessor, const Key &key)
95 {
96 return map_.insert(accessor, key);
97 }
98
99 bool add(ConstAccessor &accessor, const Key &key)
100 {
101 return map_.insert(accessor, key);
102 }
103
108 bool remove(const Key &key)
109 {
110 return map_.erase(key);
111 }
112
113#else
114 private:
119 struct SetKey {
120 std::pair<Key, Value> item;
121
122 SetKey(Key key) : item(std::move(key), Value()) {}
123
124 uint64_t hash() const
125 {
126 return Hash{}(this->item.first);
127 }
128
129 static uint64_t hash_as(const Key &key)
130 {
131 return Hash{}(key);
132 }
133
134 friend bool operator==(const SetKey &a, const SetKey &b)
135 {
136 return IsEqual{}(a.item.first, b.item.first);
137 }
138
139 friend bool operator==(const Key &a, const SetKey &b)
140 {
141 return IsEqual{}(a, b.item.first);
142 }
143
144 friend bool operator==(const SetKey &a, const Key &b)
145 {
146 return IsEqual{}(a.item.first, b);
147 }
148 };
149
150 using UsedSet = Set<SetKey>;
151
152 struct Accessor {
153 std::unique_lock<Mutex> mutex;
154 std::pair<Key, Value> *data = nullptr;
155
156 std::pair<Key, Value> *operator->()
157 {
158 return this->data;
159 }
160 };
161
162 Mutex mutex_;
163 UsedSet set_;
164
165 public:
166 using MutableAccessor = Accessor;
167 using ConstAccessor = Accessor;
168
169 bool lookup(Accessor &accessor, const Key &key)
170 {
171 accessor.mutex = std::unique_lock(mutex_);
172 SetKey *stored_key = const_cast<SetKey *>(set_.lookup_key_ptr_as(key));
173 if (!stored_key) {
174 return false;
175 }
176 accessor.data = &stored_key->item;
177 return true;
178 }
179
180 bool add(Accessor &accessor, const Key &key)
181 {
182 accessor.mutex = std::unique_lock(mutex_);
183 const bool newly_added = !set_.contains_as(key);
184 SetKey &stored_key = const_cast<SetKey &>(set_.lookup_key_or_add_as(key));
185 accessor.data = &stored_key.item;
186 return newly_added;
187 }
188
189 bool remove(const Key &key)
190 {
191 std::unique_lock lock(mutex_);
192 return set_.remove_as(key);
193 }
194
195#endif
196};
197
198} // namespace blender
volatile int lock
bool lookup(Accessor &accessor, const Key &key)
bool remove(const Key &key)
bool add(Accessor &accessor, const Key &key)
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
#define T
#define T2
Definition md5.cpp:21
#define T1
Definition md5.cpp:20
BMesh Iterators.
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
std::mutex Mutex
Definition BLI_mutex.hh:51
@ Hash
Definition types.hh:49
#define hash
Definition noise_c.cc:156