alarm.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 gRPC authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
19 
20 #include <functional>
21 #include <utility>
22 
25 #include <grpc/support/log.h>
26 #include <grpc/support/sync.h>
27 #include <grpcpp/alarm.h>
31 
39 
40 namespace grpc {
41 
42 namespace internal {
44  public:
45  AlarmImpl() : cq_(nullptr), tag_(nullptr) {
46  gpr_ref_init(&refs_, 1);
48  }
49  ~AlarmImpl() override {}
50  bool FinalizeResult(void** tag, bool* /*status*/) override {
51  *tag = tag_;
52  Unref();
53  return true;
54  }
55  void Set(grpc::CompletionQueue* cq, gpr_timespec deadline, void* tag) {
56  grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
58  GRPC_CQ_INTERNAL_REF(cq->cq(), "alarm");
59  cq_ = cq->cq();
60  tag_ = tag;
63  &on_alarm_,
64  [](void* arg, grpc_error_handle error) {
65  // queue the op on the completion queue
66  AlarmImpl* alarm = static_cast<AlarmImpl*>(arg);
67  alarm->Ref();
68  // Preserve the cq and reset the cq_ so that the alarm
69  // can be reset when the alarm tag is delivered.
70  grpc_completion_queue* cq = alarm->cq_;
71  alarm->cq_ = nullptr;
73  cq, alarm, error,
74  [](void* /*arg*/, grpc_cq_completion* /*completion*/) {}, arg,
75  &alarm->completion_);
76  GRPC_CQ_INTERNAL_UNREF(cq, "alarm");
77  },
78  this, grpc_schedule_on_exec_ctx);
81  &on_alarm_);
82  }
83  void Set(gpr_timespec deadline, std::function<void(bool)> f) {
84  grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
86  // Don't use any CQ at all. Instead just use the timer to fire the function
87  callback_ = std::move(f);
88  Ref();
90  &on_alarm_,
91  [](void* arg, grpc_error_handle error) {
94  [](void* arg, grpc_error_handle error) {
95  AlarmImpl* alarm = static_cast<AlarmImpl*>(arg);
97  alarm->Unref();
98  },
99  arg, nullptr),
100  error);
101  },
102  this, grpc_schedule_on_exec_ctx);
105  &on_alarm_);
106  }
107  void Cancel() {
108  grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
111  }
112  void Destroy() {
113  Cancel();
114  Unref();
115  }
116 
117  private:
118  void Ref() { gpr_ref(&refs_); }
119  void Unref() {
120  if (gpr_unref(&refs_)) {
121  delete this;
122  }
123  }
124 
129  // completion queue where events about this alarm will be posted
131  void* tag_;
133 };
134 } // namespace internal
135 
137 
138 Alarm::Alarm() : alarm_(new internal::AlarmImpl()) {
140 }
141 
143  void* tag) {
144  // Note that we know that alarm_ is actually an internal::AlarmImpl
145  // but we declared it as the base pointer to avoid a forward declaration
146  // or exposing core data structures in the C++ public headers.
147  // Thus it is safe to use a static_cast to the subclass here, and the
148  // C++ style guide allows us to do so in this case
149  static_cast<internal::AlarmImpl*>(alarm_)->Set(cq, deadline, tag);
150 }
151 
152 void Alarm::SetInternal(gpr_timespec deadline, std::function<void(bool)> f) {
153  // Note that we know that alarm_ is actually an internal::AlarmImpl
154  // but we declared it as the base pointer to avoid a forward declaration
155  // or exposing core data structures in the C++ public headers.
156  // Thus it is safe to use a static_cast to the subclass here, and the
157  // C++ style guide allows us to do so in this case
158  static_cast<internal::AlarmImpl*>(alarm_)->Set(deadline, std::move(f));
159 }
160 
162  if (alarm_ != nullptr) {
163  static_cast<internal::AlarmImpl*>(alarm_)->Destroy();
164  }
165 }
166 
167 void Alarm::Cancel() { static_cast<internal::AlarmImpl*>(alarm_)->Cancel(); }
168 } // namespace grpc
GRPC_CLOSURE_INIT
#define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler)
Definition: closure.h:115
log.h
grpc::internal::AlarmImpl::AlarmImpl
AlarmImpl()
Definition: alarm.cc:45
grpc::internal::AlarmImpl
Definition: alarm.cc:43
grpc
Definition: grpcpp/alarm.h:33
grpc::internal::AlarmImpl::Unref
void Unref()
Definition: alarm.cc:119
grpc::internal::GrpcLibraryInitializer::summon
int summon()
Definition: grpcpp/impl/grpc_library.h:54
grpc::g_gli_initializer
static grpc::internal::GrpcLibraryInitializer g_gli_initializer
Definition: channel_cc.cc:52
error
grpc_error_handle error
Definition: retry_filter.cc:499
completion_queue.h
grpc::internal::AlarmImpl::completion_
grpc_cq_completion completion_
Definition: alarm.cc:128
closure.h
grpc_cq_completion
Definition: src/core/lib/surface/completion_queue.h:43
grpc_core::ApplicationCallbackExecCtx
Definition: exec_ctx.h:283
grpc_cq_end_op
void grpc_cq_end_op(grpc_completion_queue *cq, void *tag, grpc_error_handle error, void(*done)(void *done_arg, grpc_cq_completion *storage), void *done_arg, grpc_cq_completion *storage, bool internal)
Definition: completion_queue.cc:894
GRPC_CLOSURE_CREATE
#define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler)
Definition: closure.h:160
grpc::internal::AlarmImpl::Set
void Set(gpr_timespec deadline, std::function< void(bool)> f)
Definition: alarm.cc:83
grpc::internal::AlarmImpl::refs_
gpr_refcount refs_
Definition: alarm.cc:126
alarm.h
env.new
def new
Definition: env.py:51
grpc_timer
Definition: iomgr/timer.h:33
grpc::internal::AlarmImpl::Set
void Set(grpc::CompletionQueue *cq, gpr_timespec deadline, void *tag)
Definition: alarm.cc:55
grpc::internal::AlarmImpl::Ref
void Ref()
Definition: alarm.cc:118
grpc_core::Executor::Run
static void Run(grpc_closure *closure, grpc_error_handle error, ExecutorType executor_type=ExecutorType::DEFAULT, ExecutorJobType job_type=ExecutorJobType::SHORT)
Definition: executor.cc:398
gpr_refcount
Definition: impl/codegen/sync_generic.h:39
grpc::Alarm::SetInternal
void SetInternal(grpc::CompletionQueue *cq, gpr_timespec deadline, void *tag)
Definition: alarm.cc:142
grpc_types.h
grpc::Alarm::alarm_
grpc::internal::CompletionQueueTag * alarm_
Definition: grpcpp/alarm.h:96
grpc_timer_init_unset
void grpc_timer_init_unset(grpc_timer *timer)
Definition: timer_generic.cc:330
grpc::internal::GrpcLibraryInitializer
Instantiating this class ensures the proper initialization of gRPC.
Definition: grpcpp/impl/grpc_library.h:39
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc::Alarm::Set
void Set(grpc::CompletionQueue *cq, const T &deadline, void *tag)
Definition: grpcpp/alarm.h:64
tag
static void * tag(intptr_t t)
Definition: bad_client.cc:318
grpc_completion_queue
Definition: completion_queue.cc:347
completion_queue.h
completion_queue_tag.h
arg
Definition: cmdline.cc:40
time.h
grpc::Alarm::Alarm
Alarm()
Create an unset completion queue alarm.
Definition: alarm.cc:138
error.h
grpc_core::ExecCtx
Definition: exec_ctx.h:97
executor.h
gpr_types.h
grpc_timer_cancel
void grpc_timer_cancel(grpc_timer *timer)
Definition: iomgr/timer.cc:36
grpc::internal::AlarmImpl::cq_
grpc_completion_queue * cq_
Definition: alarm.cc:130
grpc_cq_begin_op
bool grpc_cq_begin_op(grpc_completion_queue *cq, void *tag)
Definition: completion_queue.cc:672
grpc::internal::AlarmImpl::timer_
grpc_timer timer_
Definition: alarm.cc:125
phony_transport::Destroy
void Destroy(grpc_transport *)
Definition: bm_call_create.cc:443
grpc_library.h
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc::Alarm::~Alarm
~Alarm() override
Destroy the given completion queue alarm, cancelling it in the process.
Definition: alarm.cc:161
GRPC_CQ_INTERNAL_UNREF
#define GRPC_CQ_INTERNAL_UNREF(cq, reason)
Definition: src/core/lib/surface/completion_queue.h:65
arg
struct arg arg
exec_ctx.h
grpc_timer_init
void grpc_timer_init(grpc_timer *timer, grpc_core::Timestamp deadline, grpc_closure *closure)
Definition: iomgr/timer.cc:31
GRPC_CQ_INTERNAL_REF
#define GRPC_CQ_INTERNAL_REF(cq, reason)
Definition: src/core/lib/surface/completion_queue.h:63
grpc::internal::AlarmImpl::~AlarmImpl
~AlarmImpl() override
Definition: alarm.cc:49
grpc::internal::AlarmImpl::Cancel
void Cancel()
Definition: alarm.cc:107
timer.h
internal
Definition: benchmark/test/output_test_helper.cc:20
gpr_ref_init
GPRAPI void gpr_ref_init(gpr_refcount *r, int n)
Definition: sync.cc:86
grpc::CompletionQueue
Definition: include/grpcpp/impl/codegen/completion_queue.h:104
gpr_timespec
Definition: gpr_types.h:50
gpr_unref
GPRAPI int gpr_unref(gpr_refcount *r)
Definition: sync.cc:103
grpc::internal::CompletionQueueTag
An interface allowing implementors to process and filter event tags.
Definition: grpcpp/impl/codegen/completion_queue_tag.h:28
grpc_error
Definition: error_internal.h:42
grpc::internal::AlarmImpl::tag_
void * tag_
Definition: alarm.cc:131
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc_core::Timestamp::FromTimespecRoundUp
static Timestamp FromTimespecRoundUp(gpr_timespec t)
Definition: src/core/lib/gprpp/time.cc:136
grpc::internal::AlarmImpl::FinalizeResult
bool FinalizeResult(void **tag, bool *) override
Definition: alarm.cc:50
grpc::internal::AlarmImpl::on_alarm_
grpc_closure on_alarm_
Definition: alarm.cc:127
grpc::Alarm::Cancel
void Cancel()
Definition: alarm.cc:167
sync.h
grpc_closure
Definition: closure.h:56
gpr_ref
GPRAPI void gpr_ref(gpr_refcount *r)
Definition: sync.cc:88
grpc::internal::AlarmImpl::callback_
std::function< void(bool)> callback_
Definition: alarm.cc:132
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
grpc::internal::AlarmImpl::Destroy
void Destroy()
Definition: alarm.cc:112
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:40