ring_buffer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <algorithm>
  5. #include <array>
  6. #include <atomic>
  7. #include <cstddef>
  8. #include <cstring>
  9. #include <new>
  10. #include <type_traits>
  11. #include <vector>
  12. namespace Common {
  13. /// SPSC ring buffer
  14. /// @tparam T Element type
  15. /// @tparam capacity Number of slots in ring buffer
  16. template <typename T, std::size_t capacity>
  17. class RingBuffer {
  18. /// A "slot" is made of a single `T`.
  19. static constexpr std::size_t slot_size = sizeof(T);
  20. // T must be safely memcpy-able and have a trivial default constructor.
  21. static_assert(std::is_trivial_v<T>);
  22. // Ensure capacity is sensible.
  23. static_assert(capacity < std::numeric_limits<std::size_t>::max() / 2);
  24. static_assert((capacity & (capacity - 1)) == 0, "capacity must be a power of two");
  25. // Ensure lock-free.
  26. static_assert(std::atomic_size_t::is_always_lock_free);
  27. public:
  28. /// Pushes slots into the ring buffer
  29. /// @param new_slots Pointer to the slots to push
  30. /// @param slot_count Number of slots to push
  31. /// @returns The number of slots actually pushed
  32. std::size_t Push(const void* new_slots, std::size_t slot_count) {
  33. const std::size_t write_index = m_write_index.load();
  34. const std::size_t slots_free = capacity + m_read_index.load() - write_index;
  35. const std::size_t push_count = std::min(slot_count, slots_free);
  36. const std::size_t pos = write_index % capacity;
  37. const std::size_t first_copy = std::min(capacity - pos, push_count);
  38. const std::size_t second_copy = push_count - first_copy;
  39. const char* in = static_cast<const char*>(new_slots);
  40. std::memcpy(m_data.data() + pos, in, first_copy * slot_size);
  41. in += first_copy * slot_size;
  42. std::memcpy(m_data.data(), in, second_copy * slot_size);
  43. m_write_index.store(write_index + push_count);
  44. return push_count;
  45. }
  46. std::size_t Push(const std::vector<T>& input) {
  47. return Push(input.data(), input.size());
  48. }
  49. /// Pops slots from the ring buffer
  50. /// @param output Where to store the popped slots
  51. /// @param max_slots Maximum number of slots to pop
  52. /// @returns The number of slots actually popped
  53. std::size_t Pop(void* output, std::size_t max_slots = ~std::size_t(0)) {
  54. const std::size_t read_index = m_read_index.load();
  55. const std::size_t slots_filled = m_write_index.load() - read_index;
  56. const std::size_t pop_count = std::min(slots_filled, max_slots);
  57. const std::size_t pos = read_index % capacity;
  58. const std::size_t first_copy = std::min(capacity - pos, pop_count);
  59. const std::size_t second_copy = pop_count - first_copy;
  60. char* out = static_cast<char*>(output);
  61. std::memcpy(out, m_data.data() + pos, first_copy * slot_size);
  62. out += first_copy * slot_size;
  63. std::memcpy(out, m_data.data(), second_copy * slot_size);
  64. m_read_index.store(read_index + pop_count);
  65. return pop_count;
  66. }
  67. std::vector<T> Pop(std::size_t max_slots = ~std::size_t(0)) {
  68. std::vector<T> out(std::min(max_slots, capacity));
  69. const std::size_t count = Pop(out.data(), out.size());
  70. out.resize(count);
  71. return out;
  72. }
  73. /// @returns Number of slots used
  74. [[nodiscard]] std::size_t Size() const {
  75. return m_write_index.load() - m_read_index.load();
  76. }
  77. /// @returns Maximum size of ring buffer
  78. [[nodiscard]] constexpr std::size_t Capacity() const {
  79. return capacity;
  80. }
  81. private:
  82. // It is important to align the below variables for performance reasons:
  83. // Having them on the same cache-line would result in false-sharing between them.
  84. // TODO: Remove this ifdef whenever clang and GCC support
  85. // std::hardware_destructive_interference_size.
  86. #if defined(_MSC_VER) && _MSC_VER >= 1911
  87. alignas(std::hardware_destructive_interference_size) std::atomic_size_t m_read_index{0};
  88. alignas(std::hardware_destructive_interference_size) std::atomic_size_t m_write_index{0};
  89. #else
  90. alignas(128) std::atomic_size_t m_read_index{0};
  91. alignas(128) std::atomic_size_t m_write_index{0};
  92. #endif
  93. std::array<T, capacity> m_data;
  94. };
  95. } // namespace Common