commit
4c0640ebd6
189 changed files with 9460 additions and 4490 deletions
@ -0,0 +1,4 @@ |
||||
set noparent |
||||
@jtattermusch |
||||
@nicolasnoble |
||||
@matt-kwong |
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@ -1,137 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <inttypes.h> |
||||
|
||||
#include "src/core/lib/surface/alarm_internal.h" |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include "src/core/lib/iomgr/timer.h" |
||||
#include "src/core/lib/surface/completion_queue.h" |
||||
|
||||
grpc_core::DebugOnlyTraceFlag grpc_trace_alarm_refcount(false, |
||||
"alarm_refcount"); |
||||
|
||||
struct grpc_alarm { |
||||
gpr_refcount refs; |
||||
grpc_timer alarm; |
||||
grpc_closure on_alarm; |
||||
grpc_cq_completion completion; |
||||
/** completion queue where events about this alarm will be posted */ |
||||
grpc_completion_queue* cq; |
||||
/** user supplied tag */ |
||||
void* tag; |
||||
}; |
||||
|
||||
static void alarm_ref(grpc_alarm* alarm) { gpr_ref(&alarm->refs); } |
||||
|
||||
static void alarm_unref(grpc_alarm* alarm) { |
||||
if (gpr_unref(&alarm->refs)) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
if (alarm->cq != nullptr) { |
||||
GRPC_CQ_INTERNAL_UNREF(alarm->cq, "alarm"); |
||||
} |
||||
|
||||
gpr_free(alarm); |
||||
} |
||||
} |
||||
|
||||
#ifndef NDEBUG |
||||
static void alarm_ref_dbg(grpc_alarm* alarm, const char* reason, |
||||
const char* file, int line) { |
||||
if (grpc_trace_alarm_refcount.enabled()) { |
||||
gpr_atm val = gpr_atm_no_barrier_load(&alarm->refs.count); |
||||
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
||||
"Alarm:%p ref %" PRIdPTR " -> %" PRIdPTR " %s", alarm, val, |
||||
val + 1, reason); |
||||
} |
||||
|
||||
alarm_ref(alarm); |
||||
} |
||||
|
||||
static void alarm_unref_dbg(grpc_alarm* alarm, const char* reason, |
||||
const char* file, int line) { |
||||
if (grpc_trace_alarm_refcount.enabled()) { |
||||
gpr_atm val = gpr_atm_no_barrier_load(&alarm->refs.count); |
||||
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
||||
"Alarm:%p Unref %" PRIdPTR " -> %" PRIdPTR " %s", alarm, val, |
||||
val - 1, reason); |
||||
} |
||||
|
||||
alarm_unref(alarm); |
||||
} |
||||
#endif |
||||
|
||||
static void alarm_end_completion(void* arg, grpc_cq_completion* c) { |
||||
grpc_alarm* alarm = (grpc_alarm*)arg; |
||||
GRPC_ALARM_UNREF(alarm, "dequeue-end-op"); |
||||
} |
||||
|
||||
static void alarm_cb(void* arg, grpc_error* error) { |
||||
grpc_alarm* alarm = (grpc_alarm*)arg; |
||||
|
||||
/* We are queuing an op on completion queue. This means, the alarm's structure
|
||||
cannot be destroyed until the op is dequeued. Adding an extra ref |
||||
here and unref'ing when the op is dequeued will achieve this */ |
||||
GRPC_ALARM_REF(alarm, "queue-end-op"); |
||||
grpc_cq_end_op(alarm->cq, alarm->tag, error, alarm_end_completion, |
||||
(void*)alarm, &alarm->completion); |
||||
} |
||||
|
||||
grpc_alarm* grpc_alarm_create(void* reserved) { |
||||
grpc_alarm* alarm = (grpc_alarm*)gpr_malloc(sizeof(grpc_alarm)); |
||||
|
||||
#ifndef NDEBUG |
||||
if (grpc_trace_alarm_refcount.enabled()) { |
||||
gpr_log(GPR_DEBUG, "Alarm:%p created (ref: 1)", alarm); |
||||
} |
||||
#endif |
||||
|
||||
gpr_ref_init(&alarm->refs, 1); |
||||
grpc_timer_init_unset(&alarm->alarm); |
||||
alarm->cq = nullptr; |
||||
GRPC_CLOSURE_INIT(&alarm->on_alarm, alarm_cb, alarm, |
||||
grpc_schedule_on_exec_ctx); |
||||
return alarm; |
||||
} |
||||
|
||||
void grpc_alarm_set(grpc_alarm* alarm, grpc_completion_queue* cq, |
||||
gpr_timespec deadline, void* tag, void* reserved) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
|
||||
GRPC_CQ_INTERNAL_REF(cq, "alarm"); |
||||
alarm->cq = cq; |
||||
alarm->tag = tag; |
||||
|
||||
GPR_ASSERT(grpc_cq_begin_op(cq, tag)); |
||||
grpc_timer_init(&alarm->alarm, grpc_timespec_to_millis_round_up(deadline), |
||||
&alarm->on_alarm); |
||||
} |
||||
|
||||
void grpc_alarm_cancel(grpc_alarm* alarm, void* reserved) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
grpc_timer_cancel(&alarm->alarm); |
||||
} |
||||
|
||||
void grpc_alarm_destroy(grpc_alarm* alarm, void* reserved) { |
||||
grpc_alarm_cancel(alarm, reserved); |
||||
GRPC_ALARM_UNREF(alarm, "alarm_destroy"); |
||||
} |
@ -1,40 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015-2017 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_LIB_SURFACE_ALARM_INTERNAL_H |
||||
#define GRPC_CORE_LIB_SURFACE_ALARM_INTERNAL_H |
||||
|
||||
#include <grpc/support/log.h> |
||||
#include "src/core/lib/debug/trace.h" |
||||
|
||||
extern grpc_core::DebugOnlyTraceFlag grpc_trace_alarm_refcount; |
||||
|
||||
#ifndef NDEBUG |
||||
|
||||
#define GRPC_ALARM_REF(a, reason) alarm_ref_dbg(a, reason, __FILE__, __LINE__) |
||||
#define GRPC_ALARM_UNREF(a, reason) \ |
||||
alarm_unref_dbg(a, reason, __FILE__, __LINE__) |
||||
|
||||
#else /* !defined(NDEBUG) */ |
||||
|
||||
#define GRPC_ALARM_REF(a, reason) alarm_ref(a) |
||||
#define GRPC_ALARM_UNREF(a, reason) alarm_unref(a) |
||||
|
||||
#endif /* defined(NDEBUG) */ |
||||
|
||||
#endif /* GRPC_CORE_LIB_SURFACE_ALARM_INTERNAL_H */ |
@ -0,0 +1,123 @@ |
||||
/*
|
||||
* Copyright 2018 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc++/alarm.h> |
||||
|
||||
#include <memory> |
||||
|
||||
#include <grpc++/completion_queue.h> |
||||
#include <grpc++/impl/grpc_library.h> |
||||
#include <grpc++/support/time.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/port_platform.h> |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/iomgr/timer.h" |
||||
#include "src/core/lib/surface/completion_queue.h" |
||||
|
||||
#include <grpc/support/log.h> |
||||
#include "src/core/lib/debug/trace.h" |
||||
|
||||
namespace grpc { |
||||
|
||||
namespace internal { |
||||
class AlarmImpl : public CompletionQueueTag { |
||||
public: |
||||
AlarmImpl() : cq_(nullptr), tag_(nullptr) { |
||||
gpr_ref_init(&refs_, 1); |
||||
grpc_timer_init_unset(&timer_); |
||||
GRPC_CLOSURE_INIT(&on_alarm_, |
||||
[](void* arg, grpc_error* error) { |
||||
// queue the op on the completion queue
|
||||
AlarmImpl* alarm = static_cast<AlarmImpl*>(arg); |
||||
alarm->Ref(); |
||||
grpc_cq_end_op( |
||||
alarm->cq_, alarm, error, |
||||
[](void* arg, grpc_cq_completion* completion) {}, |
||||
arg, &alarm->completion_); |
||||
}, |
||||
this, grpc_schedule_on_exec_ctx); |
||||
} |
||||
~AlarmImpl() { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
if (cq_ != nullptr) { |
||||
GRPC_CQ_INTERNAL_UNREF(cq_, "alarm"); |
||||
} |
||||
} |
||||
bool FinalizeResult(void** tag, bool* status) override { |
||||
*tag = tag_; |
||||
Unref(); |
||||
return true; |
||||
} |
||||
void Set(CompletionQueue* cq, gpr_timespec deadline, void* tag) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
GRPC_CQ_INTERNAL_REF(cq->cq(), "alarm"); |
||||
cq_ = cq->cq(); |
||||
tag_ = tag; |
||||
GPR_ASSERT(grpc_cq_begin_op(cq_, this)); |
||||
grpc_timer_init(&timer_, grpc_timespec_to_millis_round_up(deadline), |
||||
&on_alarm_); |
||||
} |
||||
void Cancel() { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
grpc_timer_cancel(&timer_); |
||||
} |
||||
void Destroy() { |
||||
Cancel(); |
||||
Unref(); |
||||
} |
||||
|
||||
private: |
||||
void Ref() { gpr_ref(&refs_); } |
||||
void Unref() { |
||||
if (gpr_unref(&refs_)) { |
||||
delete this; |
||||
} |
||||
} |
||||
|
||||
grpc_timer timer_; |
||||
gpr_refcount refs_; |
||||
grpc_closure on_alarm_; |
||||
grpc_cq_completion completion_; |
||||
// completion queue where events about this alarm will be posted
|
||||
grpc_completion_queue* cq_; |
||||
void* tag_; |
||||
}; |
||||
} // namespace internal
|
||||
|
||||
static internal::GrpcLibraryInitializer g_gli_initializer; |
||||
|
||||
Alarm::Alarm() : alarm_(new internal::AlarmImpl()) { |
||||
g_gli_initializer.summon(); |
||||
} |
||||
|
||||
void Alarm::SetInternal(CompletionQueue* cq, gpr_timespec deadline, void* tag) { |
||||
// Note that we know that alarm_ is actually an internal::AlarmImpl
|
||||
// but we declared it as the base pointer to avoid a forward declaration
|
||||
// or exposing core data structures in the C++ public headers.
|
||||
// Thus it is safe to use a static_cast to the subclass here, and the
|
||||
// C++ style guide allows us to do so in this case
|
||||
static_cast<internal::AlarmImpl*>(alarm_)->Set(cq, deadline, tag); |
||||
} |
||||
|
||||
Alarm::~Alarm() { |
||||
if (alarm_ != nullptr) { |
||||
static_cast<internal::AlarmImpl*>(alarm_)->Destroy(); |
||||
} |
||||
} |
||||
|
||||
void Alarm::Cancel() { static_cast<internal::AlarmImpl*>(alarm_)->Cancel(); } |
||||
} // namespace grpc
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,40 @@ |
||||
# Copyright 2018 gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
|
||||
cdef void* _copy_pointer(void* pointer) |
||||
|
||||
|
||||
cdef void _destroy_pointer(void* pointer) |
||||
|
||||
|
||||
cdef int _compare_pointer(void* first_pointer, void* second_pointer) |
||||
|
||||
|
||||
cdef class _ArgumentProcessor: |
||||
|
||||
cdef grpc_arg c_argument |
||||
|
||||
cdef void c(self, argument, grpc_arg_pointer_vtable *vtable, references) |
||||
|
||||
|
||||
cdef class _ArgumentsProcessor: |
||||
|
||||
cdef readonly tuple _arguments |
||||
cdef list _argument_processors |
||||
cdef readonly list _references |
||||
cdef grpc_channel_args _c_arguments |
||||
|
||||
cdef grpc_channel_args *c(self, grpc_arg_pointer_vtable *vtable) |
||||
cdef un_c(self) |
@ -0,0 +1,88 @@ |
||||
# Copyright 2018 gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
cimport cpython |
||||
|
||||
|
||||
cdef void* _copy_pointer(void* pointer): |
||||
return pointer |
||||
|
||||
|
||||
cdef void _destroy_pointer(void* pointer): |
||||
pass |
||||
|
||||
|
||||
cdef int _compare_pointer(void* first_pointer, void* second_pointer): |
||||
if first_pointer < second_pointer: |
||||
return -1 |
||||
elif first_pointer > second_pointer: |
||||
return 1 |
||||
else: |
||||
return 0 |
||||
|
||||
|
||||
cdef class _ArgumentProcessor: |
||||
|
||||
cdef void c(self, argument, grpc_arg_pointer_vtable *vtable, references): |
||||
key, value = argument |
||||
cdef bytes encoded_key = _encode(key) |
||||
if encoded_key is not key: |
||||
references.append(encoded_key) |
||||
self.c_argument.key = encoded_key |
||||
if isinstance(value, int): |
||||
self.c_argument.type = GRPC_ARG_INTEGER |
||||
self.c_argument.value.integer = value |
||||
elif isinstance(value, (bytes, str, unicode,)): |
||||
self.c_argument.type = GRPC_ARG_STRING |
||||
encoded_value = _encode(value) |
||||
if encoded_value is not value: |
||||
references.append(encoded_value) |
||||
self.c_argument.value.string = encoded_value |
||||
elif hasattr(value, '__int__'): |
||||
# Pointer objects must override __int__() to return |
||||
# the underlying C address (Python ints are word size). The |
||||
# lifecycle of the pointer is fixed to the lifecycle of the |
||||
# python object wrapping it. |
||||
self.c_argument.type = GRPC_ARG_POINTER |
||||
self.c_argument.value.pointer.vtable = vtable |
||||
self.c_argument.value.pointer.address = <void*>(<intptr_t>int(value)) |
||||
else: |
||||
raise TypeError( |
||||
'Expected int, bytes, or behavior, got {}'.format(type(value))) |
||||
|
||||
|
||||
cdef class _ArgumentsProcessor: |
||||
|
||||
def __cinit__(self, arguments): |
||||
self._arguments = () if arguments is None else tuple(arguments) |
||||
self._argument_processors = [] |
||||
self._references = [] |
||||
|
||||
cdef grpc_channel_args *c(self, grpc_arg_pointer_vtable *vtable): |
||||
self._c_arguments.arguments_length = len(self._arguments) |
||||
if self._c_arguments.arguments_length == 0: |
||||
return NULL |
||||
else: |
||||
self._c_arguments.arguments = <grpc_arg *>gpr_malloc( |
||||
self._c_arguments.arguments_length * sizeof(grpc_arg)) |
||||
for index, argument in enumerate(self._arguments): |
||||
argument_processor = _ArgumentProcessor() |
||||
argument_processor.c(argument, vtable, self._references) |
||||
self._c_arguments.arguments[index] = argument_processor.c_argument |
||||
self._argument_processors.append(argument_processor) |
||||
return &self._c_arguments |
||||
|
||||
cdef un_c(self): |
||||
if self._arguments: |
||||
gpr_free(self._c_arguments.arguments) |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue