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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/*
* Copyright (C) 2017-2023 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.
* 3. Neither the name of Apple Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE 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 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/ConcurrentBuffer.h>
#include <wtf/Noncopyable.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace WTF {
// An iterator for ConcurrentVector. It supports only the pre ++ operator
template <typename T, size_t SegmentSize = 8> class ConcurrentVector;
template <typename T, size_t SegmentSize = 8> class ConcurrentVectorIterator {
WTF_MAKE_FAST_ALLOCATED;
private:
friend class ConcurrentVector<T, SegmentSize>;
public:
typedef ConcurrentVectorIterator<T, SegmentSize> Iterator;
~ConcurrentVectorIterator() { }
T& operator*() const { return m_vector.at(m_index); }
T* operator->() const { return &m_vector.at(m_index); }
// Only prefix ++ operator supported
Iterator& operator++()
{
m_index++;
return *this;
}
bool operator==(const Iterator& other) const
{
return m_index == other.m_index && &m_vector == &other.m_vector;
}
ConcurrentVectorIterator& operator=(const ConcurrentVectorIterator<T, SegmentSize>& other)
{
m_vector = other.m_vector;
m_index = other.m_index;
return *this;
}
private:
ConcurrentVectorIterator(ConcurrentVector<T, SegmentSize>& vector, size_t index)
: m_vector(vector)
, m_index(index)
{
}
ConcurrentVector<T, SegmentSize>& m_vector;
size_t m_index;
};
// ConcurrentVector is like SegmentedVector, but suitable for scenarios where one thread appends
// elements and another thread continues to access elements at lower indices. Only one thread can
// append at a time, so that activity still needs locking. size() and last() are racy with append(),
// in the sense that last() may crash if an append() is running concurrently because size()-1 does yet
// have a segment. If you want size() to be safe with additions use appendConcurrently() but note
// appendConcurrently() is not multi-writer safe.
//
// Typical users of ConcurrentVector already have some way of ensuring that by the time someone is
// trying to use an index, some synchronization has happened to ensure that this index contains fully
// initialized data. Thereafter, the keeper of that index is allowed to use it on this vector without
// any locking other than what is needed to protect the integrity of the element at that index. This
// works because we guarantee shrinking the vector is impossible and that growing the vector doesn't
// delete old vector spines.
template <typename T, size_t SegmentSize>
class ConcurrentVector final {
friend class ConcurrentVectorIterator<T, SegmentSize>;
WTF_MAKE_NONCOPYABLE(ConcurrentVector);
WTF_MAKE_FAST_ALLOCATED;
public:
typedef ConcurrentVectorIterator<T, SegmentSize> Iterator;
ConcurrentVector() = default;
~ConcurrentVector()
{
}
// This may return a size that is bigger than the underlying storage, since this does not fence
// manipulations of size. So if you access at size()-1, you may crash because this hasn't
// allocated storage for that index yet.
size_t size() const { return m_size; }
bool isEmpty() const { return !size(); }
T& at(size_t index)
{
ASSERT_WITH_SECURITY_IMPLICATION(index < m_size);
return segmentFor(index)->entries[subscriptFor(index)];
}
const T& at(size_t index) const
{
return const_cast<ConcurrentVector<T, SegmentSize>*>(this)->at(index);
}
T& operator[](size_t index)
{
return at(index);
}
const T& operator[](size_t index) const
{
return at(index);
}
T& first()
{
ASSERT_WITH_SECURITY_IMPLICATION(!isEmpty());
return at(0);
}
const T& first() const
{
ASSERT_WITH_SECURITY_IMPLICATION(!isEmpty());
return at(0);
}
// This may crash if run concurrently to append(). If you want to accurately track the size of
// this vector, use appendConcurrently().
T& last()
{
ASSERT_WITH_SECURITY_IMPLICATION(!isEmpty());
return at(size() - 1);
}
const T& last() const
{
ASSERT_WITH_SECURITY_IMPLICATION(!isEmpty());
return at(size() - 1);
}
T takeLast()
{
ASSERT_WITH_SECURITY_IMPLICATION(!isEmpty());
T result = WTFMove(last());
--m_size;
return result;
}
template<typename... Args>
void append(Args&&... args)
{
++m_size;
if (!segmentExistsFor(m_size - 1))
allocateSegment();
new (NotNull, &last()) T(std::forward<Args>(args)...);
}
template<typename... Args>
T& alloc(Args&&... args)
{
append(std::forward<Args>(args)...);
return last();
}
// Note, appendConcurrently() assumes only one thread can append at a time and is not safe with removeLast()/takeLast().
template<typename... Args>
void appendConcurrently(Args&&... args)
{
if (!segmentExistsFor(m_size))
allocateSegment();
T* slot = &segmentFor(m_size)->entries[subscriptFor(m_size)];
new (NotNull, slot) T(std::forward<Args>(args)...);
WTF::storeStoreFence();
++m_size;
}
void removeLast()
{
last().~T();
--m_size;
}
void grow(size_t size)
{
if (size == m_size)
return;
ASSERT(size > m_size);
ensureSegmentsFor(size);
size_t oldSize = m_size;
m_size = size;
for (size_t i = oldSize; i < m_size; ++i)
new (NotNull, &at(i)) T();
}
Iterator begin()
{
return Iterator(*this, 0);
}
Iterator end()
{
return Iterator(*this, m_size);
}
private:
struct Segment {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
T entries[SegmentSize];
};
bool segmentExistsFor(size_t index)
{
return index / SegmentSize < m_numSegments;
}
Segment* segmentFor(size_t index)
{
return m_segments[index / SegmentSize].get();
}
size_t subscriptFor(size_t index)
{
return index % SegmentSize;
}
void ensureSegmentsFor(size_t size)
{
size_t segmentCount = (m_size + SegmentSize - 1) / SegmentSize;
size_t neededSegmentCount = (size + SegmentSize - 1) / SegmentSize;
for (size_t i = segmentCount ? segmentCount - 1 : 0; i < neededSegmentCount; ++i)
ensureSegment(i);
}
void ensureSegment(size_t segmentIndex)
{
ASSERT_WITH_SECURITY_IMPLICATION(segmentIndex <= m_numSegments);
if (segmentIndex == m_numSegments)
allocateSegment();
}
void allocateSegment()
{
m_segments.grow(m_numSegments + 1);
m_segments[m_numSegments++] = makeUnique<Segment>();
}
size_t m_size { 0 };
ConcurrentBuffer<std::unique_ptr<Segment>> m_segments;
size_t m_numSegments { 0 };
};
} // namespace WTF
using WTF::ConcurrentVector;
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|