ring_buffer.h 4.5 KB

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