ring_buffer.h 4.3 KB

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