Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -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 <grpc/support/port_platform.h> |
| 35 | |
| 36 | #ifdef GPR_POSIX_SOCKET |
| 37 | |
| 38 | #include "src/core/lib/iomgr/ev_epoll_posix.h" |
| 39 | |
| 40 | #include <assert.h> |
| 41 | #include <errno.h> |
| 42 | #include <poll.h> |
| 43 | #include <signal.h> |
| 44 | #include <string.h> |
| 45 | #include <sys/epoll.h> |
| 46 | #include <sys/socket.h> |
| 47 | #include <unistd.h> |
| 48 | |
| 49 | #include <grpc/support/alloc.h> |
| 50 | #include <grpc/support/log.h> |
| 51 | #include <grpc/support/string_util.h> |
| 52 | #include <grpc/support/tls.h> |
| 53 | #include <grpc/support/useful.h> |
| 54 | |
| 55 | #include "src/core/lib/iomgr/ev_posix.h" |
| 56 | #include "src/core/lib/iomgr/iomgr_internal.h" |
| 57 | #include "src/core/lib/iomgr/wakeup_fd_posix.h" |
| 58 | #include "src/core/lib/profiling/timers.h" |
| 59 | #include "src/core/lib/support/block_annotate.h" |
| 60 | |
| 61 | struct polling_island; |
| 62 | |
| 63 | /******************************************************************************* |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 64 | * Fd Declarations |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 65 | */ |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 66 | struct grpc_fd { |
| 67 | int fd; |
| 68 | /* refst format: |
Sree Kuchibhotla | 5098f91 | 2016-05-31 10:58:17 -0700 | [diff] [blame] | 69 | bit 0 : 1=Active / 0=Orphaned |
| 70 | bits 1-n : refcount |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 71 | Ref/Unref by two to avoid altering the orphaned bit */ |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 72 | gpr_atm refst; |
| 73 | |
| 74 | gpr_mu mu; |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 75 | |
| 76 | /* Indicates that the fd is shutdown and that any pending read/write closures |
| 77 | should fail */ |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 78 | bool shutdown; |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 79 | |
| 80 | /* The fd is either closed or we relinquished control of it. In either cases, |
| 81 | this indicates that the 'fd' on this structure is no longer valid */ |
| 82 | bool orphaned; |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 83 | |
| 84 | grpc_closure *read_closure; |
| 85 | grpc_closure *write_closure; |
| 86 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 87 | /* The polling island to which this fd belongs to and the mutex protecting the |
| 88 | the field */ |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 89 | gpr_mu pi_mu; |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 90 | struct polling_island *polling_island; |
| 91 | |
| 92 | struct grpc_fd *freelist_next; |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 93 | grpc_closure *on_done_closure; |
| 94 | |
| 95 | grpc_iomgr_object iomgr_object; |
| 96 | }; |
| 97 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 98 | /* Reference counting for fds */ |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 99 | #ifdef GRPC_FD_REF_COUNT_DEBUG |
| 100 | static void fd_ref(grpc_fd *fd, const char *reason, const char *file, int line); |
| 101 | static void fd_unref(grpc_fd *fd, const char *reason, const char *file, |
| 102 | int line); |
| 103 | #define GRPC_FD_REF(fd, reason) fd_ref(fd, reason, __FILE__, __LINE__) |
| 104 | #define GRPC_FD_UNREF(fd, reason) fd_unref(fd, reason, __FILE__, __LINE__) |
| 105 | #else |
| 106 | static void fd_ref(grpc_fd *fd); |
| 107 | static void fd_unref(grpc_fd *fd); |
| 108 | #define GRPC_FD_REF(fd, reason) fd_ref(fd) |
| 109 | #define GRPC_FD_UNREF(fd, reason) fd_unref(fd) |
| 110 | #endif |
| 111 | |
| 112 | static void fd_global_init(void); |
| 113 | static void fd_global_shutdown(void); |
| 114 | |
| 115 | #define CLOSURE_NOT_READY ((grpc_closure *)0) |
| 116 | #define CLOSURE_READY ((grpc_closure *)1) |
| 117 | |
| 118 | /******************************************************************************* |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 119 | * Polling-island Declarations |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 120 | */ |
| 121 | typedef struct polling_island { |
| 122 | gpr_mu mu; |
| 123 | int ref_cnt; |
| 124 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 125 | /* Points to the polling_island this merged into. |
| 126 | * If merged_to is not NULL, all the remaining fields (except mu and ref_cnt) |
| 127 | * are invalid and must be ignored */ |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 128 | struct polling_island *merged_to; |
| 129 | |
| 130 | /* The fd of the underlying epoll set */ |
| 131 | int epoll_fd; |
| 132 | |
| 133 | /* The file descriptors in the epoll set */ |
| 134 | size_t fd_cnt; |
| 135 | size_t fd_capacity; |
| 136 | grpc_fd **fds; |
| 137 | |
| 138 | /* Polling islands that are no longer needed are kept in a freelist so that |
| 139 | they can be reused. This field points to the next polling island in the |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 140 | free list */ |
| 141 | struct polling_island *next_free; |
| 142 | } polling_island; |
| 143 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 144 | /******************************************************************************* |
| 145 | * Pollset Declarations |
| 146 | */ |
| 147 | |
| 148 | struct grpc_pollset_worker { |
| 149 | int kicked_specifically; |
| 150 | pthread_t pt_id; /* TODO (sreek) - Add an abstraction here */ |
| 151 | struct grpc_pollset_worker *next; |
| 152 | struct grpc_pollset_worker *prev; |
| 153 | }; |
| 154 | |
| 155 | struct grpc_pollset { |
| 156 | gpr_mu mu; |
| 157 | grpc_pollset_worker root_worker; |
| 158 | bool kicked_without_pollers; |
| 159 | |
| 160 | bool shutting_down; /* Is the pollset shutting down ? */ |
| 161 | bool finish_shutdown_called; /* Is the 'finish_shutdown_locked()' called ? */ |
| 162 | grpc_closure *shutdown_done; /* Called after after shutdown is complete */ |
| 163 | |
| 164 | /* The polling island to which this pollset belongs to and the mutex |
| 165 | protecting the field */ |
| 166 | gpr_mu pi_mu; |
| 167 | struct polling_island *polling_island; |
| 168 | }; |
| 169 | |
| 170 | /******************************************************************************* |
| 171 | * Pollset-set Declarations |
| 172 | */ |
| 173 | struct grpc_pollset_set { |
| 174 | gpr_mu mu; |
| 175 | |
| 176 | size_t pollset_count; |
| 177 | size_t pollset_capacity; |
| 178 | grpc_pollset **pollsets; |
| 179 | |
| 180 | size_t pollset_set_count; |
| 181 | size_t pollset_set_capacity; |
| 182 | struct grpc_pollset_set **pollset_sets; |
| 183 | |
| 184 | size_t fd_count; |
| 185 | size_t fd_capacity; |
| 186 | grpc_fd **fds; |
| 187 | }; |
| 188 | |
| 189 | /******************************************************************************* |
| 190 | * Polling-island Definitions |
| 191 | */ |
| 192 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 193 | /* Polling island freelist */ |
| 194 | static gpr_mu g_pi_freelist_mu; |
| 195 | static polling_island *g_pi_freelist = NULL; |
| 196 | |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 197 | /* The caller is expected to hold pi->mu lock before calling this function */ |
| 198 | static void polling_island_add_fds_locked(polling_island *pi, grpc_fd **fds, |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 199 | size_t fd_count, bool add_fd_refs) { |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 200 | int err; |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 201 | size_t i; |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 202 | struct epoll_event ev; |
| 203 | |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 204 | for (i = 0; i < fd_count; i++) { |
| 205 | ev.events = (uint32_t)(EPOLLIN | EPOLLOUT | EPOLLET); |
| 206 | ev.data.ptr = fds[i]; |
| 207 | err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_ADD, fds[i]->fd, &ev); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 208 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 209 | if (err < 0) { |
| 210 | if (errno != EEXIST) { |
| 211 | /* TODO: sreek - We need a better way to bubble up this error instead of |
| 212 | just logging a message */ |
| 213 | gpr_log(GPR_ERROR, "epoll_ctl add for fd: %d failed with error: %s", |
| 214 | fds[i]->fd, strerror(errno)); |
| 215 | } |
| 216 | |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 217 | continue; |
| 218 | } |
| 219 | |
| 220 | if (pi->fd_cnt == pi->fd_capacity) { |
| 221 | pi->fd_capacity = GPR_MAX(pi->fd_capacity + 8, pi->fd_cnt * 3 / 2); |
| 222 | pi->fds = gpr_realloc(pi->fds, sizeof(grpc_fd *) * pi->fd_capacity); |
| 223 | } |
| 224 | |
| 225 | pi->fds[pi->fd_cnt++] = fds[i]; |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 226 | if (add_fd_refs) { |
| 227 | GRPC_FD_REF(fds[i], "polling_island"); |
| 228 | } |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 229 | } |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 232 | /* The caller is expected to hold pi->mu lock before calling this function */ |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 233 | static void polling_island_remove_all_fds_locked(polling_island *pi, |
| 234 | bool remove_fd_refs) { |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 235 | int err; |
| 236 | size_t i; |
| 237 | |
| 238 | for (i = 0; i < pi->fd_cnt; i++) { |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 239 | if (remove_fd_refs) { |
| 240 | GRPC_FD_UNREF(pi->fds[i], "polling_island"); |
| 241 | } |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 242 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 243 | err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_DEL, pi->fds[i]->fd, NULL); |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 244 | if (err < 0 && errno != ENOENT) { |
| 245 | gpr_log(GPR_ERROR, |
| 246 | "epoll_ctl delete for fds[i]: %d failed with error: %s", i, |
| 247 | pi->fds[i]->fd, strerror(errno)); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 248 | /* TODO: sreek - We need a better way to bubble up this error instead of |
| 249 | * just logging a message */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 250 | continue; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | pi->fd_cnt = 0; |
| 255 | } |
| 256 | |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 257 | /* The caller is expected to hold pi->mu lock before calling this function */ |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 258 | static void polling_island_remove_fd_locked(polling_island *pi, grpc_fd *fd, |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 259 | bool is_fd_closed) { |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 260 | int err; |
| 261 | size_t i; |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 262 | |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 263 | /* If fd is already closed, then it would have been automatically been removed |
| 264 | from the epoll set */ |
| 265 | if (!is_fd_closed) { |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 266 | err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_DEL, fd->fd, NULL); |
| 267 | if (err < 0 && errno != ENOENT) { |
| 268 | gpr_log(GPR_ERROR, "epoll_ctl delete for fd: %d failed with error; %s", |
| 269 | fd->fd, strerror(errno)); |
| 270 | } |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | for (i = 0; i < pi->fd_cnt; i++) { |
| 274 | if (pi->fds[i] == fd) { |
| 275 | pi->fds[i] = pi->fds[--pi->fd_cnt]; |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 276 | GRPC_FD_UNREF(fd, "polling_island"); |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 277 | break; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | static polling_island *polling_island_create(grpc_fd *initial_fd, |
| 283 | int initial_ref_cnt) { |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 284 | polling_island *pi = NULL; |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 285 | struct epoll_event ev; |
| 286 | int err; |
| 287 | |
| 288 | /* Try to get one from the polling island freelist */ |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 289 | gpr_mu_lock(&g_pi_freelist_mu); |
| 290 | if (g_pi_freelist != NULL) { |
| 291 | pi = g_pi_freelist; |
| 292 | g_pi_freelist = g_pi_freelist->next_free; |
| 293 | pi->next_free = NULL; |
| 294 | } |
| 295 | gpr_mu_unlock(&g_pi_freelist_mu); |
| 296 | |
| 297 | /* Create new polling island if we could not get one from the free list */ |
| 298 | if (pi == NULL) { |
| 299 | pi = gpr_malloc(sizeof(*pi)); |
| 300 | gpr_mu_init(&pi->mu); |
| 301 | pi->fd_cnt = 0; |
| 302 | pi->fd_capacity = 0; |
| 303 | pi->fds = NULL; |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 304 | } |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 305 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 306 | pi->epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
| 307 | if (pi->epoll_fd < 0) { |
| 308 | gpr_log(GPR_ERROR, "epoll_create1() failed with error: %s", |
| 309 | strerror(errno)); |
| 310 | } |
| 311 | GPR_ASSERT(pi->epoll_fd >= 0); |
| 312 | |
| 313 | ev.events = (uint32_t)(EPOLLIN | EPOLLET); |
| 314 | ev.data.ptr = NULL; |
| 315 | err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_ADD, |
| 316 | GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd), &ev); |
| 317 | if (err < 0) { |
| 318 | gpr_log(GPR_ERROR, |
| 319 | "Failed to add grpc_global_wake_up_fd (%d) to the epoll set " |
| 320 | "(epoll_fd: %d) with error: %s", |
| 321 | GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd), pi->epoll_fd, |
| 322 | strerror(errno)); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | pi->ref_cnt = initial_ref_cnt; |
| 326 | pi->merged_to = NULL; |
| 327 | pi->next_free = NULL; |
| 328 | |
| 329 | if (initial_fd != NULL) { |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 330 | /* It is not really needed to get the pi->mu lock here. If this is a newly |
| 331 | created polling island (or one that we got from the freelist), no one |
| 332 | else would be holding a lock to it anyway */ |
| 333 | gpr_mu_lock(&pi->mu); |
| 334 | polling_island_add_fds_locked(pi, &initial_fd, 1, true); |
| 335 | gpr_mu_unlock(&pi->mu); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return pi; |
| 339 | } |
| 340 | |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 341 | static void polling_island_delete(polling_island *pi) { |
| 342 | GPR_ASSERT(pi->ref_cnt == 0); |
| 343 | GPR_ASSERT(pi->fd_cnt == 0); |
| 344 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 345 | close(pi->epoll_fd); |
| 346 | pi->epoll_fd = -1; |
| 347 | |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 348 | pi->merged_to = NULL; |
| 349 | |
| 350 | gpr_mu_lock(&g_pi_freelist_mu); |
| 351 | pi->next_free = g_pi_freelist; |
| 352 | g_pi_freelist = pi; |
| 353 | gpr_mu_unlock(&g_pi_freelist_mu); |
| 354 | } |
| 355 | |
| 356 | void polling_island_unref_and_unlock(polling_island *pi, int unref_by) { |
| 357 | pi->ref_cnt -= unref_by; |
| 358 | int ref_cnt = pi->ref_cnt; |
| 359 | GPR_ASSERT(ref_cnt >= 0); |
| 360 | |
| 361 | gpr_mu_unlock(&pi->mu); |
| 362 | |
| 363 | if (ref_cnt == 0) { |
| 364 | polling_island_delete(pi); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | polling_island *polling_island_update_and_lock(polling_island *pi, int unref_by, |
| 369 | int add_ref_by) { |
| 370 | polling_island *next = NULL; |
| 371 | gpr_mu_lock(&pi->mu); |
| 372 | while (pi->merged_to != NULL) { |
| 373 | next = pi->merged_to; |
| 374 | polling_island_unref_and_unlock(pi, unref_by); |
| 375 | pi = next; |
| 376 | gpr_mu_lock(&pi->mu); |
| 377 | } |
| 378 | |
| 379 | pi->ref_cnt += add_ref_by; |
| 380 | return pi; |
| 381 | } |
| 382 | |
| 383 | void polling_island_pair_update_and_lock(polling_island **p, |
| 384 | polling_island **q) { |
| 385 | polling_island *pi_1 = *p; |
| 386 | polling_island *pi_2 = *q; |
| 387 | polling_island *temp = NULL; |
| 388 | bool pi_1_locked = false; |
| 389 | bool pi_2_locked = false; |
| 390 | int num_swaps = 0; |
| 391 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 392 | /* Loop until either pi_1 == pi_2 or until we acquired locks on both pi_1 |
| 393 | and pi_2 */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 394 | while (pi_1 != pi_2 && !(pi_1_locked && pi_2_locked)) { |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 395 | /* The following assertions are true at this point: |
| 396 | - pi_1 != pi_2 (else, the while loop would have exited) |
| 397 | - pi_1 MAY be locked |
| 398 | - pi_2 is NOT locked */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 399 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 400 | /* To maintain lock order consistency, always lock polling_island node with |
| 401 | lower address first. |
| 402 | First, make sure pi_1 < pi_2 before proceeding any further. If it turns |
| 403 | out that pi_1 > pi_2, unlock pi_1 if locked (because pi_2 is not locked |
| 404 | at this point and having pi_1 locked would violate the lock order) and |
| 405 | swap pi_1 and pi_2 so that pi_1 becomes less than pi_2 */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 406 | if (pi_1 > pi_2) { |
| 407 | if (pi_1_locked) { |
| 408 | gpr_mu_unlock(&pi_1->mu); |
| 409 | pi_1_locked = false; |
| 410 | } |
| 411 | |
| 412 | GPR_SWAP(polling_island *, pi_1, pi_2); |
| 413 | num_swaps++; |
| 414 | } |
| 415 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 416 | /* The following assertions are true at this point: |
| 417 | - pi_1 != pi_2 |
| 418 | - pi_1 < pi_2 (address of pi_1 is less than that of pi_2) |
| 419 | - pi_1 MAYBE locked |
| 420 | - pi_2 is NOT locked */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 421 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 422 | /* Lock pi_1 (if pi_1 is pointing to the terminal node in the list) */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 423 | if (!pi_1_locked) { |
| 424 | gpr_mu_lock(&pi_1->mu); |
| 425 | pi_1_locked = true; |
| 426 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 427 | /* If pi_1 is not terminal node (i.e pi_1->merged_to != NULL), we are not |
| 428 | done locking this polling_island yet. Release the lock on this node and |
| 429 | advance pi_1 to the next node in the list; and go to the beginning of |
| 430 | the loop (we can't proceed to locking pi_2 unless we locked pi_1 first) |
| 431 | */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 432 | if (pi_1->merged_to != NULL) { |
| 433 | temp = pi_1->merged_to; |
| 434 | polling_island_unref_and_unlock(pi_1, 1); |
| 435 | pi_1 = temp; |
| 436 | pi_1_locked = false; |
| 437 | |
| 438 | continue; |
| 439 | } |
| 440 | } |
| 441 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 442 | /* The following assertions are true at this point: |
| 443 | - pi_1 is locked |
| 444 | - pi_2 is unlocked |
| 445 | - pi_1 != pi_2 */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 446 | |
| 447 | gpr_mu_lock(&pi_2->mu); |
| 448 | pi_2_locked = true; |
| 449 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 450 | /* If pi_2 is not terminal node, we are not done locking this polling_island |
| 451 | yet. Release the lock and update pi_2 to the next node in the list */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 452 | if (pi_2->merged_to != NULL) { |
| 453 | temp = pi_2->merged_to; |
| 454 | polling_island_unref_and_unlock(pi_2, 1); |
| 455 | pi_2 = temp; |
| 456 | pi_2_locked = false; |
| 457 | } |
| 458 | } |
| 459 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 460 | /* At this point, either pi_1 == pi_2 AND/OR we got both locks */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 461 | if (pi_1 == pi_2) { |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 462 | /* We may or may not have gotten the lock. If we didn't, walk the rest of |
| 463 | the polling_island list and get the lock */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 464 | GPR_ASSERT(pi_1_locked || (!pi_1_locked && !pi_2_locked)); |
| 465 | if (!pi_1_locked) { |
| 466 | pi_1 = pi_2 = polling_island_update_and_lock(pi_1, 2, 0); |
| 467 | } |
| 468 | } else { |
| 469 | GPR_ASSERT(pi_1_locked && pi_2_locked); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 470 | /* If we swapped pi_1 and pi_2 odd number of times, do one more swap so that |
| 471 | pi_1 and pi_2 point to the same polling_island lists they started off |
| 472 | with at the beginning of this function (i.e *p and *q respectively) */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 473 | if (num_swaps % 2 > 0) { |
| 474 | GPR_SWAP(polling_island *, pi_1, pi_2); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | *p = pi_1; |
| 479 | *q = pi_2; |
| 480 | } |
| 481 | |
| 482 | polling_island *polling_island_merge(polling_island *p, polling_island *q) { |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 483 | /* Get locks on both the polling islands */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 484 | polling_island_pair_update_and_lock(&p, &q); |
| 485 | |
| 486 | /* TODO: sreek: Think about this scenario some more. Is it possible ?. what |
| 487 | * does it mean, when would this happen */ |
| 488 | if (p == q) { |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 489 | /* Nothing needs to be done here */ |
| 490 | gpr_mu_unlock(&p->mu); |
| 491 | return p; |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 494 | /* Make sure that p points to the polling island with fewer fds than q */ |
| 495 | if (p->fd_cnt > q->fd_cnt) { |
| 496 | GPR_SWAP(polling_island *, p, q); |
| 497 | } |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 498 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 499 | /* "Merge" p with q i.e move all the fds from p (the polling_island with fewer |
| 500 | fds) to q. |
| 501 | Note: Not altering the ref counts on the affected fds here because they |
| 502 | would effectively remain unchanged */ |
| 503 | polling_island_add_fds_locked(q, p->fds, p->fd_cnt, false); |
| 504 | polling_island_remove_all_fds_locked(p, false); |
| 505 | |
| 506 | /* The merged polling island inherits all the ref counts of the island merging |
| 507 | with it */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 508 | q->ref_cnt += p->ref_cnt; |
| 509 | |
| 510 | gpr_mu_unlock(&p->mu); |
| 511 | gpr_mu_unlock(&q->mu); |
| 512 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 513 | return q; |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 516 | static void polling_island_global_init() { |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 517 | gpr_mu_init(&g_pi_freelist_mu); |
| 518 | g_pi_freelist = NULL; |
| 519 | } |
| 520 | |
| 521 | /******************************************************************************* |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 522 | * Fd Definitions |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 523 | */ |
| 524 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 525 | /* We need to keep a freelist not because of any concerns of malloc performance |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 526 | * but instead so that implementations with multiple threads in (for example) |
| 527 | * epoll_wait deal with the race between pollset removal and incoming poll |
| 528 | * notifications. |
| 529 | * |
| 530 | * The problem is that the poller ultimately holds a reference to this |
| 531 | * object, so it is very difficult to know when is safe to free it, at least |
| 532 | * without some expensive synchronization. |
| 533 | * |
| 534 | * If we keep the object freelisted, in the worst case losing this race just |
| 535 | * becomes a spurious read notification on a reused fd. |
| 536 | */ |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 537 | |
| 538 | /* The alarm system needs to be able to wakeup 'some poller' sometimes |
| 539 | * (specifically when a new alarm needs to be triggered earlier than the next |
| 540 | * alarm 'epoch'). This wakeup_fd gives us something to alert on when such a |
| 541 | * case occurs. */ |
| 542 | /* TODO: sreek: Right now, this wakes up all pollers */ |
| 543 | grpc_wakeup_fd grpc_global_wakeup_fd; |
| 544 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 545 | static grpc_fd *fd_freelist = NULL; |
| 546 | static gpr_mu fd_freelist_mu; |
| 547 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 548 | #ifdef GRPC_FD_REF_COUNT_DEBUG |
| 549 | #define REF_BY(fd, n, reason) ref_by(fd, n, reason, __FILE__, __LINE__) |
| 550 | #define UNREF_BY(fd, n, reason) unref_by(fd, n, reason, __FILE__, __LINE__) |
| 551 | static void ref_by(grpc_fd *fd, int n, const char *reason, const char *file, |
| 552 | int line) { |
| 553 | gpr_log(GPR_DEBUG, "FD %d %p ref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n, |
| 554 | gpr_atm_no_barrier_load(&fd->refst), |
| 555 | gpr_atm_no_barrier_load(&fd->refst) + n, reason, file, line); |
| 556 | #else |
| 557 | #define REF_BY(fd, n, reason) ref_by(fd, n) |
| 558 | #define UNREF_BY(fd, n, reason) unref_by(fd, n) |
| 559 | static void ref_by(grpc_fd *fd, int n) { |
| 560 | #endif |
| 561 | GPR_ASSERT(gpr_atm_no_barrier_fetch_add(&fd->refst, n) > 0); |
| 562 | } |
| 563 | |
| 564 | #ifdef GRPC_FD_REF_COUNT_DEBUG |
| 565 | static void unref_by(grpc_fd *fd, int n, const char *reason, const char *file, |
| 566 | int line) { |
| 567 | gpr_atm old; |
| 568 | gpr_log(GPR_DEBUG, "FD %d %p unref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n, |
| 569 | gpr_atm_no_barrier_load(&fd->refst), |
| 570 | gpr_atm_no_barrier_load(&fd->refst) - n, reason, file, line); |
| 571 | #else |
| 572 | static void unref_by(grpc_fd *fd, int n) { |
| 573 | gpr_atm old; |
| 574 | #endif |
| 575 | old = gpr_atm_full_fetch_add(&fd->refst, -n); |
| 576 | if (old == n) { |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 577 | /* Add the fd to the freelist */ |
| 578 | gpr_mu_lock(&fd_freelist_mu); |
| 579 | fd->freelist_next = fd_freelist; |
| 580 | fd_freelist = fd; |
| 581 | grpc_iomgr_unregister_object(&fd->iomgr_object); |
| 582 | gpr_mu_unlock(&fd_freelist_mu); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 583 | } else { |
| 584 | GPR_ASSERT(old > n); |
| 585 | } |
| 586 | } |
| 587 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 588 | /* Increment refcount by two to avoid changing the orphan bit */ |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 589 | #ifdef GRPC_FD_REF_COUNT_DEBUG |
| 590 | static void fd_ref(grpc_fd *fd, const char *reason, const char *file, |
| 591 | int line) { |
| 592 | ref_by(fd, 2, reason, file, line); |
| 593 | } |
| 594 | |
| 595 | static void fd_unref(grpc_fd *fd, const char *reason, const char *file, |
| 596 | int line) { |
| 597 | unref_by(fd, 2, reason, file, line); |
| 598 | } |
| 599 | #else |
| 600 | static void fd_ref(grpc_fd *fd) { ref_by(fd, 2); } |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 601 | static void fd_unref(grpc_fd *fd) { unref_by(fd, 2); } |
| 602 | #endif |
| 603 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 604 | static void fd_global_init(void) { gpr_mu_init(&fd_freelist_mu); } |
| 605 | |
| 606 | static void fd_global_shutdown(void) { |
| 607 | gpr_mu_lock(&fd_freelist_mu); |
| 608 | gpr_mu_unlock(&fd_freelist_mu); |
| 609 | while (fd_freelist != NULL) { |
| 610 | grpc_fd *fd = fd_freelist; |
| 611 | fd_freelist = fd_freelist->freelist_next; |
| 612 | gpr_mu_destroy(&fd->mu); |
| 613 | gpr_free(fd); |
| 614 | } |
| 615 | gpr_mu_destroy(&fd_freelist_mu); |
| 616 | } |
| 617 | |
| 618 | static grpc_fd *fd_create(int fd, const char *name) { |
| 619 | grpc_fd *new_fd = NULL; |
| 620 | |
| 621 | gpr_mu_lock(&fd_freelist_mu); |
| 622 | if (fd_freelist != NULL) { |
| 623 | new_fd = fd_freelist; |
| 624 | fd_freelist = fd_freelist->freelist_next; |
| 625 | } |
| 626 | gpr_mu_unlock(&fd_freelist_mu); |
| 627 | |
| 628 | if (new_fd == NULL) { |
| 629 | new_fd = gpr_malloc(sizeof(grpc_fd)); |
| 630 | gpr_mu_init(&new_fd->mu); |
| 631 | gpr_mu_init(&new_fd->pi_mu); |
| 632 | } |
| 633 | |
| 634 | /* Note: It is not really needed to get the new_fd->mu lock here. If this is a |
| 635 | newly created fd (or an fd we got from the freelist), no one else would be |
| 636 | holding a lock to it anyway. */ |
| 637 | gpr_mu_lock(&new_fd->mu); |
| 638 | |
| 639 | gpr_atm_rel_store(&new_fd->refst, 1); |
| 640 | new_fd->shutdown = false; |
| 641 | new_fd->read_closure = CLOSURE_NOT_READY; |
| 642 | new_fd->write_closure = CLOSURE_NOT_READY; |
| 643 | new_fd->fd = fd; |
| 644 | new_fd->polling_island = NULL; |
| 645 | new_fd->freelist_next = NULL; |
| 646 | new_fd->on_done_closure = NULL; |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 647 | new_fd->orphaned = false; |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 648 | |
| 649 | gpr_mu_unlock(&new_fd->mu); |
| 650 | |
| 651 | char *fd_name; |
| 652 | gpr_asprintf(&fd_name, "%s fd=%d", name, fd); |
| 653 | grpc_iomgr_register_object(&new_fd->iomgr_object, fd_name); |
| 654 | gpr_free(fd_name); |
| 655 | #ifdef GRPC_FD_REF_COUNT_DEBUG |
| 656 | gpr_log(GPR_DEBUG, "FD %d %p create %s", fd, r, fd_name); |
| 657 | #endif |
| 658 | return new_fd; |
| 659 | } |
| 660 | |
| 661 | static bool fd_is_orphaned(grpc_fd *fd) { |
| 662 | return (gpr_atm_acq_load(&fd->refst) & 1) == 0; |
| 663 | } |
| 664 | |
| 665 | static int fd_wrapped_fd(grpc_fd *fd) { |
| 666 | int ret_fd = -1; |
| 667 | gpr_mu_lock(&fd->mu); |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 668 | if (!fd->orphaned) { |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 669 | ret_fd = fd->fd; |
| 670 | } |
| 671 | gpr_mu_unlock(&fd->mu); |
| 672 | |
| 673 | return ret_fd; |
| 674 | } |
| 675 | |
| 676 | static void fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
| 677 | grpc_closure *on_done, int *release_fd, |
| 678 | const char *reason) { |
| 679 | /* TODO(sreek) In ev_poll_posix.c,the lock is acquired a little later. Why? */ |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 680 | bool is_fd_closed = false; |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 681 | gpr_mu_lock(&fd->mu); |
| 682 | fd->on_done_closure = on_done; |
| 683 | |
| 684 | /* If release_fd is not NULL, we should be relinquishing control of the file |
| 685 | descriptor fd->fd (but we still own the grpc_fd structure). */ |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 686 | if (release_fd != NULL) { |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 687 | *release_fd = fd->fd; |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 688 | } else { |
| 689 | close(fd->fd); |
| 690 | is_fd_closed = true; |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 693 | fd->orphaned = true; |
| 694 | |
| 695 | /* Remove the active status but keep referenced. We want this grpc_fd struct |
| 696 | to be alive (and not added to freelist) until the end of this function */ |
| 697 | REF_BY(fd, 1, reason); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 698 | |
| 699 | /* Remove the fd from the polling island: |
| 700 | - Update the fd->polling_island to point to the latest polling island |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 701 | - Remove the fd from the polling island. |
| 702 | - Remove a ref to the polling island and set fd->polling_island to NULL */ |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 703 | gpr_mu_lock(&fd->pi_mu); |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 704 | if (fd->polling_island != NULL) { |
| 705 | fd->polling_island = |
| 706 | polling_island_update_and_lock(fd->polling_island, 1, 0); |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 707 | polling_island_remove_fd_locked(fd->polling_island, fd, is_fd_closed); |
| 708 | |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 709 | polling_island_unref_and_unlock(fd->polling_island, 1); |
| 710 | fd->polling_island = NULL; |
| 711 | } |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 712 | gpr_mu_unlock(&fd->pi_mu); |
| 713 | |
| 714 | grpc_exec_ctx_enqueue(exec_ctx, fd->on_done_closure, true, NULL); |
| 715 | |
| 716 | gpr_mu_unlock(&fd->mu); |
| 717 | UNREF_BY(fd, 2, reason); /* Drop the reference */ |
| 718 | } |
| 719 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 720 | static void notify_on_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
| 721 | grpc_closure **st, grpc_closure *closure) { |
| 722 | if (*st == CLOSURE_NOT_READY) { |
| 723 | /* not ready ==> switch to a waiting state by setting the closure */ |
| 724 | *st = closure; |
| 725 | } else if (*st == CLOSURE_READY) { |
| 726 | /* already ready ==> queue the closure to run immediately */ |
| 727 | *st = CLOSURE_NOT_READY; |
| 728 | grpc_exec_ctx_enqueue(exec_ctx, closure, !fd->shutdown, NULL); |
| 729 | } else { |
| 730 | /* upcallptr was set to a different closure. This is an error! */ |
| 731 | gpr_log(GPR_ERROR, |
| 732 | "User called a notify_on function with a previous callback still " |
| 733 | "pending"); |
| 734 | abort(); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /* returns 1 if state becomes not ready */ |
| 739 | static int set_ready_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
| 740 | grpc_closure **st) { |
| 741 | if (*st == CLOSURE_READY) { |
| 742 | /* duplicate ready ==> ignore */ |
| 743 | return 0; |
| 744 | } else if (*st == CLOSURE_NOT_READY) { |
| 745 | /* not ready, and not waiting ==> flag ready */ |
| 746 | *st = CLOSURE_READY; |
| 747 | return 0; |
| 748 | } else { |
| 749 | /* waiting ==> queue closure */ |
| 750 | grpc_exec_ctx_enqueue(exec_ctx, *st, !fd->shutdown, NULL); |
| 751 | *st = CLOSURE_NOT_READY; |
| 752 | return 1; |
| 753 | } |
| 754 | } |
| 755 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 756 | static void fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd) { |
| 757 | gpr_mu_lock(&fd->mu); |
| 758 | GPR_ASSERT(!fd->shutdown); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 759 | fd->shutdown = true; |
| 760 | |
| 761 | /* Flush any pending read and write closures. Since fd->shutdown is 'true' at |
| 762 | this point, the closures would be called with 'success = false' */ |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 763 | set_ready_locked(exec_ctx, fd, &fd->read_closure); |
| 764 | set_ready_locked(exec_ctx, fd, &fd->write_closure); |
| 765 | gpr_mu_unlock(&fd->mu); |
| 766 | } |
| 767 | |
| 768 | static void fd_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
| 769 | grpc_closure *closure) { |
| 770 | gpr_mu_lock(&fd->mu); |
| 771 | notify_on_locked(exec_ctx, fd, &fd->read_closure, closure); |
| 772 | gpr_mu_unlock(&fd->mu); |
| 773 | } |
| 774 | |
| 775 | static void fd_notify_on_write(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
| 776 | grpc_closure *closure) { |
| 777 | gpr_mu_lock(&fd->mu); |
| 778 | notify_on_locked(exec_ctx, fd, &fd->write_closure, closure); |
| 779 | gpr_mu_unlock(&fd->mu); |
| 780 | } |
| 781 | |
| 782 | /******************************************************************************* |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 783 | * Pollset Definitions |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 784 | */ |
| 785 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 786 | static void sig_handler(int sig_num) { |
| 787 | /* TODO: sreek - Remove this expensive log line */ |
| 788 | gpr_log(GPR_INFO, "Received signal %d", sig_num); |
| 789 | } |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 790 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 791 | /* Global state management */ |
| 792 | static void pollset_global_init(void) { |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 793 | grpc_wakeup_fd_init(&grpc_global_wakeup_fd); |
| 794 | signal(SIGUSR1, sig_handler); |
| 795 | } |
| 796 | |
| 797 | static void pollset_global_shutdown(void) { |
| 798 | grpc_wakeup_fd_destroy(&grpc_global_wakeup_fd); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /* Return 1 if the pollset has active threads in pollset_work (pollset must |
| 802 | * be locked) */ |
| 803 | static int pollset_has_workers(grpc_pollset *p) { |
| 804 | return p->root_worker.next != &p->root_worker; |
| 805 | } |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 806 | |
| 807 | static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) { |
| 808 | worker->prev->next = worker->next; |
| 809 | worker->next->prev = worker->prev; |
| 810 | } |
| 811 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 812 | static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) { |
| 813 | if (pollset_has_workers(p)) { |
| 814 | grpc_pollset_worker *w = p->root_worker.next; |
| 815 | remove_worker(p, w); |
| 816 | return w; |
| 817 | } else { |
| 818 | return NULL; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) { |
| 823 | worker->next = &p->root_worker; |
| 824 | worker->prev = worker->next->prev; |
| 825 | worker->prev->next = worker->next->prev = worker; |
| 826 | } |
| 827 | |
| 828 | static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) { |
| 829 | worker->prev = &p->root_worker; |
| 830 | worker->next = worker->prev->next; |
| 831 | worker->prev->next = worker->next->prev = worker; |
| 832 | } |
| 833 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 834 | /* p->mu must be held before calling this function */ |
| 835 | static void pollset_kick(grpc_pollset *p, |
| 836 | grpc_pollset_worker *specific_worker) { |
| 837 | GPR_TIMER_BEGIN("pollset_kick", 0); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 838 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 839 | grpc_pollset_worker *worker = specific_worker; |
| 840 | if (worker != NULL) { |
| 841 | if (worker == GRPC_POLLSET_KICK_BROADCAST) { |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 842 | gpr_log(GPR_DEBUG, "pollset_kick: broadcast!"); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 843 | if (pollset_has_workers(p)) { |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 844 | GPR_TIMER_BEGIN("pollset_kick.broadcast", 0); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 845 | for (worker = p->root_worker.next; worker != &p->root_worker; |
| 846 | worker = worker->next) { |
| 847 | pthread_kill(worker->pt_id, SIGUSR1); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 848 | } |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 849 | } else { |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 850 | gpr_log(GPR_DEBUG, "pollset_kick: (broadcast) Kicked without pollers"); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 851 | p->kicked_without_pollers = true; |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 852 | } |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 853 | GPR_TIMER_END("pollset_kick.broadcast", 0); |
| 854 | } else { |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 855 | gpr_log(GPR_DEBUG, "pollset_kick: kicked kicked_specifically"); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 856 | GPR_TIMER_MARK("kicked_specifically", 0); |
| 857 | worker->kicked_specifically = true; |
| 858 | pthread_kill(worker->pt_id, SIGUSR1); |
| 859 | } |
| 860 | } else { |
| 861 | GPR_TIMER_MARK("kick_anonymous", 0); |
| 862 | worker = pop_front_worker(p); |
| 863 | if (worker != NULL) { |
| 864 | GPR_TIMER_MARK("finally_kick", 0); |
| 865 | push_back_worker(p, worker); |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 866 | gpr_log(GPR_DEBUG, "pollset_kick: anonymous kick"); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 867 | pthread_kill(worker->pt_id, SIGUSR1); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 868 | } else { |
| 869 | GPR_TIMER_MARK("kicked_no_pollers", 0); |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 870 | gpr_log(GPR_DEBUG, "pollset_kick: kicked without pollers"); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 871 | p->kicked_without_pollers = true; |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 875 | GPR_TIMER_END("pollset_kick", 0); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | static void kick_poller(void) { grpc_wakeup_fd_wakeup(&grpc_global_wakeup_fd); } |
| 879 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 880 | static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) { |
| 881 | gpr_mu_init(&pollset->mu); |
| 882 | *mu = &pollset->mu; |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 883 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 884 | pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker; |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 885 | pollset->kicked_without_pollers = false; |
| 886 | |
| 887 | pollset->shutting_down = false; |
| 888 | pollset->finish_shutdown_called = false; |
| 889 | pollset->shutdown_done = NULL; |
| 890 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 891 | gpr_mu_init(&pollset->pi_mu); |
| 892 | pollset->polling_island = NULL; |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 895 | /* Convert a timespec to milliseconds: |
| 896 | - Very small or negative poll times are clamped to zero to do a non-blocking |
| 897 | poll (which becomes spin polling) |
| 898 | - Other small values are rounded up to one millisecond |
| 899 | - Longer than a millisecond polls are rounded up to the next nearest |
| 900 | millisecond to avoid spinning |
| 901 | - Infinite timeouts are converted to -1 */ |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 902 | static int poll_deadline_to_millis_timeout(gpr_timespec deadline, |
| 903 | gpr_timespec now) { |
| 904 | gpr_timespec timeout; |
| 905 | static const int64_t max_spin_polling_us = 10; |
| 906 | if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) == 0) { |
| 907 | return -1; |
| 908 | } |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 909 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 910 | if (gpr_time_cmp(deadline, gpr_time_add(now, gpr_time_from_micros( |
| 911 | max_spin_polling_us, |
| 912 | GPR_TIMESPAN))) <= 0) { |
| 913 | return 0; |
| 914 | } |
| 915 | timeout = gpr_time_sub(deadline, now); |
| 916 | return gpr_time_to_millis(gpr_time_add( |
| 917 | timeout, gpr_time_from_nanos(GPR_NS_PER_MS - 1, GPR_TIMESPAN))); |
| 918 | } |
| 919 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 920 | static void set_ready(grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_closure **st) { |
| 921 | /* only one set_ready can be active at once (but there may be a racing |
| 922 | notify_on) */ |
| 923 | gpr_mu_lock(&fd->mu); |
| 924 | set_ready_locked(exec_ctx, fd, st); |
| 925 | gpr_mu_unlock(&fd->mu); |
| 926 | } |
| 927 | |
| 928 | static void fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) { |
| 929 | set_ready(exec_ctx, fd, &fd->read_closure); |
| 930 | } |
| 931 | |
| 932 | static void fd_become_writable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) { |
| 933 | set_ready(exec_ctx, fd, &fd->write_closure); |
| 934 | } |
| 935 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 936 | #define GRPC_EPOLL_MAX_EVENTS 1000 |
| 937 | static void pollset_work_and_unlock(grpc_exec_ctx *exec_ctx, |
| 938 | grpc_pollset *pollset, int timeout_ms, |
| 939 | sigset_t *sig_mask) { |
| 940 | struct epoll_event ep_ev[GRPC_EPOLL_MAX_EVENTS]; |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 941 | int epoll_fd = -1; |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 942 | int ep_rv; |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 943 | gpr_log(GPR_DEBUG, "pollset_work_and_unlock: Entering.."); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 944 | GPR_TIMER_BEGIN("pollset_work_and_unlock", 0); |
| 945 | |
| 946 | /* We need to get the epoll_fd to wait on. The epoll_fd is in inside the |
| 947 | polling island pointed by pollset->polling_island. |
| 948 | Acquire the following locks: |
| 949 | - pollset->mu (which we already have) |
| 950 | - pollset->pi_mu |
| 951 | - pollset->polling_island->mu */ |
| 952 | gpr_mu_lock(&pollset->pi_mu); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 953 | |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 954 | if (pollset->polling_island != NULL) { |
| 955 | pollset->polling_island = |
| 956 | polling_island_update_and_lock(pollset->polling_island, 1, 0); |
| 957 | epoll_fd = pollset->polling_island->epoll_fd; |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 958 | if (pollset->polling_island->fd_cnt == 0) { |
| 959 | gpr_log(GPR_DEBUG, "pollset_work_and_unlock: epoll_fd: %d, No other fds", |
| 960 | epoll_fd); |
| 961 | } |
| 962 | for (size_t i = 0; i < pollset->polling_island->fd_cnt; i++) { |
| 963 | gpr_log(GPR_DEBUG, |
| 964 | "pollset_work_and_unlock: epoll_fd: %d, fd_count: %d, fd[%d]: %d", |
| 965 | epoll_fd, pollset->polling_island->fd_cnt, i, |
| 966 | pollset->polling_island->fds[i]->fd); |
| 967 | } |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 968 | gpr_mu_unlock(&pollset->polling_island->mu); |
| 969 | } |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 970 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 971 | gpr_mu_unlock(&pollset->pi_mu); |
| 972 | gpr_mu_unlock(&pollset->mu); |
| 973 | |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 974 | /* If epoll_fd == -1, this is a blank pollset and does not have any fds yet */ |
| 975 | if (epoll_fd != -1) { |
| 976 | do { |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 977 | gpr_timespec before_epoll = gpr_now(GPR_CLOCK_PRECISE); |
| 978 | gpr_log(GPR_DEBUG, "pollset_work_and_unlock: epoll_wait()...."); |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 979 | ep_rv = epoll_pwait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, timeout_ms, |
| 980 | sig_mask); |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 981 | gpr_timespec after_epoll = gpr_now(GPR_CLOCK_PRECISE); |
| 982 | int dur = gpr_time_to_millis(gpr_time_sub(after_epoll, before_epoll)); |
| 983 | gpr_log(GPR_DEBUG, |
| 984 | "pollset_work_and_unlock: DONE epoll_wait() : %d ms, ep_rv: %d", |
| 985 | dur, ep_rv); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 986 | |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 987 | if (ep_rv < 0) { |
| 988 | if (errno != EINTR) { |
| 989 | /* TODO (sreek) - Check for bad file descriptor error */ |
| 990 | gpr_log(GPR_ERROR, "epoll_pwait() failed: %s", strerror(errno)); |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 991 | } else { |
| 992 | gpr_log(GPR_DEBUG, "pollset_work_and_unlock: 0-timeout epoll_wait()"); |
| 993 | ep_rv = epoll_wait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, 0); |
| 994 | gpr_log(GPR_DEBUG, "pollset_work_and_unlock: ep_rv: %d", ep_rv); |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 995 | } |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 996 | } |
| 997 | |
| 998 | int i; |
| 999 | for (i = 0; i < ep_rv; ++i) { |
| 1000 | grpc_fd *fd = ep_ev[i].data.ptr; |
| 1001 | int cancel = ep_ev[i].events & (EPOLLERR | EPOLLHUP); |
| 1002 | int read_ev = ep_ev[i].events & (EPOLLIN | EPOLLPRI); |
| 1003 | int write_ev = ep_ev[i].events & EPOLLOUT; |
| 1004 | if (fd == NULL) { |
| 1005 | grpc_wakeup_fd_consume_wakeup(&grpc_global_wakeup_fd); |
| 1006 | } else { |
| 1007 | if (read_ev || cancel) { |
| 1008 | fd_become_readable(exec_ctx, fd); |
| 1009 | } |
| 1010 | if (write_ev || cancel) { |
| 1011 | fd_become_writable(exec_ctx, fd); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | } |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 1015 | } while (ep_rv == GRPC_EPOLL_MAX_EVENTS); |
| 1016 | } |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 1017 | gpr_log(GPR_DEBUG, "pollset_work_and_unlock: Leaving.."); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 1018 | GPR_TIMER_END("pollset_work_and_unlock", 0); |
| 1019 | } |
| 1020 | |
| 1021 | /* Release the reference to pollset->polling_island and set it to NULL. |
| 1022 | pollset->mu must be held */ |
| 1023 | static void pollset_release_polling_island_locked(grpc_pollset *pollset) { |
| 1024 | gpr_mu_lock(&pollset->pi_mu); |
| 1025 | if (pollset->polling_island) { |
| 1026 | pollset->polling_island = |
| 1027 | polling_island_update_and_lock(pollset->polling_island, 1, 0); |
| 1028 | polling_island_unref_and_unlock(pollset->polling_island, 1); |
| 1029 | pollset->polling_island = NULL; |
| 1030 | } |
| 1031 | gpr_mu_unlock(&pollset->pi_mu); |
| 1032 | } |
| 1033 | |
| 1034 | static void finish_shutdown_locked(grpc_exec_ctx *exec_ctx, |
| 1035 | grpc_pollset *pollset) { |
| 1036 | /* The pollset cannot have any workers if we are at this stage */ |
| 1037 | GPR_ASSERT(!pollset_has_workers(pollset)); |
| 1038 | |
| 1039 | pollset->finish_shutdown_called = true; |
| 1040 | pollset_release_polling_island_locked(pollset); |
| 1041 | |
| 1042 | grpc_exec_ctx_enqueue(exec_ctx, pollset->shutdown_done, true, NULL); |
| 1043 | } |
| 1044 | |
| 1045 | /* pollset->mu lock must be held by the caller before calling this */ |
| 1046 | static void pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 1047 | grpc_closure *closure) { |
| 1048 | GPR_TIMER_BEGIN("pollset_shutdown", 0); |
| 1049 | GPR_ASSERT(!pollset->shutting_down); |
| 1050 | pollset->shutting_down = true; |
| 1051 | pollset->shutdown_done = closure; |
| 1052 | pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST); |
| 1053 | |
| 1054 | /* If the pollset has any workers, we cannot call finish_shutdown_locked() |
| 1055 | because it would release the underlying polling island. In such a case, we |
| 1056 | let the last worker call finish_shutdown_locked() from pollset_work() */ |
| 1057 | if (!pollset_has_workers(pollset)) { |
| 1058 | GPR_ASSERT(!pollset->finish_shutdown_called); |
| 1059 | GPR_TIMER_MARK("pollset_shutdown.finish_shutdown_locked", 0); |
| 1060 | finish_shutdown_locked(exec_ctx, pollset); |
| 1061 | } |
| 1062 | GPR_TIMER_END("pollset_shutdown", 0); |
| 1063 | } |
| 1064 | |
| 1065 | /* TODO(sreek) Is pollset_shutdown() guranteed to be called before this? */ |
| 1066 | static void pollset_destroy(grpc_pollset *pollset) { |
| 1067 | GPR_ASSERT(!pollset_has_workers(pollset)); |
| 1068 | gpr_mu_destroy(&pollset->pi_mu); |
| 1069 | gpr_mu_destroy(&pollset->mu); |
| 1070 | } |
| 1071 | |
| 1072 | static void pollset_reset(grpc_pollset *pollset) { |
| 1073 | GPR_ASSERT(pollset->shutting_down); |
| 1074 | GPR_ASSERT(!pollset_has_workers(pollset)); |
| 1075 | pollset->shutting_down = false; |
| 1076 | pollset->finish_shutdown_called = false; |
| 1077 | pollset->kicked_without_pollers = false; |
| 1078 | /* TODO(sreek) - Should pollset->shutdown closure be set to NULL here? */ |
| 1079 | pollset_release_polling_island_locked(pollset); |
| 1080 | } |
| 1081 | |
| 1082 | /* pollset->mu lock must be held by the caller before calling this. |
| 1083 | The function pollset_work() may temporarily release the lock (pollset->mu) |
| 1084 | during the course of its execution but it will always re-acquire the lock and |
| 1085 | ensure that it is held by the time the function returns */ |
| 1086 | static void pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 1087 | grpc_pollset_worker **worker_hdl, gpr_timespec now, |
| 1088 | gpr_timespec deadline) { |
| 1089 | GPR_TIMER_BEGIN("pollset_work", 0); |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 1090 | gpr_log(GPR_DEBUG, "pollset_work: enter"); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 1091 | int timeout_ms = poll_deadline_to_millis_timeout(deadline, now); |
| 1092 | |
| 1093 | sigset_t new_mask; |
| 1094 | sigset_t orig_mask; |
| 1095 | |
| 1096 | grpc_pollset_worker worker; |
| 1097 | worker.next = worker.prev = NULL; |
| 1098 | worker.kicked_specifically = 0; |
| 1099 | worker.pt_id = pthread_self(); |
| 1100 | |
| 1101 | *worker_hdl = &worker; |
| 1102 | |
| 1103 | if (pollset->kicked_without_pollers) { |
| 1104 | /* If the pollset was kicked without pollers, pretend that the current |
| 1105 | worker got the kick and skip polling. A kick indicates that there is some |
| 1106 | work that needs attention like an event on the completion queue or an |
| 1107 | alarm */ |
| 1108 | GPR_TIMER_MARK("pollset_work.kicked_without_pollers", 0); |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 1109 | gpr_log(GPR_INFO, "pollset_work: kicked without pollers.."); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 1110 | pollset->kicked_without_pollers = 0; |
| 1111 | } else if (!pollset->shutting_down) { |
| 1112 | sigemptyset(&new_mask); |
| 1113 | sigaddset(&new_mask, SIGUSR1); |
| 1114 | pthread_sigmask(SIG_BLOCK, &new_mask, &orig_mask); |
| 1115 | sigdelset(&orig_mask, SIGUSR1); |
| 1116 | |
| 1117 | push_front_worker(pollset, &worker); |
| 1118 | |
| 1119 | pollset_work_and_unlock(exec_ctx, pollset, timeout_ms, &orig_mask); |
| 1120 | grpc_exec_ctx_flush(exec_ctx); |
| 1121 | |
| 1122 | gpr_mu_lock(&pollset->mu); |
| 1123 | remove_worker(pollset, &worker); |
| 1124 | } |
| 1125 | |
| 1126 | /* If we are the last worker on the pollset (i.e pollset_has_workers() is |
| 1127 | false at this point) and the pollset is shutting down, we may have to |
| 1128 | finish the shutdown process by calling finish_shutdown_locked(). |
| 1129 | See pollset_shutdown() for more details. |
| 1130 | |
| 1131 | Note: Continuing to access pollset here is safe; it is the caller's |
| 1132 | responsibility to not destroy a pollset when it has outstanding calls to |
| 1133 | pollset_work() */ |
| 1134 | if (pollset->shutting_down && !pollset_has_workers(pollset) && |
| 1135 | !pollset->finish_shutdown_called) { |
| 1136 | GPR_TIMER_MARK("pollset_work.finish_shutdown_locked", 0); |
| 1137 | finish_shutdown_locked(exec_ctx, pollset); |
| 1138 | |
| 1139 | gpr_mu_unlock(&pollset->mu); |
| 1140 | grpc_exec_ctx_flush(exec_ctx); |
| 1141 | gpr_mu_lock(&pollset->mu); |
| 1142 | } |
| 1143 | |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 1144 | gpr_log(GPR_DEBUG, "pollset_work(): leaving"); |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 1145 | *worker_hdl = NULL; |
| 1146 | GPR_TIMER_END("pollset_work", 0); |
| 1147 | } |
| 1148 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1149 | static void pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 1150 | grpc_fd *fd) { |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 1151 | gpr_log(GPR_DEBUG, "pollset_add_fd: pollset: %p, fd: %d", pollset, fd->fd); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1152 | /* TODO sreek - Check if we need to get a pollset->mu lock here */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 1153 | gpr_mu_lock(&pollset->pi_mu); |
| 1154 | gpr_mu_lock(&fd->pi_mu); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1155 | |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 1156 | polling_island *pi_new = NULL; |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1157 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 1158 | /* 1) If fd->polling_island and pollset->polling_island are both non-NULL and |
| 1159 | * equal, do nothing. |
| 1160 | * 2) If fd->polling_island and pollset->polling_island are both NULL, create |
| 1161 | * a new polling island (with a refcount of 2) and make the polling_island |
| 1162 | * fields in both fd and pollset to point to the new island |
| 1163 | * 3) If one of fd->polling_island or pollset->polling_island is NULL, update |
| 1164 | * the NULL polling_island field to point to the non-NULL polling_island |
| 1165 | * field (ensure that the refcount on the polling island is incremented by |
| 1166 | * 1 to account for the newly added reference) |
| 1167 | * 4) Finally, if fd->polling_island and pollset->polling_island are non-NULL |
| 1168 | * and different, merge both the polling islands and update the |
| 1169 | * polling_island fields in both fd and pollset to point to the merged |
| 1170 | * polling island. |
| 1171 | */ |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 1172 | if (fd->polling_island == pollset->polling_island) { |
| 1173 | pi_new = fd->polling_island; |
| 1174 | if (pi_new == NULL) { |
| 1175 | pi_new = polling_island_create(fd, 2); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1176 | } |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 1177 | } else if (fd->polling_island == NULL) { |
| 1178 | pi_new = polling_island_update_and_lock(pollset->polling_island, 1, 1); |
Sree Kuchibhotla | 79a6233 | 2016-06-04 14:01:03 -0700 | [diff] [blame^] | 1179 | polling_island_add_fds_locked(pollset->polling_island, &fd, 1, true); |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 1180 | gpr_mu_unlock(&pi_new->mu); |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 1181 | } else if (pollset->polling_island == NULL) { |
| 1182 | pi_new = polling_island_update_and_lock(fd->polling_island, 1, 1); |
Sree Kuchibhotla | 88ee12f | 2016-06-03 19:26:48 -0700 | [diff] [blame] | 1183 | gpr_mu_unlock(&pi_new->mu); |
Sree Kuchibhotla | 5098f91 | 2016-05-31 10:58:17 -0700 | [diff] [blame] | 1184 | } else { |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 1185 | pi_new = polling_island_merge(fd->polling_island, pollset->polling_island); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 1188 | fd->polling_island = pollset->polling_island = pi_new; |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1189 | |
Sree Kuchibhotla | 9442bab | 2016-05-20 17:54:06 -0700 | [diff] [blame] | 1190 | gpr_mu_unlock(&fd->pi_mu); |
| 1191 | gpr_mu_unlock(&pollset->pi_mu); |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1194 | /******************************************************************************* |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 1195 | * Pollset-set Definitions |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1196 | */ |
| 1197 | |
| 1198 | static grpc_pollset_set *pollset_set_create(void) { |
| 1199 | grpc_pollset_set *pollset_set = gpr_malloc(sizeof(*pollset_set)); |
| 1200 | memset(pollset_set, 0, sizeof(*pollset_set)); |
| 1201 | gpr_mu_init(&pollset_set->mu); |
| 1202 | return pollset_set; |
| 1203 | } |
| 1204 | |
| 1205 | static void pollset_set_destroy(grpc_pollset_set *pollset_set) { |
| 1206 | size_t i; |
| 1207 | gpr_mu_destroy(&pollset_set->mu); |
| 1208 | for (i = 0; i < pollset_set->fd_count; i++) { |
| 1209 | GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set"); |
| 1210 | } |
| 1211 | gpr_free(pollset_set->pollsets); |
| 1212 | gpr_free(pollset_set->pollset_sets); |
| 1213 | gpr_free(pollset_set->fds); |
| 1214 | gpr_free(pollset_set); |
| 1215 | } |
| 1216 | |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 1217 | static void pollset_set_add_fd(grpc_exec_ctx *exec_ctx, |
| 1218 | grpc_pollset_set *pollset_set, grpc_fd *fd) { |
| 1219 | size_t i; |
| 1220 | gpr_mu_lock(&pollset_set->mu); |
| 1221 | if (pollset_set->fd_count == pollset_set->fd_capacity) { |
| 1222 | pollset_set->fd_capacity = GPR_MAX(8, 2 * pollset_set->fd_capacity); |
| 1223 | pollset_set->fds = gpr_realloc( |
| 1224 | pollset_set->fds, pollset_set->fd_capacity * sizeof(*pollset_set->fds)); |
| 1225 | } |
| 1226 | GRPC_FD_REF(fd, "pollset_set"); |
| 1227 | pollset_set->fds[pollset_set->fd_count++] = fd; |
| 1228 | for (i = 0; i < pollset_set->pollset_count; i++) { |
| 1229 | pollset_add_fd(exec_ctx, pollset_set->pollsets[i], fd); |
| 1230 | } |
| 1231 | for (i = 0; i < pollset_set->pollset_set_count; i++) { |
| 1232 | pollset_set_add_fd(exec_ctx, pollset_set->pollset_sets[i], fd); |
| 1233 | } |
| 1234 | gpr_mu_unlock(&pollset_set->mu); |
| 1235 | } |
| 1236 | |
| 1237 | static void pollset_set_del_fd(grpc_exec_ctx *exec_ctx, |
| 1238 | grpc_pollset_set *pollset_set, grpc_fd *fd) { |
| 1239 | size_t i; |
| 1240 | gpr_mu_lock(&pollset_set->mu); |
| 1241 | for (i = 0; i < pollset_set->fd_count; i++) { |
| 1242 | if (pollset_set->fds[i] == fd) { |
| 1243 | pollset_set->fd_count--; |
| 1244 | GPR_SWAP(grpc_fd *, pollset_set->fds[i], |
| 1245 | pollset_set->fds[pollset_set->fd_count]); |
| 1246 | GRPC_FD_UNREF(fd, "pollset_set"); |
| 1247 | break; |
| 1248 | } |
| 1249 | } |
| 1250 | for (i = 0; i < pollset_set->pollset_set_count; i++) { |
| 1251 | pollset_set_del_fd(exec_ctx, pollset_set->pollset_sets[i], fd); |
| 1252 | } |
| 1253 | gpr_mu_unlock(&pollset_set->mu); |
| 1254 | } |
| 1255 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1256 | static void pollset_set_add_pollset(grpc_exec_ctx *exec_ctx, |
| 1257 | grpc_pollset_set *pollset_set, |
| 1258 | grpc_pollset *pollset) { |
| 1259 | size_t i, j; |
| 1260 | gpr_mu_lock(&pollset_set->mu); |
| 1261 | if (pollset_set->pollset_count == pollset_set->pollset_capacity) { |
| 1262 | pollset_set->pollset_capacity = |
| 1263 | GPR_MAX(8, 2 * pollset_set->pollset_capacity); |
| 1264 | pollset_set->pollsets = |
| 1265 | gpr_realloc(pollset_set->pollsets, pollset_set->pollset_capacity * |
| 1266 | sizeof(*pollset_set->pollsets)); |
| 1267 | } |
| 1268 | pollset_set->pollsets[pollset_set->pollset_count++] = pollset; |
| 1269 | for (i = 0, j = 0; i < pollset_set->fd_count; i++) { |
| 1270 | if (fd_is_orphaned(pollset_set->fds[i])) { |
| 1271 | GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set"); |
| 1272 | } else { |
| 1273 | pollset_add_fd(exec_ctx, pollset, pollset_set->fds[i]); |
| 1274 | pollset_set->fds[j++] = pollset_set->fds[i]; |
| 1275 | } |
| 1276 | } |
| 1277 | pollset_set->fd_count = j; |
| 1278 | gpr_mu_unlock(&pollset_set->mu); |
| 1279 | } |
| 1280 | |
| 1281 | static void pollset_set_del_pollset(grpc_exec_ctx *exec_ctx, |
| 1282 | grpc_pollset_set *pollset_set, |
| 1283 | grpc_pollset *pollset) { |
| 1284 | size_t i; |
| 1285 | gpr_mu_lock(&pollset_set->mu); |
| 1286 | for (i = 0; i < pollset_set->pollset_count; i++) { |
| 1287 | if (pollset_set->pollsets[i] == pollset) { |
| 1288 | pollset_set->pollset_count--; |
| 1289 | GPR_SWAP(grpc_pollset *, pollset_set->pollsets[i], |
| 1290 | pollset_set->pollsets[pollset_set->pollset_count]); |
| 1291 | break; |
| 1292 | } |
| 1293 | } |
| 1294 | gpr_mu_unlock(&pollset_set->mu); |
| 1295 | } |
| 1296 | |
| 1297 | static void pollset_set_add_pollset_set(grpc_exec_ctx *exec_ctx, |
| 1298 | grpc_pollset_set *bag, |
| 1299 | grpc_pollset_set *item) { |
| 1300 | size_t i, j; |
| 1301 | gpr_mu_lock(&bag->mu); |
| 1302 | if (bag->pollset_set_count == bag->pollset_set_capacity) { |
| 1303 | bag->pollset_set_capacity = GPR_MAX(8, 2 * bag->pollset_set_capacity); |
| 1304 | bag->pollset_sets = |
| 1305 | gpr_realloc(bag->pollset_sets, |
| 1306 | bag->pollset_set_capacity * sizeof(*bag->pollset_sets)); |
| 1307 | } |
| 1308 | bag->pollset_sets[bag->pollset_set_count++] = item; |
| 1309 | for (i = 0, j = 0; i < bag->fd_count; i++) { |
| 1310 | if (fd_is_orphaned(bag->fds[i])) { |
| 1311 | GRPC_FD_UNREF(bag->fds[i], "pollset_set"); |
| 1312 | } else { |
| 1313 | pollset_set_add_fd(exec_ctx, item, bag->fds[i]); |
| 1314 | bag->fds[j++] = bag->fds[i]; |
| 1315 | } |
| 1316 | } |
| 1317 | bag->fd_count = j; |
| 1318 | gpr_mu_unlock(&bag->mu); |
| 1319 | } |
| 1320 | |
| 1321 | static void pollset_set_del_pollset_set(grpc_exec_ctx *exec_ctx, |
| 1322 | grpc_pollset_set *bag, |
| 1323 | grpc_pollset_set *item) { |
| 1324 | size_t i; |
| 1325 | gpr_mu_lock(&bag->mu); |
| 1326 | for (i = 0; i < bag->pollset_set_count; i++) { |
| 1327 | if (bag->pollset_sets[i] == item) { |
| 1328 | bag->pollset_set_count--; |
| 1329 | GPR_SWAP(grpc_pollset_set *, bag->pollset_sets[i], |
| 1330 | bag->pollset_sets[bag->pollset_set_count]); |
| 1331 | break; |
| 1332 | } |
| 1333 | } |
| 1334 | gpr_mu_unlock(&bag->mu); |
| 1335 | } |
| 1336 | |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1337 | /******************************************************************************* |
Sree Kuchibhotla | 0bcbd79 | 2016-06-01 15:43:03 -0700 | [diff] [blame] | 1338 | * Event engine binding |
Sree Kuchibhotla | f448c34 | 2016-05-19 10:51:24 -0700 | [diff] [blame] | 1339 | */ |
| 1340 | |
| 1341 | static void shutdown_engine(void) { |
| 1342 | fd_global_shutdown(); |
| 1343 | pollset_global_shutdown(); |
| 1344 | } |
| 1345 | |
| 1346 | static const grpc_event_engine_vtable vtable = { |
| 1347 | .pollset_size = sizeof(grpc_pollset), |
| 1348 | |
| 1349 | .fd_create = fd_create, |
| 1350 | .fd_wrapped_fd = fd_wrapped_fd, |
| 1351 | .fd_orphan = fd_orphan, |
| 1352 | .fd_shutdown = fd_shutdown, |
| 1353 | .fd_notify_on_read = fd_notify_on_read, |
| 1354 | .fd_notify_on_write = fd_notify_on_write, |
| 1355 | |
| 1356 | .pollset_init = pollset_init, |
| 1357 | .pollset_shutdown = pollset_shutdown, |
| 1358 | .pollset_reset = pollset_reset, |
| 1359 | .pollset_destroy = pollset_destroy, |
| 1360 | .pollset_work = pollset_work, |
| 1361 | .pollset_kick = pollset_kick, |
| 1362 | .pollset_add_fd = pollset_add_fd, |
| 1363 | |
| 1364 | .pollset_set_create = pollset_set_create, |
| 1365 | .pollset_set_destroy = pollset_set_destroy, |
| 1366 | .pollset_set_add_pollset = pollset_set_add_pollset, |
| 1367 | .pollset_set_del_pollset = pollset_set_del_pollset, |
| 1368 | .pollset_set_add_pollset_set = pollset_set_add_pollset_set, |
| 1369 | .pollset_set_del_pollset_set = pollset_set_del_pollset_set, |
| 1370 | .pollset_set_add_fd = pollset_set_add_fd, |
| 1371 | .pollset_set_del_fd = pollset_set_del_fd, |
| 1372 | |
| 1373 | .kick_poller = kick_poller, |
| 1374 | |
| 1375 | .shutdown_engine = shutdown_engine, |
| 1376 | }; |
| 1377 | |
| 1378 | const grpc_event_engine_vtable *grpc_init_epoll_linux(void) { |
| 1379 | fd_global_init(); |
| 1380 | pollset_global_init(); |
| 1381 | polling_island_global_init(); |
| 1382 | return &vtable; |
| 1383 | } |
| 1384 | |
| 1385 | #endif |