blob: ed3edeee9493ca2a8bc69a917b39f2ad818b580f [file] [log] [blame]
murgatroid999030c812016-09-16 13:25:08 -07001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/lib/iomgr/port.h"
35
36#ifdef GRPC_UV
37
murgatroid992c287ca2016-10-07 09:55:35 -070038#include <uv.h>
39
40#include <string.h>
41
42#include <grpc/support/log.h>
murgatroid999030c812016-09-16 13:25:08 -070043#include <grpc/support/sync.h>
44
45#include "src/core/lib/iomgr/pollset.h"
46#include "src/core/lib/iomgr/pollset_uv.h"
47
murgatroid992c287ca2016-10-07 09:55:35 -070048struct grpc_pollset {
49 uv_timer_t timer;
50 int shutting_down;
51};
52
53/* Indicates that grpc_pollset_work should run an iteration of the UV loop
54 before running callbacks. This defaults to 1, and should be disabled if
55 grpc_pollset_work will be called within the callstack of uv_run */
56int grpc_pollset_work_run_loop;
57
murgatroid999030c812016-09-16 13:25:08 -070058gpr_mu grpc_polling_mu;
59
murgatroid992c287ca2016-10-07 09:55:35 -070060size_t grpc_pollset_size() { return sizeof(grpc_pollset); }
murgatroid999030c812016-09-16 13:25:08 -070061
murgatroid992c287ca2016-10-07 09:55:35 -070062void grpc_pollset_global_init(void) {
63 gpr_mu_init(&grpc_polling_mu);
64 grpc_pollset_work_run_loop = 1;
65}
murgatroid999030c812016-09-16 13:25:08 -070066
67void grpc_pollset_global_shutdown(void) { gpr_mu_destroy(&grpc_polling_mu); }
68
69void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
70 *mu = &grpc_polling_mu;
murgatroid992c287ca2016-10-07 09:55:35 -070071 memset(pollset, 0, sizeof(grpc_pollset));
72 uv_timer_init(uv_default_loop(), &pollset->timer);
73 pollset->shutting_down = 0;
74}
75
murgatroid99aa9c5782016-10-10 12:16:13 -070076static void timer_close_cb(uv_handle_t *handle) { handle->data = (void *)1; }
murgatroid999030c812016-09-16 13:25:08 -070077
78void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
murgatroid99dedb9232016-09-26 13:54:04 -070079 grpc_closure *closure) {
murgatroid992c287ca2016-10-07 09:55:35 -070080 GPR_ASSERT(!pollset->shutting_down);
81 pollset->shutting_down = 1;
82 if (grpc_pollset_work_run_loop) {
83 // Drain any pending UV callbacks without blocking
84 uv_run(uv_default_loop(), UV_RUN_NOWAIT);
85 }
Craig Tiller91031da2016-12-28 15:44:25 -080086 grpc_closure_sched(exec_ctx, closure, GRPC_ERROR_NONE);
murgatroid999030c812016-09-16 13:25:08 -070087}
88
murgatroid992c287ca2016-10-07 09:55:35 -070089void grpc_pollset_destroy(grpc_pollset *pollset) {
murgatroid99aa9c5782016-10-10 12:16:13 -070090 uv_close((uv_handle_t *)&pollset->timer, timer_close_cb);
murgatroid992c287ca2016-10-07 09:55:35 -070091 // timer.data is a boolean indicating that the timer has finished closing
92 pollset->timer.data = (void *)0;
93 if (grpc_pollset_work_run_loop) {
94 while (!pollset->timer.data) {
95 uv_run(uv_default_loop(), UV_RUN_NOWAIT);
96 }
97 }
98}
murgatroid999030c812016-09-16 13:25:08 -070099
murgatroid992c287ca2016-10-07 09:55:35 -0700100void grpc_pollset_reset(grpc_pollset *pollset) {
101 GPR_ASSERT(pollset->shutting_down);
102 pollset->shutting_down = 0;
103}
104
murgatroid99aa9c5782016-10-10 12:16:13 -0700105static void timer_run_cb(uv_timer_t *timer) {}
murgatroid999030c812016-09-16 13:25:08 -0700106
107grpc_error *grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
108 grpc_pollset_worker **worker_hdl,
109 gpr_timespec now, gpr_timespec deadline) {
murgatroid992c287ca2016-10-07 09:55:35 -0700110 uint64_t timeout;
111 gpr_mu_unlock(&grpc_polling_mu);
112 if (grpc_pollset_work_run_loop) {
113 if (gpr_time_cmp(deadline, now) >= 0) {
114 timeout = (uint64_t)gpr_time_to_millis(gpr_time_sub(deadline, now));
115 } else {
116 timeout = 0;
117 }
118 /* We special-case timeout=0 so that we don't bother with the timer when
119 the loop won't block anyway */
120 if (timeout > 0) {
121 uv_timer_start(&pollset->timer, timer_run_cb, timeout, 0);
122 /* Run until there is some I/O activity or the timer triggers. It doesn't
123 matter which happens */
124 uv_run(uv_default_loop(), UV_RUN_ONCE);
125 uv_timer_stop(&pollset->timer);
126 } else {
127 uv_run(uv_default_loop(), UV_RUN_NOWAIT);
128 }
murgatroid999030c812016-09-16 13:25:08 -0700129 }
murgatroid992c287ca2016-10-07 09:55:35 -0700130 if (!grpc_closure_list_empty(exec_ctx->closure_list)) {
131 grpc_exec_ctx_flush(exec_ctx);
132 }
133 gpr_mu_lock(&grpc_polling_mu);
murgatroid999030c812016-09-16 13:25:08 -0700134 return GRPC_ERROR_NONE;
135}
136
137grpc_error *grpc_pollset_kick(grpc_pollset *pollset,
138 grpc_pollset_worker *specific_worker) {
139 return GRPC_ERROR_NONE;
140}
141
142#endif /* GRPC_UV */