• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 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 #ifdef GRPC_UV
24 
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 
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"
32 
33 #include <uv.h>
34 
timer_close_callback(uv_handle_t * handle)35 static void timer_close_callback(uv_handle_t* handle) { gpr_free(handle); }
36 
stop_uv_timer(uv_timer_t * 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);
41 }
42 
run_expired_timer(uv_timer_t * handle)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);
46 }
47 
timer_start(grpc_custom_timer * t)48 static void timer_start(grpc_custom_timer* t) {
49   uv_timer_t* uv_timer;
50   uv_timer = (uv_timer_t*)gpr_malloc(sizeof(uv_timer_t));
51   uv_timer_init(uv_default_loop(), uv_timer);
52   uv_timer->data = t;
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);
58 }
59 
timer_stop(grpc_custom_timer * t)60 static void timer_stop(grpc_custom_timer* t) {
61   stop_uv_timer((uv_timer_t*)t->timer);
62 }
63 
64 grpc_custom_timer_vtable uv_timer_vtable = {timer_start, timer_stop};
65 
66 #endif
67