table.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #pragma once
  2. #include <Windows.h>
  3. #include <algorithm>
  4. #include <array>
  5. #include <cstddef>
  6. #include <span>
  7. namespace sunrise::state::build_data {
  8. /**
  9. * Reader/writer lock guarding one domain's published rows.
  10. * The lock sits apart from the storage so a domain with several arrays holds one lock across all
  11. * of them. A reader then never sees one array replaced and another not.
  12. */
  13. class Lock final {
  14. public:
  15. /** Holds the lock for reading until it leaves scope. */
  16. class Shared final {
  17. public:
  18. explicit Shared(const Lock& owner) noexcept : owner_(owner) {
  19. AcquireSRWLockShared(&owner_.lock_);
  20. }
  21. ~Shared() {
  22. ReleaseSRWLockShared(&owner_.lock_);
  23. }
  24. Shared(const Shared&) = delete;
  25. Shared(Shared&&) = delete;
  26. Shared& operator=(const Shared&) = delete;
  27. Shared& operator=(Shared&&) = delete;
  28. private:
  29. const Lock& owner_;
  30. };
  31. /** Holds the lock for writing until it leaves scope. */
  32. class Exclusive final {
  33. public:
  34. explicit Exclusive(Lock& owner) noexcept : owner_(owner) {
  35. AcquireSRWLockExclusive(&owner_.lock_);
  36. }
  37. ~Exclusive() {
  38. ReleaseSRWLockExclusive(&owner_.lock_);
  39. }
  40. Exclusive(const Exclusive&) = delete;
  41. Exclusive(Exclusive&&) = delete;
  42. Exclusive& operator=(const Exclusive&) = delete;
  43. Exclusive& operator=(Exclusive&&) = delete;
  44. private:
  45. Lock& owner_;
  46. };
  47. private:
  48. // The acquire calls take a non-const pointer, and a shared hold does not modify the rows.
  49. mutable SRWLOCK lock_{SRWLOCK_INIT};
  50. };
  51. /**
  52. * Fixed row storage for one published table.
  53. * The caller must already hold the domain's Lock: exclusive to write, shared to read. Storage is
  54. * a member array, so the table lives in State for the whole process and nothing here allocates.
  55. * @tparam Row Trivially copyable published row.
  56. * @tparam Capacity Most rows the domain can publish.
  57. */
  58. template <typename Row, std::size_t Capacity> class Table final {
  59. public:
  60. /** Most rows this table accepts. */
  61. static constexpr std::size_t capacity = Capacity;
  62. /** Drops every row. Call under an exclusive hold. */
  63. void clear() noexcept {
  64. std::fill_n(rows_.begin(), count_, Row{});
  65. count_ = 0;
  66. }
  67. /**
  68. * Replaces every row. Call under an exclusive hold, and check the rows first.
  69. * @param rows Complete replacement set, already checked by the domain.
  70. * @return True when the rows fit fixed storage.
  71. */
  72. [[nodiscard]] bool replace(std::span<const Row> rows) noexcept {
  73. if (rows.size() > Capacity) {
  74. return false;
  75. }
  76. std::copy(rows.begin(), rows.end(), rows_.begin());
  77. if (count_ > rows.size()) {
  78. std::fill(rows_.begin() + static_cast<std::ptrdiff_t>(rows.size()),
  79. rows_.begin() + static_cast<std::ptrdiff_t>(count_),
  80. Row{});
  81. }
  82. // The count publishes the new rows, so it moves last.
  83. count_ = rows.size();
  84. return true;
  85. }
  86. /**
  87. * Clears the table, sizes it, and hands back writable storage. Call under an exclusive hold.
  88. * For a domain that places each row at a position the row itself names, not in input order.
  89. * No reader can see the sized but unwritten rows: writes hold exclusive, reads hold shared.
  90. * @param count Number of rows about to be written.
  91. * @return Storage for exactly that many rows, or an empty span when the count does not fit.
  92. */
  93. [[nodiscard]] std::span<Row> reset(std::size_t count) noexcept {
  94. if (count > Capacity) {
  95. return {};
  96. }
  97. std::fill_n(rows_.begin(), (std::max)(count_, count), Row{});
  98. count_ = count;
  99. return {rows_.data(), count_};
  100. }
  101. /**
  102. * Copies every row in publish order. Call under a shared hold.
  103. * @param output Caller-owned storage.
  104. * @param count Receives the copied row count, or zero when output is too small.
  105. * @return True when output can hold every row.
  106. */
  107. [[nodiscard]] bool snapshot(std::span<Row> output, std::size_t& count) const noexcept {
  108. count = 0;
  109. if (output.size() < count_) {
  110. return false;
  111. }
  112. std::copy_n(rows_.begin(), count_, output.begin());
  113. count = count_;
  114. return true;
  115. }
  116. /** @return Published rows in publish order. Call under a shared hold. */
  117. [[nodiscard]] std::span<const Row> rows() const noexcept {
  118. return {rows_.data(), count_};
  119. }
  120. /** @return Published row count. Call under a shared hold. */
  121. [[nodiscard]] std::size_t count() const noexcept {
  122. return count_;
  123. }
  124. private:
  125. std::array<Row, Capacity> rows_{};
  126. std::size_t count_{};
  127. };
  128. } // namespace sunrise::state::build_data