Blender V5.3
BLI_concurrent_set.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifdef WITH_TBB
10# include <tbb/concurrent_unordered_set.h>
11#else
12# include <memory>
13# include <utility>
14
15# include "BLI_mutex.hh"
16# include "BLI_set.hh"
17#endif
18
19#include "BLI_hash.hh"
20#include "BLI_hash_tables.hh"
21#include "BLI_utility_mixins.hh"
22
23namespace blender {
24
33template<typename Key, typename Hash = DefaultHash<Key>, typename IsEqual = DefaultEquality<Key>>
35#ifdef WITH_TBB
36 private:
41 struct TransparentIsEqual : public IsEqual {
42 using is_transparent = void;
43 };
44
45 struct Hasher : public Hash {
46 using transparent_key_equal = TransparentIsEqual;
47 };
48
49 using TBBSet = tbb::concurrent_unordered_set<Key, Hasher>;
50 TBBSet set_;
51
52 public:
57 template<typename ForwardKey> const Key *lookup_ptr(const ForwardKey &key) const
58 {
59 typename TBBSet::const_iterator it = set_.find(key);
60 return (it == set_.end()) ? nullptr : &*it;
61 }
62
68 const Key &lookup_key_or_add(const Key &key)
69 {
70 return this->lookup_key_or_add_as(key);
71 }
72 const Key &lookup_key_or_add(Key &&key)
73 {
74 return this->lookup_key_or_add_as(std::move(key));
75 }
76 template<typename ForwardKey> const Key &lookup_key_or_add_as(ForwardKey &&key)
77 {
78 /* Use #emplace instead of #insert, because of a TBB bug that can cause duplicate values to be
79 * added to the set via #insert. */
80 return *set_.emplace(std::forward<ForwardKey>(key)).first;
81 }
82
84 int64_t size() const
85 {
86 return int64_t(set_.size());
87 }
88
90 void reserve(const int64_t n)
91 {
92 set_.reserve(typename TBBSet::size_type(n));
93 }
94
95#else
96 private:
102 template<typename ForwardKey> struct Unwrap {
103 static const ForwardKey &get(const ForwardKey &key)
104 {
105 return key;
106 }
107 };
108 template<typename T> struct Unwrap<std::unique_ptr<T>> {
109 static const T &get(const std::unique_ptr<T> &key)
110 {
111 return *key;
112 }
113 };
114
115 struct PtrHash {
116 template<typename ForwardKey> uint64_t operator()(const ForwardKey &key) const
117 {
118 return Hash{}(Unwrap<ForwardKey>::get(key));
119 }
120 };
121 struct PtrIsEqual {
122 template<typename ForwardKeyA, typename ForwardKeyB>
123 bool operator()(const ForwardKeyA &a, const ForwardKeyB &b) const
124 {
125 return IsEqual{}(Unwrap<ForwardKeyA>::get(a), Unwrap<ForwardKeyB>::get(b));
126 }
127 };
128
129 using UsedSet = Set<std::unique_ptr<Key>,
130 default_inline_buffer_capacity(sizeof(std::unique_ptr<Key>)),
132 PtrHash,
133 PtrIsEqual>;
134
135 /* Wrapped in a unique_ptr to allow ConcurrentSet to be moveable. */
136 mutable std::unique_ptr<Mutex> mutex_ = std::make_unique<Mutex>();
137 UsedSet set_;
138
139 public:
140 ConcurrentSet() = default;
141 ConcurrentSet(ConcurrentSet &&other) noexcept
142 : mutex_(std::exchange(other.mutex_, std::make_unique<Mutex>())), set_(std::move(other.set_))
143 {
144 }
146 {
147 std::destroy_at(this);
148 new (this) ConcurrentSet(std::move(other));
149 return *this;
150 }
151
156 template<typename ForwardKey> const Key *lookup_ptr(const ForwardKey &key) const
157 {
158 std::lock_guard lock{*mutex_};
159 const std::unique_ptr<Key> *stored = set_.lookup_key_ptr_as(key);
160 return stored ? stored->get() : nullptr;
161 }
162
168 const Key &lookup_key_or_add(const Key &key)
169 {
170 return this->lookup_key_or_add_as(key);
171 }
173 {
174 return this->lookup_key_or_add_as(std::move(key));
175 }
176 template<typename ForwardKey> const Key &lookup_key_or_add_as(ForwardKey &&key)
177 {
178 std::lock_guard lock{*mutex_};
179 return *set_.lookup_key_or_add_as(std::make_unique<Key>(std::forward<ForwardKey>(key)));
180 }
181
183 int64_t size() const
184 {
185 std::lock_guard lock{*mutex_};
186 return set_.size();
187 }
188
190 void reserve(const int64_t n)
191 {
192 std::lock_guard lock{*mutex_};
193 set_.reserve(n);
194 }
195
196#endif
197};
198
199} // namespace blender
volatile int lock
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
void reserve(const int64_t n)
const Key & lookup_key_or_add_as(ForwardKey &&key)
const Key & lookup_key_or_add(const Key &key)
const Key * lookup_ptr(const ForwardKey &key) const
ConcurrentSet(ConcurrentSet &&other) noexcept
const Key & lookup_key_or_add(Key &&key)
ConcurrentSet & operator=(ConcurrentSet &&other) noexcept
NonCopyable(const NonCopyable &other)=delete
#define T
BMesh Iterators.
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
constexpr int64_t default_inline_buffer_capacity(size_t element_size)
PythonProbingStrategy<> DefaultProbingStrategy
@ Hash
Definition types.hh:49