3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/iomgr/port.h"
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
28 #include "src/core/lib/debug/trace.h"
29 #include "src/core/lib/iomgr/iomgr_custom.h"
30 #include "src/core/lib/iomgr/timer.h"
31 #include "src/core/lib/iomgr/timer_custom.h"
35 static void timer_close_callback(uv_handle_t* handle) { gpr_free(handle); }
37 static void stop_uv_timer(uv_timer_t* handle) {
38 uv_timer_stop(handle);
39 uv_unref((uv_handle_t*)handle);
40 uv_close((uv_handle_t*)handle, timer_close_callback);
43 void run_expired_timer(uv_timer_t* handle) {
44 grpc_custom_timer* timer_wrapper = (grpc_custom_timer*)handle->data;
45 grpc_custom_timer_callback(timer_wrapper, GRPC_ERROR_NONE);
48 static void timer_start(grpc_custom_timer* t) {
50 uv_timer = (uv_timer_t*)gpr_malloc(sizeof(uv_timer_t));
51 uv_timer_init(uv_default_loop(), uv_timer);
53 t->timer = (void*)uv_timer;
54 uv_timer_start(uv_timer, run_expired_timer, t->timeout_ms, 0);
55 // Node uses a garbage collector to call destructors, so we don't
56 // want to hold the uv loop open with active gRPC objects.
57 uv_unref((uv_handle_t*)uv_timer);
60 static void timer_stop(grpc_custom_timer* t) {
61 stop_uv_timer((uv_timer_t*)t->timer);
64 grpc_custom_timer_vtable uv_timer_vtable = {timer_start, timer_stop};