ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 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 | |
Craig Tiller | d14a1a5 | 2015-01-21 15:26:29 -0800 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
| 35 | |
| 36 | #ifdef GPR_POSIX_SOCKET |
| 37 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 38 | #include "src/core/iomgr/pollset_posix.h" |
| 39 | |
| 40 | #include <errno.h> |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <unistd.h> |
| 44 | |
| 45 | #include "src/core/iomgr/alarm_internal.h" |
| 46 | #include "src/core/iomgr/fd_posix.h" |
| 47 | #include "src/core/iomgr/iomgr_internal.h" |
| 48 | #include "src/core/iomgr/socket_utils_posix.h" |
Craig Tiller | 1be373c | 2015-04-27 07:58:16 -0700 | [diff] [blame] | 49 | #include "src/core/profiling/timers.h" |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 50 | #include "src/core/support/block_annotate.h" |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 51 | #include <grpc/support/alloc.h> |
| 52 | #include <grpc/support/log.h> |
| 53 | #include <grpc/support/thd.h> |
Craig Tiller | 1be373c | 2015-04-27 07:58:16 -0700 | [diff] [blame] | 54 | #include <grpc/support/tls.h> |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 55 | #include <grpc/support/useful.h> |
| 56 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 57 | GPR_TLS_DECL(g_current_thread_poller); |
| 58 | GPR_TLS_DECL(g_current_thread_worker); |
Craig Tiller | 1be373c | 2015-04-27 07:58:16 -0700 | [diff] [blame] | 59 | |
Craig Tiller | 191b79c | 2015-09-30 10:49:58 -0700 | [diff] [blame] | 60 | /** Default poll() function - a pointer so that it can be overridden by some |
| 61 | * tests */ |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 62 | grpc_poll_function_type grpc_poll_function = poll; |
Craig Tiller | 191b79c | 2015-09-30 10:49:58 -0700 | [diff] [blame] | 63 | |
| 64 | /** The alarm system needs to be able to wakeup 'some poller' sometimes |
| 65 | * (specifically when a new alarm needs to be triggered earlier than the next |
| 66 | * alarm 'epoch'). |
| 67 | * This wakeup_fd gives us something to alert on when such a case occurs. */ |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 68 | grpc_wakeup_fd grpc_global_wakeup_fd; |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 69 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 70 | static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) { |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 71 | worker->prev->next = worker->next; |
| 72 | worker->next->prev = worker->prev; |
| 73 | } |
| 74 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 75 | int grpc_pollset_has_workers(grpc_pollset *p) { |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 76 | return p->root_worker.next != &p->root_worker; |
| 77 | } |
| 78 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 79 | static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) { |
| 80 | if (grpc_pollset_has_workers(p)) { |
| 81 | grpc_pollset_worker *w = p->root_worker.next; |
| 82 | remove_worker(p, w); |
| 83 | return w; |
| 84 | } else { |
| 85 | return NULL; |
| 86 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 89 | static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) { |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 90 | worker->next = &p->root_worker; |
| 91 | worker->prev = worker->next->prev; |
| 92 | worker->prev->next = worker->next->prev = worker; |
Craig Tiller | 7d41321 | 2015-02-09 08:00:02 -0800 | [diff] [blame] | 93 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 94 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) { |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 96 | worker->prev = &p->root_worker; |
| 97 | worker->next = worker->prev->next; |
| 98 | worker->prev->next = worker->next->prev = worker; |
| 99 | } |
| 100 | |
Craig Tiller | d0a0000 | 2015-10-06 11:30:37 -0700 | [diff] [blame] | 101 | void grpc_pollset_kick_ext(grpc_pollset *p, |
| 102 | grpc_pollset_worker *specific_worker, |
Craig Tiller | b937aa1 | 2015-10-06 11:30:30 -0700 | [diff] [blame] | 103 | gpr_uint32 flags) { |
Craig Tiller | 44011e8 | 2015-10-09 16:16:43 -0700 | [diff] [blame^] | 104 | GRPC_TIMER_BEGIN("grpc_pollset_kick_ext", 0); |
| 105 | |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 106 | /* pollset->mu already held */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 107 | if (specific_worker != NULL) { |
| 108 | if (specific_worker == GRPC_POLLSET_KICK_BROADCAST) { |
Craig Tiller | 44011e8 | 2015-10-09 16:16:43 -0700 | [diff] [blame^] | 109 | GRPC_TIMER_BEGIN("grpc_pollset_kick_ext.broadcast", 0); |
Craig Tiller | 988e37f | 2015-10-01 07:53:56 -0700 | [diff] [blame] | 110 | GPR_ASSERT((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) == 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 111 | for (specific_worker = p->root_worker.next; |
| 112 | specific_worker != &p->root_worker; |
| 113 | specific_worker = specific_worker->next) { |
| 114 | grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd); |
| 115 | } |
| 116 | p->kicked_without_pollers = 1; |
Craig Tiller | 44011e8 | 2015-10-09 16:16:43 -0700 | [diff] [blame^] | 117 | GRPC_TIMER_END("grpc_pollset_kick_ext.broadcast", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | } else if (gpr_tls_get(&g_current_thread_worker) != |
| 119 | (gpr_intptr)specific_worker) { |
Craig Tiller | 44011e8 | 2015-10-09 16:16:43 -0700 | [diff] [blame^] | 120 | GRPC_TIMER_MARK("different_thread_worker", 0); |
Craig Tiller | 988e37f | 2015-10-01 07:53:56 -0700 | [diff] [blame] | 121 | if ((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) != 0) { |
| 122 | specific_worker->reevaluate_polling_on_wakeup = 1; |
| 123 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 124 | grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd); |
Craig Tiller | 988e37f | 2015-10-01 07:53:56 -0700 | [diff] [blame] | 125 | } else if ((flags & GRPC_POLLSET_CAN_KICK_SELF) != 0) { |
Craig Tiller | 44011e8 | 2015-10-09 16:16:43 -0700 | [diff] [blame^] | 126 | GRPC_TIMER_MARK("kick_yoself", 0); |
Craig Tiller | 988e37f | 2015-10-01 07:53:56 -0700 | [diff] [blame] | 127 | if ((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) != 0) { |
| 128 | specific_worker->reevaluate_polling_on_wakeup = 1; |
| 129 | } |
| 130 | grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd); |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 131 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 132 | } else if (gpr_tls_get(&g_current_thread_poller) != (gpr_intptr)p) { |
Craig Tiller | 988e37f | 2015-10-01 07:53:56 -0700 | [diff] [blame] | 133 | GPR_ASSERT((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) == 0); |
Craig Tiller | 44011e8 | 2015-10-09 16:16:43 -0700 | [diff] [blame^] | 134 | GRPC_TIMER_MARK("kick_anonymous", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 135 | specific_worker = pop_front_worker(p); |
| 136 | if (specific_worker != NULL) { |
Craig Tiller | 57f79d6 | 2015-10-02 14:00:12 -0700 | [diff] [blame] | 137 | if (gpr_tls_get(&g_current_thread_worker) == |
| 138 | (gpr_intptr)specific_worker) { |
Craig Tiller | 44011e8 | 2015-10-09 16:16:43 -0700 | [diff] [blame^] | 139 | GRPC_TIMER_MARK("kick_anonymous_not_self", 0); |
Craig Tiller | 988e37f | 2015-10-01 07:53:56 -0700 | [diff] [blame] | 140 | push_back_worker(p, specific_worker); |
| 141 | specific_worker = pop_front_worker(p); |
Craig Tiller | 57f79d6 | 2015-10-02 14:00:12 -0700 | [diff] [blame] | 142 | if ((flags & GRPC_POLLSET_CAN_KICK_SELF) == 0 && |
| 143 | gpr_tls_get(&g_current_thread_worker) == |
| 144 | (gpr_intptr)specific_worker) { |
Craig Tiller | 988e37f | 2015-10-01 07:53:56 -0700 | [diff] [blame] | 145 | push_back_worker(p, specific_worker); |
Craig Tiller | 44011e8 | 2015-10-09 16:16:43 -0700 | [diff] [blame^] | 146 | specific_worker = NULL; |
Craig Tiller | 988e37f | 2015-10-01 07:53:56 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
Craig Tiller | 44011e8 | 2015-10-09 16:16:43 -0700 | [diff] [blame^] | 149 | if (specific_worker != NULL) { |
| 150 | GRPC_TIMER_MARK("finally_kick", 0); |
| 151 | push_back_worker(p, specific_worker); |
| 152 | grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd); |
| 153 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 154 | } else { |
Craig Tiller | 44011e8 | 2015-10-09 16:16:43 -0700 | [diff] [blame^] | 155 | GRPC_TIMER_MARK("kicked_no_pollers", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | p->kicked_without_pollers = 1; |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 157 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 158 | } |
Craig Tiller | 44011e8 | 2015-10-09 16:16:43 -0700 | [diff] [blame^] | 159 | |
| 160 | GRPC_TIMER_END("grpc_pollset_kick_ext", 0); |
David Klempner | baced4d | 2015-02-10 17:10:15 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Craig Tiller | 988e37f | 2015-10-01 07:53:56 -0700 | [diff] [blame] | 163 | void grpc_pollset_kick(grpc_pollset *p, grpc_pollset_worker *specific_worker) { |
Craig Tiller | b937aa1 | 2015-10-06 11:30:30 -0700 | [diff] [blame] | 164 | grpc_pollset_kick_ext(p, specific_worker, 0); |
Craig Tiller | 988e37f | 2015-10-01 07:53:56 -0700 | [diff] [blame] | 165 | } |
| 166 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 167 | /* global state management */ |
| 168 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 169 | void grpc_pollset_global_init(void) { |
| 170 | gpr_tls_init(&g_current_thread_poller); |
Craig Tiller | 926c0e9 | 2015-09-28 14:25:38 -0700 | [diff] [blame] | 171 | gpr_tls_init(&g_current_thread_worker); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 172 | grpc_wakeup_fd_global_init(); |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 173 | grpc_wakeup_fd_init(&grpc_global_wakeup_fd); |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 176 | void grpc_pollset_global_shutdown(void) { |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 177 | grpc_wakeup_fd_destroy(&grpc_global_wakeup_fd); |
| 178 | grpc_wakeup_fd_global_destroy(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 179 | gpr_tls_destroy(&g_current_thread_poller); |
Craig Tiller | 926c0e9 | 2015-09-28 14:25:38 -0700 | [diff] [blame] | 180 | gpr_tls_destroy(&g_current_thread_worker); |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 183 | void grpc_kick_poller(void) { grpc_wakeup_fd_wakeup(&grpc_global_wakeup_fd); } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 184 | |
| 185 | /* main interface */ |
| 186 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | static void become_basic_pollset(grpc_pollset *pollset, grpc_fd *fd_or_null); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 188 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 189 | void grpc_pollset_init(grpc_pollset *pollset) { |
| 190 | gpr_mu_init(&pollset->mu); |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 191 | pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker; |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 192 | pollset->in_flight_cbs = 0; |
| 193 | pollset->shutting_down = 0; |
Jan Tattermusch | d6ca8b4 | 2015-06-10 15:58:39 -0700 | [diff] [blame] | 194 | pollset->called_shutdown = 0; |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 195 | pollset->idle_jobs.head = pollset->idle_jobs.tail = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 196 | become_basic_pollset(pollset, NULL); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 199 | void grpc_pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 200 | grpc_fd *fd) { |
| 201 | gpr_mu_lock(&pollset->mu); |
| 202 | pollset->vtable->add_fd(exec_ctx, pollset, fd, 1); |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 203 | /* the following (enabled only in debug) will reacquire and then release |
| 204 | our lock - meaning that if the unlocking flag passed to del_fd above is |
| 205 | not respected, the code will deadlock (in a way that we have a chance of |
| 206 | debugging) */ |
Craig Tiller | b0c13ad | 2015-07-16 08:42:31 -0700 | [diff] [blame] | 207 | #ifndef NDEBUG |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 208 | gpr_mu_lock(&pollset->mu); |
| 209 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | b0c13ad | 2015-07-16 08:42:31 -0700 | [diff] [blame] | 210 | #endif |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 213 | void grpc_pollset_del_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 214 | grpc_fd *fd) { |
| 215 | gpr_mu_lock(&pollset->mu); |
| 216 | pollset->vtable->del_fd(exec_ctx, pollset, fd, 1); |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 217 | /* the following (enabled only in debug) will reacquire and then release |
| 218 | our lock - meaning that if the unlocking flag passed to del_fd above is |
| 219 | not respected, the code will deadlock (in a way that we have a chance of |
| 220 | debugging) */ |
Craig Tiller | abfaf2a | 2015-07-16 17:23:40 -0700 | [diff] [blame] | 221 | #ifndef NDEBUG |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 222 | gpr_mu_lock(&pollset->mu); |
| 223 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | b0c13ad | 2015-07-16 08:42:31 -0700 | [diff] [blame] | 224 | #endif |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 227 | static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset) { |
Craig Tiller | 6301038 | 2015-09-24 15:00:58 -0700 | [diff] [blame] | 228 | GPR_ASSERT(grpc_closure_list_empty(pollset->idle_jobs)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 229 | pollset->vtable->finish_shutdown(pollset); |
| 230 | grpc_exec_ctx_enqueue(exec_ctx, pollset->shutdown_done, 1); |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 233 | void grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 234 | grpc_pollset_worker *worker, gpr_timespec now, |
| 235 | gpr_timespec deadline) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 236 | /* pollset->mu already held */ |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 237 | int added_worker = 0; |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 238 | int locked = 1; |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 239 | int queued_work = 0; |
Craig Tiller | 6078a7d | 2015-10-06 11:50:21 -0700 | [diff] [blame] | 240 | int keep_polling = 0; |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 241 | GRPC_TIMER_BEGIN("grpc_pollset_work", 0); |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 242 | /* this must happen before we (potentially) drop pollset->mu */ |
| 243 | worker->next = worker->prev = NULL; |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 244 | worker->reevaluate_polling_on_wakeup = 0; |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 245 | /* TODO(ctiller): pool these */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 246 | grpc_wakeup_fd_init(&worker->wakeup_fd); |
Craig Tiller | 6078a7d | 2015-10-06 11:50:21 -0700 | [diff] [blame] | 247 | /* If there's work waiting for the pollset to be idle, and the |
| 248 | pollset is idle, then do that work */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 249 | if (!grpc_pollset_has_workers(pollset) && |
| 250 | !grpc_closure_list_empty(pollset->idle_jobs)) { |
| 251 | grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs); |
| 252 | goto done; |
| 253 | } |
Craig Tiller | 6078a7d | 2015-10-06 11:50:21 -0700 | [diff] [blame] | 254 | /* Check alarms - these are a global resource so we just ping |
| 255 | each time through on every pollset. |
| 256 | May update deadline to ensure timely wakeups. |
| 257 | TODO(ctiller): can this work be localized? */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 258 | if (grpc_alarm_check(exec_ctx, now, &deadline)) { |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 259 | gpr_mu_unlock(&pollset->mu); |
| 260 | locked = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 261 | goto done; |
| 262 | } |
Craig Tiller | 6078a7d | 2015-10-06 11:50:21 -0700 | [diff] [blame] | 263 | /* If we're shutting down then we don't execute any extended work */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 264 | if (pollset->shutting_down) { |
| 265 | goto done; |
| 266 | } |
Craig Tiller | 6078a7d | 2015-10-06 11:50:21 -0700 | [diff] [blame] | 267 | /* Give do_promote priority so we don't starve it out */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 268 | if (pollset->in_flight_cbs) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 269 | gpr_mu_unlock(&pollset->mu); |
| 270 | locked = 0; |
| 271 | goto done; |
| 272 | } |
Craig Tiller | 6078a7d | 2015-10-06 11:50:21 -0700 | [diff] [blame] | 273 | /* Start polling, and keep doing so while we're being asked to |
| 274 | re-evaluate our pollers (this allows poll() based pollers to |
| 275 | ensure they don't miss wakeups) */ |
| 276 | keep_polling = 1; |
| 277 | while (keep_polling) { |
| 278 | keep_polling = 0; |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 279 | if (!pollset->kicked_without_pollers) { |
| 280 | if (!added_worker) { |
| 281 | push_front_worker(pollset, worker); |
| 282 | added_worker = 1; |
| 283 | } |
| 284 | gpr_tls_set(&g_current_thread_poller, (gpr_intptr)pollset); |
| 285 | gpr_tls_set(&g_current_thread_worker, (gpr_intptr)worker); |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 286 | GRPC_TIMER_BEGIN("maybe_work_and_unlock", 0); |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 287 | pollset->vtable->maybe_work_and_unlock(exec_ctx, pollset, worker, |
| 288 | deadline, now); |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 289 | GRPC_TIMER_END("maybe_work_and_unlock", 0); |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 290 | locked = 0; |
| 291 | gpr_tls_set(&g_current_thread_poller, 0); |
| 292 | gpr_tls_set(&g_current_thread_worker, 0); |
| 293 | } else { |
| 294 | pollset->kicked_without_pollers = 0; |
| 295 | } |
Craig Tiller | 6078a7d | 2015-10-06 11:50:21 -0700 | [diff] [blame] | 296 | /* Finished execution - start cleaning up. |
| 297 | Note that we may arrive here from outside the enclosing while() loop. |
| 298 | In that case we won't loop though as we haven't added worker to the |
| 299 | worker list, which means nobody could ask us to re-evaluate polling). */ |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 300 | done: |
| 301 | if (!locked) { |
| 302 | queued_work |= grpc_exec_ctx_flush(exec_ctx); |
| 303 | gpr_mu_lock(&pollset->mu); |
| 304 | locked = 1; |
| 305 | } |
Craig Tiller | 6078a7d | 2015-10-06 11:50:21 -0700 | [diff] [blame] | 306 | /* If we're forced to re-evaluate polling (via grpc_pollset_kick with |
| 307 | GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) then we land here and force |
| 308 | a loop */ |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 309 | if (worker->reevaluate_polling_on_wakeup) { |
| 310 | worker->reevaluate_polling_on_wakeup = 0; |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 311 | pollset->kicked_without_pollers = 0; |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 312 | if (queued_work) { |
Craig Tiller | 6078a7d | 2015-10-06 11:50:21 -0700 | [diff] [blame] | 313 | /* If there's queued work on the list, then set the deadline to be |
| 314 | immediate so we get back out of the polling loop quickly */ |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 315 | deadline = gpr_inf_past(GPR_CLOCK_MONOTONIC); |
| 316 | } |
Craig Tiller | 6078a7d | 2015-10-06 11:50:21 -0700 | [diff] [blame] | 317 | keep_polling = 1; |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 318 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 319 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 320 | if (added_worker) { |
| 321 | remove_worker(pollset, worker); |
| 322 | } |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 323 | grpc_wakeup_fd_destroy(&worker->wakeup_fd); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 324 | if (pollset->shutting_down) { |
| 325 | if (grpc_pollset_has_workers(pollset)) { |
| 326 | grpc_pollset_kick(pollset, NULL); |
| 327 | } else if (!pollset->called_shutdown && pollset->in_flight_cbs == 0) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 328 | pollset->called_shutdown = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 329 | gpr_mu_unlock(&pollset->mu); |
| 330 | finish_shutdown(exec_ctx, pollset); |
| 331 | grpc_exec_ctx_flush(exec_ctx); |
| 332 | /* Continuing to access pollset here is safe -- it is the caller's |
| 333 | * responsibility to not destroy when it has outstanding calls to |
| 334 | * grpc_pollset_work. |
| 335 | * TODO(dklempner): Can we refactor the shutdown logic to avoid this? */ |
| 336 | gpr_mu_lock(&pollset->mu); |
Craig Tiller | 6301038 | 2015-09-24 15:00:58 -0700 | [diff] [blame] | 337 | } else if (!grpc_closure_list_empty(pollset->idle_jobs)) { |
| 338 | gpr_mu_unlock(&pollset->mu); |
| 339 | grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs); |
| 340 | grpc_exec_ctx_flush(exec_ctx); |
| 341 | gpr_mu_lock(&pollset->mu); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 342 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 343 | } |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 344 | GRPC_TIMER_END("grpc_pollset_work", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 348 | grpc_closure *closure) { |
| 349 | int call_shutdown = 0; |
| 350 | gpr_mu_lock(&pollset->mu); |
| 351 | GPR_ASSERT(!pollset->shutting_down); |
| 352 | pollset->shutting_down = 1; |
| 353 | if (!pollset->called_shutdown && pollset->in_flight_cbs == 0 && |
| 354 | !grpc_pollset_has_workers(pollset)) { |
| 355 | pollset->called_shutdown = 1; |
| 356 | call_shutdown = 1; |
| 357 | } |
Craig Tiller | 6301038 | 2015-09-24 15:00:58 -0700 | [diff] [blame] | 358 | if (!grpc_pollset_has_workers(pollset)) { |
| 359 | grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs); |
| 360 | } |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 361 | pollset->shutdown_done = closure; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 362 | grpc_pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST); |
| 363 | gpr_mu_unlock(&pollset->mu); |
David Klempner | a4fb700 | 2015-05-22 13:44:15 -0700 | [diff] [blame] | 364 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 365 | if (call_shutdown) { |
| 366 | finish_shutdown(exec_ctx, pollset); |
| 367 | } |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 370 | void grpc_pollset_destroy(grpc_pollset *pollset) { |
| 371 | GPR_ASSERT(pollset->shutting_down); |
| 372 | GPR_ASSERT(pollset->in_flight_cbs == 0); |
| 373 | GPR_ASSERT(!grpc_pollset_has_workers(pollset)); |
| 374 | pollset->vtable->destroy(pollset); |
| 375 | gpr_mu_destroy(&pollset->mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 378 | int grpc_poll_deadline_to_millis_timeout(gpr_timespec deadline, |
| 379 | gpr_timespec now) { |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 380 | gpr_timespec timeout; |
| 381 | static const int max_spin_polling_us = 10; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 382 | if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) == 0) { |
| 383 | return -1; |
| 384 | } |
| 385 | if (gpr_time_cmp(deadline, gpr_time_add(now, gpr_time_from_micros( |
| 386 | max_spin_polling_us, |
| 387 | GPR_TIMESPAN))) <= 0) { |
| 388 | return 0; |
| 389 | } |
| 390 | timeout = gpr_time_sub(deadline, now); |
| 391 | return gpr_time_to_millis(gpr_time_add( |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 392 | timeout, gpr_time_from_nanos(GPR_NS_PER_MS - 1, GPR_TIMESPAN))); |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 393 | } |
| 394 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 395 | /* |
Craig Tiller | 5ec3bfa | 2015-05-28 14:13:18 -0700 | [diff] [blame] | 396 | * basic_pollset - a vtable that provides polling for zero or one file |
Craig Tiller | 3f529a2c | 2015-05-28 14:00:47 -0700 | [diff] [blame] | 397 | * descriptor via poll() |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 398 | */ |
| 399 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 400 | typedef struct grpc_unary_promote_args { |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 401 | const grpc_pollset_vtable *original_vtable; |
| 402 | grpc_pollset *pollset; |
| 403 | grpc_fd *fd; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 404 | grpc_closure promotion_closure; |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 405 | } grpc_unary_promote_args; |
| 406 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 407 | static void basic_do_promote(grpc_exec_ctx *exec_ctx, void *args, int success) { |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 408 | grpc_unary_promote_args *up_args = args; |
| 409 | const grpc_pollset_vtable *original_vtable = up_args->original_vtable; |
| 410 | grpc_pollset *pollset = up_args->pollset; |
| 411 | grpc_fd *fd = up_args->fd; |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 412 | |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 413 | /* |
| 414 | * This is quite tricky. There are a number of cases to keep in mind here: |
| 415 | * 1. fd may have been orphaned |
| 416 | * 2. The pollset may no longer be a unary poller (and we can't let case #1 |
| 417 | * leak to other pollset types!) |
| 418 | * 3. pollset's fd (which may have changed) may have been orphaned |
| 419 | * 4. The pollset may be shutting down. |
| 420 | */ |
| 421 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 422 | gpr_mu_lock(&pollset->mu); |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 423 | /* First we need to ensure that nobody is polling concurrently */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 424 | GPR_ASSERT(!grpc_pollset_has_workers(pollset)); |
Craig Tiller | 6bf4501 | 2015-05-15 17:11:44 -0700 | [diff] [blame] | 425 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 426 | gpr_free(up_args); |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 427 | /* At this point the pollset may no longer be a unary poller. In that case |
| 428 | * we should just call the right add function and be done. */ |
| 429 | /* TODO(klempner): If we're not careful this could cause infinite recursion. |
| 430 | * That's not a problem for now because empty_pollset has a trivial poller |
| 431 | * and we don't have any mechanism to unbecome multipoller. */ |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 432 | pollset->in_flight_cbs--; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 433 | if (pollset->shutting_down) { |
| 434 | /* We don't care about this pollset anymore. */ |
| 435 | if (pollset->in_flight_cbs == 0 && !pollset->called_shutdown) { |
yang-g | 2c1c000 | 2015-10-02 16:15:05 -0700 | [diff] [blame] | 436 | pollset->called_shutdown = 1; |
Craig Tiller | 6301038 | 2015-09-24 15:00:58 -0700 | [diff] [blame] | 437 | finish_shutdown(exec_ctx, pollset); |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 438 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 439 | } else if (grpc_fd_is_orphaned(fd)) { |
| 440 | /* Don't try to add it to anything, we'll drop our ref on it below */ |
| 441 | } else if (pollset->vtable != original_vtable) { |
| 442 | pollset->vtable->add_fd(exec_ctx, pollset, fd, 0); |
| 443 | } else if (fd != pollset->data.ptr) { |
| 444 | grpc_fd *fds[2]; |
| 445 | fds[0] = pollset->data.ptr; |
| 446 | fds[1] = fd; |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 447 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 448 | if (fds[0] && !grpc_fd_is_orphaned(fds[0])) { |
| 449 | grpc_platform_become_multipoller(exec_ctx, pollset, fds, |
| 450 | GPR_ARRAY_SIZE(fds)); |
| 451 | GRPC_FD_UNREF(fds[0], "basicpoll"); |
| 452 | } else { |
| 453 | /* old fd is orphaned and we haven't cleaned it up until now, so remain a |
| 454 | * unary poller */ |
| 455 | /* Note that it is possible that fds[1] is also orphaned at this point. |
| 456 | * That's okay, we'll correct it at the next add or poll. */ |
| 457 | if (fds[0]) GRPC_FD_UNREF(fds[0], "basicpoll"); |
| 458 | pollset->data.ptr = fd; |
| 459 | GRPC_FD_REF(fd, "basicpoll"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 460 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 461 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 462 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 463 | gpr_mu_unlock(&pollset->mu); |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 464 | |
Craig Tiller | 3f529a2c | 2015-05-28 14:00:47 -0700 | [diff] [blame] | 465 | /* Matching ref in basic_pollset_add_fd */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 466 | GRPC_FD_UNREF(fd, "basicpoll_add"); |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 469 | static void basic_pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 470 | grpc_fd *fd, int and_unlock_pollset) { |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 471 | grpc_unary_promote_args *up_args; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 472 | GPR_ASSERT(fd); |
| 473 | if (fd == pollset->data.ptr) goto exit; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 474 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 475 | if (!grpc_pollset_has_workers(pollset)) { |
| 476 | /* Fast path -- no in flight cbs */ |
| 477 | /* TODO(klempner): Comment this out and fix any test failures or establish |
| 478 | * they are due to timing issues */ |
| 479 | grpc_fd *fds[2]; |
| 480 | fds[0] = pollset->data.ptr; |
| 481 | fds[1] = fd; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 482 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 483 | if (fds[0] == NULL) { |
| 484 | pollset->data.ptr = fd; |
| 485 | GRPC_FD_REF(fd, "basicpoll"); |
| 486 | } else if (!grpc_fd_is_orphaned(fds[0])) { |
| 487 | grpc_platform_become_multipoller(exec_ctx, pollset, fds, |
| 488 | GPR_ARRAY_SIZE(fds)); |
| 489 | GRPC_FD_UNREF(fds[0], "basicpoll"); |
| 490 | } else { |
| 491 | /* old fd is orphaned and we haven't cleaned it up until now, so remain a |
| 492 | * unary poller */ |
| 493 | GRPC_FD_UNREF(fds[0], "basicpoll"); |
| 494 | pollset->data.ptr = fd; |
| 495 | GRPC_FD_REF(fd, "basicpoll"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 496 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 497 | goto exit; |
| 498 | } |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 499 | |
| 500 | /* Now we need to promote. This needs to happen when we're not polling. Since |
| 501 | * this may be called from poll, the wait needs to happen asynchronously. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 502 | GRPC_FD_REF(fd, "basicpoll_add"); |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 503 | pollset->in_flight_cbs++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 504 | up_args = gpr_malloc(sizeof(*up_args)); |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 505 | up_args->fd = fd; |
| 506 | up_args->original_vtable = pollset->vtable; |
Craig Tiller | ba49645 | 2015-09-21 17:15:19 -0700 | [diff] [blame] | 507 | up_args->pollset = pollset; |
Craig Tiller | b475655 | 2015-06-01 20:33:37 -0700 | [diff] [blame] | 508 | up_args->promotion_closure.cb = basic_do_promote; |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 509 | up_args->promotion_closure.cb_arg = up_args; |
David Klempner | 7f43eaf | 2015-02-18 17:00:31 -0800 | [diff] [blame] | 510 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 511 | grpc_closure_list_add(&pollset->idle_jobs, &up_args->promotion_closure, 1); |
| 512 | grpc_pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST); |
Craig Tiller | 5c785d4 | 2015-07-14 08:23:43 -0700 | [diff] [blame] | 513 | |
| 514 | exit: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 515 | if (and_unlock_pollset) { |
| 516 | gpr_mu_unlock(&pollset->mu); |
| 517 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 520 | static void basic_pollset_del_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 521 | grpc_fd *fd, int and_unlock_pollset) { |
| 522 | GPR_ASSERT(fd); |
| 523 | if (fd == pollset->data.ptr) { |
| 524 | GRPC_FD_UNREF(pollset->data.ptr, "basicpoll"); |
| 525 | pollset->data.ptr = NULL; |
| 526 | } |
Craig Tiller | 5c785d4 | 2015-07-14 08:23:43 -0700 | [diff] [blame] | 527 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 528 | if (and_unlock_pollset) { |
| 529 | gpr_mu_unlock(&pollset->mu); |
| 530 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 533 | static void basic_pollset_maybe_work_and_unlock(grpc_exec_ctx *exec_ctx, |
| 534 | grpc_pollset *pollset, |
| 535 | grpc_pollset_worker *worker, |
| 536 | gpr_timespec deadline, |
| 537 | gpr_timespec now) { |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 538 | #define POLLOUT_CHECK (POLLOUT | POLLHUP | POLLERR) |
Craig Tiller | 57f79d6 | 2015-10-02 14:00:12 -0700 | [diff] [blame] | 539 | #define POLLIN_CHECK (POLLIN | POLLHUP | POLLERR) |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 540 | |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 541 | struct pollfd pfd[3]; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 542 | grpc_fd *fd; |
Craig Tiller | 7d41321 | 2015-02-09 08:00:02 -0800 | [diff] [blame] | 543 | grpc_fd_watcher fd_watcher; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 544 | int timeout; |
| 545 | int r; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 546 | nfds_t nfds; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 547 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 548 | fd = pollset->data.ptr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 549 | if (fd && grpc_fd_is_orphaned(fd)) { |
| 550 | GRPC_FD_UNREF(fd, "basicpoll"); |
| 551 | fd = pollset->data.ptr = NULL; |
| 552 | } |
| 553 | timeout = grpc_poll_deadline_to_millis_timeout(deadline, now); |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 554 | pfd[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 555 | pfd[0].events = POLLIN; |
| 556 | pfd[0].revents = 0; |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 557 | pfd[1].fd = GRPC_WAKEUP_FD_GET_READ_FD(&worker->wakeup_fd); |
| 558 | pfd[1].events = POLLIN; |
| 559 | pfd[1].revents = 0; |
| 560 | nfds = 2; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 561 | if (fd) { |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 562 | pfd[2].fd = fd->fd; |
| 563 | pfd[2].revents = 0; |
Craig Tiller | bae235c | 2015-10-05 11:33:36 -0700 | [diff] [blame] | 564 | GRPC_FD_REF(fd, "basicpoll_begin"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 565 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | dc17471 | 2015-10-01 10:25:02 -0700 | [diff] [blame] | 566 | pfd[2].events = (short)grpc_fd_begin_poll(fd, pollset, worker, POLLIN, |
| 567 | POLLOUT, &fd_watcher); |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 568 | if (pfd[2].events != 0) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 569 | nfds++; |
Craig Tiller | 3f529a2c | 2015-05-28 14:00:47 -0700 | [diff] [blame] | 570 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 571 | } else { |
| 572 | gpr_mu_unlock(&pollset->mu); |
| 573 | } |
Craig Tiller | 48d01ae | 2015-01-29 22:08:59 -0800 | [diff] [blame] | 574 | |
Vijay Pai | 8c7665e | 2015-09-25 21:40:19 -0700 | [diff] [blame] | 575 | /* TODO(vpai): Consider first doing a 0 timeout poll here to avoid |
| 576 | even going into the blocking annotation if possible */ |
Craig Tiller | baac940 | 2015-05-28 07:39:31 -0700 | [diff] [blame] | 577 | /* poll fd count (argument 2) is shortened by one if we have no events |
| 578 | to poll on - such that it only includes the kicker */ |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 579 | GRPC_TIMER_BEGIN("poll", 0); |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 580 | GRPC_SCHEDULING_START_BLOCKING_REGION; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 581 | r = grpc_poll_function(pfd, nfds, timeout); |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 582 | GRPC_SCHEDULING_END_BLOCKING_REGION; |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 583 | GRPC_TIMER_END("poll", 0); |
Craig Tiller | a13752d | 2015-03-06 23:01:38 -0800 | [diff] [blame] | 584 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 585 | if (r < 0) { |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 586 | gpr_log(GPR_ERROR, "poll() failed: %s", strerror(errno)); |
| 587 | if (fd) { |
| 588 | grpc_fd_end_poll(exec_ctx, &fd_watcher, 0, 0); |
ctiller | d9962df | 2015-01-07 15:31:39 -0800 | [diff] [blame] | 589 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 590 | } else if (r == 0) { |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 591 | if (fd) { |
| 592 | grpc_fd_end_poll(exec_ctx, &fd_watcher, 0, 0); |
| 593 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 594 | } else { |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 595 | if (pfd[0].revents & POLLIN_CHECK) { |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 596 | grpc_wakeup_fd_consume_wakeup(&grpc_global_wakeup_fd); |
| 597 | } |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 598 | if (pfd[1].revents & POLLIN_CHECK) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 599 | grpc_wakeup_fd_consume_wakeup(&worker->wakeup_fd); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 600 | } |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 601 | if (nfds > 2) { |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 602 | grpc_fd_end_poll(exec_ctx, &fd_watcher, pfd[2].revents & POLLIN_CHECK, |
| 603 | pfd[2].revents & POLLOUT_CHECK); |
| 604 | } else if (fd) { |
| 605 | grpc_fd_end_poll(exec_ctx, &fd_watcher, 0, 0); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 606 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 607 | } |
Craig Tiller | bae235c | 2015-10-05 11:33:36 -0700 | [diff] [blame] | 608 | |
| 609 | if (fd) { |
| 610 | GRPC_FD_UNREF(fd, "basicpoll_begin"); |
| 611 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 614 | static void basic_pollset_destroy(grpc_pollset *pollset) { |
| 615 | if (pollset->data.ptr != NULL) { |
| 616 | GRPC_FD_UNREF(pollset->data.ptr, "basicpoll"); |
| 617 | pollset->data.ptr = NULL; |
| 618 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Craig Tiller | 3f529a2c | 2015-05-28 14:00:47 -0700 | [diff] [blame] | 621 | static const grpc_pollset_vtable basic_pollset = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 622 | basic_pollset_add_fd, basic_pollset_del_fd, |
| 623 | basic_pollset_maybe_work_and_unlock, basic_pollset_destroy, |
| 624 | basic_pollset_destroy}; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 625 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 626 | static void become_basic_pollset(grpc_pollset *pollset, grpc_fd *fd_or_null) { |
Craig Tiller | 3f529a2c | 2015-05-28 14:00:47 -0700 | [diff] [blame] | 627 | pollset->vtable = &basic_pollset; |
Craig Tiller | 3f529a2c | 2015-05-28 14:00:47 -0700 | [diff] [blame] | 628 | pollset->data.ptr = fd_or_null; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 629 | if (fd_or_null != NULL) { |
| 630 | GRPC_FD_REF(fd_or_null, "basicpoll"); |
| 631 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 632 | } |
Craig Tiller | d14a1a5 | 2015-01-21 15:26:29 -0800 | [diff] [blame] | 633 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 634 | #endif /* GPR_POSIX_POLLSET */ |