src/core/lib/promise/sleep.cc
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 
16 
18 
20 
23 
24 namespace grpc_core {
25 
27 
28 Sleep::Sleep(Timestamp deadline) : deadline_(deadline) {}
29 
31  if (deadline_ == Timestamp::InfPast()) return;
32  ReleasableMutexLock lock(&mu_);
33  switch (stage_) {
34  case Stage::kInitial:
35  break;
36  case Stage::kStarted:
37  if (GetDefaultEventEngine()->Cancel(timer_handle_)) {
38  lock.Release();
39  OnTimer();
40  }
41  break;
42  case Stage::kDone:
43  break;
44  }
45 }
46 
48  Waker tmp_waker;
49  {
50  MutexLock lock(&mu_);
51  stage_ = Stage::kDone;
52  tmp_waker = std::move(waker_);
53  }
54  tmp_waker.Wakeup();
55 }
56 
58  MutexLock lock(&mu_);
59  switch (stage_) {
60  case Stage::kInitial:
61  if (deadline_ <= ExecCtx::Get()->Now()) {
62  return absl::OkStatus();
63  }
64  stage_ = Stage::kStarted;
66  deadline_ - ExecCtx::Get()->Now(), [this] {
67  ApplicationCallbackExecCtx callback_exec_ctx;
69  OnTimer();
70  });
71  break;
72  case Stage::kStarted:
73  break;
74  case Stage::kDone:
75  return absl::OkStatus();
76  }
78  return Pending{};
79 }
80 
81 } // namespace grpc_core
grpc_core::Activity::MakeNonOwningWaker
virtual Waker MakeNonOwningWaker()=0
deadline_
Timestamp deadline_
Definition: channel_connectivity.cc:163
grpc_core::Sleep::Stage::kStarted
@ kStarted
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
grpc_core::Activity::current
static Activity * current()
Definition: activity.h:124
event_engine.h
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
grpc_core::ReleasableMutexLock::Release
void Release() ABSL_UNLOCK_FUNCTION()
Definition: src/core/lib/gprpp/sync.h:115
absl::OkStatus
Status OkStatus()
Definition: third_party/abseil-cpp/absl/status/status.h:882
grpc_core::Sleep::deadline_
Timestamp deadline_
Definition: src/core/lib/promise/sleep.h:67
grpc_core::ApplicationCallbackExecCtx
Definition: exec_ctx.h:283
grpc_core::Timestamp::InfPast
static constexpr Timestamp InfPast()
Definition: src/core/lib/gprpp/time.h:83
grpc_event_engine::experimental::EventEngine::RunAfter
virtual TaskHandle RunAfter(Duration when, Closure *closure)=0
grpc_core::Sleep::Stage::kInitial
@ kInitial
grpc_core::Pending
Definition: poll.h:29
sleep.h
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::ReleasableMutexLock
Definition: src/core/lib/gprpp/sync.h:102
grpc_core::Sleep::~Sleep
~Sleep()
Definition: src/core/lib/promise/sleep.cc:30
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_core::Sleep::Stage::kDone
@ kDone
absl::Now
ABSL_NAMESPACE_BEGIN Time Now()
Definition: abseil-cpp/absl/time/clock.cc:39
grpc_core::Waker
Definition: activity.h:61
grpc_core::Sleep::mu_
Mutex mu_
Definition: src/core/lib/promise/sleep.h:69
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_event_engine::experimental::GetDefaultEventEngine
EventEngine * GetDefaultEventEngine()
Definition: event_engine.cc:47
exec_ctx.h
grpc_core::Sleep::OnTimer
void OnTimer()
Definition: src/core/lib/promise/sleep.cc:47
grpc_core::Sleep::Sleep
Sleep(Timestamp deadline)
Definition: src/core/lib/promise/sleep.cc:28
grpc_core::Waker::Wakeup
void Wakeup()
Definition: activity.h:77
event_engine_factory.h
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
port_platform.h


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