src/core/lib/promise/sleep.h
Go to the documentation of this file.
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 
19 
20 #include <utility>
21 
22 #include "absl/base/thread_annotations.h"
23 #include "absl/status/status.h"
24 
26 
31 
32 namespace grpc_core {
33 
34 // Promise that sleeps until a deadline and then finishes.
35 class Sleep {
36  public:
37  explicit Sleep(Timestamp deadline);
38  ~Sleep();
39 
40  Sleep(const Sleep&) = delete;
41  Sleep& operator=(const Sleep&) = delete;
42  Sleep(Sleep&& other) noexcept
43  : deadline_(other.deadline_), timer_handle_(other.timer_handle_) {
44  MutexLock lock2(&other.mu_);
45  stage_ = other.stage_;
46  waker_ = std::move(other.waker_);
47  other.deadline_ = Timestamp::InfPast();
48  };
49  Sleep& operator=(Sleep&& other) noexcept {
50  if (&other == this) return *this;
51  MutexLock lock1(&mu_);
52  MutexLock lock2(&other.mu_);
53  deadline_ = other.deadline_;
54  timer_handle_ = other.timer_handle_;
55  stage_ = other.stage_;
56  waker_ = std::move(other.waker_);
57  other.deadline_ = Timestamp::InfPast();
58  return *this;
59  };
60 
62 
63  private:
64  enum class Stage { kInitial, kStarted, kDone };
65  void OnTimer();
66 
71  Waker waker_ ABSL_GUARDED_BY(mu_);
72 };
73 
74 } // namespace grpc_core
75 
76 #endif // GRPC_CORE_LIB_PROMISE_SLEEP_H
grpc_core::Sleep::Stage::kStarted
@ kStarted
grpc_core::Sleep::Sleep
Sleep(Sleep &&other) noexcept
Definition: src/core/lib/promise/sleep.h:42
grpc_core::Sleep::operator()
Poll< absl::Status > operator()()
Definition: src/core/lib/promise/sleep.cc:57
grpc_core::Sleep::timer_handle_
grpc_event_engine::experimental::EventEngine::TaskHandle timer_handle_
Definition: src/core/lib/promise/sleep.h:68
grpc_core
Definition: call_metric_recorder.h:31
event_engine.h
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
grpc_core::Sleep
Definition: src/core/lib/promise/sleep.h:35
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
grpc_core::Sleep::deadline_
Timestamp deadline_
Definition: src/core/lib/promise/sleep.h:67
grpc_core::Timestamp::InfPast
static constexpr Timestamp InfPast()
Definition: src/core/lib/gprpp/time.h:83
grpc_core::Sleep::ABSL_GUARDED_BY
Stage stage_ ABSL_GUARDED_BY(mu_)
grpc_event_engine::experimental::EventEngine::TaskHandle
Definition: event_engine.h:102
grpc_core::Sleep::Stage::kInitial
@ kInitial
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::Sleep::~Sleep
~Sleep()
Definition: src/core/lib/promise/sleep.cc:30
time.h
grpc_core::Sleep::operator=
Sleep & operator=(const Sleep &)=delete
grpc_core::Sleep::operator=
Sleep & operator=(Sleep &&other) noexcept
Definition: src/core/lib/promise/sleep.h:49
grpc_core::Sleep::Stage::kDone
@ kDone
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
grpc_core::Waker
Definition: activity.h:61
grpc_core::Sleep::mu_
Mutex mu_
Definition: src/core/lib/promise/sleep.h:69
poll.h
grpc_core::Sleep::OnTimer
void OnTimer()
Definition: src/core/lib/promise/sleep.cc:47
grpc_core::Sleep::Stage
Stage
Definition: src/core/lib/promise/sleep.h:64
grpc_core::Sleep::Sleep
Sleep(Timestamp deadline)
Definition: src/core/lib/promise/sleep.cc:28
activity.h
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
sync.h
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:13