/* Copyright (c) 2020 Erik Rigtorp Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #pragma once #include #include #include #include #include // std::enable_if, std::is_*_constructible #include "../common/TracyAlloc.hpp" #if defined (_MSC_VER) #pragma warning(push) #pragma warning(disable:4324) #endif namespace tracy { template class SPSCQueue { public: explicit SPSCQueue(const size_t capacity) : capacity_(capacity) { capacity_++; // Needs one slack element slots_ = (T*)tracy_malloc(sizeof(T) * (capacity_ + 2 * kPadding)); static_assert(alignof(SPSCQueue) == kCacheLineSize, ""); static_assert(sizeof(SPSCQueue) >= 3 * kCacheLineSize, ""); assert(reinterpret_cast(&readIdx_) - reinterpret_cast(&writeIdx_) >= static_cast(kCacheLineSize)); } ~SPSCQueue() { while (front()) { pop(); } tracy_free(slots_); } // non-copyable and non-movable SPSCQueue(const SPSCQueue &) = delete; SPSCQueue &operator=(const SPSCQueue &) = delete; template void emplace(Args &&...args) noexcept( std::is_nothrow_constructible::value) { static_assert(std::is_constructible::value, "T must be constructible with Args&&..."); auto const writeIdx = writeIdx_.load(std::memory_order_relaxed); auto nextWriteIdx = writeIdx + 1; if (nextWriteIdx == capacity_) { nextWriteIdx = 0; } while (nextWriteIdx == readIdxCache_) { readIdxCache_ = readIdx_.load(std::memory_order_acquire); } new (&slots_[writeIdx + kPadding]) T(std::forward(args)...); writeIdx_.store(nextWriteIdx, std::memory_order_release); } T *front() noexcept { auto const readIdx = readIdx_.load(std::memory_order_relaxed); if (readIdx == writeIdxCache_) { writeIdxCache_ = writeIdx_.load(std::memory_order_acquire); if (writeIdxCache_ == readIdx) { return nullptr; } } return &slots_[readIdx + kPadding]; } void pop() noexcept { static_assert(std::is_nothrow_destructible::value, "T must be nothrow destructible"); auto const readIdx = readIdx_.load(std::memory_order_relaxed); assert(writeIdx_.load(std::memory_order_acquire) != readIdx); slots_[readIdx + kPadding].~T(); auto nextReadIdx = readIdx + 1; if (nextReadIdx == capacity_) { nextReadIdx = 0; } readIdx_.store(nextReadIdx, std::memory_order_release); } size_t size() const noexcept { std::ptrdiff_t diff = writeIdx_.load(std::memory_order_acquire) - readIdx_.load(std::memory_order_acquire); if (diff < 0) { diff += capacity_; } return static_cast(diff); } bool empty() const noexcept { return size() == 0; } size_t capacity() const noexcept { return capacity_ - 1; } private: static constexpr size_t kCacheLineSize = 64; // Padding to avoid false sharing between slots_ and adjacent allocations static constexpr size_t kPadding = (kCacheLineSize - 1) / sizeof(T) + 1; private: size_t capacity_; T *slots_; // Align to cache line size in order to avoid false sharing // readIdxCache_ and writeIdxCache_ is used to reduce the amount of cache // coherency traffic alignas(kCacheLineSize) std::atomic writeIdx_ = {0}; alignas(kCacheLineSize) size_t readIdxCache_ = 0; alignas(kCacheLineSize) std::atomic readIdx_ = {0}; alignas(kCacheLineSize) size_t writeIdxCache_ = 0; // Padding to avoid adjacent allocations to share cache line with // writeIdxCache_ char padding_[kCacheLineSize - sizeof(writeIdxCache_)]; }; } // namespace rigtorp #if defined (_MSC_VER) #pragma warning(pop) #endif