blob: d5aac96fa4684d2a4af69dcf933e465aa045d5af [file] [log] [blame]
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001/*
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
Sree Kuchibhotla4c11a202016-06-06 09:23:25 -070038#include "src/core/lib/iomgr/ev_epoll_linux.h"
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -070039
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
61struct polling_island;
62
63/*******************************************************************************
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -070064 * Fd Declarations
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -070065 */
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -070066struct grpc_fd {
67 int fd;
68 /* refst format:
Sree Kuchibhotla5098f912016-05-31 10:58:17 -070069 bit 0 : 1=Active / 0=Orphaned
70 bits 1-n : refcount
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -070071 Ref/Unref by two to avoid altering the orphaned bit */
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -070072 gpr_atm refst;
73
74 gpr_mu mu;
Sree Kuchibhotla79a62332016-06-04 14:01:03 -070075
76 /* Indicates that the fd is shutdown and that any pending read/write closures
77 should fail */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -070078 bool shutdown;
Sree Kuchibhotla79a62332016-06-04 14:01:03 -070079
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 Kuchibhotlaf448c342016-05-19 10:51:24 -070083
84 grpc_closure *read_closure;
85 grpc_closure *write_closure;
86
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -070087 /* The polling island to which this fd belongs to and the mutex protecting the
88 the field */
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -070089 gpr_mu pi_mu;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -070090 struct polling_island *polling_island;
91
92 struct grpc_fd *freelist_next;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -070093 grpc_closure *on_done_closure;
94
95 grpc_iomgr_object iomgr_object;
96};
97
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -070098/* Reference counting for fds */
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -070099#ifdef GRPC_FD_REF_COUNT_DEBUG
100static void fd_ref(grpc_fd *fd, const char *reason, const char *file, int line);
101static 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
106static void fd_ref(grpc_fd *fd);
107static 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
112static void fd_global_init(void);
113static void fd_global_shutdown(void);
114
115#define CLOSURE_NOT_READY ((grpc_closure *)0)
116#define CLOSURE_READY ((grpc_closure *)1)
117
118/*******************************************************************************
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700119 * Polling-island Declarations
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700120 */
121typedef struct polling_island {
122 gpr_mu mu;
123 int ref_cnt;
124
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700125 /* 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 Kuchibhotlaf448c342016-05-19 10:51:24 -0700128 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 Kuchibhotlaf448c342016-05-19 10:51:24 -0700140 free list */
141 struct polling_island *next_free;
142} polling_island;
143
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700144/*******************************************************************************
145 * Pollset Declarations
146 */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700147struct grpc_pollset_worker {
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -0700148 pthread_t pt_id; /* Thread id of this worker */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700149 struct grpc_pollset_worker *next;
150 struct grpc_pollset_worker *prev;
151};
152
153struct grpc_pollset {
154 gpr_mu mu;
155 grpc_pollset_worker root_worker;
156 bool kicked_without_pollers;
157
158 bool shutting_down; /* Is the pollset shutting down ? */
159 bool finish_shutdown_called; /* Is the 'finish_shutdown_locked()' called ? */
160 grpc_closure *shutdown_done; /* Called after after shutdown is complete */
161
162 /* The polling island to which this pollset belongs to and the mutex
163 protecting the field */
164 gpr_mu pi_mu;
165 struct polling_island *polling_island;
166};
167
168/*******************************************************************************
169 * Pollset-set Declarations
170 */
171struct grpc_pollset_set {
172 gpr_mu mu;
173
174 size_t pollset_count;
175 size_t pollset_capacity;
176 grpc_pollset **pollsets;
177
178 size_t pollset_set_count;
179 size_t pollset_set_capacity;
180 struct grpc_pollset_set **pollset_sets;
181
182 size_t fd_count;
183 size_t fd_capacity;
184 grpc_fd **fds;
185};
186
187/*******************************************************************************
188 * Polling-island Definitions
189 */
190
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700191/* Polling island freelist */
192static gpr_mu g_pi_freelist_mu;
193static polling_island *g_pi_freelist = NULL;
194
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700195/* The caller is expected to hold pi->mu lock before calling this function */
196static void polling_island_add_fds_locked(polling_island *pi, grpc_fd **fds,
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700197 size_t fd_count, bool add_fd_refs) {
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700198 int err;
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700199 size_t i;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700200 struct epoll_event ev;
201
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700202 for (i = 0; i < fd_count; i++) {
203 ev.events = (uint32_t)(EPOLLIN | EPOLLOUT | EPOLLET);
204 ev.data.ptr = fds[i];
205 err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_ADD, fds[i]->fd, &ev);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700206
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700207 if (err < 0) {
208 if (errno != EEXIST) {
209 /* TODO: sreek - We need a better way to bubble up this error instead of
210 just logging a message */
211 gpr_log(GPR_ERROR, "epoll_ctl add for fd: %d failed with error: %s",
212 fds[i]->fd, strerror(errno));
213 }
214
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700215 continue;
216 }
217
218 if (pi->fd_cnt == pi->fd_capacity) {
219 pi->fd_capacity = GPR_MAX(pi->fd_capacity + 8, pi->fd_cnt * 3 / 2);
220 pi->fds = gpr_realloc(pi->fds, sizeof(grpc_fd *) * pi->fd_capacity);
221 }
222
223 pi->fds[pi->fd_cnt++] = fds[i];
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700224 if (add_fd_refs) {
225 GRPC_FD_REF(fds[i], "polling_island");
226 }
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700227 }
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700228}
229
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700230/* The caller is expected to hold pi->mu lock before calling this function */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700231static void polling_island_remove_all_fds_locked(polling_island *pi,
232 bool remove_fd_refs) {
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700233 int err;
234 size_t i;
235
236 for (i = 0; i < pi->fd_cnt; i++) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700237 err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_DEL, pi->fds[i]->fd, NULL);
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700238 if (err < 0 && errno != ENOENT) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700239 /* TODO: sreek - We need a better way to bubble up this error instead of
Sree Kuchibhotlaad162ba2016-06-06 16:23:37 -0700240 * just logging a message */
241 gpr_log(GPR_ERROR, "epoll_ctl deleting fds[%d]: %d failed with error: %s",
242 i, pi->fds[i]->fd, strerror(errno));
243 }
244
245 if (remove_fd_refs) {
246 GRPC_FD_UNREF(pi->fds[i], "polling_island");
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700247 }
248 }
249
250 pi->fd_cnt = 0;
251}
252
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700253/* The caller is expected to hold pi->mu lock before calling this function */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700254static void polling_island_remove_fd_locked(polling_island *pi, grpc_fd *fd,
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700255 bool is_fd_closed) {
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700256 int err;
257 size_t i;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700258
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700259 /* If fd is already closed, then it would have been automatically been removed
260 from the epoll set */
261 if (!is_fd_closed) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700262 err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_DEL, fd->fd, NULL);
263 if (err < 0 && errno != ENOENT) {
Sree Kuchibhotlaad162ba2016-06-06 16:23:37 -0700264 gpr_log(GPR_ERROR, "epoll_ctl deleting fd: %d failed with error; %s",
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700265 fd->fd, strerror(errno));
266 }
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700267 }
268
269 for (i = 0; i < pi->fd_cnt; i++) {
270 if (pi->fds[i] == fd) {
271 pi->fds[i] = pi->fds[--pi->fd_cnt];
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700272 GRPC_FD_UNREF(fd, "polling_island");
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700273 break;
274 }
275 }
276}
277
278static polling_island *polling_island_create(grpc_fd *initial_fd,
279 int initial_ref_cnt) {
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700280 polling_island *pi = NULL;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700281 struct epoll_event ev;
282 int err;
283
284 /* Try to get one from the polling island freelist */
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700285 gpr_mu_lock(&g_pi_freelist_mu);
286 if (g_pi_freelist != NULL) {
287 pi = g_pi_freelist;
288 g_pi_freelist = g_pi_freelist->next_free;
289 pi->next_free = NULL;
290 }
291 gpr_mu_unlock(&g_pi_freelist_mu);
292
293 /* Create new polling island if we could not get one from the free list */
294 if (pi == NULL) {
295 pi = gpr_malloc(sizeof(*pi));
296 gpr_mu_init(&pi->mu);
297 pi->fd_cnt = 0;
298 pi->fd_capacity = 0;
299 pi->fds = NULL;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700300 }
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700301
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700302 pi->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
303 if (pi->epoll_fd < 0) {
304 gpr_log(GPR_ERROR, "epoll_create1() failed with error: %s",
305 strerror(errno));
306 }
307 GPR_ASSERT(pi->epoll_fd >= 0);
308
309 ev.events = (uint32_t)(EPOLLIN | EPOLLET);
310 ev.data.ptr = NULL;
311 err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_ADD,
312 GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd), &ev);
313 if (err < 0) {
314 gpr_log(GPR_ERROR,
315 "Failed to add grpc_global_wake_up_fd (%d) to the epoll set "
316 "(epoll_fd: %d) with error: %s",
317 GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd), pi->epoll_fd,
318 strerror(errno));
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700319 }
320
321 pi->ref_cnt = initial_ref_cnt;
322 pi->merged_to = NULL;
323 pi->next_free = NULL;
324
325 if (initial_fd != NULL) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700326 /* It is not really needed to get the pi->mu lock here. If this is a newly
327 created polling island (or one that we got from the freelist), no one
328 else would be holding a lock to it anyway */
329 gpr_mu_lock(&pi->mu);
330 polling_island_add_fds_locked(pi, &initial_fd, 1, true);
331 gpr_mu_unlock(&pi->mu);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700332 }
333
334 return pi;
335}
336
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700337static void polling_island_delete(polling_island *pi) {
338 GPR_ASSERT(pi->ref_cnt == 0);
339 GPR_ASSERT(pi->fd_cnt == 0);
340
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700341 close(pi->epoll_fd);
342 pi->epoll_fd = -1;
343
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700344 pi->merged_to = NULL;
345
346 gpr_mu_lock(&g_pi_freelist_mu);
347 pi->next_free = g_pi_freelist;
348 g_pi_freelist = pi;
349 gpr_mu_unlock(&g_pi_freelist_mu);
350}
351
352void polling_island_unref_and_unlock(polling_island *pi, int unref_by) {
353 pi->ref_cnt -= unref_by;
354 int ref_cnt = pi->ref_cnt;
355 GPR_ASSERT(ref_cnt >= 0);
356
357 gpr_mu_unlock(&pi->mu);
358
359 if (ref_cnt == 0) {
360 polling_island_delete(pi);
361 }
362}
363
364polling_island *polling_island_update_and_lock(polling_island *pi, int unref_by,
365 int add_ref_by) {
366 polling_island *next = NULL;
367 gpr_mu_lock(&pi->mu);
368 while (pi->merged_to != NULL) {
369 next = pi->merged_to;
370 polling_island_unref_and_unlock(pi, unref_by);
371 pi = next;
372 gpr_mu_lock(&pi->mu);
373 }
374
375 pi->ref_cnt += add_ref_by;
376 return pi;
377}
378
379void polling_island_pair_update_and_lock(polling_island **p,
380 polling_island **q) {
381 polling_island *pi_1 = *p;
382 polling_island *pi_2 = *q;
383 polling_island *temp = NULL;
384 bool pi_1_locked = false;
385 bool pi_2_locked = false;
386 int num_swaps = 0;
387
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700388 /* Loop until either pi_1 == pi_2 or until we acquired locks on both pi_1
389 and pi_2 */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700390 while (pi_1 != pi_2 && !(pi_1_locked && pi_2_locked)) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700391 /* The following assertions are true at this point:
392 - pi_1 != pi_2 (else, the while loop would have exited)
393 - pi_1 MAY be locked
394 - pi_2 is NOT locked */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700395
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700396 /* To maintain lock order consistency, always lock polling_island node with
397 lower address first.
398 First, make sure pi_1 < pi_2 before proceeding any further. If it turns
399 out that pi_1 > pi_2, unlock pi_1 if locked (because pi_2 is not locked
400 at this point and having pi_1 locked would violate the lock order) and
401 swap pi_1 and pi_2 so that pi_1 becomes less than pi_2 */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700402 if (pi_1 > pi_2) {
403 if (pi_1_locked) {
404 gpr_mu_unlock(&pi_1->mu);
405 pi_1_locked = false;
406 }
407
408 GPR_SWAP(polling_island *, pi_1, pi_2);
409 num_swaps++;
410 }
411
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700412 /* The following assertions are true at this point:
413 - pi_1 != pi_2
414 - pi_1 < pi_2 (address of pi_1 is less than that of pi_2)
415 - pi_1 MAYBE locked
416 - pi_2 is NOT locked */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700417
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700418 /* Lock pi_1 (if pi_1 is pointing to the terminal node in the list) */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700419 if (!pi_1_locked) {
420 gpr_mu_lock(&pi_1->mu);
421 pi_1_locked = true;
422
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700423 /* If pi_1 is not terminal node (i.e pi_1->merged_to != NULL), we are not
424 done locking this polling_island yet. Release the lock on this node and
425 advance pi_1 to the next node in the list; and go to the beginning of
426 the loop (we can't proceed to locking pi_2 unless we locked pi_1 first)
427 */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700428 if (pi_1->merged_to != NULL) {
429 temp = pi_1->merged_to;
430 polling_island_unref_and_unlock(pi_1, 1);
431 pi_1 = temp;
432 pi_1_locked = false;
433
434 continue;
435 }
436 }
437
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700438 /* The following assertions are true at this point:
439 - pi_1 is locked
440 - pi_2 is unlocked
441 - pi_1 != pi_2 */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700442
443 gpr_mu_lock(&pi_2->mu);
444 pi_2_locked = true;
445
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700446 /* If pi_2 is not terminal node, we are not done locking this polling_island
447 yet. Release the lock and update pi_2 to the next node in the list */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700448 if (pi_2->merged_to != NULL) {
449 temp = pi_2->merged_to;
450 polling_island_unref_and_unlock(pi_2, 1);
451 pi_2 = temp;
452 pi_2_locked = false;
453 }
454 }
455
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700456 /* At this point, either pi_1 == pi_2 AND/OR we got both locks */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700457 if (pi_1 == pi_2) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700458 /* We may or may not have gotten the lock. If we didn't, walk the rest of
459 the polling_island list and get the lock */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700460 GPR_ASSERT(pi_1_locked || (!pi_1_locked && !pi_2_locked));
461 if (!pi_1_locked) {
462 pi_1 = pi_2 = polling_island_update_and_lock(pi_1, 2, 0);
463 }
464 } else {
465 GPR_ASSERT(pi_1_locked && pi_2_locked);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700466 /* If we swapped pi_1 and pi_2 odd number of times, do one more swap so that
467 pi_1 and pi_2 point to the same polling_island lists they started off
468 with at the beginning of this function (i.e *p and *q respectively) */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700469 if (num_swaps % 2 > 0) {
470 GPR_SWAP(polling_island *, pi_1, pi_2);
471 }
472 }
473
474 *p = pi_1;
475 *q = pi_2;
476}
477
478polling_island *polling_island_merge(polling_island *p, polling_island *q) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700479 /* Get locks on both the polling islands */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700480 polling_island_pair_update_and_lock(&p, &q);
481
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -0700482 /* TODO: sreek: Think about this scenario some more */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700483 if (p == q) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700484 /* Nothing needs to be done here */
485 gpr_mu_unlock(&p->mu);
486 return p;
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700487 }
488
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700489 /* Make sure that p points to the polling island with fewer fds than q */
490 if (p->fd_cnt > q->fd_cnt) {
491 GPR_SWAP(polling_island *, p, q);
492 }
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700493
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700494 /* "Merge" p with q i.e move all the fds from p (the polling_island with fewer
495 fds) to q.
496 Note: Not altering the ref counts on the affected fds here because they
497 would effectively remain unchanged */
498 polling_island_add_fds_locked(q, p->fds, p->fd_cnt, false);
499 polling_island_remove_all_fds_locked(p, false);
500
501 /* The merged polling island inherits all the ref counts of the island merging
502 with it */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700503 q->ref_cnt += p->ref_cnt;
504
505 gpr_mu_unlock(&p->mu);
506 gpr_mu_unlock(&q->mu);
507
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700508 return q;
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700509}
510
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700511static void polling_island_global_init() {
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700512 gpr_mu_init(&g_pi_freelist_mu);
513 g_pi_freelist = NULL;
514}
515
Sree Kuchibhotlad627c102016-06-06 15:49:32 -0700516static void polling_island_global_shutdown() {
517 polling_island *next;
518 gpr_mu_lock(&g_pi_freelist_mu);
519 gpr_mu_unlock(&g_pi_freelist_mu);
520 while (g_pi_freelist != NULL) {
521 next = g_pi_freelist->next_free;
522 gpr_mu_destroy(&g_pi_freelist->mu);
523 gpr_free(g_pi_freelist->fds);
524 gpr_free(g_pi_freelist);
525 g_pi_freelist = next;
526 }
527
528 gpr_mu_destroy(&g_pi_freelist_mu);
529}
530
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700531/*******************************************************************************
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700532 * Fd Definitions
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700533 */
534
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700535/* We need to keep a freelist not because of any concerns of malloc performance
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700536 * but instead so that implementations with multiple threads in (for example)
537 * epoll_wait deal with the race between pollset removal and incoming poll
538 * notifications.
539 *
540 * The problem is that the poller ultimately holds a reference to this
541 * object, so it is very difficult to know when is safe to free it, at least
542 * without some expensive synchronization.
543 *
544 * If we keep the object freelisted, in the worst case losing this race just
545 * becomes a spurious read notification on a reused fd.
546 */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700547
548/* The alarm system needs to be able to wakeup 'some poller' sometimes
549 * (specifically when a new alarm needs to be triggered earlier than the next
550 * alarm 'epoch'). This wakeup_fd gives us something to alert on when such a
551 * case occurs. */
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -0700552
553/* TODO: sreek: Right now, this wakes up all pollers. In future we should make
554 * sure to wake up one polling thread (which can wake up other threads if
555 * needed) */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700556grpc_wakeup_fd grpc_global_wakeup_fd;
557
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700558static grpc_fd *fd_freelist = NULL;
559static gpr_mu fd_freelist_mu;
560
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700561#ifdef GRPC_FD_REF_COUNT_DEBUG
562#define REF_BY(fd, n, reason) ref_by(fd, n, reason, __FILE__, __LINE__)
563#define UNREF_BY(fd, n, reason) unref_by(fd, n, reason, __FILE__, __LINE__)
564static void ref_by(grpc_fd *fd, int n, const char *reason, const char *file,
565 int line) {
566 gpr_log(GPR_DEBUG, "FD %d %p ref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n,
567 gpr_atm_no_barrier_load(&fd->refst),
568 gpr_atm_no_barrier_load(&fd->refst) + n, reason, file, line);
569#else
570#define REF_BY(fd, n, reason) ref_by(fd, n)
571#define UNREF_BY(fd, n, reason) unref_by(fd, n)
572static void ref_by(grpc_fd *fd, int n) {
573#endif
574 GPR_ASSERT(gpr_atm_no_barrier_fetch_add(&fd->refst, n) > 0);
575}
576
577#ifdef GRPC_FD_REF_COUNT_DEBUG
578static void unref_by(grpc_fd *fd, int n, const char *reason, const char *file,
579 int line) {
580 gpr_atm old;
581 gpr_log(GPR_DEBUG, "FD %d %p unref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n,
582 gpr_atm_no_barrier_load(&fd->refst),
583 gpr_atm_no_barrier_load(&fd->refst) - n, reason, file, line);
584#else
585static void unref_by(grpc_fd *fd, int n) {
586 gpr_atm old;
587#endif
588 old = gpr_atm_full_fetch_add(&fd->refst, -n);
589 if (old == n) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700590 /* Add the fd to the freelist */
591 gpr_mu_lock(&fd_freelist_mu);
592 fd->freelist_next = fd_freelist;
593 fd_freelist = fd;
594 grpc_iomgr_unregister_object(&fd->iomgr_object);
595 gpr_mu_unlock(&fd_freelist_mu);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700596 } else {
597 GPR_ASSERT(old > n);
598 }
599}
600
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700601/* Increment refcount by two to avoid changing the orphan bit */
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700602#ifdef GRPC_FD_REF_COUNT_DEBUG
603static void fd_ref(grpc_fd *fd, const char *reason, const char *file,
604 int line) {
605 ref_by(fd, 2, reason, file, line);
606}
607
608static void fd_unref(grpc_fd *fd, const char *reason, const char *file,
609 int line) {
610 unref_by(fd, 2, reason, file, line);
611}
612#else
613static void fd_ref(grpc_fd *fd) { ref_by(fd, 2); }
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700614static void fd_unref(grpc_fd *fd) { unref_by(fd, 2); }
615#endif
616
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700617static void fd_global_init(void) { gpr_mu_init(&fd_freelist_mu); }
618
619static void fd_global_shutdown(void) {
620 gpr_mu_lock(&fd_freelist_mu);
621 gpr_mu_unlock(&fd_freelist_mu);
622 while (fd_freelist != NULL) {
623 grpc_fd *fd = fd_freelist;
624 fd_freelist = fd_freelist->freelist_next;
625 gpr_mu_destroy(&fd->mu);
626 gpr_free(fd);
627 }
628 gpr_mu_destroy(&fd_freelist_mu);
629}
630
631static grpc_fd *fd_create(int fd, const char *name) {
632 grpc_fd *new_fd = NULL;
633
634 gpr_mu_lock(&fd_freelist_mu);
635 if (fd_freelist != NULL) {
636 new_fd = fd_freelist;
637 fd_freelist = fd_freelist->freelist_next;
638 }
639 gpr_mu_unlock(&fd_freelist_mu);
640
641 if (new_fd == NULL) {
642 new_fd = gpr_malloc(sizeof(grpc_fd));
643 gpr_mu_init(&new_fd->mu);
644 gpr_mu_init(&new_fd->pi_mu);
645 }
646
647 /* Note: It is not really needed to get the new_fd->mu lock here. If this is a
648 newly created fd (or an fd we got from the freelist), no one else would be
649 holding a lock to it anyway. */
650 gpr_mu_lock(&new_fd->mu);
651
652 gpr_atm_rel_store(&new_fd->refst, 1);
653 new_fd->shutdown = false;
654 new_fd->read_closure = CLOSURE_NOT_READY;
655 new_fd->write_closure = CLOSURE_NOT_READY;
656 new_fd->fd = fd;
657 new_fd->polling_island = NULL;
658 new_fd->freelist_next = NULL;
659 new_fd->on_done_closure = NULL;
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700660 new_fd->orphaned = false;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700661
662 gpr_mu_unlock(&new_fd->mu);
663
664 char *fd_name;
665 gpr_asprintf(&fd_name, "%s fd=%d", name, fd);
666 grpc_iomgr_register_object(&new_fd->iomgr_object, fd_name);
667 gpr_free(fd_name);
668#ifdef GRPC_FD_REF_COUNT_DEBUG
669 gpr_log(GPR_DEBUG, "FD %d %p create %s", fd, r, fd_name);
670#endif
671 return new_fd;
672}
673
674static bool fd_is_orphaned(grpc_fd *fd) {
675 return (gpr_atm_acq_load(&fd->refst) & 1) == 0;
676}
677
678static int fd_wrapped_fd(grpc_fd *fd) {
679 int ret_fd = -1;
680 gpr_mu_lock(&fd->mu);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700681 if (!fd->orphaned) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700682 ret_fd = fd->fd;
683 }
684 gpr_mu_unlock(&fd->mu);
685
686 return ret_fd;
687}
688
689static void fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
690 grpc_closure *on_done, int *release_fd,
691 const char *reason) {
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700692 bool is_fd_closed = false;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700693 gpr_mu_lock(&fd->mu);
694 fd->on_done_closure = on_done;
695
696 /* If release_fd is not NULL, we should be relinquishing control of the file
697 descriptor fd->fd (but we still own the grpc_fd structure). */
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700698 if (release_fd != NULL) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700699 *release_fd = fd->fd;
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700700 } else {
701 close(fd->fd);
702 is_fd_closed = true;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700703 }
704
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700705 fd->orphaned = true;
706
707 /* Remove the active status but keep referenced. We want this grpc_fd struct
708 to be alive (and not added to freelist) until the end of this function */
709 REF_BY(fd, 1, reason);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700710
711 /* Remove the fd from the polling island:
712 - Update the fd->polling_island to point to the latest polling island
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700713 - Remove the fd from the polling island.
714 - Remove a ref to the polling island and set fd->polling_island to NULL */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700715 gpr_mu_lock(&fd->pi_mu);
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700716 if (fd->polling_island != NULL) {
717 fd->polling_island =
718 polling_island_update_and_lock(fd->polling_island, 1, 0);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700719 polling_island_remove_fd_locked(fd->polling_island, fd, is_fd_closed);
720
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700721 polling_island_unref_and_unlock(fd->polling_island, 1);
722 fd->polling_island = NULL;
723 }
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700724 gpr_mu_unlock(&fd->pi_mu);
725
726 grpc_exec_ctx_enqueue(exec_ctx, fd->on_done_closure, true, NULL);
727
728 gpr_mu_unlock(&fd->mu);
729 UNREF_BY(fd, 2, reason); /* Drop the reference */
730}
731
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700732static void notify_on_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
733 grpc_closure **st, grpc_closure *closure) {
734 if (*st == CLOSURE_NOT_READY) {
735 /* not ready ==> switch to a waiting state by setting the closure */
736 *st = closure;
737 } else if (*st == CLOSURE_READY) {
738 /* already ready ==> queue the closure to run immediately */
739 *st = CLOSURE_NOT_READY;
740 grpc_exec_ctx_enqueue(exec_ctx, closure, !fd->shutdown, NULL);
741 } else {
742 /* upcallptr was set to a different closure. This is an error! */
743 gpr_log(GPR_ERROR,
744 "User called a notify_on function with a previous callback still "
745 "pending");
746 abort();
747 }
748}
749
750/* returns 1 if state becomes not ready */
751static int set_ready_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
752 grpc_closure **st) {
753 if (*st == CLOSURE_READY) {
754 /* duplicate ready ==> ignore */
755 return 0;
756 } else if (*st == CLOSURE_NOT_READY) {
757 /* not ready, and not waiting ==> flag ready */
758 *st = CLOSURE_READY;
759 return 0;
760 } else {
761 /* waiting ==> queue closure */
762 grpc_exec_ctx_enqueue(exec_ctx, *st, !fd->shutdown, NULL);
763 *st = CLOSURE_NOT_READY;
764 return 1;
765 }
766}
767
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700768static void fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
769 gpr_mu_lock(&fd->mu);
770 GPR_ASSERT(!fd->shutdown);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700771 fd->shutdown = true;
772
773 /* Flush any pending read and write closures. Since fd->shutdown is 'true' at
774 this point, the closures would be called with 'success = false' */
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700775 set_ready_locked(exec_ctx, fd, &fd->read_closure);
776 set_ready_locked(exec_ctx, fd, &fd->write_closure);
777 gpr_mu_unlock(&fd->mu);
778}
779
780static void fd_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
781 grpc_closure *closure) {
782 gpr_mu_lock(&fd->mu);
783 notify_on_locked(exec_ctx, fd, &fd->read_closure, closure);
784 gpr_mu_unlock(&fd->mu);
785}
786
787static void fd_notify_on_write(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
788 grpc_closure *closure) {
789 gpr_mu_lock(&fd->mu);
790 notify_on_locked(exec_ctx, fd, &fd->write_closure, closure);
791 gpr_mu_unlock(&fd->mu);
792}
793
794/*******************************************************************************
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700795 * Pollset Definitions
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700796 */
797
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700798static void sig_handler(int sig_num) {
Sree Kuchibhotlad627c102016-06-06 15:49:32 -0700799#ifdef GRPC_EPOLL_DEBUG
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700800 gpr_log(GPR_INFO, "Received signal %d", sig_num);
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -0700801#endif
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700802}
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700803
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700804/* Global state management */
805static void pollset_global_init(void) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700806 grpc_wakeup_fd_init(&grpc_global_wakeup_fd);
Sree Kuchibhotlad627c102016-06-06 15:49:32 -0700807 signal(SIGUSR1, sig_handler); /* TODO: sreek - Do not hardcode SIGUSR1 */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700808}
809
810static void pollset_global_shutdown(void) {
811 grpc_wakeup_fd_destroy(&grpc_global_wakeup_fd);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700812}
813
814/* Return 1 if the pollset has active threads in pollset_work (pollset must
815 * be locked) */
816static int pollset_has_workers(grpc_pollset *p) {
817 return p->root_worker.next != &p->root_worker;
818}
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700819
820static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
821 worker->prev->next = worker->next;
822 worker->next->prev = worker->prev;
823}
824
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700825static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) {
826 if (pollset_has_workers(p)) {
827 grpc_pollset_worker *w = p->root_worker.next;
828 remove_worker(p, w);
829 return w;
830 } else {
831 return NULL;
832 }
833}
834
835static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
836 worker->next = &p->root_worker;
837 worker->prev = worker->next->prev;
838 worker->prev->next = worker->next->prev = worker;
839}
840
841static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
842 worker->prev = &p->root_worker;
843 worker->next = worker->prev->next;
844 worker->prev->next = worker->next->prev = worker;
845}
846
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700847/* p->mu must be held before calling this function */
848static void pollset_kick(grpc_pollset *p,
849 grpc_pollset_worker *specific_worker) {
850 GPR_TIMER_BEGIN("pollset_kick", 0);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700851
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700852 grpc_pollset_worker *worker = specific_worker;
853 if (worker != NULL) {
854 if (worker == GRPC_POLLSET_KICK_BROADCAST) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700855 if (pollset_has_workers(p)) {
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700856 GPR_TIMER_BEGIN("pollset_kick.broadcast", 0);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700857 for (worker = p->root_worker.next; worker != &p->root_worker;
858 worker = worker->next) {
859 pthread_kill(worker->pt_id, SIGUSR1);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700860 }
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700861 } else {
862 p->kicked_without_pollers = true;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700863 }
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700864 GPR_TIMER_END("pollset_kick.broadcast", 0);
865 } else {
866 GPR_TIMER_MARK("kicked_specifically", 0);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700867 pthread_kill(worker->pt_id, SIGUSR1);
868 }
869 } else {
870 GPR_TIMER_MARK("kick_anonymous", 0);
871 worker = pop_front_worker(p);
872 if (worker != NULL) {
873 GPR_TIMER_MARK("finally_kick", 0);
874 push_back_worker(p, worker);
875 pthread_kill(worker->pt_id, SIGUSR1);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700876 } else {
877 GPR_TIMER_MARK("kicked_no_pollers", 0);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700878 p->kicked_without_pollers = true;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700879 }
880 }
881
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700882 GPR_TIMER_END("pollset_kick", 0);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700883}
884
885static void kick_poller(void) { grpc_wakeup_fd_wakeup(&grpc_global_wakeup_fd); }
886
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700887static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
888 gpr_mu_init(&pollset->mu);
889 *mu = &pollset->mu;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700890
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700891 pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700892 pollset->kicked_without_pollers = false;
893
894 pollset->shutting_down = false;
895 pollset->finish_shutdown_called = false;
896 pollset->shutdown_done = NULL;
897
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700898 gpr_mu_init(&pollset->pi_mu);
899 pollset->polling_island = NULL;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700900}
901
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700902/* Convert a timespec to milliseconds:
903 - Very small or negative poll times are clamped to zero to do a non-blocking
904 poll (which becomes spin polling)
905 - Other small values are rounded up to one millisecond
906 - Longer than a millisecond polls are rounded up to the next nearest
907 millisecond to avoid spinning
908 - Infinite timeouts are converted to -1 */
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700909static int poll_deadline_to_millis_timeout(gpr_timespec deadline,
910 gpr_timespec now) {
911 gpr_timespec timeout;
912 static const int64_t max_spin_polling_us = 10;
913 if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) == 0) {
914 return -1;
915 }
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700916
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700917 if (gpr_time_cmp(deadline, gpr_time_add(now, gpr_time_from_micros(
918 max_spin_polling_us,
919 GPR_TIMESPAN))) <= 0) {
920 return 0;
921 }
922 timeout = gpr_time_sub(deadline, now);
923 return gpr_time_to_millis(gpr_time_add(
924 timeout, gpr_time_from_nanos(GPR_NS_PER_MS - 1, GPR_TIMESPAN)));
925}
926
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700927static void set_ready(grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_closure **st) {
928 /* only one set_ready can be active at once (but there may be a racing
929 notify_on) */
930 gpr_mu_lock(&fd->mu);
931 set_ready_locked(exec_ctx, fd, st);
932 gpr_mu_unlock(&fd->mu);
933}
934
935static void fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
936 set_ready(exec_ctx, fd, &fd->read_closure);
937}
938
939static void fd_become_writable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
940 set_ready(exec_ctx, fd, &fd->write_closure);
941}
942
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700943#define GRPC_EPOLL_MAX_EVENTS 1000
944static void pollset_work_and_unlock(grpc_exec_ctx *exec_ctx,
945 grpc_pollset *pollset, int timeout_ms,
946 sigset_t *sig_mask) {
947 struct epoll_event ep_ev[GRPC_EPOLL_MAX_EVENTS];
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700948 int epoll_fd = -1;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700949 int ep_rv;
950 GPR_TIMER_BEGIN("pollset_work_and_unlock", 0);
951
952 /* We need to get the epoll_fd to wait on. The epoll_fd is in inside the
953 polling island pointed by pollset->polling_island.
954 Acquire the following locks:
955 - pollset->mu (which we already have)
956 - pollset->pi_mu
957 - pollset->polling_island->mu */
958 gpr_mu_lock(&pollset->pi_mu);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700959
Sree Kuchibhotlad627c102016-06-06 15:49:32 -0700960 if (pollset->polling_island == NULL) {
961 pollset->polling_island = polling_island_create(NULL, 1);
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700962 }
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700963
Sree Kuchibhotlad627c102016-06-06 15:49:32 -0700964 pollset->polling_island =
965 polling_island_update_and_lock(pollset->polling_island, 1, 0);
966 epoll_fd = pollset->polling_island->epoll_fd;
967
968#ifdef GRPC_EPOLL_DEBUG
969 if (pollset->polling_island->fd_cnt == 0) {
970 gpr_log(GPR_DEBUG, "pollset_work_and_unlock: epoll_fd: %d, No other fds",
971 epoll_fd);
972 }
973 for (size_t i = 0; i < pollset->polling_island->fd_cnt; i++) {
974 gpr_log(GPR_DEBUG,
975 "pollset_work_and_unlock: epoll_fd: %d, fd_count: %d, fd[%d]: %d",
976 epoll_fd, pollset->polling_island->fd_cnt, i,
977 pollset->polling_island->fds[i]->fd);
978 }
979#endif
980 gpr_mu_unlock(&pollset->polling_island->mu);
981
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700982 gpr_mu_unlock(&pollset->pi_mu);
983 gpr_mu_unlock(&pollset->mu);
984
Sree Kuchibhotlae5012ba2016-06-06 16:01:45 -0700985 do {
986 ep_rv = epoll_pwait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, timeout_ms,
987 sig_mask);
988 if (ep_rv < 0) {
989 if (errno != EINTR) {
990 /* TODO (sreek) - Do not log an error in case of bad file descriptor
991 * (A bad file descriptor here would just mean that the epoll set was
992 * merged with another epoll set and that the current epoll_fd is
993 * closed) */
994 gpr_log(GPR_ERROR, "epoll_pwait() failed: %s", strerror(errno));
995 } else {
996 ep_rv = epoll_wait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, 0);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700997 }
Sree Kuchibhotlae5012ba2016-06-06 16:01:45 -0700998 }
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700999
Sree Kuchibhotlae5012ba2016-06-06 16:01:45 -07001000 int i;
1001 for (i = 0; i < ep_rv; ++i) {
1002 grpc_fd *fd = ep_ev[i].data.ptr;
1003 int cancel = ep_ev[i].events & (EPOLLERR | EPOLLHUP);
1004 int read_ev = ep_ev[i].events & (EPOLLIN | EPOLLPRI);
1005 int write_ev = ep_ev[i].events & EPOLLOUT;
1006 if (fd == NULL) {
1007 grpc_wakeup_fd_consume_wakeup(&grpc_global_wakeup_fd);
1008 } else {
1009 if (read_ev || cancel) {
1010 fd_become_readable(exec_ctx, fd);
1011 }
1012 if (write_ev || cancel) {
1013 fd_become_writable(exec_ctx, fd);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001014 }
1015 }
Sree Kuchibhotlae5012ba2016-06-06 16:01:45 -07001016 }
1017 } while (ep_rv == GRPC_EPOLL_MAX_EVENTS);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001018 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 */
1023static 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
1034static 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 */
1046static 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
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -07001065/* pollset_shutdown is guaranteed to be called before pollset_destroy. So other
1066 * than destroying the mutexes, there is nothing special that needs to be done
1067 * here */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001068static void pollset_destroy(grpc_pollset *pollset) {
1069 GPR_ASSERT(!pollset_has_workers(pollset));
1070 gpr_mu_destroy(&pollset->pi_mu);
1071 gpr_mu_destroy(&pollset->mu);
1072}
1073
1074static void pollset_reset(grpc_pollset *pollset) {
1075 GPR_ASSERT(pollset->shutting_down);
1076 GPR_ASSERT(!pollset_has_workers(pollset));
1077 pollset->shutting_down = false;
1078 pollset->finish_shutdown_called = false;
1079 pollset->kicked_without_pollers = false;
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -07001080 pollset->shutdown_done = NULL;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001081 pollset_release_polling_island_locked(pollset);
1082}
1083
1084/* pollset->mu lock must be held by the caller before calling this.
1085 The function pollset_work() may temporarily release the lock (pollset->mu)
1086 during the course of its execution but it will always re-acquire the lock and
1087 ensure that it is held by the time the function returns */
1088static void pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1089 grpc_pollset_worker **worker_hdl, gpr_timespec now,
1090 gpr_timespec deadline) {
1091 GPR_TIMER_BEGIN("pollset_work", 0);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001092 int timeout_ms = poll_deadline_to_millis_timeout(deadline, now);
1093
1094 sigset_t new_mask;
1095 sigset_t orig_mask;
1096
1097 grpc_pollset_worker worker;
1098 worker.next = worker.prev = NULL;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001099 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);
1109 pollset->kicked_without_pollers = 0;
1110 } else if (!pollset->shutting_down) {
1111 sigemptyset(&new_mask);
1112 sigaddset(&new_mask, SIGUSR1);
1113 pthread_sigmask(SIG_BLOCK, &new_mask, &orig_mask);
1114 sigdelset(&orig_mask, SIGUSR1);
1115
1116 push_front_worker(pollset, &worker);
1117
1118 pollset_work_and_unlock(exec_ctx, pollset, timeout_ms, &orig_mask);
1119 grpc_exec_ctx_flush(exec_ctx);
1120
1121 gpr_mu_lock(&pollset->mu);
1122 remove_worker(pollset, &worker);
1123 }
1124
1125 /* If we are the last worker on the pollset (i.e pollset_has_workers() is
1126 false at this point) and the pollset is shutting down, we may have to
1127 finish the shutdown process by calling finish_shutdown_locked().
1128 See pollset_shutdown() for more details.
1129
1130 Note: Continuing to access pollset here is safe; it is the caller's
1131 responsibility to not destroy a pollset when it has outstanding calls to
1132 pollset_work() */
1133 if (pollset->shutting_down && !pollset_has_workers(pollset) &&
1134 !pollset->finish_shutdown_called) {
1135 GPR_TIMER_MARK("pollset_work.finish_shutdown_locked", 0);
1136 finish_shutdown_locked(exec_ctx, pollset);
1137
1138 gpr_mu_unlock(&pollset->mu);
1139 grpc_exec_ctx_flush(exec_ctx);
1140 gpr_mu_lock(&pollset->mu);
1141 }
1142
1143 *worker_hdl = NULL;
1144 GPR_TIMER_END("pollset_work", 0);
1145}
1146
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001147static void pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1148 grpc_fd *fd) {
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -07001149 /* TODO sreek - Double check if we need to get a pollset->mu lock here */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001150 gpr_mu_lock(&pollset->pi_mu);
1151 gpr_mu_lock(&fd->pi_mu);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001152
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001153 polling_island *pi_new = NULL;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001154
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001155 /* 1) If fd->polling_island and pollset->polling_island are both non-NULL and
1156 * equal, do nothing.
1157 * 2) If fd->polling_island and pollset->polling_island are both NULL, create
1158 * a new polling island (with a refcount of 2) and make the polling_island
1159 * fields in both fd and pollset to point to the new island
1160 * 3) If one of fd->polling_island or pollset->polling_island is NULL, update
1161 * the NULL polling_island field to point to the non-NULL polling_island
1162 * field (ensure that the refcount on the polling island is incremented by
1163 * 1 to account for the newly added reference)
1164 * 4) Finally, if fd->polling_island and pollset->polling_island are non-NULL
1165 * and different, merge both the polling islands and update the
1166 * polling_island fields in both fd and pollset to point to the merged
1167 * polling island.
1168 */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001169 if (fd->polling_island == pollset->polling_island) {
1170 pi_new = fd->polling_island;
1171 if (pi_new == NULL) {
1172 pi_new = polling_island_create(fd, 2);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001173 }
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001174 } else if (fd->polling_island == NULL) {
1175 pi_new = polling_island_update_and_lock(pollset->polling_island, 1, 1);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -07001176 polling_island_add_fds_locked(pollset->polling_island, &fd, 1, true);
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -07001177 gpr_mu_unlock(&pi_new->mu);
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001178 } else if (pollset->polling_island == NULL) {
1179 pi_new = polling_island_update_and_lock(fd->polling_island, 1, 1);
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -07001180 gpr_mu_unlock(&pi_new->mu);
Sree Kuchibhotla5098f912016-05-31 10:58:17 -07001181 } else {
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001182 pi_new = polling_island_merge(fd->polling_island, pollset->polling_island);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001183 }
1184
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001185 fd->polling_island = pollset->polling_island = pi_new;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001186
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001187 gpr_mu_unlock(&fd->pi_mu);
1188 gpr_mu_unlock(&pollset->pi_mu);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001189}
1190
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001191/*******************************************************************************
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001192 * Pollset-set Definitions
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001193 */
1194
1195static grpc_pollset_set *pollset_set_create(void) {
1196 grpc_pollset_set *pollset_set = gpr_malloc(sizeof(*pollset_set));
1197 memset(pollset_set, 0, sizeof(*pollset_set));
1198 gpr_mu_init(&pollset_set->mu);
1199 return pollset_set;
1200}
1201
1202static void pollset_set_destroy(grpc_pollset_set *pollset_set) {
1203 size_t i;
1204 gpr_mu_destroy(&pollset_set->mu);
1205 for (i = 0; i < pollset_set->fd_count; i++) {
1206 GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set");
1207 }
1208 gpr_free(pollset_set->pollsets);
1209 gpr_free(pollset_set->pollset_sets);
1210 gpr_free(pollset_set->fds);
1211 gpr_free(pollset_set);
1212}
1213
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001214static void pollset_set_add_fd(grpc_exec_ctx *exec_ctx,
1215 grpc_pollset_set *pollset_set, grpc_fd *fd) {
1216 size_t i;
1217 gpr_mu_lock(&pollset_set->mu);
1218 if (pollset_set->fd_count == pollset_set->fd_capacity) {
1219 pollset_set->fd_capacity = GPR_MAX(8, 2 * pollset_set->fd_capacity);
1220 pollset_set->fds = gpr_realloc(
1221 pollset_set->fds, pollset_set->fd_capacity * sizeof(*pollset_set->fds));
1222 }
1223 GRPC_FD_REF(fd, "pollset_set");
1224 pollset_set->fds[pollset_set->fd_count++] = fd;
1225 for (i = 0; i < pollset_set->pollset_count; i++) {
1226 pollset_add_fd(exec_ctx, pollset_set->pollsets[i], fd);
1227 }
1228 for (i = 0; i < pollset_set->pollset_set_count; i++) {
1229 pollset_set_add_fd(exec_ctx, pollset_set->pollset_sets[i], fd);
1230 }
1231 gpr_mu_unlock(&pollset_set->mu);
1232}
1233
1234static void pollset_set_del_fd(grpc_exec_ctx *exec_ctx,
1235 grpc_pollset_set *pollset_set, grpc_fd *fd) {
1236 size_t i;
1237 gpr_mu_lock(&pollset_set->mu);
1238 for (i = 0; i < pollset_set->fd_count; i++) {
1239 if (pollset_set->fds[i] == fd) {
1240 pollset_set->fd_count--;
1241 GPR_SWAP(grpc_fd *, pollset_set->fds[i],
1242 pollset_set->fds[pollset_set->fd_count]);
1243 GRPC_FD_UNREF(fd, "pollset_set");
1244 break;
1245 }
1246 }
1247 for (i = 0; i < pollset_set->pollset_set_count; i++) {
1248 pollset_set_del_fd(exec_ctx, pollset_set->pollset_sets[i], fd);
1249 }
1250 gpr_mu_unlock(&pollset_set->mu);
1251}
1252
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001253static void pollset_set_add_pollset(grpc_exec_ctx *exec_ctx,
1254 grpc_pollset_set *pollset_set,
1255 grpc_pollset *pollset) {
1256 size_t i, j;
1257 gpr_mu_lock(&pollset_set->mu);
1258 if (pollset_set->pollset_count == pollset_set->pollset_capacity) {
1259 pollset_set->pollset_capacity =
1260 GPR_MAX(8, 2 * pollset_set->pollset_capacity);
1261 pollset_set->pollsets =
1262 gpr_realloc(pollset_set->pollsets, pollset_set->pollset_capacity *
1263 sizeof(*pollset_set->pollsets));
1264 }
1265 pollset_set->pollsets[pollset_set->pollset_count++] = pollset;
1266 for (i = 0, j = 0; i < pollset_set->fd_count; i++) {
1267 if (fd_is_orphaned(pollset_set->fds[i])) {
1268 GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set");
1269 } else {
1270 pollset_add_fd(exec_ctx, pollset, pollset_set->fds[i]);
1271 pollset_set->fds[j++] = pollset_set->fds[i];
1272 }
1273 }
1274 pollset_set->fd_count = j;
1275 gpr_mu_unlock(&pollset_set->mu);
1276}
1277
1278static void pollset_set_del_pollset(grpc_exec_ctx *exec_ctx,
1279 grpc_pollset_set *pollset_set,
1280 grpc_pollset *pollset) {
1281 size_t i;
1282 gpr_mu_lock(&pollset_set->mu);
1283 for (i = 0; i < pollset_set->pollset_count; i++) {
1284 if (pollset_set->pollsets[i] == pollset) {
1285 pollset_set->pollset_count--;
1286 GPR_SWAP(grpc_pollset *, pollset_set->pollsets[i],
1287 pollset_set->pollsets[pollset_set->pollset_count]);
1288 break;
1289 }
1290 }
1291 gpr_mu_unlock(&pollset_set->mu);
1292}
1293
1294static void pollset_set_add_pollset_set(grpc_exec_ctx *exec_ctx,
1295 grpc_pollset_set *bag,
1296 grpc_pollset_set *item) {
1297 size_t i, j;
1298 gpr_mu_lock(&bag->mu);
1299 if (bag->pollset_set_count == bag->pollset_set_capacity) {
1300 bag->pollset_set_capacity = GPR_MAX(8, 2 * bag->pollset_set_capacity);
1301 bag->pollset_sets =
1302 gpr_realloc(bag->pollset_sets,
1303 bag->pollset_set_capacity * sizeof(*bag->pollset_sets));
1304 }
1305 bag->pollset_sets[bag->pollset_set_count++] = item;
1306 for (i = 0, j = 0; i < bag->fd_count; i++) {
1307 if (fd_is_orphaned(bag->fds[i])) {
1308 GRPC_FD_UNREF(bag->fds[i], "pollset_set");
1309 } else {
1310 pollset_set_add_fd(exec_ctx, item, bag->fds[i]);
1311 bag->fds[j++] = bag->fds[i];
1312 }
1313 }
1314 bag->fd_count = j;
1315 gpr_mu_unlock(&bag->mu);
1316}
1317
1318static void pollset_set_del_pollset_set(grpc_exec_ctx *exec_ctx,
1319 grpc_pollset_set *bag,
1320 grpc_pollset_set *item) {
1321 size_t i;
1322 gpr_mu_lock(&bag->mu);
1323 for (i = 0; i < bag->pollset_set_count; i++) {
1324 if (bag->pollset_sets[i] == item) {
1325 bag->pollset_set_count--;
1326 GPR_SWAP(grpc_pollset_set *, bag->pollset_sets[i],
1327 bag->pollset_sets[bag->pollset_set_count]);
1328 break;
1329 }
1330 }
1331 gpr_mu_unlock(&bag->mu);
1332}
1333
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001334/*******************************************************************************
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001335 * Event engine binding
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001336 */
1337
1338static void shutdown_engine(void) {
1339 fd_global_shutdown();
1340 pollset_global_shutdown();
Sree Kuchibhotlad627c102016-06-06 15:49:32 -07001341 polling_island_global_shutdown();
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001342}
1343
1344static const grpc_event_engine_vtable vtable = {
1345 .pollset_size = sizeof(grpc_pollset),
1346
1347 .fd_create = fd_create,
1348 .fd_wrapped_fd = fd_wrapped_fd,
1349 .fd_orphan = fd_orphan,
1350 .fd_shutdown = fd_shutdown,
1351 .fd_notify_on_read = fd_notify_on_read,
1352 .fd_notify_on_write = fd_notify_on_write,
1353
1354 .pollset_init = pollset_init,
1355 .pollset_shutdown = pollset_shutdown,
1356 .pollset_reset = pollset_reset,
1357 .pollset_destroy = pollset_destroy,
1358 .pollset_work = pollset_work,
1359 .pollset_kick = pollset_kick,
1360 .pollset_add_fd = pollset_add_fd,
1361
1362 .pollset_set_create = pollset_set_create,
1363 .pollset_set_destroy = pollset_set_destroy,
1364 .pollset_set_add_pollset = pollset_set_add_pollset,
1365 .pollset_set_del_pollset = pollset_set_del_pollset,
1366 .pollset_set_add_pollset_set = pollset_set_add_pollset_set,
1367 .pollset_set_del_pollset_set = pollset_set_del_pollset_set,
1368 .pollset_set_add_fd = pollset_set_add_fd,
1369 .pollset_set_del_fd = pollset_set_del_fd,
1370
1371 .kick_poller = kick_poller,
1372
1373 .shutdown_engine = shutdown_engine,
1374};
1375
1376const grpc_event_engine_vtable *grpc_init_epoll_linux(void) {
1377 fd_global_init();
1378 pollset_global_init();
1379 polling_island_global_init();
1380 return &vtable;
1381}
1382
1383#endif