1 /*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/iomgr/port.h"
22
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25
26 #include "src/core/lib/debug/trace.h"
27 #include "src/core/lib/iomgr/iomgr_custom.h"
28 #include "src/core/lib/iomgr/timer.h"
29 #include "src/core/lib/iomgr/timer_custom.h"
30
31 static grpc_custom_timer_vtable* custom_timer_impl;
32
grpc_custom_timer_callback(grpc_custom_timer * t,grpc_error * error)33 void grpc_custom_timer_callback(grpc_custom_timer* t, grpc_error* error) {
34 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
35 grpc_core::ExecCtx exec_ctx;
36 grpc_timer* timer = t->original;
37 GPR_ASSERT(timer->pending);
38 timer->pending = 0;
39 GRPC_CLOSURE_SCHED(timer->closure, GRPC_ERROR_NONE);
40 custom_timer_impl->stop(t);
41 gpr_free(t);
42 }
43
timer_init(grpc_timer * timer,grpc_millis deadline,grpc_closure * closure)44 static void timer_init(grpc_timer* timer, grpc_millis deadline,
45 grpc_closure* closure) {
46 uint64_t timeout;
47 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
48 grpc_millis now = grpc_core::ExecCtx::Get()->Now();
49 if (deadline <= grpc_core::ExecCtx::Get()->Now()) {
50 GRPC_CLOSURE_SCHED(closure, GRPC_ERROR_NONE);
51 timer->pending = false;
52 return;
53 } else {
54 timeout = deadline - now;
55 }
56 timer->pending = true;
57 timer->closure = closure;
58 grpc_custom_timer* timer_wrapper =
59 (grpc_custom_timer*)gpr_malloc(sizeof(grpc_custom_timer));
60 timer_wrapper->timeout_ms = timeout;
61 timer->custom_timer = (void*)timer_wrapper;
62 timer_wrapper->original = timer;
63 custom_timer_impl->start(timer_wrapper);
64 }
65
timer_cancel(grpc_timer * timer)66 static void timer_cancel(grpc_timer* timer) {
67 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
68 grpc_custom_timer* tw = (grpc_custom_timer*)timer->custom_timer;
69 if (timer->pending) {
70 timer->pending = 0;
71 GRPC_CLOSURE_SCHED(timer->closure, GRPC_ERROR_CANCELLED);
72 custom_timer_impl->stop(tw);
73 gpr_free(tw);
74 }
75 }
76
timer_check(grpc_millis * next)77 static grpc_timer_check_result timer_check(grpc_millis* next) {
78 return GRPC_TIMERS_NOT_CHECKED;
79 }
80
timer_list_init()81 static void timer_list_init() {}
timer_list_shutdown()82 static void timer_list_shutdown() {}
83
timer_consume_kick(void)84 static void timer_consume_kick(void) {}
85
86 static grpc_timer_vtable custom_timer_vtable = {
87 timer_init, timer_cancel, timer_check,
88 timer_list_init, timer_list_shutdown, timer_consume_kick};
89
grpc_custom_timer_init(grpc_custom_timer_vtable * impl)90 void grpc_custom_timer_init(grpc_custom_timer_vtable* impl) {
91 custom_timer_impl = impl;
92 grpc_set_timer_impl(&custom_timer_vtable);
93 }
94