Hash :
6f794eab
Author :
Date :
2023-10-05T11:23:30
Change angle::FixedQueue's storage from std::array to std::vector Right now angle::FixedQueue uses std::array as the storage. In the case when queue is full, the only choice is to wait for dequeue thread to run until there is more room to enqueue. This CL try to add extra flexibility. In this CL< it switches storage to std::vector so that we could reallocate to double the storage when it is full. The trick is that before doing that, you must ensure no one is accessing the queue other than check the size. In a lot of usage cases that is easy to do by just grabbing the necessary locks. Bug: b/302739073 Change-Id: Ibefe0fd0e3e89c17dd6ee2cac6adc3368122adb9 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4915811 Reviewed-by: Hailin Zhang <hailinzhang@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Charlie Lao <cclao@google.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
//
// 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>
#include <atomic>
namespace angle
{
// class FixedQueue: An vector based 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, but only with single producer single
// consumer usage. If caller want to push from two different threads, proper mutex must be used to
// ensure the access is serialized. You can also call updateCapacity to adjust the storage size, but
// caller must take proper mutex lock to ensure no one is accessing the storage. In a typical usage
// case is that you have two mutex lock, enqueueLock and dequeueLock. You use enqueueLock to push
// and use dequeueLock to pop. You dont need the lock for checking size (i.e, call empty/full). You
// take both lock in a given order to call updateCapacity. See unit test
// FixedQueue.ConcurrentPushPopWithResize for example.
template <class T>
class FixedQueue final : angle::NonCopyable
{
public:
using Storage = std::vector<T>;
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;
FixedQueue(size_t capacity);
~FixedQueue();
size_type size() const;
bool empty() const;
bool full() const;
size_type capacity() const;
// Caller must ensure no one is accessing the data while update storage. This should happen
// infrequently since all data will be copied between old storage and new storage.
void updateCapacity(size_t newCapacity);
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();
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;
size_type mMaxSize;
};
template <class T>
FixedQueue<T>::FixedQueue(size_t capacity)
: mFrontIndex(0), mEndIndex(0), mSize(0), mMaxSize(capacity)
{
mData.resize(mMaxSize);
}
template <class T>
FixedQueue<T>::~FixedQueue()
{
mData.clear();
}
template <class T>
ANGLE_INLINE typename FixedQueue<T>::size_type FixedQueue<T>::size() const
{
return mSize;
}
template <class T>
ANGLE_INLINE bool FixedQueue<T>::empty() const
{
return mSize == 0;
}
template <class T>
ANGLE_INLINE bool FixedQueue<T>::full() const
{
return mSize >= mMaxSize;
}
template <class T>
ANGLE_INLINE typename FixedQueue<T>::size_type FixedQueue<T>::capacity() const
{
return mMaxSize;
}
template <class T>
ANGLE_INLINE void FixedQueue<T>::updateCapacity(size_t newCapacity)
{
ASSERT(newCapacity >= mSize);
Storage newData(newCapacity);
for (size_type i = mFrontIndex; i < mEndIndex; i++)
{
newData[i % newCapacity] = std::move(mData[i % mMaxSize]);
}
mData.clear();
std::swap(newData, mData);
mMaxSize = newCapacity;
ASSERT(mData.size() == mMaxSize);
}
template <class T>
ANGLE_INLINE typename FixedQueue<T>::reference FixedQueue<T>::front()
{
ASSERT(mSize > 0);
return mData[mFrontIndex % mMaxSize];
}
template <class T>
ANGLE_INLINE typename FixedQueue<T>::const_reference FixedQueue<T>::front() const
{
ASSERT(mSize > 0);
return mData[mFrontIndex % mMaxSize];
}
template <class T>
void FixedQueue<T>::push(const value_type &value)
{
ASSERT(mSize < mMaxSize);
mData[mEndIndex % mMaxSize] = 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>
void FixedQueue<T>::push(value_type &&value)
{
ASSERT(mSize < mMaxSize);
mData[mEndIndex % mMaxSize] = 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>
ANGLE_INLINE typename FixedQueue<T>::reference FixedQueue<T>::back()
{
ASSERT(mSize > 0);
return mData[(mEndIndex + (mMaxSize - 1)) % mMaxSize];
}
template <class T>
ANGLE_INLINE typename FixedQueue<T>::const_reference FixedQueue<T>::back() const
{
ASSERT(mSize > 0);
return mData[(mEndIndex + (mMaxSize - 1)) % mMaxSize];
}
template <class T>
void FixedQueue<T>::pop()
{
ASSERT(mSize > 0);
mData[mFrontIndex % mMaxSize] = 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>
void FixedQueue<T>::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();
}
}
} // namespace angle
#endif // COMMON_FIXEDQUEUE_H_