Hash :
321c6b63
Author :
Date :
2024-03-29T10:30:59
Remove is_always_lock_free assertion from AtomicQueueSerial Build will fail when compiled on 32 bit architecture. The code will still function on 32 bit architecture but with performance penalty due to lock. But we are not really expecting it actually run on 32 bit platform with vulkan backend (the atomic queue serial is only used by vulkan backend). We could move AtomicQueueSerial into vulkan backend, but that will be a much larger change that I try to avoid. This CL removes the static_assertion and make it 8 bytes aligned as well. Bug: angleproject:7989 Change-Id: I3c0bd9877c4171485ca1aa9af0cf4621c1c23f56 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5407870 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 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
//
// Copyright 2019 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.
//
// serial_utils:
// Utilities for generating unique IDs for resources in ANGLE.
//
#ifndef LIBANGLE_RENDERER_SERIAL_UTILS_H_
#define LIBANGLE_RENDERER_SERIAL_UTILS_H_
#include <array>
#include <atomic>
#include <limits>
#include "common/angleutils.h"
#include "common/debug.h"
namespace rx
{
class ResourceSerial
{
public:
constexpr ResourceSerial() : mValue(kDirty) {}
explicit constexpr ResourceSerial(uintptr_t value) : mValue(value) {}
constexpr bool operator==(ResourceSerial other) const { return mValue == other.mValue; }
constexpr bool operator!=(ResourceSerial other) const { return mValue != other.mValue; }
void dirty() { mValue = kDirty; }
void clear() { mValue = kEmpty; }
constexpr bool valid() const { return mValue != kEmpty && mValue != kDirty; }
constexpr bool empty() const { return mValue == kEmpty; }
private:
constexpr static uintptr_t kDirty = std::numeric_limits<uintptr_t>::max();
constexpr static uintptr_t kEmpty = 0;
uintptr_t mValue;
};
// Class UniqueSerial defines unique serial number for object identification. It has only
// equal/unequal comparison but no greater/smaller comparison. The default constructor creates an
// invalid value.
class UniqueSerial final
{
public:
constexpr UniqueSerial() : mValue(kInvalid) {}
constexpr UniqueSerial(const UniqueSerial &other) = default;
UniqueSerial &operator=(const UniqueSerial &other) = default;
constexpr bool operator==(const UniqueSerial &other) const
{
return mValue != kInvalid && mValue == other.mValue;
}
constexpr bool operator!=(const UniqueSerial &other) const
{
return mValue == kInvalid || mValue != other.mValue;
}
// Useful for serialization.
constexpr uint64_t getValue() const { return mValue; }
constexpr bool valid() const { return mValue != kInvalid; }
private:
friend class UniqueSerialFactory;
constexpr explicit UniqueSerial(uint64_t value) : mValue(value) {}
uint64_t mValue;
static constexpr uint64_t kInvalid = 0;
};
class UniqueSerialFactory final : angle::NonCopyable
{
public:
UniqueSerialFactory() : mSerial(1) {}
UniqueSerial generate()
{
uint64_t current = mSerial++;
ASSERT(mSerial > current); // Integer overflow
return UniqueSerial(current);
}
private:
uint64_t mSerial;
};
// Class Serial defines a monotonically increasing serial number that indicates the timeline of
// execution.
class Serial final
{
public:
constexpr Serial() : mValue(0) {}
constexpr Serial(const Serial &other) = default;
Serial &operator=(const Serial &other) = default;
static constexpr Serial Infinite() { return Serial(std::numeric_limits<uint64_t>::max()); }
constexpr bool operator==(const Serial &other) const { return mValue == other.mValue; }
constexpr bool operator!=(const Serial &other) const { return mValue != other.mValue; }
constexpr bool operator>(const Serial &other) const { return mValue > other.mValue; }
constexpr bool operator>=(const Serial &other) const { return mValue >= other.mValue; }
constexpr bool operator<(const Serial &other) const { return mValue < other.mValue; }
constexpr bool operator<=(const Serial &other) const { return mValue <= other.mValue; }
// Useful for serialization.
constexpr uint64_t getValue() const { return mValue; }
private:
friend class AtomicSerialFactory;
friend class RangedSerialFactory;
friend class AtomicQueueSerial;
constexpr explicit Serial(uint64_t value) : mValue(value) {}
uint64_t mValue;
};
// Defines class to track the queue serial that can be load/store from multiple threads atomically.
class alignas(8) AtomicQueueSerial final
{
public:
AtomicQueueSerial &operator=(const Serial &other)
{
mValue.store(other.mValue, std::memory_order_release);
return *this;
}
Serial getSerial() const { return Serial(mValue.load(std::memory_order_consume)); }
private:
static constexpr uint64_t kInvalid = 0;
std::atomic<uint64_t> mValue = kInvalid;
};
// Used as default/initial serial
static constexpr Serial kZeroSerial = Serial();
// The factory to generate a serial number within the range [mSerial, mSerial+mCount}
class RangedSerialFactory final : angle::NonCopyable
{
public:
RangedSerialFactory() : mSerial(0), mCount(0) {}
void reset() { mCount = 0; }
bool empty() const { return mCount == 0; }
bool generate(Serial *serialOut)
{
if (mCount > 0)
{
uint64_t current = mSerial++;
ASSERT(mSerial > current); // Integer overflow
*serialOut = Serial(current);
mCount--;
return true;
}
return false;
}
private:
friend class AtomicSerialFactory;
void initialize(uint64_t initialSerial, size_t count)
{
mSerial = initialSerial;
mCount = count;
}
uint64_t mSerial;
size_t mCount;
};
class AtomicSerialFactory final : angle::NonCopyable
{
public:
AtomicSerialFactory() : mSerial(1) {}
Serial generate()
{
uint64_t current = mSerial++;
ASSERT(mSerial > current); // Integer overflow
return Serial(current);
}
void reserve(RangedSerialFactory *rangeFactory, size_t count)
{
uint64_t current = mSerial;
mSerial += count;
ASSERT(mSerial > current); // Integer overflow
rangeFactory->initialize(current, count);
}
private:
std::atomic<uint64_t> mSerial;
};
// For backend that supports multiple queue serials, QueueSerial includes a Serial and an index.
using SerialIndex = uint32_t;
static constexpr SerialIndex kInvalidQueueSerialIndex = SerialIndex(-1);
class QueueSerial;
// Because we release queue index when context becomes non-current, in order to use up all index
// count, you will need to have 256 threads each has a context current. This is not a reasonable
// usage case.
constexpr size_t kMaxQueueSerialIndexCount = 256;
// Fixed array of queue serials
class AtomicQueueSerialFixedArray final
{
public:
AtomicQueueSerialFixedArray() = default;
~AtomicQueueSerialFixedArray() = default;
void setQueueSerial(SerialIndex index, Serial serial);
void setQueueSerial(const QueueSerial &queueSerial);
void fill(Serial serial) { std::fill(mSerials.begin(), mSerials.end(), serial); }
Serial operator[](SerialIndex index) const { return mSerials[index].getSerial(); }
size_t size() const { return mSerials.size(); }
private:
std::array<AtomicQueueSerial, kMaxQueueSerialIndexCount> mSerials;
};
std::ostream &operator<<(std::ostream &os, const AtomicQueueSerialFixedArray &serials);
class QueueSerial final
{
public:
QueueSerial() : mIndex(kInvalidQueueSerialIndex) {}
QueueSerial(SerialIndex index, Serial serial) : mIndex(index), mSerial(serial)
{
ASSERT(index != kInvalidQueueSerialIndex);
}
constexpr QueueSerial(const QueueSerial &other) = default;
QueueSerial &operator=(const QueueSerial &other) = default;
constexpr bool operator==(const QueueSerial &other) const
{
return mIndex == other.mIndex && mSerial == other.mSerial;
}
constexpr bool operator!=(const QueueSerial &other) const
{
return mIndex != other.mIndex || mSerial != other.mSerial;
}
constexpr bool operator<(const QueueSerial &other) const
{
ASSERT(mIndex != kInvalidQueueSerialIndex);
ASSERT(mIndex == other.mIndex);
return mSerial < other.mSerial;
}
constexpr bool operator<=(const QueueSerial &other) const
{
ASSERT(mIndex != kInvalidQueueSerialIndex);
ASSERT(mIndex == other.mIndex);
return mSerial <= other.mSerial;
}
constexpr bool operator>(const QueueSerial &other) const
{
ASSERT(mIndex != kInvalidQueueSerialIndex);
ASSERT(mIndex == other.mIndex);
return mSerial > other.mSerial;
}
constexpr bool operator>=(const QueueSerial &other) const
{
ASSERT(mIndex != kInvalidQueueSerialIndex);
ASSERT(mIndex == other.mIndex);
return mSerial >= other.mSerial;
}
bool operator>(const AtomicQueueSerialFixedArray &serials) const
{
ASSERT(mIndex != kInvalidQueueSerialIndex);
return mSerial > serials[mIndex];
}
bool operator<=(const AtomicQueueSerialFixedArray &serials) const
{
ASSERT(mIndex != kInvalidQueueSerialIndex);
return mSerial <= serials[mIndex];
}
constexpr bool valid() const { return mIndex != kInvalidQueueSerialIndex; }
SerialIndex getIndex() const { return mIndex; }
Serial getSerial() const { return mSerial; }
private:
SerialIndex mIndex;
Serial mSerial;
};
std::ostream &operator<<(std::ostream &os, const QueueSerial &queueSerial);
ANGLE_INLINE void AtomicQueueSerialFixedArray::setQueueSerial(SerialIndex index, Serial serial)
{
ASSERT(index != kInvalidQueueSerialIndex);
ASSERT(index < mSerials.size());
// Serial can only increase
ASSERT(serial > mSerials[index].getSerial());
mSerials[index] = serial;
}
ANGLE_INLINE void AtomicQueueSerialFixedArray::setQueueSerial(const QueueSerial &queueSerial)
{
setQueueSerial(queueSerial.getIndex(), queueSerial.getSerial());
}
ANGLE_INLINE std::ostream &operator<<(std::ostream &os, const AtomicQueueSerialFixedArray &serials)
{
// Search for last non-zero index (or 0 if all zeros).
SerialIndex lastIndex = serials.size() == 0 ? 0 : static_cast<SerialIndex>(serials.size() - 1);
while (lastIndex > 0 && serials[lastIndex].getValue() == 0)
{
lastIndex--;
}
os << '{';
for (SerialIndex i = 0; i < lastIndex; i++)
{
os << serials[i].getValue() << ',';
}
os << serials[lastIndex].getValue() << '}';
return os;
}
ANGLE_INLINE std::ostream &operator<<(std::ostream &os, const QueueSerial &queueSerial)
{
os << '{' << queueSerial.getIndex() << ':' << queueSerial.getSerial().getValue() << '}';
return os;
}
} // namespace rx
#endif // LIBANGLE_RENDERER_SERIAL_UTILS_H_