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>
27 #include "src/core/lib/iomgr/pollset_custom.h"
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;
36 static bool g_kicked = false;
38 typedef struct uv_poller_handle {
39 uv_timer_t poll_timer;
40 uv_timer_t kick_timer;
44 static uv_poller_handle* g_handle;
47 g_handle = (uv_poller_handle*)gpr_malloc(sizeof(uv_poller_handle));
49 uv_timer_init(uv_default_loop(), &g_handle->poll_timer);
50 uv_timer_init(uv_default_loop(), &g_handle->kick_timer);
53 static void empty_timer_cb(uv_timer_t* handle) {}
55 static void kick_timer_cb(uv_timer_t* handle) { g_kicked = false; }
57 static void run_loop(size_t timeout) {
58 if (grpc_pollset_work_run_loop) {
60 uv_run(uv_default_loop(), UV_RUN_NOWAIT);
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);
72 uv_timer_start(&g_handle->kick_timer, kick_timer_cb, 0, 0);
76 static void close_timer_cb(uv_handle_t* handle) {
78 if (g_handle->refs == 0) {
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);
91 grpc_custom_poller_vtable uv_pollset_vtable = {init, run_loop, kick, shutdown};