| 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 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include <functional> |
| 21 |
#include <utility> |
| 22 |
|
| 23 |
#include <grpc/impl/codegen/gpr_types.h> |
| 24 |
#include <grpc/impl/codegen/grpc_types.h> |
| 25 |
#include <grpc/support/log.h> |
| 26 |
#include <grpc/support/sync.h> |
| 27 |
#include <grpcpp/alarm.h> |
| 28 |
#include <grpcpp/completion_queue.h> |
| 29 |
#include <grpcpp/impl/codegen/completion_queue_tag.h> |
| 30 |
#include <grpcpp/impl/grpc_library.h> |
| 31 |
|
| 32 |
#include "src/core/lib/gprpp/time.h" |
| 33 |
#include "src/core/lib/iomgr/closure.h" |
| 34 |
#include "src/core/lib/iomgr/error.h" |
| 35 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 36 |
#include "src/core/lib/iomgr/executor.h" |
| 37 |
#include "src/core/lib/iomgr/timer.h" |
| 38 |
#include "src/core/lib/surface/completion_queue.h" |
| 39 |
|
| 40 |
namespace grpc { |
| 41 |
|
| 42 |
namespace internal { |
| 43 |
class AlarmImpl : public grpc::internal::CompletionQueueTag { |
| 44 |
public: |
| 45 |
AlarmImpl() : cq_(nullptr), tag_(nullptr) { |
| 46 |
gpr_ref_init(&refs_, 1); |
| 47 |
grpc_timer_init_unset(&timer_); |
| 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; |
| 57 |
grpc_core::ExecCtx exec_ctx; |
| 58 |
GRPC_CQ_INTERNAL_REF(cq->cq(), "alarm"); |
| 59 |
cq_ = cq->cq(); |
| 60 |
tag_ = tag; |
| 61 |
GPR_ASSERT(grpc_cq_begin_op(cq_, this)); |
| 62 |
GRPC_CLOSURE_INIT( |
| 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; |
| 72 |
grpc_cq_end_op( |
| 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); |
| 79 |
grpc_timer_init(&timer_, |
| 80 |
grpc_core::Timestamp::FromTimespecRoundUp(deadline), |
| 81 |
&on_alarm_); |
| 82 |
} |
| 83 |
void Set(gpr_timespec deadline, std::function<void(bool)> f) { |
| 84 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 85 |
grpc_core::ExecCtx 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(); |
| 89 |
GRPC_CLOSURE_INIT( |
| 90 |
&on_alarm_, |
| 91 |
[](void* arg, grpc_error_handle error) { |
| 92 |
grpc_core::Executor::Run(GRPC_CLOSURE_CREATE( |
| 93 |
[](void* arg, grpc_error_handle error) { |
| 94 |
AlarmImpl* alarm = |
| 95 |
static_cast<AlarmImpl*>(arg); |
| 96 |
alarm->callback_(error.ok()); |
| 97 |
alarm->Unref(); |
| 98 |
}, |
| 99 |
arg, nullptr), |
| 100 |
error); |
| 101 |
}, |
| 102 |
this, grpc_schedule_on_exec_ctx); |
| 103 |
grpc_timer_init(&timer_, |
| 104 |
grpc_core::Timestamp::FromTimespecRoundUp(deadline), |
| 105 |
&on_alarm_); |
| 106 |
} |
| 107 |
void Cancel() { |
| 108 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 109 |
grpc_core::ExecCtx exec_ctx; |
| 110 |
grpc_timer_cancel(&timer_); |
| 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 |
|
| 125 |
grpc_timer timer_; |
| 126 |
gpr_refcount refs_; |
| 127 |
grpc_closure on_alarm_; |
| 128 |
grpc_cq_completion completion_; |
| 129 |
// completion queue where events about this alarm will be posted |
| 130 |
grpc_completion_queue* cq_; |
| 131 |
void* tag_; |
| 132 |
std::function<void(bool)> callback_; |
| 133 |
}; |
| 134 |
} // namespace internal |
| 135 |
|
| 136 |
static grpc::internal::GrpcLibraryInitializer g_gli_initializer; |
| 137 |
|
| 138 |
Alarm::Alarm() : alarm_(new internal::AlarmImpl()) { |
| 139 |
g_gli_initializer.summon(); |
| 140 |
} |
| 141 |
|
| 142 |
void Alarm::SetInternal(grpc::CompletionQueue* cq, gpr_timespec deadline, |
| 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 |
|
| 161 |
Alarm::~Alarm() { |
| 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 |