| 1 |
// Copyright 2022 The gRPC Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
#include <grpc/support/port_platform.h> |
| 15 |
|
| 16 |
#include "src/core/lib/event_engine/posix_engine/lockfree_event.h" |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
|
| 20 |
#include <atomic> |
| 21 |
#include <cstdint> |
| 22 |
|
| 23 |
#include "absl/status/status.h" |
| 24 |
|
| 25 |
#include <grpc/support/atm.h> |
| 26 |
#include <grpc/support/log.h> |
| 27 |
|
| 28 |
#include "src/core/lib/event_engine/posix_engine/event_poller.h" |
| 29 |
#include "src/core/lib/event_engine/posix_engine/posix_engine_closure.h" |
| 30 |
#include "src/core/lib/gprpp/status_helper.h" |
| 31 |
|
| 32 |
// 'state' holds the to call when the fd is readable or writable respectively. |
| 33 |
// It can contain one of the following values: |
| 34 |
// kClosureReady : The fd has an I/O event of interest but there is no |
| 35 |
// closure yet to execute |
| 36 |
|
| 37 |
// kClosureNotReady : The fd has no I/O event of interest |
| 38 |
|
| 39 |
// closure ptr : The closure to be executed when the fd has an I/O |
| 40 |
// event of interest |
| 41 |
|
| 42 |
// shutdown_error | kShutdownBit : |
| 43 |
// 'shutdown_error' field ORed with kShutdownBit. |
| 44 |
// This indicates that the fd is shutdown. Since all |
| 45 |
// memory allocations are word-aligned, the lower two |
| 46 |
// bits of the shutdown_error pointer are always 0. So |
| 47 |
// it is safe to OR these with kShutdownBit |
| 48 |
|
| 49 |
// Valid state transitions: |
| 50 |
|
| 51 |
// <closure ptr> <-----3------ kClosureNotReady -----1-------> kClosureReady |
| 52 |
// | | ^ | ^ | | |
| 53 |
// | | | | | | | |
| 54 |
// | +--------------4----------+ 6 +---------2---------------+ | |
| 55 |
// | | | |
| 56 |
// | v | |
| 57 |
// +-----5-------> [shutdown_error | kShutdownBit] <-------7---------+ |
| 58 |
|
| 59 |
// For 1, 4 : See SetReady() function |
| 60 |
// For 2, 3 : See NotifyOn() function |
| 61 |
// For 5,6,7: See SetShutdown() function |
| 62 |
|
| 63 |
namespace grpc_event_engine { |
| 64 |
namespace posix_engine { |
| 65 |
|
| 66 |
void LockfreeEvent::InitEvent() { |
| 67 |
// Perform an atomic store to start the state machine. |
| 68 |
|
| 69 |
// Note carefully that LockfreeEvent *MAY* be used whilst in a destroyed |
| 70 |
// state, while a file descriptor is on a freelist. In such a state it may |
| 71 |
// be SetReady'd, and so we need to perform an atomic operation here to |
| 72 |
// ensure no races |
| 73 |
state_.store(kClosureNotReady, std::memory_order_relaxed); |
| 74 |
} |
| 75 |
|
| 76 |
void LockfreeEvent::DestroyEvent() { |
| 77 |
intptr_t curr; |
| 78 |
do { |
| 79 |
curr = state_.load(std::memory_order_relaxed); |
| 80 |
if (curr & kShutdownBit) { |
| 81 |
grpc_core::internal::StatusFreeHeapPtr(curr & ~kShutdownBit); |
| 82 |
} else { |
| 83 |
GPR_ASSERT(curr == kClosureNotReady || curr == kClosureReady); |
| 84 |
} |
| 85 |
// we CAS in a shutdown, no error value here. If this event is interacted |
| 86 |
// with post-deletion (see the note in the constructor) we want the bit |
| 87 |
// pattern to prevent error retention in a deleted object |
| 88 |
} while (!state_.compare_exchange_strong(curr, kShutdownBit, |
| 89 |
std::memory_order_relaxed, |
| 90 |
std::memory_order_relaxed)); |
| 91 |
} |
| 92 |
|
| 93 |
void LockfreeEvent::NotifyOn(PosixEngineClosure* closure) { |
| 94 |
// This load needs to be an acquire load because this can be a shutdown |
| 95 |
// error that we might need to reference. Adding acquire semantics makes |
| 96 |
// sure that the shutdown error has been initialized properly before us |
| 97 |
// referencing it. The load() needs to be performed only once before entry |
| 98 |
// into the loop. This is because if any of the compare_exchange_strong |
| 99 |
// operations inside the loop return false, they automatically update curr |
| 100 |
// with the new value. So it doesn't need to be loaded again. |
| 101 |
intptr_t curr = state_.load(std::memory_order_acquire); |
| 102 |
while (true) { |
| 103 |
switch (curr) { |
| 104 |
case kClosureNotReady: { |
| 105 |
// kClosureNotReady -> <closure>. |
| 106 |
|
| 107 |
// We're guaranteed by API that there's an acquire barrier before here, |
| 108 |
// so there's no need to double-dip and this can be a release-only. |
| 109 |
|
| 110 |
// The release itself pairs with the acquire half of a set_ready full |
| 111 |
// barrier. |
| 112 |
if (state_.compare_exchange_strong( |
| 113 |
curr, reinterpret_cast<intptr_t>(closure), |
| 114 |
std::memory_order_release, std::memory_order_relaxed)) { |
| 115 |
return; // Successful. Return |
| 116 |
} |
| 117 |
|
| 118 |
break; // retry |
| 119 |
} |
| 120 |
|
| 121 |
case kClosureReady: { |
| 122 |
// Change the state to kClosureNotReady. Schedule the closure if |
| 123 |
// successful. If not, the state most likely transitioned to shutdown. |
| 124 |
// We should retry. |
| 125 |
|
| 126 |
// This can be a no-barrier cas since the state is being transitioned to |
| 127 |
// kClosureNotReady; set_ready and set_shutdown do not schedule any |
| 128 |
// closure when transitioning out of CLOSURE_NO_READY state (i.e there |
| 129 |
// is no other code that needs to 'happen-after' this) |
| 130 |
if (state_.compare_exchange_strong(curr, kClosureNotReady, |
| 131 |
std::memory_order_relaxed, |
| 132 |
std::memory_order_relaxed)) { |
| 133 |
scheduler_->Run(closure); |
| 134 |
return; // Successful. Return. |
| 135 |
} |
| 136 |
break; // retry |
| 137 |
} |
| 138 |
|
| 139 |
default: { |
| 140 |
// 'curr' is either a closure or the fd is shutdown(in which case 'curr' |
| 141 |
// contains a pointer to the shutdown-error). If the fd is shutdown, |
| 142 |
// schedule the closure with the shutdown error |
| 143 |
if ((curr & kShutdownBit) > 0) { |
| 144 |
absl::Status shutdown_err = |
| 145 |
grpc_core::internal::StatusGetFromHeapPtr(curr & ~kShutdownBit); |
| 146 |
closure->SetStatus(shutdown_err); |
| 147 |
scheduler_->Run(closure); |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
// There is already a closure!. This indicates a bug in the code. |
| 152 |
gpr_log(GPR_ERROR, |
| 153 |
"LockfreeEvent::NotifyOn: notify_on called with a previous " |
| 154 |
"callback still pending"); |
| 155 |
abort(); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
GPR_UNREACHABLE_CODE(return); |
| 161 |
} |
| 162 |
|
| 163 |
bool LockfreeEvent::SetShutdown(absl::Status shutdown_error) { |
| 164 |
intptr_t status_ptr = grpc_core::internal::StatusAllocHeapPtr(shutdown_error); |
| 165 |
gpr_atm new_state = status_ptr | kShutdownBit; |
| 166 |
// The load() needs to be performed only once before entry |
| 167 |
// into the loop. This is because if any of the compare_exchange_strong |
| 168 |
// operations inside the loop return false, they automatically update curr |
| 169 |
// with the new value. So it doesn't need to be loaded again. |
| 170 |
intptr_t curr = state_.load(std::memory_order_acquire); |
| 171 |
|
| 172 |
while (true) { |
| 173 |
switch (curr) { |
| 174 |
case kClosureReady: |
| 175 |
case kClosureNotReady: |
| 176 |
// Need a full barrier here so that the initial load in notify_on |
| 177 |
// doesn't need a barrier |
| 178 |
if (state_.compare_exchange_strong(curr, new_state, |
| 179 |
std::memory_order_acq_rel, |
| 180 |
std::memory_order_relaxed)) { |
| 181 |
return true; // early out |
| 182 |
} |
| 183 |
break; // retry |
| 184 |
|
| 185 |
default: { |
| 186 |
// 'curr' is either a closure or the fd is already shutdown |
| 187 |
|
| 188 |
// If fd is already shutdown, we are done. |
| 189 |
if ((curr & kShutdownBit) > 0) { |
| 190 |
grpc_core::internal::StatusFreeHeapPtr(status_ptr); |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
// Fd is not shutdown. Schedule the closure and move the state to |
| 195 |
// shutdown state. |
| 196 |
// Needs an acquire to pair with setting the closure (and get a |
| 197 |
// happens-after on that edge), and a release to pair with anything |
| 198 |
// loading the shutdown state. |
| 199 |
if (state_.compare_exchange_strong(curr, new_state, |
| 200 |
std::memory_order_acq_rel, |
| 201 |
std::memory_order_relaxed)) { |
| 202 |
auto closure = reinterpret_cast<PosixEngineClosure*>(curr); |
| 203 |
closure->SetStatus(shutdown_error); |
| 204 |
scheduler_->Run(closure); |
| 205 |
return true; |
| 206 |
} |
| 207 |
// 'curr' was a closure but now changed to a different state. We will |
| 208 |
// have to retry |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
GPR_UNREACHABLE_CODE(return false); |
| 214 |
} |
| 215 |
|
| 216 |
void LockfreeEvent::SetReady() { |
| 217 |
// The load() needs to be performed only once before entry |
| 218 |
// into the loop. This is because if any of the compare_exchange_strong |
| 219 |
// operations inside the loop return false, they automatically update curr |
| 220 |
// with the new value. So it doesn't need to be loaded again. |
| 221 |
intptr_t curr = state_.load(std::memory_order_acquire); |
| 222 |
while (true) { |
| 223 |
switch (curr) { |
| 224 |
case kClosureReady: { |
| 225 |
// Already ready. We are done here. |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
case kClosureNotReady: { |
| 230 |
// No barrier required as we're transitioning to a state that does not |
| 231 |
// involve a closure |
| 232 |
if (state_.compare_exchange_strong(curr, kClosureReady, |
| 233 |
std::memory_order_relaxed, |
| 234 |
std::memory_order_relaxed)) { |
| 235 |
return; // early out |
| 236 |
} |
| 237 |
break; // retry |
| 238 |
} |
| 239 |
|
| 240 |
default: { |
| 241 |
// 'curr' is either a closure or the fd is shutdown |
| 242 |
if ((curr & kShutdownBit) > 0) { |
| 243 |
// The fd is shutdown. Do nothing. |
| 244 |
return; |
| 245 |
} else if (state_.compare_exchange_strong(curr, kClosureNotReady, |
| 246 |
std::memory_order_acq_rel, |
| 247 |
std::memory_order_relaxed)) { |
| 248 |
// Full cas: acquire pairs with this cas' release in the event of a |
| 249 |
// spurious set_ready; release pairs with this or the acquire in |
| 250 |
// notify_on (or set_shutdown) |
| 251 |
auto closure = reinterpret_cast<PosixEngineClosure*>(curr); |
| 252 |
closure->SetStatus(absl::OkStatus()); |
| 253 |
scheduler_->Run(closure); |
| 254 |
return; |
| 255 |
} |
| 256 |
// else the state changed again (only possible by either a racing |
| 257 |
// set_ready or set_shutdown functions. In both these cases, the |
| 258 |
// closure would have been scheduled for execution. So we are done |
| 259 |
// here |
| 260 |
return; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
} // namespace posix_engine |
| 267 |
} // namespace grpc_event_engine |