| 1 |
e. |
| 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 |
#pragma once |
| 32 |
|
| 33 |
#include "../common/TracyAlloc.hpp" |
| 34 |
#include "../common/TracyForceInline.hpp" |
| 35 |
#include "../common/TracySystem.hpp" |
| 36 |
|
| 37 |
#if defined(__GNUC__) |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#pragma GCC diagnostic push |
| 42 |
#pragma GCC diagnostic ignored "-Wconversion" |
| 43 |
#endif |
| 44 |
|
| 45 |
#if defined(__APPLE__) |
| 46 |
#include "TargetConditionals.h" |
| 47 |
#endif |
| 48 |
|
| 49 |
#include <atomic> |
| 50 |
#include <cassert> |
| 51 |
#include <cstddef> |
| 52 |
#include <cstdint> |
| 53 |
#include <cstdlib> |
| 54 |
#include <type_traits> |
| 55 |
#include <algorithm> |
| 56 |
#include <utility> |
| 57 |
#include <limits> |
| 58 |
#include <climits> |
| 59 |
#include <array> |
| 60 |
#include <thread> |
| 61 |
|
| 62 |
namespace tracy |
| 63 |
{ |
| 64 |
|
| 65 |
|
| 66 |
namespace moodycamel { namespace details { |
| 67 |
#if defined(__GNUC__) |
| 68 |
inline bool cqLikely(bool x) { return __builtin_expect((x), true); } |
| 69 |
inline bool cqUnlikely(bool x) { return __builtin_expect((x), false); } |
| 70 |
#else |
| 71 |
inline bool cqLikely(bool x) { return x; } |
| 72 |
inline bool cqUnlikely(bool x) { return x; } |
| 73 |
#endif |
| 74 |
} } |
| 75 |
|
| 76 |
namespace |
| 77 |
{ |
| 78 |
|
| 79 |
template <bool> |
| 80 |
struct compile_time_condition |
| 81 |
{ |
| 82 |
static const bool value = false; |
| 83 |
}; |
| 84 |
template <> |
| 85 |
struct compile_time_condition<true> |
| 86 |
{ |
| 87 |
static const bool value = true; |
| 88 |
}; |
| 89 |
} |
| 90 |
|
| 91 |
namespace moodycamel { |
| 92 |
namespace details { |
| 93 |
template<typename T> |
| 94 |
struct const_numeric_max { |
| 95 |
static_assert(std::is_integral<T>::value, "const_numeric_max can only be used with integers"); |
| 96 |
static const T value = std::numeric_limits<T>::is_signed |
| 97 |
? (static_cast<T>(1) << (sizeof(T) * CHAR_BIT - 1)) - static_cast<T>(1) |
| 98 |
: static_cast<T>(-1); |
| 99 |
}; |
| 100 |
|
| 101 |
#if defined(__GLIBCXX__) |
| 102 |
typedef ::max_align_t std_max_align_t; |
| 103 |
#else |
| 104 |
typedef std::max_align_t std_max_align_t; |
| 105 |
#endif |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
typedef union { |
| 110 |
std_max_align_t x; |
| 111 |
long long y; |
| 112 |
void* z; |
| 113 |
} max_align_t; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
struct ConcurrentQueueDefaultTraits |
| 123 |
{ |
| 124 |
|
| 125 |
typedef std::size_t size_t; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
typedef std::size_t index_t; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
static const size_t BLOCK_SIZE = 64*1024; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static const size_t EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD = 32; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
static const size_t EXPLICIT_INITIAL_INDEX_SIZE = 32; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
static const std::uint32_t EXPLICIT_CONSUMER_CONSUMPTION_QUOTA_BEFORE_ROTATE = 256; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
static const size_t MAX_SUBQUEUE_SIZE = details::const_numeric_max<size_t>::value; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
#if defined(malloc) || defined(free) |
| 172 |
|
| 173 |
|
| 174 |
static inline void* WORKAROUND_malloc(size_t size) { return malloc(size); } |
| 175 |
static inline void WORKAROUND_free(void* ptr) { return free(ptr); } |
| 176 |
static inline void* (malloc)(size_t size) { return WORKAROUND_malloc(size); } |
| 177 |
static inline void (free)(void* ptr) { return WORKAROUND_free(ptr); } |
| 178 |
#else |
| 179 |
static inline void* malloc(size_t size) { return tracy::tracy_malloc(size); } |
| 180 |
static inline void free(void* ptr) { return tracy::tracy_free(ptr); } |
| 181 |
#endif |
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
struct ProducerToken; |
| 193 |
struct ConsumerToken; |
| 194 |
|
| 195 |
template<typename T, typename Traits> class ConcurrentQueue; |
| 196 |
|
| 197 |
|
| 198 |
namespace details |
| 199 |
{ |
| 200 |
struct ConcurrentQueueProducerTypelessBase |
| 201 |
{ |
| 202 |
ConcurrentQueueProducerTypelessBase* next; |
| 203 |
std::atomic<bool> inactive; |
| 204 |
ProducerToken* token; |
| 205 |
uint32_t threadId; |
| 206 |
|
| 207 |
ConcurrentQueueProducerTypelessBase() |
| 208 |
: next(nullptr), inactive(false), token(nullptr), threadId(0) |
| 209 |
{ |
| 210 |
} |
| 211 |
}; |
| 212 |
|
| 213 |
template<typename T> |
| 214 |
static inline bool circular_less_than(T a, T b) |
| 215 |
{ |
| 216 |
static_assert(std::is_integral<T>::value && !std::numeric_limits<T>::is_signed, "circular_less_than is intended to be used only with unsigned integer types"); |
| 217 |
return static_cast<T>(a - b) > static_cast<T>(static_cast<T>(1) << (static_cast<T>(sizeof(T) * CHAR_BIT - 1))); |
| 218 |
|
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
template<typename U> |
| 223 |
static inline char* align_for(char* ptr) |
| 224 |
{ |
| 225 |
const std::size_t alignment = std::alignment_of<U>::value; |
| 226 |
return ptr + (alignment - (reinterpret_cast<std::uintptr_t>(ptr) % alignment)) % alignment; |
| 227 |
} |
| 228 |
|
| 229 |
template<typename T> |
| 230 |
static inline T ceil_to_pow_2(T x) |
| 231 |
{ |
| 232 |
static_assert(std::is_integral<T>::value && !std::numeric_limits<T>::is_signed, "ceil_to_pow_2 is intended to be used only with unsigned integer types"); |
| 233 |
|
| 234 |
|
| 235 |
--x; |
| 236 |
x |= x >> 1; |
| 237 |
x |= x >> 2; |
| 238 |
x |= x >> 4; |
| 239 |
for (std::size_t i = 1; i < sizeof(T); i <<= 1) { |
| 240 |
x |= x >> (i << 3); |
| 241 |
} |
| 242 |
++x; |
| 243 |
return x; |
| 244 |
} |
| 245 |
|
| 246 |
template<typename T> |
| 247 |
static inline void swap_relaxed(std::atomic<T>& left, std::atomic<T>& right) |
| 248 |
{ |
| 249 |
T temp = std::move(left.load(std::memory_order_relaxed)); |
| 250 |
left.store(std::move(right.load(std::memory_order_relaxed)), std::memory_order_relaxed); |
| 251 |
right.store(std::move(temp), std::memory_order_relaxed); |
| 252 |
} |
| 253 |
|
| 254 |
template<typename T> |
| 255 |
static inline T const& nomove(T const& x) |
| 256 |
{ |
| 257 |
return x; |
| 258 |
} |
| 259 |
|
| 260 |
template<bool Enable> |
| 261 |
struct nomove_if |
| 262 |
{ |
| 263 |
template<typename T> |
| 264 |
static inline T const& eval(T const& x) |
| 265 |
{ |
| 266 |
return x; |
| 267 |
} |
| 268 |
}; |
| 269 |
|
| 270 |
template<> |
| 271 |
struct nomove_if<false> |
| 272 |
{ |
| 273 |
template<typename U> |
| 274 |
static inline auto eval(U&& x) |
| 275 |
-> decltype(std::forward<U>(x)) |
| 276 |
{ |
| 277 |
return std::forward<U>(x); |
| 278 |
} |
| 279 |
}; |
| 280 |
|
| 281 |
template<typename It> |
| 282 |
static inline auto deref_noexcept(It& it) noexcept -> decltype(*it) |
| 283 |
{ |
| 284 |
return *it; |
| 285 |
} |
| 286 |
|
| 287 |
#if defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) |
| 288 |
template<typename T> struct is_trivially_destructible : std::is_trivially_destructible<T> { }; |
| 289 |
#else |
| 290 |
template<typename T> struct is_trivially_destructible : std::has_trivial_destructor<T> { }; |
| 291 |
#endif |
| 292 |
|
| 293 |
template<typename T> struct static_is_lock_free_num { enum { value = 0 }; }; |
| 294 |
template<> struct static_is_lock_free_num<signed char> { enum { value = ATOMIC_CHAR_LOCK_FREE }; }; |
| 295 |
template<> struct static_is_lock_free_num<short> { enum { value = ATOMIC_SHORT_LOCK_FREE }; }; |
| 296 |
template<> struct static_is_lock_free_num<int> { enum { value = ATOMIC_INT_LOCK_FREE }; }; |
| 297 |
template<> struct static_is_lock_free_num<long> { enum { value = ATOMIC_LONG_LOCK_FREE }; }; |
| 298 |
template<> struct static_is_lock_free_num<long long> { enum { value = ATOMIC_LLONG_LOCK_FREE }; }; |
| 299 |
template<typename T> struct static_is_lock_free : static_is_lock_free_num<typename std::make_signed<T>::type> { }; |
| 300 |
template<> struct static_is_lock_free<bool> { enum { value = ATOMIC_BOOL_LOCK_FREE }; }; |
| 301 |
template<typename U> struct static_is_lock_free<U*> { enum { value = ATOMIC_POINTER_LOCK_FREE }; }; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
struct ProducerToken |
| 306 |
{ |
| 307 |
template<typename T, typename Traits> |
| 308 |
explicit ProducerToken(ConcurrentQueue<T, Traits>& queue); |
| 309 |
|
| 310 |
ProducerToken(ProducerToken&& other) noexcept |
| 311 |
: producer(other.producer) |
| 312 |
{ |
| 313 |
other.producer = nullptr; |
| 314 |
if (producer != nullptr) { |
| 315 |
producer->token = this; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
inline ProducerToken& operator=(ProducerToken&& other) noexcept |
| 320 |
{ |
| 321 |
swap(other); |
| 322 |
return *this; |
| 323 |
} |
| 324 |
|
| 325 |
void swap(ProducerToken& other) noexcept |
| 326 |
{ |
| 327 |
std::swap(producer, other.producer); |
| 328 |
if (producer != nullptr) { |
| 329 |
producer->token = this; |
| 330 |
} |
| 331 |
if (other.producer != nullptr) { |
| 332 |
other.producer->token = &other; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
inline bool valid() const { return producer != nullptr; } |
| 345 |
|
| 346 |
~ProducerToken() |
| 347 |
{ |
| 348 |
if (producer != nullptr) { |
| 349 |
producer->token = nullptr; |
| 350 |
producer->inactive.store(true, std::memory_order_release); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
ProducerToken(ProducerToken const&) = delete; |
| 356 |
ProducerToken& operator=(ProducerToken const&) = delete; |
| 357 |
|
| 358 |
private: |
| 359 |
template<typename T, typename Traits> friend class ConcurrentQueue; |
| 360 |
|
| 361 |
protected: |
| 362 |
details::ConcurrentQueueProducerTypelessBase* producer; |
| 363 |
}; |
| 364 |
|
| 365 |
|
| 366 |
struct ConsumerToken |
| 367 |
{ |
| 368 |
template<typename T, typename Traits> |
| 369 |
explicit ConsumerToken(ConcurrentQueue<T, Traits>& q); |
| 370 |
|
| 371 |
ConsumerToken(ConsumerToken&& other) noexcept |
| 372 |
: initialOffset(other.initialOffset), lastKnownGlobalOffset(other.lastKnownGlobalOffset), itemsConsumedFromCurrent(other.itemsConsumedFromCurrent), currentProducer(other.currentProducer), desiredProducer(other.desiredProducer) |
| 373 |
{ |
| 374 |
} |
| 375 |
|
| 376 |
inline ConsumerToken& operator=(ConsumerToken&& other) noexcept |
| 377 |
{ |
| 378 |
swap(other); |
| 379 |
return *this; |
| 380 |
} |
| 381 |
|
| 382 |
void swap(ConsumerToken& other) noexcept |
| 383 |
{ |
| 384 |
std::swap(initialOffset, other.initialOffset); |
| 385 |
std::swap(lastKnownGlobalOffset, other.lastKnownGlobalOffset); |
| 386 |
std::swap(itemsConsumedFromCurrent, other.itemsConsumedFromCurrent); |
| 387 |
std::swap(currentProducer, other.currentProducer); |
| 388 |
std::swap(desiredProducer, other.desiredProducer); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
ConsumerToken(ConsumerToken const&) = delete; |
| 393 |
ConsumerToken& operator=(ConsumerToken const&) = delete; |
| 394 |
|
| 395 |
private: |
| 396 |
template<typename T, typename Traits> friend class ConcurrentQueue; |
| 397 |
|
| 398 |
private: |
| 399 |
std::uint32_t initialOffset; |
| 400 |
std::uint32_t lastKnownGlobalOffset; |
| 401 |
std::uint32_t itemsConsumedFromCurrent; |
| 402 |
details::ConcurrentQueueProducerTypelessBase* currentProducer; |
| 403 |
details::ConcurrentQueueProducerTypelessBase* desiredProducer; |
| 404 |
}; |
| 405 |
|
| 406 |
|
| 407 |
template<typename T, typename Traits = ConcurrentQueueDefaultTraits> |
| 408 |
class ConcurrentQueue |
| 409 |
{ |
| 410 |
public: |
| 411 |
struct ExplicitProducer; |
| 412 |
|
| 413 |
typedef moodycamel::ProducerToken producer_token_t; |
| 414 |
typedef moodycamel::ConsumerToken consumer_token_t; |
| 415 |
|
| 416 |
typedef typename Traits::index_t index_t; |
| 417 |
typedef typename Traits::size_t size_t; |
| 418 |
|
| 419 |
static const size_t BLOCK_SIZE = static_cast<size_t>(Traits::BLOCK_SIZE); |
| 420 |
static const size_t EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD = static_cast<size_t>(Traits::EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD); |
| 421 |
static const size_t EXPLICIT_INITIAL_INDEX_SIZE = static_cast<size_t>(Traits::EXPLICIT_INITIAL_INDEX_SIZE); |
| 422 |
static const std::uint32_t EXPLICIT_CONSUMER_CONSUMPTION_QUOTA_BEFORE_ROTATE = static_cast<std::uint32_t>(Traits::EXPLICIT_CONSUMER_CONSUMPTION_QUOTA_BEFORE_ROTATE); |
| 423 |
#ifdef _MSC_VER |
| 424 |
#pragma warning(push) |
| 425 |
#pragma warning(disable: 4307) |
| 426 |
#pragma warning(disable: 4309) |
| 427 |
#endif |
| 428 |
static const size_t MAX_SUBQUEUE_SIZE = (details::const_numeric_max<size_t>::value - static_cast<size_t>(Traits::MAX_SUBQUEUE_SIZE) < BLOCK_SIZE) ? details::const_numeric_max<size_t>::value : ((static_cast<size_t>(Traits::MAX_SUBQUEUE_SIZE) + (BLOCK_SIZE - 1)) / BLOCK_SIZE * BLOCK_SIZE); |
| 429 |
#ifdef _MSC_VER |
| 430 |
#pragma warning(pop) |
| 431 |
#endif |
| 432 |
|
| 433 |
static_assert(!std::numeric_limits<size_t>::is_signed && std::is_integral<size_t>::value, "Traits::size_t must be an unsigned integral type"); |
| 434 |
static_assert(!std::numeric_limits<index_t>::is_signed && std::is_integral<index_t>::value, "Traits::index_t must be an unsigned integral type"); |
| 435 |
static_assert(sizeof(index_t) >= sizeof(size_t), "Traits::index_t must be at least as wide as Traits::size_t"); |
| 436 |
static_assert((BLOCK_SIZE > 1) && !(BLOCK_SIZE & (BLOCK_SIZE - 1)), "Traits::BLOCK_SIZE must be a power of 2 (and at least 2)"); |
| 437 |
static_assert((EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD > 1) && !(EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD & (EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD - 1)), "Traits::EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD must be a power of 2 (and greater than 1)"); |
| 438 |
static_assert((EXPLICIT_INITIAL_INDEX_SIZE > 1) && !(EXPLICIT_INITIAL_INDEX_SIZE & (EXPLICIT_INITIAL_INDEX_SIZE - 1)), "Traits::EXPLICIT_INITIAL_INDEX_SIZE must be a power of 2 (and greater than 1)"); |
| 439 |
|
| 440 |
public: |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
explicit ConcurrentQueue(size_t capacity = 6 * BLOCK_SIZE) |
| 452 |
: producerListTail(nullptr), |
| 453 |
producerCount(0), |
| 454 |
initialBlockPoolIndex(0), |
| 455 |
nextExplicitConsumerId(0), |
| 456 |
globalExplicitConsumerOffset(0) |
| 457 |
{ |
| 458 |
populate_initial_block_list(capacity / BLOCK_SIZE + ((capacity & (BLOCK_SIZE - 1)) == 0 ? 0 : 1)); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
ConcurrentQueue(size_t minCapacity, size_t maxExplicitProducers) |
| 465 |
: producerListTail(nullptr), |
| 466 |
producerCount(0), |
| 467 |
initialBlockPoolIndex(0), |
| 468 |
nextExplicitConsumerId(0), |
| 469 |
globalExplicitConsumerOffset(0) |
| 470 |
{ |
| 471 |
size_t blocks = (((minCapacity + BLOCK_SIZE - 1) / BLOCK_SIZE) - 1) * (maxExplicitProducers + 1) + 2 * (maxExplicitProducers); |
| 472 |
populate_initial_block_list(blocks); |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
~ConcurrentQueue() |
| 479 |
{ |
| 480 |
|
| 481 |
auto ptr = producerListTail.load(std::memory_order_relaxed); |
| 482 |
while (ptr != nullptr) { |
| 483 |
auto next = ptr->next_prod(); |
| 484 |
if (ptr->token != nullptr) { |
| 485 |
ptr->token->producer = nullptr; |
| 486 |
} |
| 487 |
destroy(ptr); |
| 488 |
ptr = next; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
auto block = freeList.head_unsafe(); |
| 493 |
while (block != nullptr) { |
| 494 |
auto next = block->freeListNext.load(std::memory_order_relaxed); |
| 495 |
if (block->dynamicallyAllocated) { |
| 496 |
destroy(block); |
| 497 |
} |
| 498 |
block = next; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
destroy_array(initialBlockPool, initialBlockPoolSize); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
ConcurrentQueue(ConcurrentQueue const&) = delete; |
| 507 |
ConcurrentQueue(ConcurrentQueue&& other) = delete; |
| 508 |
ConcurrentQueue& operator=(ConcurrentQueue const&) = delete; |
| 509 |
ConcurrentQueue& operator=(ConcurrentQueue&& other) = delete; |
| 510 |
|
| 511 |
public: |
| 512 |
tracy_force_inline T* enqueue_begin(producer_token_t const& token, index_t& currentTailIndex) |
| 513 |
{ |
| 514 |
return static_cast<ExplicitProducer*>(token.producer)->ConcurrentQueue::ExplicitProducer::enqueue_begin(currentTailIndex); |
| 515 |
} |
| 516 |
|
| 517 |
template<class NotifyThread, class ProcessData> |
| 518 |
size_t try_dequeue_bulk_single(consumer_token_t& token, NotifyThread notifyThread, ProcessData processData ) |
| 519 |
{ |
| 520 |
if (token.desiredProducer == nullptr || token.lastKnownGlobalOffset != globalExplicitConsumerOffset.load(std::memory_order_relaxed)) { |
| 521 |
if (!update_current_producer_after_rotation(token)) { |
| 522 |
return 0; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
size_t count = static_cast<ProducerBase*>(token.currentProducer)->dequeue_bulk(notifyThread, processData); |
| 527 |
token.itemsConsumedFromCurrent += static_cast<std::uint32_t>(count); |
| 528 |
|
| 529 |
auto tail = producerListTail.load(std::memory_order_acquire); |
| 530 |
auto ptr = static_cast<ProducerBase*>(token.currentProducer)->next_prod(); |
| 531 |
if (ptr == nullptr) { |
| 532 |
ptr = tail; |
| 533 |
} |
| 534 |
if( count == 0 ) |
| 535 |
{ |
| 536 |
while (ptr != static_cast<ProducerBase*>(token.currentProducer)) { |
| 537 |
auto dequeued = ptr->dequeue_bulk(notifyThread, processData); |
| 538 |
if (dequeued != 0) { |
| 539 |
token.currentProducer = ptr; |
| 540 |
token.itemsConsumedFromCurrent = static_cast<std::uint32_t>(dequeued); |
| 541 |
return dequeued; |
| 542 |
} |
| 543 |
ptr = ptr->next_prod(); |
| 544 |
if (ptr == nullptr) { |
| 545 |
ptr = tail; |
| 546 |
} |
| 547 |
} |
| 548 |
return 0; |
| 549 |
} |
| 550 |
else |
| 551 |
{ |
| 552 |
token.currentProducer = ptr; |
| 553 |
token.itemsConsumedFromCurrent = 0; |
| 554 |
return count; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
size_t size_approx() const |
| 566 |
{ |
| 567 |
size_t size = 0; |
| 568 |
for (auto ptr = producerListTail.load(std::memory_order_acquire); ptr != nullptr; ptr = ptr->next_prod()) { |
| 569 |
size += ptr->size_approx(); |
| 570 |
} |
| 571 |
return size; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
static bool is_lock_free() |
| 579 |
{ |
| 580 |
return |
| 581 |
details::static_is_lock_free<bool>::value == 2 && |
| 582 |
details::static_is_lock_free<size_t>::value == 2 && |
| 583 |
details::static_is_lock_free<std::uint32_t>::value == 2 && |
| 584 |
details::static_is_lock_free<index_t>::value == 2 && |
| 585 |
details::static_is_lock_free<void*>::value == 2; |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
private: |
| 590 |
friend struct ProducerToken; |
| 591 |
friend struct ConsumerToken; |
| 592 |
friend struct ExplicitProducer; |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
inline bool update_current_producer_after_rotation(consumer_token_t& token) |
| 600 |
{ |
| 601 |
|
| 602 |
auto tail = producerListTail.load(std::memory_order_acquire); |
| 603 |
if (token.desiredProducer == nullptr && tail == nullptr) { |
| 604 |
return false; |
| 605 |
} |
| 606 |
auto prodCount = producerCount.load(std::memory_order_relaxed); |
| 607 |
auto globalOffset = globalExplicitConsumerOffset.load(std::memory_order_relaxed); |
| 608 |
if (details::cqUnlikely(token.desiredProducer == nullptr)) { |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
std::uint32_t offset = prodCount - 1 - (token.initialOffset % prodCount); |
| 613 |
token.desiredProducer = tail; |
| 614 |
for (std::uint32_t i = 0; i != offset; ++i) { |
| 615 |
token.desiredProducer = static_cast<ProducerBase*>(token.desiredProducer)->next_prod(); |
| 616 |
if (token.desiredProducer == nullptr) { |
| 617 |
token.desiredProducer = tail; |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
std::uint32_t delta = globalOffset - token.lastKnownGlobalOffset; |
| 623 |
if (delta >= prodCount) { |
| 624 |
delta = delta % prodCount; |
| 625 |
} |
| 626 |
for (std::uint32_t i = 0; i != delta; ++i) { |
| 627 |
token.desiredProducer = static_cast<ProducerBase*>(token.desiredProducer)->next_prod(); |
| 628 |
if (token.desiredProducer == nullptr) { |
| 629 |
token.desiredProducer = tail; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
token.lastKnownGlobalOffset = globalOffset; |
| 634 |
token.currentProducer = token.desiredProducer; |
| 635 |
token.itemsConsumedFromCurrent = 0; |
| 636 |
return true; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
template <typename N> |
| 645 |
struct FreeListNode |
| 646 |
{ |
| 647 |
FreeListNode() : freeListRefs(0), freeListNext(nullptr) { } |
| 648 |
|
| 649 |
std::atomic<std::uint32_t> freeListRefs; |
| 650 |
std::atomic<N*> freeListNext; |
| 651 |
}; |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
template<typename N> |
| 657 |
struct FreeList |
| 658 |
{ |
| 659 |
FreeList() : freeListHead(nullptr) { } |
| 660 |
FreeList(FreeList&& other) : freeListHead(other.freeListHead.load(std::memory_order_relaxed)) { other.freeListHead.store(nullptr, std::memory_order_relaxed); } |
| 661 |
void swap(FreeList& other) { details::swap_relaxed(freeListHead, other.freeListHead); } |
| 662 |
|
| 663 |
FreeList(FreeList const&) = delete; |
| 664 |
FreeList& operator=(FreeList const&) = delete; |
| 665 |
|
| 666 |
inline void add(N* node) |
| 667 |
{ |
| 668 |
|
| 669 |
|
| 670 |
if (node->freeListRefs.fetch_add(SHOULD_BE_ON_FREELIST, std::memory_order_acq_rel) == 0) { |
| 671 |
|
| 672 |
|
| 673 |
add_knowing_refcount_is_zero(node); |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
inline N* try_get() |
| 678 |
{ |
| 679 |
auto head = freeListHead.load(std::memory_order_acquire); |
| 680 |
while (head != nullptr) { |
| 681 |
auto prevHead = head; |
| 682 |
auto refs = head->freeListRefs.load(std::memory_order_relaxed); |
| 683 |
if ((refs & REFS_MASK) == 0 || !head->freeListRefs.compare_exchange_strong(refs, refs + 1, std::memory_order_acquire, std::memory_order_relaxed)) { |
| 684 |
head = freeListHead.load(std::memory_order_acquire); |
| 685 |
continue; |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
auto next = head->freeListNext.load(std::memory_order_relaxed); |
| 691 |
if (freeListHead.compare_exchange_strong(head, next, std::memory_order_acquire, std::memory_order_relaxed)) { |
| 692 |
|
| 693 |
|
| 694 |
assert((head->freeListRefs.load(std::memory_order_relaxed) & SHOULD_BE_ON_FREELIST) == 0); |
| 695 |
|
| 696 |
|
| 697 |
head->freeListRefs.fetch_sub(2, std::memory_order_release); |
| 698 |
return head; |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
refs = prevHead->freeListRefs.fetch_sub(1, std::memory_order_acq_rel); |
| 705 |
if (refs == SHOULD_BE_ON_FREELIST + 1) { |
| 706 |
add_knowing_refcount_is_zero(prevHead); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
return nullptr; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
N* head_unsafe() const { return freeListHead.load(std::memory_order_relaxed); } |
| 715 |
|
| 716 |
private: |
| 717 |
inline void add_knowing_refcount_is_zero(N* node) |
| 718 |
{ |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
auto head = freeListHead.load(std::memory_order_relaxed); |
| 728 |
while (true) { |
| 729 |
node->freeListNext.store(head, std::memory_order_relaxed); |
| 730 |
node->freeListRefs.store(1, std::memory_order_release); |
| 731 |
if (!freeListHead.compare_exchange_strong(head, node, std::memory_order_release, std::memory_order_relaxed)) { |
| 732 |
|
| 733 |
if (node->freeListRefs.fetch_add(SHOULD_BE_ON_FREELIST - 1, std::memory_order_release) == 1) { |
| 734 |
continue; |
| 735 |
} |
| 736 |
} |
| 737 |
return; |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
private: |
| 742 |
|
| 743 |
std::atomic<N*> freeListHead; |
| 744 |
|
| 745 |
static const std::uint32_t REFS_MASK = 0x7FFFFFFF; |
| 746 |
static const std::uint32_t SHOULD_BE_ON_FREELIST = 0x80000000; |
| 747 |
}; |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
struct Block |
| 755 |
{ |
| 756 |
Block() |
| 757 |
: next(nullptr), elementsCompletelyDequeued(0), freeListRefs(0), freeListNext(nullptr), shouldBeOnFreeList(false), dynamicallyAllocated(true) |
| 758 |
{ |
| 759 |
} |
| 760 |
|
| 761 |
inline bool is_empty() const |
| 762 |
{ |
| 763 |
if (compile_time_condition<BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD>::value) { |
| 764 |
|
| 765 |
for (size_t i = 0; i < BLOCK_SIZE; ++i) { |
| 766 |
if (!emptyFlags[i].load(std::memory_order_relaxed)) { |
| 767 |
return false; |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
std::atomic_thread_fence(std::memory_order_acquire); |
| 773 |
return true; |
| 774 |
} |
| 775 |
else { |
| 776 |
|
| 777 |
if (elementsCompletelyDequeued.load(std::memory_order_relaxed) == BLOCK_SIZE) { |
| 778 |
std::atomic_thread_fence(std::memory_order_acquire); |
| 779 |
return true; |
| 780 |
} |
| 781 |
assert(elementsCompletelyDequeued.load(std::memory_order_relaxed) <= BLOCK_SIZE); |
| 782 |
return false; |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
inline bool set_empty(index_t i) |
| 788 |
{ |
| 789 |
if (BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD) { |
| 790 |
|
| 791 |
assert(!emptyFlags[BLOCK_SIZE - 1 - static_cast<size_t>(i & static_cast<index_t>(BLOCK_SIZE - 1))].load(std::memory_order_relaxed)); |
| 792 |
emptyFlags[BLOCK_SIZE - 1 - static_cast<size_t>(i & static_cast<index_t>(BLOCK_SIZE - 1))].store(true, std::memory_order_release); |
| 793 |
return false; |
| 794 |
} |
| 795 |
else { |
| 796 |
|
| 797 |
auto prevVal = elementsCompletelyDequeued.fetch_add(1, std::memory_order_release); |
| 798 |
assert(prevVal < BLOCK_SIZE); |
| 799 |
return prevVal == BLOCK_SIZE - 1; |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
inline bool set_many_empty(index_t i, size_t count) |
| 806 |
{ |
| 807 |
if (compile_time_condition<BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD>::value) { |
| 808 |
|
| 809 |
std::atomic_thread_fence(std::memory_order_release); |
| 810 |
i = BLOCK_SIZE - 1 - static_cast<size_t>(i & static_cast<index_t>(BLOCK_SIZE - 1)) - count + 1; |
| 811 |
for (size_t j = 0; j != count; ++j) { |
| 812 |
assert(!emptyFlags[i + j].load(std::memory_order_relaxed)); |
| 813 |
emptyFlags[i + j].store(true, std::memory_order_relaxed); |
| 814 |
} |
| 815 |
return false; |
| 816 |
} |
| 817 |
else { |
| 818 |
|
| 819 |
auto prevVal = elementsCompletelyDequeued.fetch_add(count, std::memory_order_release); |
| 820 |
assert(prevVal + count <= BLOCK_SIZE); |
| 821 |
return prevVal + count == BLOCK_SIZE; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
inline void set_all_empty() |
| 826 |
{ |
| 827 |
if (BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD) { |
| 828 |
|
| 829 |
for (size_t i = 0; i != BLOCK_SIZE; ++i) { |
| 830 |
emptyFlags[i].store(true, std::memory_order_relaxed); |
| 831 |
} |
| 832 |
} |
| 833 |
else { |
| 834 |
|
| 835 |
elementsCompletelyDequeued.store(BLOCK_SIZE, std::memory_order_relaxed); |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
inline void reset_empty() |
| 840 |
{ |
| 841 |
if (compile_time_condition<BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD>::value) { |
| 842 |
|
| 843 |
for (size_t i = 0; i != BLOCK_SIZE; ++i) { |
| 844 |
emptyFlags[i].store(false, std::memory_order_relaxed); |
| 845 |
} |
| 846 |
} |
| 847 |
else { |
| 848 |
|
| 849 |
elementsCompletelyDequeued.store(0, std::memory_order_relaxed); |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
inline T* operator[](index_t idx) noexcept { return static_cast<T*>(static_cast<void*>(elements)) + static_cast<size_t>(idx & static_cast<index_t>(BLOCK_SIZE - 1)); } |
| 854 |
inline T const* operator[](index_t idx) const noexcept { return static_cast<T const*>(static_cast<void const*>(elements)) + static_cast<size_t>(idx & static_cast<index_t>(BLOCK_SIZE - 1)); } |
| 855 |
|
| 856 |
private: |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
static_assert(std::alignment_of<T>::value <= std::alignment_of<details::max_align_t>::value, "The queue does not support super-aligned types at this time"); |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
union { |
| 868 |
char elements[sizeof(T) * BLOCK_SIZE]; |
| 869 |
details::max_align_t dummy; |
| 870 |
}; |
| 871 |
public: |
| 872 |
Block* next; |
| 873 |
std::atomic<size_t> elementsCompletelyDequeued; |
| 874 |
std::atomic<bool> emptyFlags[BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD ? BLOCK_SIZE : 1]; |
| 875 |
public: |
| 876 |
std::atomic<std::uint32_t> freeListRefs; |
| 877 |
std::atomic<Block*> freeListNext; |
| 878 |
std::atomic<bool> shouldBeOnFreeList; |
| 879 |
bool dynamicallyAllocated; |
| 880 |
}; |
| 881 |
static_assert(std::alignment_of<Block>::value >= std::alignment_of<details::max_align_t>::value, "Internal error: Blocks must be at least as aligned as the type they are wrapping"); |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
struct ProducerBase : public details::ConcurrentQueueProducerTypelessBase |
| 889 |
{ |
| 890 |
ProducerBase(ConcurrentQueue* parent_) : |
| 891 |
tailIndex(0), |
| 892 |
headIndex(0), |
| 893 |
dequeueOptimisticCount(0), |
| 894 |
dequeueOvercommit(0), |
| 895 |
tailBlock(nullptr), |
| 896 |
parent(parent_) |
| 897 |
{ |
| 898 |
} |
| 899 |
|
| 900 |
virtual ~ProducerBase() { }; |
| 901 |
|
| 902 |
template<class NotifyThread, class ProcessData> |
| 903 |
inline size_t dequeue_bulk(NotifyThread notifyThread, ProcessData processData) |
| 904 |
{ |
| 905 |
return static_cast<ExplicitProducer*>(this)->dequeue_bulk(notifyThread, processData); |
| 906 |
} |
| 907 |
|
| 908 |
inline ProducerBase* next_prod() const { return static_cast<ProducerBase*>(next); } |
| 909 |
|
| 910 |
inline size_t size_approx() const |
| 911 |
{ |
| 912 |
auto tail = tailIndex.load(std::memory_order_relaxed); |
| 913 |
auto head = headIndex.load(std::memory_order_relaxed); |
| 914 |
return details::circular_less_than(head, tail) ? static_cast<size_t>(tail - head) : 0; |
| 915 |
} |
| 916 |
|
| 917 |
inline index_t getTail() const { return tailIndex.load(std::memory_order_relaxed); } |
| 918 |
protected: |
| 919 |
std::atomic<index_t> tailIndex; |
| 920 |
std::atomic<index_t> headIndex; |
| 921 |
|
| 922 |
std::atomic<index_t> dequeueOptimisticCount; |
| 923 |
std::atomic<index_t> dequeueOvercommit; |
| 924 |
|
| 925 |
Block* tailBlock; |
| 926 |
|
| 927 |
public: |
| 928 |
ConcurrentQueue* parent; |
| 929 |
}; |
| 930 |
|
| 931 |
|
| 932 |
public: |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
struct ExplicitProducer : public ProducerBase |
| 937 |
{ |
| 938 |
explicit ExplicitProducer(ConcurrentQueue* _parent) : |
| 939 |
ProducerBase(_parent), |
| 940 |
blockIndex(nullptr), |
| 941 |
pr_blockIndexSlotsUsed(0), |
| 942 |
pr_blockIndexSize(EXPLICIT_INITIAL_INDEX_SIZE >> 1), |
| 943 |
pr_blockIndexFront(0), |
| 944 |
pr_blockIndexEntries(nullptr), |
| 945 |
pr_blockIndexRaw(nullptr) |
| 946 |
{ |
| 947 |
size_t poolBasedIndexSize = details::ceil_to_pow_2(_parent->initialBlockPoolSize) >> 1; |
| 948 |
if (poolBasedIndexSize > pr_blockIndexSize) { |
| 949 |
pr_blockIndexSize = poolBasedIndexSize; |
| 950 |
} |
| 951 |
|
| 952 |
new_block_index(0); |
| 953 |
} |
| 954 |
|
| 955 |
~ExplicitProducer() |
| 956 |
{ |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
if (this->tailBlock != nullptr) { |
| 961 |
|
| 962 |
Block* halfDequeuedBlock = nullptr; |
| 963 |
if ((this->headIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1)) != 0) { |
| 964 |
|
| 965 |
|
| 966 |
size_t i = (pr_blockIndexFront - pr_blockIndexSlotsUsed) & (pr_blockIndexSize - 1); |
| 967 |
while (details::circular_less_than<index_t>(pr_blockIndexEntries[i].base + BLOCK_SIZE, this->headIndex.load(std::memory_order_relaxed))) { |
| 968 |
i = (i + 1) & (pr_blockIndexSize - 1); |
| 969 |
} |
| 970 |
assert(details::circular_less_than<index_t>(pr_blockIndexEntries[i].base, this->headIndex.load(std::memory_order_relaxed))); |
| 971 |
halfDequeuedBlock = pr_blockIndexEntries[i].block; |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
auto block = this->tailBlock; |
| 976 |
do { |
| 977 |
block = block->next; |
| 978 |
if (block->ConcurrentQueue::Block::is_empty()) { |
| 979 |
continue; |
| 980 |
} |
| 981 |
|
| 982 |
size_t i = 0; |
| 983 |
if (block == halfDequeuedBlock) { |
| 984 |
i = static_cast<size_t>(this->headIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1)); |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
auto lastValidIndex = (this->tailIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1)) == 0 ? BLOCK_SIZE : static_cast<size_t>(this->tailIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1)); |
| 989 |
while (i != BLOCK_SIZE && (block != this->tailBlock || i != lastValidIndex)) { |
| 990 |
(*block)[i++]->~T(); |
| 991 |
} |
| 992 |
} while (block != this->tailBlock); |
| 993 |
} |
| 994 |
|
| 995 |
|
| 996 |
if (this->tailBlock != nullptr) { |
| 997 |
auto block = this->tailBlock; |
| 998 |
do { |
| 999 |
auto nextBlock = block->next; |
| 1000 |
if (block->dynamicallyAllocated) { |
| 1001 |
destroy(block); |
| 1002 |
} |
| 1003 |
else { |
| 1004 |
this->parent->add_block_to_free_list(block); |
| 1005 |
} |
| 1006 |
block = nextBlock; |
| 1007 |
} while (block != this->tailBlock); |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
auto header = static_cast<BlockIndexHeader*>(pr_blockIndexRaw); |
| 1012 |
while (header != nullptr) { |
| 1013 |
auto prev = static_cast<BlockIndexHeader*>(header->prev); |
| 1014 |
header->~BlockIndexHeader(); |
| 1015 |
(Traits::free)(header); |
| 1016 |
header = prev; |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
inline void enqueue_begin_alloc(index_t currentTailIndex) |
| 1021 |
{ |
| 1022 |
|
| 1023 |
if (this->tailBlock != nullptr && this->tailBlock->next->ConcurrentQueue::Block::is_empty()) { |
| 1024 |
|
| 1025 |
this->tailBlock = this->tailBlock->next; |
| 1026 |
this->tailBlock->ConcurrentQueue::Block::reset_empty(); |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
} |
| 1034 |
else { |
| 1035 |
|
| 1036 |
if (pr_blockIndexRaw == nullptr || pr_blockIndexSlotsUsed == pr_blockIndexSize) { |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
new_block_index(pr_blockIndexSlotsUsed); |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
auto newBlock = this->parent->ConcurrentQueue::requisition_block(); |
| 1045 |
newBlock->ConcurrentQueue::Block::reset_empty(); |
| 1046 |
if (this->tailBlock == nullptr) { |
| 1047 |
newBlock->next = newBlock; |
| 1048 |
} |
| 1049 |
else { |
| 1050 |
newBlock->next = this->tailBlock->next; |
| 1051 |
this->tailBlock->next = newBlock; |
| 1052 |
} |
| 1053 |
this->tailBlock = newBlock; |
| 1054 |
++pr_blockIndexSlotsUsed; |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
auto& entry = blockIndex.load(std::memory_order_relaxed)->entries[pr_blockIndexFront]; |
| 1059 |
entry.base = currentTailIndex; |
| 1060 |
entry.block = this->tailBlock; |
| 1061 |
blockIndex.load(std::memory_order_relaxed)->front.store(pr_blockIndexFront, std::memory_order_release); |
| 1062 |
pr_blockIndexFront = (pr_blockIndexFront + 1) & (pr_blockIndexSize - 1); |
| 1063 |
} |
| 1064 |
|
| 1065 |
tracy_force_inline T* enqueue_begin(index_t& currentTailIndex) |
| 1066 |
{ |
| 1067 |
currentTailIndex = this->tailIndex.load(std::memory_order_relaxed); |
| 1068 |
if (details::cqUnlikely((currentTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0)) { |
| 1069 |
this->enqueue_begin_alloc(currentTailIndex); |
| 1070 |
} |
| 1071 |
return (*this->tailBlock)[currentTailIndex]; |
| 1072 |
} |
| 1073 |
|
| 1074 |
tracy_force_inline std::atomic<index_t>& get_tail_index() |
| 1075 |
{ |
| 1076 |
return this->tailIndex; |
| 1077 |
} |
| 1078 |
|
| 1079 |
template<class NotifyThread, class ProcessData> |
| 1080 |
size_t dequeue_bulk(NotifyThread notifyThread, ProcessData processData) |
| 1081 |
{ |
| 1082 |
auto tail = this->tailIndex.load(std::memory_order_relaxed); |
| 1083 |
auto overcommit = this->dequeueOvercommit.load(std::memory_order_relaxed); |
| 1084 |
auto desiredCount = static_cast<size_t>(tail - (this->dequeueOptimisticCount.load(std::memory_order_relaxed) - overcommit)); |
| 1085 |
if (details::circular_less_than<size_t>(0, desiredCount)) { |
| 1086 |
desiredCount = desiredCount < 8192 ? desiredCount : 8192; |
| 1087 |
std::atomic_thread_fence(std::memory_order_acquire); |
| 1088 |
|
| 1089 |
auto myDequeueCount = this->dequeueOptimisticCount.fetch_add(desiredCount, std::memory_order_relaxed); |
| 1090 |
assert(overcommit <= myDequeueCount); |
| 1091 |
|
| 1092 |
tail = this->tailIndex.load(std::memory_order_acquire); |
| 1093 |
auto actualCount = static_cast<size_t>(tail - (myDequeueCount - overcommit)); |
| 1094 |
if (details::circular_less_than<size_t>(0, actualCount)) { |
| 1095 |
actualCount = desiredCount < actualCount ? desiredCount : actualCount; |
| 1096 |
if (actualCount < desiredCount) { |
| 1097 |
this->dequeueOvercommit.fetch_add(desiredCount - actualCount, std::memory_order_release); |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
auto firstIndex = this->headIndex.fetch_add(actualCount, std::memory_order_acq_rel); |
| 1103 |
|
| 1104 |
|
| 1105 |
auto localBlockIndex = blockIndex.load(std::memory_order_acquire); |
| 1106 |
auto localBlockIndexHead = localBlockIndex->front.load(std::memory_order_acquire); |
| 1107 |
|
| 1108 |
auto headBase = localBlockIndex->entries[localBlockIndexHead].base; |
| 1109 |
auto firstBlockBaseIndex = firstIndex & ~static_cast<index_t>(BLOCK_SIZE - 1); |
| 1110 |
auto offset = static_cast<size_t>(static_cast<typename std::make_signed<index_t>::type>(firstBlockBaseIndex - headBase) / BLOCK_SIZE); |
| 1111 |
auto indexIndex = (localBlockIndexHead + offset) & (localBlockIndex->size - 1); |
| 1112 |
|
| 1113 |
notifyThread( this->threadId ); |
| 1114 |
|
| 1115 |
|
| 1116 |
auto index = firstIndex; |
| 1117 |
do { |
| 1118 |
auto firstIndexInBlock = index; |
| 1119 |
auto endIndex = (index & ~static_cast<index_t>(BLOCK_SIZE - 1)) + static_cast<index_t>(BLOCK_SIZE); |
| 1120 |
endIndex = details::circular_less_than<index_t>(firstIndex + static_cast<index_t>(actualCount), endIndex) ? firstIndex + static_cast<index_t>(actualCount) : endIndex; |
| 1121 |
auto block = localBlockIndex->entries[indexIndex].block; |
| 1122 |
|
| 1123 |
const auto sz = endIndex - index; |
| 1124 |
processData( (*block)[index], sz ); |
| 1125 |
index += sz; |
| 1126 |
|
| 1127 |
block->ConcurrentQueue::Block::set_many_empty(firstIndexInBlock, static_cast<size_t>(endIndex - firstIndexInBlock)); |
| 1128 |
indexIndex = (indexIndex + 1) & (localBlockIndex->size - 1); |
| 1129 |
} while (index != firstIndex + actualCount); |
| 1130 |
|
| 1131 |
return actualCount; |
| 1132 |
} |
| 1133 |
else { |
| 1134 |
|
| 1135 |
this->dequeueOvercommit.fetch_add(desiredCount, std::memory_order_release); |
| 1136 |
} |
| 1137 |
} |
| 1138 |
|
| 1139 |
return 0; |
| 1140 |
} |
| 1141 |
|
| 1142 |
private: |
| 1143 |
struct BlockIndexEntry |
| 1144 |
{ |
| 1145 |
index_t base; |
| 1146 |
Block* block; |
| 1147 |
}; |
| 1148 |
|
| 1149 |
struct BlockIndexHeader |
| 1150 |
{ |
| 1151 |
size_t size; |
| 1152 |
std::atomic<size_t> front; |
| 1153 |
BlockIndexEntry* entries; |
| 1154 |
void* prev; |
| 1155 |
}; |
| 1156 |
|
| 1157 |
|
| 1158 |
bool new_block_index(size_t numberOfFilledSlotsToExpose) |
| 1159 |
{ |
| 1160 |
auto prevBlockSizeMask = pr_blockIndexSize - 1; |
| 1161 |
|
| 1162 |
|
| 1163 |
pr_blockIndexSize <<= 1; |
| 1164 |
auto newRawPtr = static_cast<char*>((Traits::malloc)(sizeof(BlockIndexHeader) + std::alignment_of<BlockIndexEntry>::value - 1 + sizeof(BlockIndexEntry) * pr_blockIndexSize)); |
| 1165 |
if (newRawPtr == nullptr) { |
| 1166 |
pr_blockIndexSize >>= 1; |
| 1167 |
return false; |
| 1168 |
} |
| 1169 |
|
| 1170 |
auto newBlockIndexEntries = reinterpret_cast<BlockIndexEntry*>(details::align_for<BlockIndexEntry>(newRawPtr + sizeof(BlockIndexHeader))); |
| 1171 |
|
| 1172 |
|
| 1173 |
size_t j = 0; |
| 1174 |
if (pr_blockIndexSlotsUsed != 0) { |
| 1175 |
auto i = (pr_blockIndexFront - pr_blockIndexSlotsUsed) & prevBlockSizeMask; |
| 1176 |
do { |
| 1177 |
newBlockIndexEntries[j++] = pr_blockIndexEntries[i]; |
| 1178 |
i = (i + 1) & prevBlockSizeMask; |
| 1179 |
} while (i != pr_blockIndexFront); |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
auto header = new (newRawPtr) BlockIndexHeader; |
| 1184 |
header->size = pr_blockIndexSize; |
| 1185 |
header->front.store(numberOfFilledSlotsToExpose - 1, std::memory_order_relaxed); |
| 1186 |
header->entries = newBlockIndexEntries; |
| 1187 |
header->prev = pr_blockIndexRaw; |
| 1188 |
|
| 1189 |
pr_blockIndexFront = j; |
| 1190 |
pr_blockIndexEntries = newBlockIndexEntries; |
| 1191 |
pr_blockIndexRaw = newRawPtr; |
| 1192 |
blockIndex.store(header, std::memory_order_release); |
| 1193 |
|
| 1194 |
return true; |
| 1195 |
} |
| 1196 |
|
| 1197 |
private: |
| 1198 |
std::atomic<BlockIndexHeader*> blockIndex; |
| 1199 |
|
| 1200 |
|
| 1201 |
size_t pr_blockIndexSlotsUsed; |
| 1202 |
size_t pr_blockIndexSize; |
| 1203 |
size_t pr_blockIndexFront; |
| 1204 |
BlockIndexEntry* pr_blockIndexEntries; |
| 1205 |
void* pr_blockIndexRaw; |
| 1206 |
}; |
| 1207 |
|
| 1208 |
ExplicitProducer* get_explicit_producer(producer_token_t const& token) |
| 1209 |
{ |
| 1210 |
return static_cast<ExplicitProducer*>(token.producer); |
| 1211 |
} |
| 1212 |
|
| 1213 |
private: |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
void populate_initial_block_list(size_t blockCount) |
| 1220 |
{ |
| 1221 |
initialBlockPoolSize = blockCount; |
| 1222 |
if (initialBlockPoolSize == 0) { |
| 1223 |
initialBlockPool = nullptr; |
| 1224 |
return; |
| 1225 |
} |
| 1226 |
|
| 1227 |
initialBlockPool = create_array<Block>(blockCount); |
| 1228 |
if (initialBlockPool == nullptr) { |
| 1229 |
initialBlockPoolSize = 0; |
| 1230 |
} |
| 1231 |
for (size_t i = 0; i < initialBlockPoolSize; ++i) { |
| 1232 |
initialBlockPool[i].dynamicallyAllocated = false; |
| 1233 |
} |
| 1234 |
} |
| 1235 |
|
| 1236 |
inline Block* try_get_block_from_initial_pool() |
| 1237 |
{ |
| 1238 |
if (initialBlockPoolIndex.load(std::memory_order_relaxed) >= initialBlockPoolSize) { |
| 1239 |
return nullptr; |
| 1240 |
} |
| 1241 |
|
| 1242 |
auto index = initialBlockPoolIndex.fetch_add(1, std::memory_order_relaxed); |
| 1243 |
|
| 1244 |
return index < initialBlockPoolSize ? (initialBlockPool + index) : nullptr; |
| 1245 |
} |
| 1246 |
|
| 1247 |
inline void add_block_to_free_list(Block* block) |
| 1248 |
{ |
| 1249 |
freeList.add(block); |
| 1250 |
} |
| 1251 |
|
| 1252 |
inline void add_blocks_to_free_list(Block* block) |
| 1253 |
{ |
| 1254 |
while (block != nullptr) { |
| 1255 |
auto next = block->next; |
| 1256 |
add_block_to_free_list(block); |
| 1257 |
block = next; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
inline Block* try_get_block_from_free_list() |
| 1262 |
{ |
| 1263 |
return freeList.try_get(); |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
Block* requisition_block() |
| 1268 |
{ |
| 1269 |
auto block = try_get_block_from_initial_pool(); |
| 1270 |
if (block != nullptr) { |
| 1271 |
return block; |
| 1272 |
} |
| 1273 |
|
| 1274 |
block = try_get_block_from_free_list(); |
| 1275 |
if (block != nullptr) { |
| 1276 |
return block; |
| 1277 |
} |
| 1278 |
|
| 1279 |
return create<Block>(); |
| 1280 |
} |
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
ProducerBase* recycle_or_create_producer() |
| 1288 |
{ |
| 1289 |
bool recycled; |
| 1290 |
return recycle_or_create_producer(recycled); |
| 1291 |
} |
| 1292 |
|
| 1293 |
ProducerBase* recycle_or_create_producer(bool& recycled) |
| 1294 |
{ |
| 1295 |
|
| 1296 |
for (auto ptr = producerListTail.load(std::memory_order_acquire); ptr != nullptr; ptr = ptr->next_prod()) { |
| 1297 |
if (ptr->inactive.load(std::memory_order_relaxed)) { |
| 1298 |
if( ptr->size_approx() == 0 ) |
| 1299 |
{ |
| 1300 |
bool expected = true; |
| 1301 |
if (ptr->inactive.compare_exchange_strong(expected, false, std::memory_order_acquire, std::memory_order_relaxed)) { |
| 1302 |
|
| 1303 |
recycled = true; |
| 1304 |
return ptr; |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
} |
| 1309 |
|
| 1310 |
recycled = false; |
| 1311 |
return add_producer(static_cast<ProducerBase*>(create<ExplicitProducer>(this))); |
| 1312 |
} |
| 1313 |
|
| 1314 |
ProducerBase* add_producer(ProducerBase* producer) |
| 1315 |
{ |
| 1316 |
|
| 1317 |
if (producer == nullptr) { |
| 1318 |
return nullptr; |
| 1319 |
} |
| 1320 |
|
| 1321 |
producerCount.fetch_add(1, std::memory_order_relaxed); |
| 1322 |
|
| 1323 |
|
| 1324 |
auto prevTail = producerListTail.load(std::memory_order_relaxed); |
| 1325 |
do { |
| 1326 |
producer->next = prevTail; |
| 1327 |
} while (!producerListTail.compare_exchange_weak(prevTail, producer, std::memory_order_release, std::memory_order_relaxed)); |
| 1328 |
|
| 1329 |
return producer; |
| 1330 |
} |
| 1331 |
|
| 1332 |
void reown_producers() |
| 1333 |
{ |
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
for (auto ptr = producerListTail.load(std::memory_order_relaxed); ptr != nullptr; ptr = ptr->next_prod()) { |
| 1338 |
ptr->parent = this; |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
template<typename U> |
| 1347 |
static inline U* create_array(size_t count) |
| 1348 |
{ |
| 1349 |
assert(count > 0); |
| 1350 |
return static_cast<U*>((Traits::malloc)(sizeof(U) * count)); |
| 1351 |
} |
| 1352 |
|
| 1353 |
template<typename U> |
| 1354 |
static inline void destroy_array(U* p, size_t count) |
| 1355 |
{ |
| 1356 |
((void)count); |
| 1357 |
if (p != nullptr) { |
| 1358 |
assert(count > 0); |
| 1359 |
(Traits::free)(p); |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
template<typename U> |
| 1364 |
static inline U* create() |
| 1365 |
{ |
| 1366 |
auto p = (Traits::malloc)(sizeof(U)); |
| 1367 |
return new (p) U; |
| 1368 |
} |
| 1369 |
|
| 1370 |
template<typename U, typename A1> |
| 1371 |
static inline U* create(A1&& a1) |
| 1372 |
{ |
| 1373 |
auto p = (Traits::malloc)(sizeof(U)); |
| 1374 |
return new (p) U(std::forward<A1>(a1)); |
| 1375 |
} |
| 1376 |
|
| 1377 |
template<typename U> |
| 1378 |
static inline void destroy(U* p) |
| 1379 |
{ |
| 1380 |
if (p != nullptr) { |
| 1381 |
p->~U(); |
| 1382 |
} |
| 1383 |
(Traits::free)(p); |
| 1384 |
} |
| 1385 |
|
| 1386 |
private: |
| 1387 |
std::atomic<ProducerBase*> producerListTail; |
| 1388 |
std::atomic<std::uint32_t> producerCount; |
| 1389 |
|
| 1390 |
std::atomic<size_t> initialBlockPoolIndex; |
| 1391 |
Block* initialBlockPool; |
| 1392 |
size_t initialBlockPoolSize; |
| 1393 |
|
| 1394 |
FreeList<Block> freeList; |
| 1395 |
|
| 1396 |
std::atomic<std::uint32_t> nextExplicitConsumerId; |
| 1397 |
std::atomic<std::uint32_t> globalExplicitConsumerOffset; |
| 1398 |
}; |
| 1399 |
|
| 1400 |
|
| 1401 |
template<typename T, typename Traits> |
| 1402 |
ProducerToken::ProducerToken(ConcurrentQueue<T, Traits>& queue) |
| 1403 |
: producer(queue.recycle_or_create_producer()) |
| 1404 |
{ |
| 1405 |
if (producer != nullptr) { |
| 1406 |
producer->token = this; |
| 1407 |
producer->threadId = detail::GetThreadHandleImpl(); |
| 1408 |
} |
| 1409 |
} |
| 1410 |
|
| 1411 |
template<typename T, typename Traits> |
| 1412 |
ConsumerToken::ConsumerToken(ConcurrentQueue<T, Traits>& queue) |
| 1413 |
: itemsConsumedFromCurrent(0), currentProducer(nullptr), desiredProducer(nullptr) |
| 1414 |
{ |
| 1415 |
initialOffset = queue.nextExplicitConsumerId.fetch_add(1, std::memory_order_release); |
| 1416 |
lastKnownGlobalOffset = static_cast<std::uint32_t>(-1); |
| 1417 |
} |
| 1418 |
|
| 1419 |
template<typename T, typename Traits> |
| 1420 |
inline void swap(ConcurrentQueue<T, Traits>& a, ConcurrentQueue<T, Traits>& b) noexcept |
| 1421 |
{ |
| 1422 |
a.swap(b); |
| 1423 |
} |
| 1424 |
|
| 1425 |
inline void swap(ProducerToken& a, ProducerToken& b) noexcept |
| 1426 |
{ |
| 1427 |
a.swap(b); |
| 1428 |
} |
| 1429 |
|
| 1430 |
inline void swap(ConsumerToken& a, ConsumerToken& b) noexcept |
| 1431 |
{ |
| 1432 |
a.swap(b); |
| 1433 |
} |
| 1434 |
|
| 1435 |
} |
| 1436 |
|
| 1437 |
} |
| 1438 |
|
| 1439 |
#if defined(__GNUC__) |
| 1440 |
#pragma GCC diagnostic pop |
| 1441 |
#endif |