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 | uv_timer_init(uv_default_loop(), &pollset->timer); |
| 72 | pollset->shutting_down = 0; |
| 73 | } |
| 74 | |
murgatroid99 | aa9c578 | 2016-10-10 12:16:13 -0700 | [diff] [blame] | 75 | static void timer_close_cb(uv_handle_t *handle) { handle->data = (void *)1; } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 76 | |
| 77 | void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 78 | grpc_closure *closure) { |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 79 | GPR_ASSERT(!pollset->shutting_down); |
| 80 | pollset->shutting_down = 1; |
| 81 | if (grpc_pollset_work_run_loop) { |
| 82 | // Drain any pending UV callbacks without blocking |
| 83 | uv_run(uv_default_loop(), UV_RUN_NOWAIT); |
| 84 | } |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 85 | grpc_closure_sched(exec_ctx, closure, GRPC_ERROR_NONE); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 86 | } |
| 87 | |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 88 | void grpc_pollset_destroy(grpc_pollset *pollset) { |
murgatroid99 | aa9c578 | 2016-10-10 12:16:13 -0700 | [diff] [blame] | 89 | uv_close((uv_handle_t *)&pollset->timer, timer_close_cb); |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 90 | // timer.data is a boolean indicating that the timer has finished closing |
| 91 | pollset->timer.data = (void *)0; |
| 92 | if (grpc_pollset_work_run_loop) { |
| 93 | while (!pollset->timer.data) { |
| 94 | uv_run(uv_default_loop(), UV_RUN_NOWAIT); |
| 95 | } |
| 96 | } |
| 97 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 98 | |
murgatroid99 | aa9c578 | 2016-10-10 12:16:13 -0700 | [diff] [blame] | 99 | static void timer_run_cb(uv_timer_t *timer) {} |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 100 | |
| 101 | grpc_error *grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 102 | grpc_pollset_worker **worker_hdl, |
| 103 | gpr_timespec now, gpr_timespec deadline) { |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 104 | uint64_t timeout; |
| 105 | gpr_mu_unlock(&grpc_polling_mu); |
| 106 | if (grpc_pollset_work_run_loop) { |
| 107 | if (gpr_time_cmp(deadline, now) >= 0) { |
| 108 | timeout = (uint64_t)gpr_time_to_millis(gpr_time_sub(deadline, now)); |
| 109 | } else { |
| 110 | timeout = 0; |
| 111 | } |
| 112 | /* We special-case timeout=0 so that we don't bother with the timer when |
| 113 | the loop won't block anyway */ |
| 114 | if (timeout > 0) { |
| 115 | uv_timer_start(&pollset->timer, timer_run_cb, timeout, 0); |
| 116 | /* Run until there is some I/O activity or the timer triggers. It doesn't |
| 117 | matter which happens */ |
| 118 | uv_run(uv_default_loop(), UV_RUN_ONCE); |
| 119 | uv_timer_stop(&pollset->timer); |
| 120 | } else { |
| 121 | uv_run(uv_default_loop(), UV_RUN_NOWAIT); |
| 122 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 123 | } |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 124 | if (!grpc_closure_list_empty(exec_ctx->closure_list)) { |
| 125 | grpc_exec_ctx_flush(exec_ctx); |
| 126 | } |
| 127 | gpr_mu_lock(&grpc_polling_mu); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 128 | return GRPC_ERROR_NONE; |
| 129 | } |
| 130 | |
| 131 | grpc_error *grpc_pollset_kick(grpc_pollset *pollset, |
| 132 | grpc_pollset_worker *specific_worker) { |
| 133 | return GRPC_ERROR_NONE; |
| 134 | } |
| 135 | |
| 136 | #endif /* GRPC_UV */ |