| 1 |
// Copyright 2021 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 |
|
| 15 |
#ifndef GRPC_CORE_LIB_PROMISE_PIPE_H |
| 16 |
#define GRPC_CORE_LIB_PROMISE_PIPE_H |
| 17 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include <stdint.h> |
| 21 |
|
| 22 |
#include <string> |
| 23 |
#include <utility> |
| 24 |
|
| 25 |
#include "absl/strings/str_cat.h" |
| 26 |
#include "absl/types/optional.h" |
| 27 |
#include "absl/types/variant.h" |
| 28 |
|
| 29 |
#include <grpc/support/log.h> |
| 30 |
|
| 31 |
#include "src/core/lib/debug/trace.h" |
| 32 |
#include "src/core/lib/promise/activity.h" |
| 33 |
#include "src/core/lib/promise/context.h" |
| 34 |
#include "src/core/lib/promise/intra_activity_waiter.h" |
| 35 |
#include "src/core/lib/promise/poll.h" |
| 36 |
#include "src/core/lib/resource_quota/arena.h" |
| 37 |
|
| 38 |
extern grpc_core::DebugOnlyTraceFlag grpc_trace_promise_pipe; |
| 39 |
|
| 40 |
namespace grpc_core { |
| 41 |
|
| 42 |
namespace pipe_detail { |
| 43 |
template <typename T> |
| 44 |
class Center; |
| 45 |
} |
| 46 |
|
| 47 |
template <typename T> |
| 48 |
struct Pipe; |
| 49 |
|
| 50 |
// Result of Pipe::Next - represents a received value. |
| 51 |
// If has_value() is false, the pipe was closed by the time we polled for the |
| 52 |
// next value. No value was received, nor will there ever be. |
| 53 |
// This type is movable but not copyable. |
| 54 |
// Once the final move is destroyed the pipe will ack the read and unblock the |
| 55 |
// send. |
| 56 |
template <typename T> |
| 57 |
class NextResult final { |
| 58 |
public: |
| 59 |
explicit NextResult(pipe_detail::Center<T>* center) : center_(center) {} |
| 60 |
~NextResult(); |
| 61 |
NextResult(const NextResult&) = delete; |
| 62 |
NextResult& operator=(const NextResult&) = delete; |
| 63 |
NextResult(NextResult&& other) noexcept |
| 64 |
: center_(std::exchange(other.center_, nullptr)) {} |
| 65 |
NextResult& operator=(NextResult&& other) noexcept { |
| 66 |
center_ = std::exchange(other.center_, nullptr); |
| 67 |
return *this; |
| 68 |
} |
| 69 |
|
| 70 |
using value_type = T; |
| 71 |
|
| 72 |
void reset(); |
| 73 |
bool has_value() const; |
| 74 |
const T& value() const { |
| 75 |
GPR_ASSERT(has_value()); |
| 76 |
return **this; |
| 77 |
} |
| 78 |
T& value() { |
| 79 |
GPR_ASSERT(has_value()); |
| 80 |
return **this; |
| 81 |
} |
| 82 |
const T& operator*() const; |
| 83 |
T& operator*(); |
| 84 |
|
| 85 |
private: |
| 86 |
pipe_detail::Center<T>* center_; |
| 87 |
}; |
| 88 |
|
| 89 |
namespace pipe_detail { |
| 90 |
|
| 91 |
template <typename T> |
| 92 |
class Push; |
| 93 |
template <typename T> |
| 94 |
class Next; |
| 95 |
|
| 96 |
// Center sits between a sender and a receiver to provide a one-deep buffer of |
| 97 |
// Ts |
| 98 |
template <typename T> |
| 99 |
class Center { |
| 100 |
public: |
| 101 |
// Initialize with one send ref (held by PipeSender) and one recv ref (held by |
| 102 |
// PipeReceiver) |
| 103 |
Center() { |
| 104 |
send_refs_ = 1; |
| 105 |
recv_refs_ = 1; |
| 106 |
value_state_ = ValueState::kEmpty; |
| 107 |
} |
| 108 |
|
| 109 |
// Add one ref to the send side of this object, and return this. |
| 110 |
Center* RefSend() { |
| 111 |
if (grpc_trace_promise_pipe.enabled()) { |
| 112 |
gpr_log(GPR_INFO, "%s", DebugOpString("RefSend").c_str()); |
| 113 |
} |
| 114 |
send_refs_++; |
| 115 |
GPR_ASSERT(send_refs_ != 0); |
| 116 |
return this; |
| 117 |
} |
| 118 |
|
| 119 |
// Add one ref to the recv side of this object, and return this. |
| 120 |
Center* RefRecv() { |
| 121 |
if (grpc_trace_promise_pipe.enabled()) { |
| 122 |
gpr_log(GPR_INFO, "%s", DebugOpString("RefRecv").c_str()); |
| 123 |
} |
| 124 |
recv_refs_++; |
| 125 |
GPR_ASSERT(recv_refs_ != 0); |
| 126 |
return this; |
| 127 |
} |
| 128 |
|
| 129 |
// Drop a send side ref |
| 130 |
// If no send refs remain, wake due to send closure |
| 131 |
// If no refs remain, destroy this object |
| 132 |
void UnrefSend() { |
| 133 |
if (grpc_trace_promise_pipe.enabled()) { |
| 134 |
gpr_log(GPR_INFO, "%s", DebugOpString("UnrefSend").c_str()); |
| 135 |
} |
| 136 |
GPR_DEBUG_ASSERT(send_refs_ > 0); |
| 137 |
send_refs_--; |
| 138 |
if (0 == send_refs_) { |
| 139 |
on_full_.Wake(); |
| 140 |
on_empty_.Wake(); |
| 141 |
if (0 == recv_refs_) { |
| 142 |
this->~Center(); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// Drop a recv side ref |
| 148 |
// If no recv refs remain, wake due to recv closure |
| 149 |
// If no refs remain, destroy this object |
| 150 |
void UnrefRecv() { |
| 151 |
if (grpc_trace_promise_pipe.enabled()) { |
| 152 |
gpr_log(GPR_INFO, "%s", DebugOpString("UnrefRecv").c_str()); |
| 153 |
} |
| 154 |
GPR_DEBUG_ASSERT(recv_refs_ > 0); |
| 155 |
recv_refs_--; |
| 156 |
if (0 == recv_refs_) { |
| 157 |
on_full_.Wake(); |
| 158 |
on_empty_.Wake(); |
| 159 |
if (0 == send_refs_) { |
| 160 |
this->~Center(); |
| 161 |
} else if (value_state_ == ValueState::kReady) { |
| 162 |
ResetValue(); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// Try to push *value into the pipe. |
| 168 |
// Return Pending if there is no space. |
| 169 |
// Return true if the value was pushed. |
| 170 |
// Return false if the recv end is closed. |
| 171 |
Poll<bool> Push(T* value) { |
| 172 |
if (grpc_trace_promise_pipe.enabled()) { |
| 173 |
gpr_log(GPR_INFO, "%s", DebugOpString("Push").c_str()); |
| 174 |
} |
| 175 |
GPR_DEBUG_ASSERT(send_refs_ != 0); |
| 176 |
if (recv_refs_ == 0) return false; |
| 177 |
if (value_state_ != ValueState::kEmpty) return on_empty_.pending(); |
| 178 |
value_state_ = ValueState::kReady; |
| 179 |
value_ = std::move(*value); |
| 180 |
on_full_.Wake(); |
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
Poll<bool> PollAck() { |
| 185 |
if (grpc_trace_promise_pipe.enabled()) { |
| 186 |
gpr_log(GPR_INFO, "%s", DebugOpString("PollAck").c_str()); |
| 187 |
} |
| 188 |
GPR_DEBUG_ASSERT(send_refs_ != 0); |
| 189 |
if (recv_refs_ == 0) return value_state_ == ValueState::kAcked; |
| 190 |
if (value_state_ != ValueState::kAcked) return on_empty_.pending(); |
| 191 |
value_state_ = ValueState::kEmpty; |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
// Try to receive a value from the pipe. |
| 196 |
// Return Pending if there is no value. |
| 197 |
// Return the value if one was retrieved. |
| 198 |
// Return nullopt if the send end is closed and no value had been pushed. |
| 199 |
Poll<NextResult<T>> Next() { |
| 200 |
if (grpc_trace_promise_pipe.enabled()) { |
| 201 |
gpr_log(GPR_INFO, "%s", DebugOpString("Next").c_str()); |
| 202 |
} |
| 203 |
GPR_DEBUG_ASSERT(recv_refs_ != 0); |
| 204 |
if (value_state_ != ValueState::kReady) { |
| 205 |
if (send_refs_ == 0) return NextResult<T>(nullptr); |
| 206 |
return on_full_.pending(); |
| 207 |
} |
| 208 |
return NextResult<T>(RefRecv()); |
| 209 |
} |
| 210 |
|
| 211 |
void AckNext() { |
| 212 |
if (grpc_trace_promise_pipe.enabled()) { |
| 213 |
gpr_log(GPR_INFO, "%s", DebugOpString("AckNext").c_str()); |
| 214 |
} |
| 215 |
GPR_DEBUG_ASSERT(value_state_ == ValueState::kReady); |
| 216 |
value_state_ = ValueState::kAcked; |
| 217 |
on_empty_.Wake(); |
| 218 |
UnrefRecv(); |
| 219 |
} |
| 220 |
|
| 221 |
T& value() { return value_; } |
| 222 |
const T& value() const { return value_; } |
| 223 |
|
| 224 |
private: |
| 225 |
std::string DebugTag() { |
| 226 |
return absl::StrCat(Activity::current()->DebugTag(), "PIPE[0x", |
| 227 |
reinterpret_cast<uintptr_t>(this), "]: "); |
| 228 |
} |
| 229 |
std::string DebugOpString(std::string op) { |
| 230 |
return absl::StrCat(DebugTag(), op, " send_refs=", send_refs_, |
| 231 |
" recv_refs=", recv_refs_, |
| 232 |
" value_state=", ValueStateName(value_state_)); |
| 233 |
} |
| 234 |
void ResetValue() { |
| 235 |
// Fancy dance to move out of value in the off chance that we reclaim some |
| 236 |
// memory earlier. |
| 237 |
[](T) {}(std::move(value_)); |
| 238 |
value_state_ = ValueState::kEmpty; |
| 239 |
} |
| 240 |
// State of value_. |
| 241 |
enum class ValueState : uint8_t { |
| 242 |
// No value is set, it's possible to send. |
| 243 |
kEmpty, |
| 244 |
// Value has been pushed but not acked, it's possible to receive. |
| 245 |
kReady, |
| 246 |
// Value has been received and acked, we can unblock senders and transition |
| 247 |
// to empty. |
| 248 |
kAcked, |
| 249 |
}; |
| 250 |
static const char* ValueStateName(ValueState state) { |
| 251 |
switch (state) { |
| 252 |
case ValueState::kEmpty: |
| 253 |
return "kEmpty"; |
| 254 |
case ValueState::kReady: |
| 255 |
return "kReady"; |
| 256 |
case ValueState::kAcked: |
| 257 |
return "kAcked"; |
| 258 |
} |
| 259 |
GPR_UNREACHABLE_CODE(return "unknown"); |
| 260 |
} |
| 261 |
T value_; |
| 262 |
// Number of sending objects. |
| 263 |
// 0 => send is closed. |
| 264 |
// 1 ref each for PipeSender and Push. |
| 265 |
uint8_t send_refs_ : 2; |
| 266 |
// Number of receiving objects. |
| 267 |
// 0 => recv is closed. |
| 268 |
// 1 ref each for PipeReceiver, Next, and NextResult. |
| 269 |
uint8_t recv_refs_ : 2; |
| 270 |
// Current state of the value. |
| 271 |
ValueState value_state_ : 2; |
| 272 |
IntraActivityWaiter on_empty_; |
| 273 |
IntraActivityWaiter on_full_; |
| 274 |
}; |
| 275 |
|
| 276 |
} // namespace pipe_detail |
| 277 |
|
| 278 |
// Send end of a Pipe. |
| 279 |
template <typename T> |
| 280 |
class PipeSender { |
| 281 |
public: |
| 282 |
using PushType = pipe_detail::Push<T>; |
| 283 |
|
| 284 |
PipeSender(const PipeSender&) = delete; |
| 285 |
PipeSender& operator=(const PipeSender&) = delete; |
| 286 |
|
| 287 |
PipeSender(PipeSender&& other) noexcept : center_(other.center_) { |
| 288 |
other.center_ = nullptr; |
| 289 |
} |
| 290 |
PipeSender& operator=(PipeSender&& other) noexcept { |
| 291 |
if (center_ != nullptr) center_->UnrefSend(); |
| 292 |
center_ = other.center_; |
| 293 |
other.center_ = nullptr; |
| 294 |
return *this; |
| 295 |
} |
| 296 |
|
| 297 |
~PipeSender() { |
| 298 |
if (center_ != nullptr) center_->UnrefSend(); |
| 299 |
} |
| 300 |
|
| 301 |
void Close() { |
| 302 |
if (auto* center = std::exchange(center_, nullptr)) center->UnrefSend(); |
| 303 |
} |
| 304 |
|
| 305 |
void Swap(PipeSender<T>* other) { std::swap(center_, other->center_); } |
| 306 |
|
| 307 |
// Send a single message along the pipe. |
| 308 |
// Returns a promise that will resolve to a bool - true if the message was |
| 309 |
// sent, false if it could never be sent. Blocks the promise until the |
| 310 |
// receiver is either closed or able to receive another message. |
| 311 |
PushType Push(T value); |
| 312 |
|
| 313 |
private: |
| 314 |
friend struct Pipe<T>; |
| 315 |
explicit PipeSender(pipe_detail::Center<T>* center) : center_(center) {} |
| 316 |
pipe_detail::Center<T>* center_; |
| 317 |
}; |
| 318 |
|
| 319 |
// Receive end of a Pipe. |
| 320 |
template <typename T> |
| 321 |
class PipeReceiver { |
| 322 |
public: |
| 323 |
using NextType = pipe_detail::Next<T>; |
| 324 |
|
| 325 |
PipeReceiver(const PipeReceiver&) = delete; |
| 326 |
PipeReceiver& operator=(const PipeReceiver&) = delete; |
| 327 |
|
| 328 |
PipeReceiver(PipeReceiver&& other) noexcept : center_(other.center_) { |
| 329 |
other.center_ = nullptr; |
| 330 |
} |
| 331 |
PipeReceiver& operator=(PipeReceiver&& other) noexcept { |
| 332 |
if (center_ != nullptr) center_->UnrefRecv(); |
| 333 |
center_ = other.center_; |
| 334 |
other.center_ = nullptr; |
| 335 |
return *this; |
| 336 |
} |
| 337 |
~PipeReceiver() { |
| 338 |
if (center_ != nullptr) center_->UnrefRecv(); |
| 339 |
} |
| 340 |
|
| 341 |
void Swap(PipeReceiver<T>* other) { std::swap(center_, other->center_); } |
| 342 |
|
| 343 |
// Receive a single message from the pipe. |
| 344 |
// Returns a promise that will resolve to an optional<T> - with a value if a |
| 345 |
// message was received, or no value if the other end of the pipe was closed. |
| 346 |
// Blocks the promise until the receiver is either closed or a message is |
| 347 |
// available. |
| 348 |
NextType Next(); |
| 349 |
|
| 350 |
private: |
| 351 |
friend struct Pipe<T>; |
| 352 |
explicit PipeReceiver(pipe_detail::Center<T>* center) : center_(center) {} |
| 353 |
pipe_detail::Center<T>* center_; |
| 354 |
}; |
| 355 |
|
| 356 |
namespace pipe_detail { |
| 357 |
|
| 358 |
// Implementation of PipeSender::Push promise. |
| 359 |
template <typename T> |
| 360 |
class Push { |
| 361 |
public: |
| 362 |
Push(const Push&) = delete; |
| 363 |
Push& operator=(const Push&) = delete; |
| 364 |
Push(Push&& other) noexcept |
| 365 |
: center_(other.center_), push_(std::move(other.push_)) { |
| 366 |
other.center_ = nullptr; |
| 367 |
} |
| 368 |
Push& operator=(Push&& other) noexcept { |
| 369 |
if (center_ != nullptr) center_->UnrefSend(); |
| 370 |
center_ = other.center_; |
| 371 |
other.center_ = nullptr; |
| 372 |
push_ = std::move(other.push_); |
| 373 |
return *this; |
| 374 |
} |
| 375 |
|
| 376 |
~Push() { |
| 377 |
if (center_ != nullptr) center_->UnrefSend(); |
| 378 |
} |
| 379 |
|
| 380 |
Poll<bool> operator()() { |
| 381 |
if (push_.has_value()) { |
| 382 |
auto r = center_->Push(&*push_); |
| 383 |
if (auto* ok = absl::get_if<bool>(&r)) { |
| 384 |
push_.reset(); |
| 385 |
if (!*ok) return false; |
| 386 |
} else { |
| 387 |
return Pending{}; |
| 388 |
} |
| 389 |
} |
| 390 |
GPR_DEBUG_ASSERT(!push_.has_value()); |
| 391 |
return center_->PollAck(); |
| 392 |
} |
| 393 |
|
| 394 |
private: |
| 395 |
friend class PipeSender<T>; |
| 396 |
explicit Push(pipe_detail::Center<T>* center, T push) |
| 397 |
: center_(center), push_(std::move(push)) {} |
| 398 |
Center<T>* center_; |
| 399 |
absl::optional<T> push_; |
| 400 |
}; |
| 401 |
|
| 402 |
// Implementation of PipeReceiver::Next promise. |
| 403 |
template <typename T> |
| 404 |
class Next { |
| 405 |
public: |
| 406 |
Next(const Next&) = delete; |
| 407 |
Next& operator=(const Next&) = delete; |
| 408 |
Next(Next&& other) noexcept : center_(other.center_) { |
| 409 |
other.center_ = nullptr; |
| 410 |
} |
| 411 |
Next& operator=(Next&& other) noexcept { |
| 412 |
if (center_ != nullptr) center_->UnrefRecv(); |
| 413 |
center_ = other.center_; |
| 414 |
other.center_ = nullptr; |
| 415 |
return *this; |
| 416 |
} |
| 417 |
|
| 418 |
~Next() { |
| 419 |
if (center_ != nullptr) center_->UnrefRecv(); |
| 420 |
} |
| 421 |
|
| 422 |
Poll<NextResult<T>> operator()() { |
| 423 |
auto r = center_->Next(); |
| 424 |
if (!absl::holds_alternative<Pending>(r)) { |
| 425 |
std::exchange(center_, nullptr)->UnrefRecv(); |
| 426 |
} |
| 427 |
return r; |
| 428 |
} |
| 429 |
|
| 430 |
private: |
| 431 |
friend class PipeReceiver<T>; |
| 432 |
explicit Next(pipe_detail::Center<T>* center) : center_(center) {} |
| 433 |
Center<T>* center_; |
| 434 |
}; |
| 435 |
|
| 436 |
} // namespace pipe_detail |
| 437 |
|
| 438 |
template <typename T> |
| 439 |
pipe_detail::Push<T> PipeSender<T>::Push(T value) { |
| 440 |
return pipe_detail::Push<T>(center_->RefSend(), std::move(value)); |
| 441 |
} |
| 442 |
|
| 443 |
template <typename T> |
| 444 |
pipe_detail::Next<T> PipeReceiver<T>::Next() { |
| 445 |
return pipe_detail::Next<T>(center_->RefRecv()); |
| 446 |
} |
| 447 |
|
| 448 |
template <typename T> |
| 449 |
bool NextResult<T>::has_value() const { |
| 450 |
return center_ != nullptr; |
| 451 |
} |
| 452 |
|
| 453 |
template <typename T> |
| 454 |
T& NextResult<T>::operator*() { |
| 455 |
return center_->value(); |
| 456 |
} |
| 457 |
|
| 458 |
template <typename T> |
| 459 |
const T& NextResult<T>::operator*() const { |
| 460 |
return center_->value(); |
| 461 |
} |
| 462 |
|
| 463 |
template <typename T> |
| 464 |
NextResult<T>::~NextResult() { |
| 465 |
if (center_ != nullptr) center_->AckNext(); |
| 466 |
} |
| 467 |
|
| 468 |
template <typename T> |
| 469 |
void NextResult<T>::reset() { |
| 470 |
if (auto* p = std::exchange(center_, nullptr)) p->AckNext(); |
| 471 |
} |
| 472 |
|
| 473 |
// A Pipe is an intra-Activity communications channel that transmits T's from |
| 474 |
// one end to the other. |
| 475 |
// It is only safe to use a Pipe within the context of a single Activity. |
| 476 |
// No synchronization is performed internally. |
| 477 |
// The primary Pipe data structure is allocated from an arena, so the activity |
| 478 |
// must have an arena as part of its context. |
| 479 |
// By performing that allocation we can ensure stable pointer to shared data |
| 480 |
// allowing PipeSender/PipeReceiver/Push/Next to be relatively simple in their |
| 481 |
// implementation. |
| 482 |
// This type has been optimized with the expectation that there are relatively |
| 483 |
// few pipes per activity. If this assumption does not hold then a design |
| 484 |
// allowing inline filtering of pipe contents (instead of connecting pipes with |
| 485 |
// polling code) would likely be more appropriate. |
| 486 |
template <typename T> |
| 487 |
struct Pipe { |
| 488 |
Pipe() : Pipe(GetContext<Arena>()) {} |
| 489 |
explicit Pipe(Arena* arena) : Pipe(arena->New<pipe_detail::Center<T>>()) {} |
| 490 |
Pipe(const Pipe&) = delete; |
| 491 |
Pipe& operator=(const Pipe&) = delete; |
| 492 |
Pipe(Pipe&&) noexcept = default; |
| 493 |
Pipe& operator=(Pipe&&) noexcept = default; |
| 494 |
|
| 495 |
PipeSender<T> sender; |
| 496 |
PipeReceiver<T> receiver; |
| 497 |
|
| 498 |
private: |
| 499 |
explicit Pipe(pipe_detail::Center<T>* center) |
| 500 |
: sender(center), receiver(center) {} |
| 501 |
}; |
| 502 |
|
| 503 |
} // namespace grpc_core |
| 504 |
|
| 505 |
#endif // GRPC_CORE_LIB_PROMISE_PIPE_H |