1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/*
* Copyright (C) 2022-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <wtf/Algorithms.h>
#include <wtf/HashSet.h>
#include <wtf/ThreadSafeWeakPtr.h>
#include <wtf/Vector.h>
namespace WTF {
template<typename T>
class ThreadSafeWeakHashSet final {
WTF_MAKE_FAST_ALLOCATED;
public:
ThreadSafeWeakHashSet() = default;
ThreadSafeWeakHashSet(ThreadSafeWeakHashSet&& other) { moveFrom(WTFMove(other)); }
ThreadSafeWeakHashSet& operator=(ThreadSafeWeakHashSet&& other)
{
moveFrom(WTFMove(other));
return *this;
}
class const_iterator {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using pointer = const value_type*;
using reference = const value_type&;
private:
const_iterator(Vector<Ref<T>>&& strongReferences)
: m_strongReferences(WTFMove(strongReferences)) { }
public:
T* get() const
{
RELEASE_ASSERT(m_position < m_strongReferences.size());
return m_strongReferences[m_position].ptr();
}
T& operator*() const { return *get(); }
T* operator->() const { return get(); }
const_iterator& operator++()
{
RELEASE_ASSERT(m_position < m_strongReferences.size());
++m_position;
return *this;
}
bool operator==(const const_iterator& other) const
{
// This should only be used to compare with end.
ASSERT_UNUSED(other, other.m_strongReferences.isEmpty());
return m_position == m_strongReferences.size();
}
private:
template<typename> friend class ThreadSafeWeakHashSet;
Vector<Ref<T>> m_strongReferences;
size_t m_position { 0 };
};
const_iterator begin() const
{
return { values() };
}
const_iterator end() const { return { { } }; }
template<typename U>
void add(const U& value) requires (std::is_convertible_v<U*, T*>)
{
RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(!value.controlBlock().objectHasStartedDeletion());
Locker locker { m_lock };
ControlBlockRefPtr retainedControlBlock { &value.controlBlock() };
ASSERT(retainedControlBlock);
amortizedCleanupIfNeeded();
m_set.add(std::make_pair(WTFMove(retainedControlBlock), &value));
}
template<typename U>
bool remove(const U& value) requires (std::is_convertible_v<U*, T*>)
{
Locker locker { m_lock };
amortizedCleanupIfNeeded();
// If there are no weak refs then it can't be in our table. In that case
// there's no point in potentially allocating a ControlBlock.
if (!value.weakRefCount())
return false;
auto it = m_set.find(std::make_pair(&value.controlBlock(), &value));
if (it == m_set.end())
return false;
bool wasDeleted = it->first->objectHasStartedDeletion();
bool result = m_set.remove(it);
ASSERT_UNUSED(result, result);
return !wasDeleted;
}
void clear()
{
Locker locker { m_lock };
m_set.clear();
cleanupHappened();
}
template<typename U>
bool contains(const U& value) const requires (std::is_convertible_v<U*, T*>)
{
Locker locker { m_lock };
amortizedCleanupIfNeeded();
// If there are no weak refs then it can't be in our table. In that case
// there's no point in potentially allocating a ControlBlock.
if (!value.weakRefCount())
return false;
auto it = m_set.find(std::make_pair(&value.controlBlock(), &value));
if (it == m_set.end())
return false;
bool wasDeleted = it->first->objectHasStartedDeletion();
if (wasDeleted)
m_set.remove(it);
return !wasDeleted;
}
bool isEmptyIgnoringNullReferences() const
{
Locker locker { m_lock };
amortizedCleanupIfNeeded();
// FIXME: This seems like it should remove any stale entries it finds along the way. Although, it might require a
// HashSet::removeNoRehash function. https://bugs.webkit.org/show_bug.cgi?id=283928
for (auto& pair : m_set) {
if (!pair.first->objectHasStartedDeletion())
return false;
}
return true;
}
Vector<Ref<T>> values() const
{
Vector<Ref<T>> strongReferences;
{
Locker locker { m_lock };
bool hasNullReferences = false;
strongReferences = compactMap(m_set, [&hasNullReferences](auto& pair) -> RefPtr<T> {
if (RefPtr strongReference = pair.first->template makeStrongReferenceIfPossible<T>(pair.second))
return strongReference;
hasNullReferences = true;
return nullptr;
});
if (hasNullReferences)
m_set.removeIf([](auto& pair) { return pair.first->objectHasStartedDeletion(); });
cleanupHappened();
}
return strongReferences;
}
Vector<ThreadSafeWeakPtr<T>> weakValues() const
{
Vector<ThreadSafeWeakPtr<T>> weakReferences;
{
// FIXME: It seems like this should prune known dead entries as it goes. https://bugs.webkit.org/show_bug.cgi?id=283928
Locker locker { m_lock };
weakReferences = WTF::map(m_set, [](auto& pair) {
return ThreadSafeWeakPtr<T> { *pair.first, *pair.second };
});
}
return weakReferences;
}
template<typename Functor>
void forEach(const Functor& callback) const
{
for (auto& item : values())
callback(item.get());
}
unsigned sizeIncludingEmptyEntriesForTesting()
{
Locker locker { m_lock };
return m_set.size();
}
private:
ALWAYS_INLINE void cleanupHappened() const WTF_REQUIRES_LOCK(m_lock)
{
m_operationCountSinceLastCleanup = 0;
m_maxOperationCountWithoutCleanup = std::min(std::numeric_limits<unsigned>::max() / 2, m_set.size()) * 2;
}
ALWAYS_INLINE void moveFrom(ThreadSafeWeakHashSet&& other)
{
Locker locker { m_lock };
Locker otherLocker { other.m_lock };
m_set = std::exchange(other.m_set, { });
m_operationCountSinceLastCleanup = std::exchange(other.m_operationCountSinceLastCleanup, 0);
m_maxOperationCountWithoutCleanup = std::exchange(other.m_maxOperationCountWithoutCleanup, 0);
}
ALWAYS_INLINE void amortizedCleanupIfNeeded() const WTF_REQUIRES_LOCK(m_lock)
{
if (++m_operationCountSinceLastCleanup > m_maxOperationCountWithoutCleanup) {
m_set.removeIf([] (auto& pair) {
ASSERT(pair.first->weakRefCount());
return pair.first->objectHasStartedDeletion();
});
cleanupHappened();
}
}
mutable UncheckedKeyHashSet<std::pair<ControlBlockRefPtr, const T*>> m_set WTF_GUARDED_BY_LOCK(m_lock);
mutable unsigned m_operationCountSinceLastCleanup WTF_GUARDED_BY_LOCK(m_lock) { 0 };
mutable unsigned m_maxOperationCountWithoutCleanup WTF_GUARDED_BY_LOCK(m_lock) { 0 };
mutable Lock m_lock;
};
} // namespace WTF
using WTF::ThreadSafeWeakHashSet;
|