• 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 #include "src/core/lib/iomgr/pollset_custom.h"
28 
29 #include <uv.h>
30 
31 /* Indicates that grpc_pollset_work should run an iteration of the UV loop
32    before running callbacks. This defaults to 1, and should be disabled if
33    grpc_pollset_work will be called within the callstack of uv_run */
34 int grpc_pollset_work_run_loop = 1;
35 
36 static bool g_kicked = false;
37 
38 typedef struct uv_poller_handle {
39   uv_timer_t poll_timer;
40   uv_timer_t kick_timer;
41   int refs;
42 } uv_poller_handle;
43 
44 static uv_poller_handle* g_handle;
45 
init()46 static void init() {
47   g_handle = (uv_poller_handle*)gpr_malloc(sizeof(uv_poller_handle));
48   g_handle->refs = 2;
49   uv_timer_init(uv_default_loop(), &g_handle->poll_timer);
50   uv_timer_init(uv_default_loop(), &g_handle->kick_timer);
51 }
52 
empty_timer_cb(uv_timer_t * handle)53 static void empty_timer_cb(uv_timer_t* handle) {}
54 
kick_timer_cb(uv_timer_t * handle)55 static void kick_timer_cb(uv_timer_t* handle) { g_kicked = false; }
56 
run_loop(size_t timeout)57 static void run_loop(size_t timeout) {
58   if (grpc_pollset_work_run_loop) {
59     if (timeout == 0) {
60       uv_run(uv_default_loop(), UV_RUN_NOWAIT);
61     } else {
62       uv_timer_start(&g_handle->poll_timer, empty_timer_cb, timeout, 0);
63       uv_run(uv_default_loop(), UV_RUN_ONCE);
64       uv_timer_stop(&g_handle->poll_timer);
65     }
66   }
67 }
68 
kick()69 static void kick() {
70   if (!g_kicked) {
71     g_kicked = true;
72     uv_timer_start(&g_handle->kick_timer, kick_timer_cb, 0, 0);
73   }
74 }
75 
close_timer_cb(uv_handle_t * handle)76 static void close_timer_cb(uv_handle_t* handle) {
77   g_handle->refs--;
78   if (g_handle->refs == 0) {
79     gpr_free(g_handle);
80   }
81 }
82 
shutdown()83 static void shutdown() {
84   uv_close((uv_handle_t*)&g_handle->poll_timer, close_timer_cb);
85   uv_close((uv_handle_t*)&g_handle->kick_timer, close_timer_cb);
86   if (grpc_pollset_work_run_loop) {
87     GPR_ASSERT(uv_run(uv_default_loop(), UV_RUN_DEFAULT) == 0);
88   }
89 }
90 
91 grpc_custom_poller_vtable uv_pollset_vtable = {init, run_loop, kick, shutdown};
92 
93 #endif /* GRPC_UV */
94