2 * Copyright 2018 gRPC authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <grpcpp/alarm.h>
22 #include <grpc/support/log.h>
23 #include <grpc/support/port_platform.h>
24 #include <grpcpp/completion_queue.h>
25 #include <grpcpp/impl/grpc_library.h>
26 #include <grpcpp/support/time.h>
27 #include "src/core/lib/iomgr/exec_ctx.h"
28 #include "src/core/lib/iomgr/executor.h"
29 #include "src/core/lib/iomgr/timer.h"
30 #include "src/core/lib/surface/completion_queue.h"
32 #include <grpc/support/log.h>
33 #include "src/core/lib/debug/trace.h"
38 class AlarmImpl : public ::grpc::internal::CompletionQueueTag {
40 AlarmImpl() : cq_(nullptr), tag_(nullptr) {
41 gpr_ref_init(&refs_, 1);
42 grpc_timer_init_unset(&timer_);
44 ~AlarmImpl() override {}
45 bool FinalizeResult(void** tag, bool* /*status*/) override {
50 void Set(::grpc::CompletionQueue* cq, gpr_timespec deadline, void* tag) {
51 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
52 grpc_core::ExecCtx exec_ctx;
53 GRPC_CQ_INTERNAL_REF(cq->cq(), "alarm");
56 GPR_ASSERT(grpc_cq_begin_op(cq_, this));
59 [](void* arg, grpc_error_handle error) {
60 // queue the op on the completion queue
61 AlarmImpl* alarm = static_cast<AlarmImpl*>(arg);
63 // Preserve the cq and reset the cq_ so that the alarm
64 // can be reset when the alarm tag is delivered.
65 grpc_completion_queue* cq = alarm->cq_;
69 [](void* /*arg*/, grpc_cq_completion* /*completion*/) {}, arg,
71 GRPC_CQ_INTERNAL_UNREF(cq, "alarm");
73 this, grpc_schedule_on_exec_ctx);
74 grpc_timer_init(&timer_, grpc_timespec_to_millis_round_up(deadline),
77 void Set(gpr_timespec deadline, std::function<void(bool)> f) {
78 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
79 grpc_core::ExecCtx exec_ctx;
80 // Don't use any CQ at all. Instead just use the timer to fire the function
81 callback_ = std::move(f);
85 [](void* arg, grpc_error_handle error) {
86 grpc_core::Executor::Run(
88 [](void* arg, grpc_error_handle error) {
89 AlarmImpl* alarm = static_cast<AlarmImpl*>(arg);
90 alarm->callback_(error == GRPC_ERROR_NONE);
96 this, grpc_schedule_on_exec_ctx);
97 grpc_timer_init(&timer_, grpc_timespec_to_millis_round_up(deadline),
101 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
102 grpc_core::ExecCtx exec_ctx;
103 grpc_timer_cancel(&timer_);
111 void Ref() { gpr_ref(&refs_); }
113 if (gpr_unref(&refs_)) {
120 grpc_closure on_alarm_;
121 grpc_cq_completion completion_;
122 // completion queue where events about this alarm will be posted
123 grpc_completion_queue* cq_;
125 std::function<void(bool)> callback_;
127 } // namespace internal
129 static ::grpc::internal::GrpcLibraryInitializer g_gli_initializer;
131 Alarm::Alarm() : alarm_(new internal::AlarmImpl()) {
132 g_gli_initializer.summon();
135 void Alarm::SetInternal(::grpc::CompletionQueue* cq, gpr_timespec deadline,
137 // Note that we know that alarm_ is actually an internal::AlarmImpl
138 // but we declared it as the base pointer to avoid a forward declaration
139 // or exposing core data structures in the C++ public headers.
140 // Thus it is safe to use a static_cast to the subclass here, and the
141 // C++ style guide allows us to do so in this case
142 static_cast<internal::AlarmImpl*>(alarm_)->Set(cq, deadline, tag);
145 void Alarm::SetInternal(gpr_timespec deadline, std::function<void(bool)> f) {
146 // Note that we know that alarm_ is actually an internal::AlarmImpl
147 // but we declared it as the base pointer to avoid a forward declaration
148 // or exposing core data structures in the C++ public headers.
149 // Thus it is safe to use a static_cast to the subclass here, and the
150 // C++ style guide allows us to do so in this case
151 static_cast<internal::AlarmImpl*>(alarm_)->Set(deadline, std::move(f));
155 if (alarm_ != nullptr) {
156 static_cast<internal::AlarmImpl*>(alarm_)->Destroy();
160 void Alarm::Cancel() { static_cast<internal::AlarmImpl*>(alarm_)->Cancel(); }