| 1 |
// Copyright 2022 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_SLEEP_H |
| 16 |
#define GRPC_CORE_LIB_PROMISE_SLEEP_H |
| 17 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include <atomic> |
| 21 |
|
| 22 |
#include "absl/status/status.h" |
| 23 |
|
| 24 |
#include <grpc/event_engine/event_engine.h> |
| 25 |
#include <grpc/support/log.h> |
| 26 |
|
| 27 |
#include "src/core/lib/gprpp/time.h" |
| 28 |
#include "src/core/lib/promise/activity.h" |
| 29 |
#include "src/core/lib/promise/poll.h" |
| 30 |
|
| 31 |
namespace grpc_core { |
| 32 |
|
| 33 |
// Promise that sleeps until a deadline and then finishes. |
| 34 |
class Sleep final { |
| 35 |
public: |
| 36 |
explicit Sleep(Timestamp deadline); |
| 37 |
~Sleep(); |
| 38 |
|
| 39 |
Sleep(const Sleep&) = delete; |
| 40 |
Sleep& operator=(const Sleep&) = delete; |
| 41 |
Sleep(Sleep&& other) noexcept : deadline_(other.deadline_) { |
| 42 |
// Promises can be moved only until they're polled, and since we only create |
| 43 |
// the closure when first polled we can assume it's nullptr here. |
| 44 |
GPR_DEBUG_ASSERT(other.closure_ == nullptr); |
| 45 |
}; |
| 46 |
Sleep& operator=(Sleep&& other) noexcept { |
| 47 |
// Promises can be moved only until they're polled, and since we only create |
| 48 |
// the closure when first polled we can assume it's nullptr here. |
| 49 |
GPR_DEBUG_ASSERT(closure_ == nullptr); |
| 50 |
GPR_DEBUG_ASSERT(other.closure_ == nullptr); |
| 51 |
deadline_ = other.deadline_; |
| 52 |
return *this; |
| 53 |
}; |
| 54 |
|
| 55 |
Poll<absl::Status> operator()(); |
| 56 |
|
| 57 |
private: |
| 58 |
class ActiveClosure final |
| 59 |
: public grpc_event_engine::experimental::EventEngine::Closure { |
| 60 |
public: |
| 61 |
explicit ActiveClosure(Timestamp deadline); |
| 62 |
|
| 63 |
void Run() override; |
| 64 |
// After calling Cancel, it's no longer safe to access this object. |
| 65 |
void Cancel(); |
| 66 |
|
| 67 |
bool HasRun() const; |
| 68 |
|
| 69 |
private: |
| 70 |
bool Unref(); |
| 71 |
|
| 72 |
Waker waker_; |
| 73 |
// One ref dropped by Run(), the other by Cancel(). |
| 74 |
std::atomic<int> refs_{2}; |
| 75 |
grpc_event_engine::experimental::EventEngine::TaskHandle timer_handle_; |
| 76 |
}; |
| 77 |
|
| 78 |
Timestamp deadline_; |
| 79 |
ActiveClosure* closure_{nullptr}; |
| 80 |
}; |
| 81 |
|
| 82 |
} // namespace grpc_core |
| 83 |
|
| 84 |
#endif // GRPC_CORE_LIB_PROMISE_SLEEP_H |