murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "src/core/lib/iomgr/port.h" |
| 20 | |
| 21 | #ifdef GRPC_UV |
| 22 | |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 23 | #include <uv.h> |
| 24 | |
| 25 | #include <string.h> |
| 26 | |
murgatroid99 | d0cda5c | 2017-03-07 18:04:52 -0800 | [diff] [blame] | 27 | #include <grpc/support/alloc.h> |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 28 | #include <grpc/support/log.h> |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 29 | #include <grpc/support/sync.h> |
| 30 | |
murgatroid99 | b8e07ad | 2017-07-18 13:20:55 -0700 | [diff] [blame] | 31 | #include "src/core/lib/iomgr/iomgr_uv.h" |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 32 | #include "src/core/lib/iomgr/pollset.h" |
| 33 | #include "src/core/lib/iomgr/pollset_uv.h" |
| 34 | |
ncteisen | 973863d | 2017-06-12 10:28:50 -0700 | [diff] [blame] | 35 | #include "src/core/lib/debug/trace.h" |
| 36 | |
| 37 | #ifndef NDEBUG |
ncteisen | 7712c7c | 2017-07-12 23:11:27 -0700 | [diff] [blame] | 38 | grpc_tracer_flag grpc_trace_fd_refcount = |
| 39 | GRPC_TRACER_INITIALIZER(false, "fd_refcount"); |
ncteisen | 973863d | 2017-06-12 10:28:50 -0700 | [diff] [blame] | 40 | #endif |
| 41 | |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 42 | struct grpc_pollset { |
| 43 | uv_timer_t timer; |
| 44 | int shutting_down; |
| 45 | }; |
| 46 | |
| 47 | /* Indicates that grpc_pollset_work should run an iteration of the UV loop |
| 48 | before running callbacks. This defaults to 1, and should be disabled if |
| 49 | grpc_pollset_work will be called within the callstack of uv_run */ |
| 50 | int grpc_pollset_work_run_loop; |
| 51 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 52 | gpr_mu grpc_polling_mu; |
| 53 | |
murgatroid99 | 8c2946a | 2017-02-27 10:19:21 -0800 | [diff] [blame] | 54 | /* This is used solely to kick the uv loop, by setting a callback to be run |
| 55 | immediately in the next loop iteration. |
| 56 | Note: In the future, if there is a bug that involves missing wakeups in the |
| 57 | future, try adding a uv_async_t to kick the loop differently */ |
murgatroid99 | d0cda5c | 2017-03-07 18:04:52 -0800 | [diff] [blame] | 58 | uv_timer_t *dummy_uv_handle; |
murgatroid99 | 8c2946a | 2017-02-27 10:19:21 -0800 | [diff] [blame] | 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 | 06ab69d | 2017-02-27 16:39:27 -0800 | [diff] [blame] | 62 | void dummy_timer_cb(uv_timer_t *handle) {} |
murgatroid99 | 8c2946a | 2017-02-27 10:19:21 -0800 | [diff] [blame] | 63 | |
murgatroid99 | d0cda5c | 2017-03-07 18:04:52 -0800 | [diff] [blame] | 64 | void dummy_handle_close_cb(uv_handle_t *handle) { gpr_free(handle); } |
| 65 | |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 66 | void grpc_pollset_global_init(void) { |
| 67 | gpr_mu_init(&grpc_polling_mu); |
Yash Tibrewal | 19f0a90 | 2017-09-22 09:56:18 -0700 | [diff] [blame] | 68 | dummy_uv_handle = (uv_timer_t *)gpr_malloc(sizeof(uv_timer_t)); |
murgatroid99 | d0cda5c | 2017-03-07 18:04:52 -0800 | [diff] [blame] | 69 | uv_timer_init(uv_default_loop(), dummy_uv_handle); |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 70 | grpc_pollset_work_run_loop = 1; |
| 71 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 72 | |
murgatroid99 | 8c2946a | 2017-02-27 10:19:21 -0800 | [diff] [blame] | 73 | void grpc_pollset_global_shutdown(void) { |
murgatroid99 | 9d7def7 | 2017-07-20 16:41:27 -0700 | [diff] [blame] | 74 | GRPC_UV_ASSERT_SAME_THREAD(); |
murgatroid99 | 8c2946a | 2017-02-27 10:19:21 -0800 | [diff] [blame] | 75 | gpr_mu_destroy(&grpc_polling_mu); |
murgatroid99 | d0cda5c | 2017-03-07 18:04:52 -0800 | [diff] [blame] | 76 | uv_close((uv_handle_t *)dummy_uv_handle, dummy_handle_close_cb); |
murgatroid99 | 8c2946a | 2017-02-27 10:19:21 -0800 | [diff] [blame] | 77 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 78 | |
murgatroid99 | d0cda5c | 2017-03-07 18:04:52 -0800 | [diff] [blame] | 79 | static void timer_run_cb(uv_timer_t *timer) {} |
| 80 | |
| 81 | static void timer_close_cb(uv_handle_t *handle) { handle->data = (void *)1; } |
| 82 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 83 | void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu) { |
murgatroid99 | 9d7def7 | 2017-07-20 16:41:27 -0700 | [diff] [blame] | 84 | GRPC_UV_ASSERT_SAME_THREAD(); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 85 | *mu = &grpc_polling_mu; |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 86 | uv_timer_init(uv_default_loop(), &pollset->timer); |
| 87 | pollset->shutting_down = 0; |
| 88 | } |
| 89 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 90 | void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 91 | grpc_closure *closure) { |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 92 | GPR_ASSERT(!pollset->shutting_down); |
murgatroid99 | 9d7def7 | 2017-07-20 16:41:27 -0700 | [diff] [blame] | 93 | GRPC_UV_ASSERT_SAME_THREAD(); |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 94 | pollset->shutting_down = 1; |
| 95 | if (grpc_pollset_work_run_loop) { |
| 96 | // Drain any pending UV callbacks without blocking |
| 97 | uv_run(uv_default_loop(), UV_RUN_NOWAIT); |
murgatroid99 | 8c2946a | 2017-02-27 10:19:21 -0800 | [diff] [blame] | 98 | } else { |
| 99 | // kick the loop once |
murgatroid99 | d0cda5c | 2017-03-07 18:04:52 -0800 | [diff] [blame] | 100 | uv_timer_start(dummy_uv_handle, dummy_timer_cb, 0, 0); |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 101 | } |
ncteisen | 969b46e | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 102 | GRPC_CLOSURE_SCHED(exec_ctx, closure, GRPC_ERROR_NONE); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Craig Tiller | ffe51bd | 2017-04-21 08:30:24 -0700 | [diff] [blame] | 105 | void grpc_pollset_destroy(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset) { |
murgatroid99 | 9d7def7 | 2017-07-20 16:41:27 -0700 | [diff] [blame] | 106 | GRPC_UV_ASSERT_SAME_THREAD(); |
murgatroid99 | aa9c578 | 2016-10-10 12:16:13 -0700 | [diff] [blame] | 107 | uv_close((uv_handle_t *)&pollset->timer, timer_close_cb); |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 108 | // timer.data is a boolean indicating that the timer has finished closing |
| 109 | pollset->timer.data = (void *)0; |
| 110 | if (grpc_pollset_work_run_loop) { |
| 111 | while (!pollset->timer.data) { |
| 112 | uv_run(uv_default_loop(), UV_RUN_NOWAIT); |
| 113 | } |
| 114 | } |
| 115 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 116 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 117 | grpc_error *grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 118 | grpc_pollset_worker **worker_hdl, |
Craig Tiller | de682c5 | 2017-09-20 22:18:16 -0700 | [diff] [blame] | 119 | grpc_millis deadline) { |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 120 | uint64_t timeout; |
murgatroid99 | 9d7def7 | 2017-07-20 16:41:27 -0700 | [diff] [blame] | 121 | GRPC_UV_ASSERT_SAME_THREAD(); |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 122 | gpr_mu_unlock(&grpc_polling_mu); |
| 123 | if (grpc_pollset_work_run_loop) { |
Craig Tiller | de682c5 | 2017-09-20 22:18:16 -0700 | [diff] [blame] | 124 | grpc_millis now = grpc_exec_ctx_now(exec_ctx); |
| 125 | if (deadline >= now) { |
| 126 | timeout = deadline - now; |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 127 | } else { |
| 128 | timeout = 0; |
| 129 | } |
| 130 | /* We special-case timeout=0 so that we don't bother with the timer when |
| 131 | the loop won't block anyway */ |
| 132 | if (timeout > 0) { |
| 133 | uv_timer_start(&pollset->timer, timer_run_cb, timeout, 0); |
| 134 | /* Run until there is some I/O activity or the timer triggers. It doesn't |
| 135 | matter which happens */ |
| 136 | uv_run(uv_default_loop(), UV_RUN_ONCE); |
| 137 | uv_timer_stop(&pollset->timer); |
| 138 | } else { |
| 139 | uv_run(uv_default_loop(), UV_RUN_NOWAIT); |
| 140 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 141 | } |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 142 | if (!grpc_closure_list_empty(exec_ctx->closure_list)) { |
| 143 | grpc_exec_ctx_flush(exec_ctx); |
| 144 | } |
| 145 | gpr_mu_lock(&grpc_polling_mu); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 146 | return GRPC_ERROR_NONE; |
| 147 | } |
| 148 | |
Yash Tibrewal | 85629d0 | 2017-09-19 16:47:12 -0700 | [diff] [blame] | 149 | grpc_error *grpc_pollset_kick(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 150 | grpc_pollset_worker *specific_worker) { |
murgatroid99 | 9d7def7 | 2017-07-20 16:41:27 -0700 | [diff] [blame] | 151 | GRPC_UV_ASSERT_SAME_THREAD(); |
murgatroid99 | d0cda5c | 2017-03-07 18:04:52 -0800 | [diff] [blame] | 152 | uv_timer_start(dummy_uv_handle, dummy_timer_cb, 0, 0); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 153 | return GRPC_ERROR_NONE; |
| 154 | } |
| 155 | |
| 156 | #endif /* GRPC_UV */ |