Hash :
8374bf5f
Author :
Date :
2023-02-13T20:35:32
Fix bug in FixedQueue::clear() and refactoring. - bug: "mSize" used to end the loop but also changed inside the loop by call to the "pop()" method. - refactoring: "mBackIndex" name is not correct, because variable references to the "index for next write", in other words - to the element past "back" (last written). Renamed to "mEndIndex" to match the "std" terminology. Bug: b/267348918 Test: angle_unittests --gtest_filter="FixedQueue.Clear" Change-Id: Ic65291a7ff2ff6f4eed223ca80fef187e42df3e5 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4245420 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Charlie Lao <cclao@google.com> Commit-Queue: Igor Nazarov <i.nazarov@samsung.com>
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
//
// Copyright 2023 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// FixedQueue.h:
// An array based fifo queue class that supports concurrent push and pop.
//
#ifndef COMMON_FIXEDQUEUE_H_
#define COMMON_FIXEDQUEUE_H_
#include "common/debug.h"
#include <algorithm>
#include <array>
namespace angle
{
// class FixedQueue: An array based fix storage fifo queue class that supports concurrent push and
// pop. Caller must ensure queue is not empty before pop and not full before push. This class
// supports concurrent push and pop from different threads. If caller want to push from two
// different threads, proper mutex must be used to ensure the access is serialized.
template <class T, size_t N, class Storage = std::array<T, N>>
class FixedQueue final : angle::NonCopyable
{
public:
using value_type = typename Storage::value_type;
using size_type = typename Storage::size_type;
using reference = typename Storage::reference;
using const_reference = typename Storage::const_reference;
class iterator
{
public:
iterator() : mData(nullptr), mIndex(-1) {}
T &operator*() const { return mData[mIndex % N]; }
T *operator->() const { return &mData[mIndex % N]; }
bool operator==(const iterator &rhs) const { return mIndex == rhs.mIndex; }
bool operator!=(const iterator &rhs) const { return mIndex != rhs.mIndex; }
iterator &operator++()
{
mIndex++;
return *this;
}
private:
Storage &mData;
size_type mIndex;
friend class FixedQueue<T, N, Storage>;
iterator(Storage &data, size_type index) : mData(data), mIndex(index) {}
};
class const_iterator
{
public:
const_iterator() : mData(nullptr), mIndex(-1) {}
const T &operator*() const { return mData[mIndex % N]; }
const T *operator->() const { return &mData[mIndex % N]; }
bool operator==(const iterator &rhs) const { mIndex == rhs.mIndex; }
bool operator!=(const iterator &rhs) const { mIndex != rhs.mIndex; }
const_iterator &operator++()
{
mIndex++;
return *this;
}
private:
const Storage &mData;
size_type mIndex;
friend class FixedQueue<T, N, Storage>;
const_iterator(const Storage &data, size_type index) : mData(data), mIndex(index) {}
};
FixedQueue();
~FixedQueue();
size_type size() const;
bool empty() const;
bool full() const;
reference operator[](size_type pos);
const_reference operator[](size_type pos) const;
reference front();
const_reference front() const;
void push(const value_type &value);
void push(value_type &&value);
reference back();
const_reference back() const;
void pop();
void clear();
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
private:
Storage mData;
// The front and back indices are virtual indices (think about queue sizd is infinite). They
// will never wrap around when hit N. The wrap around occur when element is referenced. Virtual
// index for current head
size_type mFrontIndex;
// Virtual index for next write.
size_type mEndIndex;
// Atomic so that we can support concurrent push and pop.
std::atomic<size_type> mSize;
};
template <class T, size_t N, class Storage>
FixedQueue<T, N, Storage>::FixedQueue() : mFrontIndex(0), mEndIndex(0), mSize(0)
{}
template <class T, size_t N, class Storage>
FixedQueue<T, N, Storage>::~FixedQueue()
{}
template <class T, size_t N, class Storage>
ANGLE_INLINE typename FixedQueue<T, N, Storage>::size_type FixedQueue<T, N, Storage>::size() const
{
return mSize;
}
template <class T, size_t N, class Storage>
ANGLE_INLINE bool FixedQueue<T, N, Storage>::empty() const
{
return mSize == 0;
}
template <class T, size_t N, class Storage>
ANGLE_INLINE bool FixedQueue<T, N, Storage>::full() const
{
return mSize >= N;
}
template <class T, size_t N, class Storage>
typename FixedQueue<T, N, Storage>::reference FixedQueue<T, N, Storage>::operator[](size_type pos)
{
ASSERT(pos < mSize);
return mData[(pos + mFrontIndex) % N];
}
template <class T, size_t N, class Storage>
typename FixedQueue<T, N, Storage>::const_reference FixedQueue<T, N, Storage>::operator[](
size_type pos) const
{
ASSERT(pos < mSize);
return mData[(pos + mFrontIndex) % N];
}
template <class T, size_t N, class Storage>
ANGLE_INLINE typename FixedQueue<T, N, Storage>::reference FixedQueue<T, N, Storage>::front()
{
ASSERT(mSize > 0);
return mData[mFrontIndex % N];
}
template <class T, size_t N, class Storage>
ANGLE_INLINE typename FixedQueue<T, N, Storage>::const_reference FixedQueue<T, N, Storage>::front()
const
{
ASSERT(mSize > 0);
return mData[mFrontIndex % N];
}
template <class T, size_t N, class Storage>
void FixedQueue<T, N, Storage>::push(const value_type &value)
{
ASSERT(mSize < N);
mData[mEndIndex % N] = value;
mEndIndex++;
// We must increment size last, after we wrote data. That way if another thread is doing
// `if(!dq.empty()){ s = dq.front(); }`, it will only see not empty until element is fully
// pushed.
mSize++;
}
template <class T, size_t N, class Storage>
void FixedQueue<T, N, Storage>::push(value_type &&value)
{
ASSERT(mSize < N);
mData[mEndIndex % N] = std::move(value);
mEndIndex++;
// We must increment size last, after we wrote data. That way if another thread is doing
// `if(!dq.empty()){ s = dq.front(); }`, it will only see not empty until element is fully
// pushed.
mSize++;
}
template <class T, size_t N, class Storage>
ANGLE_INLINE typename FixedQueue<T, N, Storage>::reference FixedQueue<T, N, Storage>::back()
{
ASSERT(mSize > 0);
return mData[(mEndIndex + (N - 1)) % N];
}
template <class T, size_t N, class Storage>
ANGLE_INLINE typename FixedQueue<T, N, Storage>::const_reference FixedQueue<T, N, Storage>::back()
const
{
ASSERT(mSize > 0);
return mData[(mEndIndex + (N - 1)) % N];
}
template <class T, size_t N, class Storage>
void FixedQueue<T, N, Storage>::pop()
{
ASSERT(mSize > 0);
mData[mFrontIndex % N] = value_type();
mFrontIndex++;
// We must decrement size last, after we wrote data. That way if another thread is doing
// `if(!dq.full()){ dq.push; }`, it will only see not full until element is fully popped.
mSize--;
}
template <class T, size_t N, class Storage>
void FixedQueue<T, N, Storage>::clear()
{
// Size will change in the "pop()" and also by "push()" calls from other thread.
const size_type localSize = mSize;
for (size_type i = 0; i < localSize; i++)
{
pop();
}
}
template <class T, size_t N, class Storage>
typename FixedQueue<T, N, Storage>::iterator FixedQueue<T, N, Storage>::begin()
{
return iterator(mData, mFrontIndex);
}
template <class T, size_t N, class Storage>
typename FixedQueue<T, N, Storage>::const_iterator FixedQueue<T, N, Storage>::begin() const
{
return const_iterator(mData, mFrontIndex);
}
template <class T, size_t N, class Storage>
typename FixedQueue<T, N, Storage>::iterator FixedQueue<T, N, Storage>::end()
{
return iterator(mData, mFrontIndex + mSize);
}
template <class T, size_t N, class Storage>
typename FixedQueue<T, N, Storage>::const_iterator FixedQueue<T, N, Storage>::end() const
{
return const_iterator(mData, mFrontIndex + mSize);
}
} // namespace angle
#endif // COMMON_FIXEDQUEUE_H_