murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 38 | #include <uv.h> |
| 39 | |
| 40 | #include <string.h> |
| 41 | |
| 42 | #include <grpc/support/log.h> |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 43 | #include <grpc/support/sync.h> |
| 44 | |
| 45 | #include "src/core/lib/iomgr/pollset.h" |
| 46 | #include "src/core/lib/iomgr/pollset_uv.h" |
| 47 | |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 48 | struct 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 */ |
| 56 | int grpc_pollset_work_run_loop; |
| 57 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 58 | gpr_mu grpc_polling_mu; |
| 59 | |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 60 | size_t grpc_pollset_size() { return sizeof(grpc_pollset); } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 61 | |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 62 | void grpc_pollset_global_init(void) { |
| 63 | gpr_mu_init(&grpc_polling_mu); |
| 64 | grpc_pollset_work_run_loop = 1; |
| 65 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 66 | |
| 67 | void grpc_pollset_global_shutdown(void) { gpr_mu_destroy(&grpc_polling_mu); } |
| 68 | |
| 69 | void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu) { |
| 70 | *mu = &grpc_polling_mu; |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 71 | memset(pollset, 0, sizeof(grpc_pollset)); |
| 72 | uv_timer_init(uv_default_loop(), &pollset->timer); |
| 73 | pollset->shutting_down = 0; |
| 74 | } |
| 75 | |
murgatroid99 | aa9c578 | 2016-10-10 12:16:13 -0700 | [diff] [blame] | 76 | static void timer_close_cb(uv_handle_t *handle) { handle->data = (void *)1; } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 77 | |
| 78 | void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 79 | grpc_closure *closure) { |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 80 | 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 Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 86 | grpc_closure_sched(exec_ctx, closure, GRPC_ERROR_NONE); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 87 | } |
| 88 | |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 89 | void grpc_pollset_destroy(grpc_pollset *pollset) { |
murgatroid99 | aa9c578 | 2016-10-10 12:16:13 -0700 | [diff] [blame] | 90 | uv_close((uv_handle_t *)&pollset->timer, timer_close_cb); |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 91 | // 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 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 99 | |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 100 | void grpc_pollset_reset(grpc_pollset *pollset) { |
| 101 | GPR_ASSERT(pollset->shutting_down); |
| 102 | pollset->shutting_down = 0; |
| 103 | } |
| 104 | |
murgatroid99 | aa9c578 | 2016-10-10 12:16:13 -0700 | [diff] [blame] | 105 | static void timer_run_cb(uv_timer_t *timer) {} |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 106 | |
| 107 | grpc_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) { |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 110 | 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 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 129 | } |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 130 | if (!grpc_closure_list_empty(exec_ctx->closure_list)) { |
| 131 | grpc_exec_ctx_flush(exec_ctx); |
| 132 | } |
| 133 | gpr_mu_lock(&grpc_polling_mu); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 134 | return GRPC_ERROR_NONE; |
| 135 | } |
| 136 | |
| 137 | grpc_error *grpc_pollset_kick(grpc_pollset *pollset, |
| 138 | grpc_pollset_worker *specific_worker) { |
| 139 | return GRPC_ERROR_NONE; |
| 140 | } |
| 141 | |
| 142 | #endif /* GRPC_UV */ |