|
| 1 | +#include "wait_list_light_with_epoch.hpp" |
| 2 | + |
| 3 | +#include <memory> |
| 4 | + |
| 5 | +#include <fmt/format.h> |
| 6 | + |
| 7 | +#include <engine/impl/wait_list_light.hpp> |
| 8 | +#include <userver/engine/impl/awaiter.hpp> |
| 9 | +#include <userver/logging/log.hpp> |
| 10 | +#include <userver/utils/assert.hpp> |
| 11 | + |
| 12 | +USERVER_NAMESPACE_BEGIN |
| 13 | + |
| 14 | +namespace engine::impl { |
| 15 | + |
| 16 | +namespace { |
| 17 | + |
| 18 | +AwaiterWithContext* const kSignaled = reinterpret_cast<AwaiterWithContext*>(1); |
| 19 | + |
| 20 | +} // namespace |
| 21 | + |
| 22 | +WaitListLightWithEpoch::WaitListLightWithEpoch(std::uint32_t epoch) noexcept : state_({nullptr, epoch}) {} |
| 23 | + |
| 24 | +WaitListLightWithEpoch::~WaitListLightWithEpoch() { |
| 25 | + const auto state = state_.load<std::memory_order_relaxed>(); |
| 26 | + UASSERT_MSG( |
| 27 | + state.awaiter_with_context == nullptr || state.awaiter_with_context == kSignaled, |
| 28 | + "Someone is awaiting on WaitListLightWithEpoch while it's being destroyed" |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +bool WaitListLightWithEpoch::SetEpochThenGetAndResetSignal(std::uint32_t epoch) noexcept { |
| 33 | + const AwaiterWithContextPtrAndEpoch new_value{nullptr, epoch}; |
| 34 | + const auto old_state = state_.exchange<std::memory_order_seq_cst>(new_value); |
| 35 | + UASSERT_MSG( |
| 36 | + old_state.awaiter_with_context == nullptr || old_state.awaiter_with_context == kSignaled, |
| 37 | + "Someone is awaiting on WaitListLightWithEpoch during SetEpoch" |
| 38 | + ); |
| 39 | + return old_state.awaiter_with_context == kSignaled; |
| 40 | +} |
| 41 | + |
| 42 | +std::uint32_t WaitListLightWithEpoch::GetEpoch() const noexcept { return state_.LoadWithTearing().epoch; } |
| 43 | + |
| 44 | +bool WaitListLightWithEpoch::GetSignalOrAppend(boost::intrusive_ptr<Awaiter> awaiter, std::uintptr_t context) { |
| 45 | + UASSERT(awaiter); |
| 46 | + |
| 47 | + const auto epoch = state_.LoadWithTearing().epoch; |
| 48 | + |
| 49 | + auto new_awaiter_with_context = std::make_unique<AwaiterWithContext>(AwaiterWithContext{awaiter.get(), context}); |
| 50 | + |
| 51 | + const AwaiterWithContextPtrAndEpoch new_state{new_awaiter_with_context.get(), epoch}; |
| 52 | + |
| 53 | + // Expect either {nullptr, epoch} or {kSignaled, epoch}. |
| 54 | + AwaiterWithContextPtrAndEpoch expected{nullptr, epoch}; |
| 55 | + |
| 56 | + // seq_cst is important for the "Append-Check-Wakeup" sequence. |
| 57 | + bool success = |
| 58 | + state_.compare_exchange_strong<std::memory_order_seq_cst, std::memory_order_relaxed>(expected, new_state); |
| 59 | + |
| 60 | + if (!success) { |
| 61 | + // CAS failed - new_awaiter_with_context will clean up. |
| 62 | + |
| 63 | + UASSERT_MSG( |
| 64 | + expected.awaiter_with_context == kSignaled && expected.epoch == epoch, |
| 65 | + "An unexpected awaiter is occupying the WaitListLightWithEpoch or epoch does not match" |
| 66 | + ); |
| 67 | + |
| 68 | + // Already signaled with matching epoch. |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + // CAS succeeded - release ownership to the atomic. |
| 73 | + [[maybe_unused]] auto* const released_ptr = new_awaiter_with_context.release(); |
| 74 | + |
| 75 | + // Keep a reference logically stored in the WaitListLightWithEpoch. |
| 76 | + awaiter.detach(); |
| 77 | + return false; |
| 78 | +} |
| 79 | + |
| 80 | +void WaitListLightWithEpoch::Remove(Awaiter& awaiter, std::uintptr_t context) noexcept { |
| 81 | + const auto current = state_.LoadWithTearing(); |
| 82 | + AwaiterWithContextPtrAndEpoch expected = current; |
| 83 | + |
| 84 | + UASSERT(expected.awaiter_with_context != nullptr); |
| 85 | + if (expected.awaiter_with_context == kSignaled) { |
| 86 | + // Already removed/signaled. |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + UASSERT_MSG( |
| 91 | + expected.awaiter_with_context->awaiter == &awaiter && expected.awaiter_with_context->context == context, |
| 92 | + "An unexpected awaiter is occupying the WaitListLightWithEpoch" |
| 93 | + ); |
| 94 | + |
| 95 | + // Preserve the epoch when removing the awaiter. |
| 96 | + const AwaiterWithContextPtrAndEpoch new_value{nullptr, current.epoch}; |
| 97 | + |
| 98 | + const bool success = |
| 99 | + state_.compare_exchange_strong<std::memory_order_release, std::memory_order_relaxed>(expected, new_value); |
| 100 | + |
| 101 | + if (!success) { |
| 102 | + UASSERT_MSG( |
| 103 | + expected.awaiter_with_context == nullptr || expected.awaiter_with_context == kSignaled, |
| 104 | + "An unexpected awaiter is occupying the WaitListLightWithEpoch or epoch changed mid-await" |
| 105 | + ); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (current.awaiter_with_context != nullptr && current.awaiter_with_context != kSignaled) { |
| 110 | + std::default_delete<AwaiterWithContext>{}(current.awaiter_with_context); |
| 111 | + } |
| 112 | + intrusive_ptr_release(&awaiter); |
| 113 | +} |
| 114 | + |
| 115 | +bool WaitListLightWithEpoch::GetAndResetSignal() noexcept { |
| 116 | + const auto current = state_.load<std::memory_order_acquire>(); |
| 117 | + |
| 118 | + // Reset signal from {kSignaled, epoch} to {nullptr, epoch}. |
| 119 | + AwaiterWithContextPtrAndEpoch expected{kSignaled, current.epoch}; |
| 120 | + const AwaiterWithContextPtrAndEpoch new_value{nullptr, current.epoch}; |
| 121 | + |
| 122 | + const bool success = |
| 123 | + state_.compare_exchange_strong<std::memory_order_relaxed, std::memory_order_relaxed>(expected, new_value); |
| 124 | + |
| 125 | + if (!success) { |
| 126 | + // Only two states are allowed: {nullptr, epoch} or {kSignaled, epoch}. |
| 127 | + UASSERT_MSG( |
| 128 | + expected.epoch == current.epoch && expected.awaiter_with_context == nullptr, |
| 129 | + "An unexpected awaiter is occupying the WaitListLightWithEpoch or epoch does not match" |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + return success; |
| 134 | +} |
| 135 | + |
| 136 | +void WaitListLightWithEpoch::SetSignalAndNotifyOneIfEpochMatches(std::uint32_t epoch) noexcept { |
| 137 | + auto current = state_.LoadWithTearing(); |
| 138 | + |
| 139 | + for (;;) { |
| 140 | + if (current.epoch != epoch) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + const AwaiterWithContextPtrAndEpoch new_value{kSignaled, epoch}; |
| 145 | + |
| 146 | + const bool success = |
| 147 | + state_.compare_exchange_strong<std::memory_order_seq_cst, std::memory_order_relaxed>(current, new_value); |
| 148 | + |
| 149 | + if (success) { |
| 150 | + if (current.awaiter_with_context != nullptr && current.awaiter_with_context != kSignaled) { |
| 151 | + const boost::intrusive_ptr<Awaiter> awaiter{current.awaiter_with_context->awaiter, /*add_ref=*/false}; |
| 152 | + awaiter->Notify(current.awaiter_with_context->context); |
| 153 | + std::default_delete<AwaiterWithContext>{}(current.awaiter_with_context); |
| 154 | + } |
| 155 | + return; |
| 156 | + } |
| 157 | + // CAS failed, current is updated with actual value, retry. |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +bool WaitListLightWithEpoch::IsSignaled() const noexcept { |
| 162 | + const auto torn_state = state_.LoadWithTearing(); |
| 163 | + std::atomic_thread_fence(std::memory_order_acquire); |
| 164 | + return torn_state.awaiter_with_context == kSignaled; |
| 165 | +} |
| 166 | + |
| 167 | +} // namespace engine::impl |
| 168 | + |
| 169 | +USERVER_NAMESPACE_END |
0 commit comments