blob: d3abf3bd84fe2d568d06dbefd368659cf6cc5d7d [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 {
148 int kicked_specifically;
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -0700149 pthread_t pt_id; /* Thread id of this worker */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700150 struct grpc_pollset_worker *next;
151 struct grpc_pollset_worker *prev;
152};
153
154struct grpc_pollset {
155 gpr_mu mu;
156 grpc_pollset_worker root_worker;
157 bool kicked_without_pollers;
158
159 bool shutting_down; /* Is the pollset shutting down ? */
160 bool finish_shutdown_called; /* Is the 'finish_shutdown_locked()' called ? */
161 grpc_closure *shutdown_done; /* Called after after shutdown is complete */
162
163 /* The polling island to which this pollset belongs to and the mutex
164 protecting the field */
165 gpr_mu pi_mu;
166 struct polling_island *polling_island;
167};
168
169/*******************************************************************************
170 * Pollset-set Declarations
171 */
172struct grpc_pollset_set {
173 gpr_mu mu;
174
175 size_t pollset_count;
176 size_t pollset_capacity;
177 grpc_pollset **pollsets;
178
179 size_t pollset_set_count;
180 size_t pollset_set_capacity;
181 struct grpc_pollset_set **pollset_sets;
182
183 size_t fd_count;
184 size_t fd_capacity;
185 grpc_fd **fds;
186};
187
188/*******************************************************************************
189 * Polling-island Definitions
190 */
191
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700192/* Polling island freelist */
193static gpr_mu g_pi_freelist_mu;
194static polling_island *g_pi_freelist = NULL;
195
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700196/* The caller is expected to hold pi->mu lock before calling this function */
197static void polling_island_add_fds_locked(polling_island *pi, grpc_fd **fds,
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700198 size_t fd_count, bool add_fd_refs) {
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700199 int err;
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700200 size_t i;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700201 struct epoll_event ev;
202
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700203 for (i = 0; i < fd_count; i++) {
204 ev.events = (uint32_t)(EPOLLIN | EPOLLOUT | EPOLLET);
205 ev.data.ptr = fds[i];
206 err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_ADD, fds[i]->fd, &ev);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700207
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700208 if (err < 0) {
209 if (errno != EEXIST) {
210 /* TODO: sreek - We need a better way to bubble up this error instead of
211 just logging a message */
212 gpr_log(GPR_ERROR, "epoll_ctl add for fd: %d failed with error: %s",
213 fds[i]->fd, strerror(errno));
214 }
215
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700216 continue;
217 }
218
219 if (pi->fd_cnt == pi->fd_capacity) {
220 pi->fd_capacity = GPR_MAX(pi->fd_capacity + 8, pi->fd_cnt * 3 / 2);
221 pi->fds = gpr_realloc(pi->fds, sizeof(grpc_fd *) * pi->fd_capacity);
222 }
223
224 pi->fds[pi->fd_cnt++] = fds[i];
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700225 if (add_fd_refs) {
226 GRPC_FD_REF(fds[i], "polling_island");
227 }
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700228 }
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700229}
230
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700231/* The caller is expected to hold pi->mu lock before calling this function */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700232static void polling_island_remove_all_fds_locked(polling_island *pi,
233 bool remove_fd_refs) {
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700234 int err;
235 size_t i;
236
237 for (i = 0; i < pi->fd_cnt; i++) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700238 if (remove_fd_refs) {
239 GRPC_FD_UNREF(pi->fds[i], "polling_island");
240 }
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700241
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700242 err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_DEL, pi->fds[i]->fd, NULL);
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700243 if (err < 0 && errno != ENOENT) {
244 gpr_log(GPR_ERROR,
245 "epoll_ctl delete for fds[i]: %d failed with error: %s", i,
246 pi->fds[i]->fd, strerror(errno));
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700247 /* TODO: sreek - We need a better way to bubble up this error instead of
248 * just logging a message */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700249 continue;
250 }
251 }
252
253 pi->fd_cnt = 0;
254}
255
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700256/* The caller is expected to hold pi->mu lock before calling this function */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700257static void polling_island_remove_fd_locked(polling_island *pi, grpc_fd *fd,
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700258 bool is_fd_closed) {
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700259 int err;
260 size_t i;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700261
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700262 /* If fd is already closed, then it would have been automatically been removed
263 from the epoll set */
264 if (!is_fd_closed) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700265 err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_DEL, fd->fd, NULL);
266 if (err < 0 && errno != ENOENT) {
267 gpr_log(GPR_ERROR, "epoll_ctl delete for fd: %d failed with error; %s",
268 fd->fd, strerror(errno));
269 }
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700270 }
271
272 for (i = 0; i < pi->fd_cnt; i++) {
273 if (pi->fds[i] == fd) {
274 pi->fds[i] = pi->fds[--pi->fd_cnt];
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700275 GRPC_FD_UNREF(fd, "polling_island");
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700276 break;
277 }
278 }
279}
280
281static polling_island *polling_island_create(grpc_fd *initial_fd,
282 int initial_ref_cnt) {
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700283 polling_island *pi = NULL;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700284 struct epoll_event ev;
285 int err;
286
287 /* Try to get one from the polling island freelist */
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700288 gpr_mu_lock(&g_pi_freelist_mu);
289 if (g_pi_freelist != NULL) {
290 pi = g_pi_freelist;
291 g_pi_freelist = g_pi_freelist->next_free;
292 pi->next_free = NULL;
293 }
294 gpr_mu_unlock(&g_pi_freelist_mu);
295
296 /* Create new polling island if we could not get one from the free list */
297 if (pi == NULL) {
298 pi = gpr_malloc(sizeof(*pi));
299 gpr_mu_init(&pi->mu);
300 pi->fd_cnt = 0;
301 pi->fd_capacity = 0;
302 pi->fds = NULL;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700303 }
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700304
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700305 pi->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
306 if (pi->epoll_fd < 0) {
307 gpr_log(GPR_ERROR, "epoll_create1() failed with error: %s",
308 strerror(errno));
309 }
310 GPR_ASSERT(pi->epoll_fd >= 0);
311
312 ev.events = (uint32_t)(EPOLLIN | EPOLLET);
313 ev.data.ptr = NULL;
314 err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_ADD,
315 GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd), &ev);
316 if (err < 0) {
317 gpr_log(GPR_ERROR,
318 "Failed to add grpc_global_wake_up_fd (%d) to the epoll set "
319 "(epoll_fd: %d) with error: %s",
320 GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd), pi->epoll_fd,
321 strerror(errno));
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700322 }
323
324 pi->ref_cnt = initial_ref_cnt;
325 pi->merged_to = NULL;
326 pi->next_free = NULL;
327
328 if (initial_fd != NULL) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700329 /* It is not really needed to get the pi->mu lock here. If this is a newly
330 created polling island (or one that we got from the freelist), no one
331 else would be holding a lock to it anyway */
332 gpr_mu_lock(&pi->mu);
333 polling_island_add_fds_locked(pi, &initial_fd, 1, true);
334 gpr_mu_unlock(&pi->mu);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700335 }
336
337 return pi;
338}
339
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700340static void polling_island_delete(polling_island *pi) {
341 GPR_ASSERT(pi->ref_cnt == 0);
342 GPR_ASSERT(pi->fd_cnt == 0);
343
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700344 close(pi->epoll_fd);
345 pi->epoll_fd = -1;
346
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700347 pi->merged_to = NULL;
348
349 gpr_mu_lock(&g_pi_freelist_mu);
350 pi->next_free = g_pi_freelist;
351 g_pi_freelist = pi;
352 gpr_mu_unlock(&g_pi_freelist_mu);
353}
354
355void polling_island_unref_and_unlock(polling_island *pi, int unref_by) {
356 pi->ref_cnt -= unref_by;
357 int ref_cnt = pi->ref_cnt;
358 GPR_ASSERT(ref_cnt >= 0);
359
360 gpr_mu_unlock(&pi->mu);
361
362 if (ref_cnt == 0) {
363 polling_island_delete(pi);
364 }
365}
366
367polling_island *polling_island_update_and_lock(polling_island *pi, int unref_by,
368 int add_ref_by) {
369 polling_island *next = NULL;
370 gpr_mu_lock(&pi->mu);
371 while (pi->merged_to != NULL) {
372 next = pi->merged_to;
373 polling_island_unref_and_unlock(pi, unref_by);
374 pi = next;
375 gpr_mu_lock(&pi->mu);
376 }
377
378 pi->ref_cnt += add_ref_by;
379 return pi;
380}
381
382void polling_island_pair_update_and_lock(polling_island **p,
383 polling_island **q) {
384 polling_island *pi_1 = *p;
385 polling_island *pi_2 = *q;
386 polling_island *temp = NULL;
387 bool pi_1_locked = false;
388 bool pi_2_locked = false;
389 int num_swaps = 0;
390
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700391 /* Loop until either pi_1 == pi_2 or until we acquired locks on both pi_1
392 and pi_2 */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700393 while (pi_1 != pi_2 && !(pi_1_locked && pi_2_locked)) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700394 /* The following assertions are true at this point:
395 - pi_1 != pi_2 (else, the while loop would have exited)
396 - pi_1 MAY be locked
397 - pi_2 is NOT locked */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700398
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700399 /* To maintain lock order consistency, always lock polling_island node with
400 lower address first.
401 First, make sure pi_1 < pi_2 before proceeding any further. If it turns
402 out that pi_1 > pi_2, unlock pi_1 if locked (because pi_2 is not locked
403 at this point and having pi_1 locked would violate the lock order) and
404 swap pi_1 and pi_2 so that pi_1 becomes less than pi_2 */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700405 if (pi_1 > pi_2) {
406 if (pi_1_locked) {
407 gpr_mu_unlock(&pi_1->mu);
408 pi_1_locked = false;
409 }
410
411 GPR_SWAP(polling_island *, pi_1, pi_2);
412 num_swaps++;
413 }
414
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700415 /* The following assertions are true at this point:
416 - pi_1 != pi_2
417 - pi_1 < pi_2 (address of pi_1 is less than that of pi_2)
418 - pi_1 MAYBE locked
419 - pi_2 is NOT locked */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700420
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700421 /* Lock pi_1 (if pi_1 is pointing to the terminal node in the list) */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700422 if (!pi_1_locked) {
423 gpr_mu_lock(&pi_1->mu);
424 pi_1_locked = true;
425
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700426 /* If pi_1 is not terminal node (i.e pi_1->merged_to != NULL), we are not
427 done locking this polling_island yet. Release the lock on this node and
428 advance pi_1 to the next node in the list; and go to the beginning of
429 the loop (we can't proceed to locking pi_2 unless we locked pi_1 first)
430 */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700431 if (pi_1->merged_to != NULL) {
432 temp = pi_1->merged_to;
433 polling_island_unref_and_unlock(pi_1, 1);
434 pi_1 = temp;
435 pi_1_locked = false;
436
437 continue;
438 }
439 }
440
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700441 /* The following assertions are true at this point:
442 - pi_1 is locked
443 - pi_2 is unlocked
444 - pi_1 != pi_2 */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700445
446 gpr_mu_lock(&pi_2->mu);
447 pi_2_locked = true;
448
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700449 /* If pi_2 is not terminal node, we are not done locking this polling_island
450 yet. Release the lock and update pi_2 to the next node in the list */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700451 if (pi_2->merged_to != NULL) {
452 temp = pi_2->merged_to;
453 polling_island_unref_and_unlock(pi_2, 1);
454 pi_2 = temp;
455 pi_2_locked = false;
456 }
457 }
458
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700459 /* At this point, either pi_1 == pi_2 AND/OR we got both locks */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700460 if (pi_1 == pi_2) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700461 /* We may or may not have gotten the lock. If we didn't, walk the rest of
462 the polling_island list and get the lock */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700463 GPR_ASSERT(pi_1_locked || (!pi_1_locked && !pi_2_locked));
464 if (!pi_1_locked) {
465 pi_1 = pi_2 = polling_island_update_and_lock(pi_1, 2, 0);
466 }
467 } else {
468 GPR_ASSERT(pi_1_locked && pi_2_locked);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700469 /* If we swapped pi_1 and pi_2 odd number of times, do one more swap so that
470 pi_1 and pi_2 point to the same polling_island lists they started off
471 with at the beginning of this function (i.e *p and *q respectively) */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700472 if (num_swaps % 2 > 0) {
473 GPR_SWAP(polling_island *, pi_1, pi_2);
474 }
475 }
476
477 *p = pi_1;
478 *q = pi_2;
479}
480
481polling_island *polling_island_merge(polling_island *p, polling_island *q) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700482 /* Get locks on both the polling islands */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700483 polling_island_pair_update_and_lock(&p, &q);
484
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -0700485 /* TODO: sreek: Think about this scenario some more */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700486 if (p == q) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700487 /* Nothing needs to be done here */
488 gpr_mu_unlock(&p->mu);
489 return p;
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700490 }
491
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700492 /* Make sure that p points to the polling island with fewer fds than q */
493 if (p->fd_cnt > q->fd_cnt) {
494 GPR_SWAP(polling_island *, p, q);
495 }
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700496
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700497 /* "Merge" p with q i.e move all the fds from p (the polling_island with fewer
498 fds) to q.
499 Note: Not altering the ref counts on the affected fds here because they
500 would effectively remain unchanged */
501 polling_island_add_fds_locked(q, p->fds, p->fd_cnt, false);
502 polling_island_remove_all_fds_locked(p, false);
503
504 /* The merged polling island inherits all the ref counts of the island merging
505 with it */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700506 q->ref_cnt += p->ref_cnt;
507
508 gpr_mu_unlock(&p->mu);
509 gpr_mu_unlock(&q->mu);
510
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700511 return q;
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -0700512}
513
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700514static void polling_island_global_init() {
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700515 gpr_mu_init(&g_pi_freelist_mu);
516 g_pi_freelist = NULL;
517}
518
519/*******************************************************************************
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700520 * Fd Definitions
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700521 */
522
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700523/* We need to keep a freelist not because of any concerns of malloc performance
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700524 * but instead so that implementations with multiple threads in (for example)
525 * epoll_wait deal with the race between pollset removal and incoming poll
526 * notifications.
527 *
528 * The problem is that the poller ultimately holds a reference to this
529 * object, so it is very difficult to know when is safe to free it, at least
530 * without some expensive synchronization.
531 *
532 * If we keep the object freelisted, in the worst case losing this race just
533 * becomes a spurious read notification on a reused fd.
534 */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700535
536/* The alarm system needs to be able to wakeup 'some poller' sometimes
537 * (specifically when a new alarm needs to be triggered earlier than the next
538 * alarm 'epoch'). This wakeup_fd gives us something to alert on when such a
539 * case occurs. */
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -0700540
541/* TODO: sreek: Right now, this wakes up all pollers. In future we should make
542 * sure to wake up one polling thread (which can wake up other threads if
543 * needed) */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700544grpc_wakeup_fd grpc_global_wakeup_fd;
545
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700546static grpc_fd *fd_freelist = NULL;
547static gpr_mu fd_freelist_mu;
548
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700549#ifdef GRPC_FD_REF_COUNT_DEBUG
550#define REF_BY(fd, n, reason) ref_by(fd, n, reason, __FILE__, __LINE__)
551#define UNREF_BY(fd, n, reason) unref_by(fd, n, reason, __FILE__, __LINE__)
552static void ref_by(grpc_fd *fd, int n, const char *reason, const char *file,
553 int line) {
554 gpr_log(GPR_DEBUG, "FD %d %p ref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n,
555 gpr_atm_no_barrier_load(&fd->refst),
556 gpr_atm_no_barrier_load(&fd->refst) + n, reason, file, line);
557#else
558#define REF_BY(fd, n, reason) ref_by(fd, n)
559#define UNREF_BY(fd, n, reason) unref_by(fd, n)
560static void ref_by(grpc_fd *fd, int n) {
561#endif
562 GPR_ASSERT(gpr_atm_no_barrier_fetch_add(&fd->refst, n) > 0);
563}
564
565#ifdef GRPC_FD_REF_COUNT_DEBUG
566static void unref_by(grpc_fd *fd, int n, const char *reason, const char *file,
567 int line) {
568 gpr_atm old;
569 gpr_log(GPR_DEBUG, "FD %d %p unref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n,
570 gpr_atm_no_barrier_load(&fd->refst),
571 gpr_atm_no_barrier_load(&fd->refst) - n, reason, file, line);
572#else
573static void unref_by(grpc_fd *fd, int n) {
574 gpr_atm old;
575#endif
576 old = gpr_atm_full_fetch_add(&fd->refst, -n);
577 if (old == n) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700578 /* Add the fd to the freelist */
579 gpr_mu_lock(&fd_freelist_mu);
580 fd->freelist_next = fd_freelist;
581 fd_freelist = fd;
582 grpc_iomgr_unregister_object(&fd->iomgr_object);
583 gpr_mu_unlock(&fd_freelist_mu);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700584 } else {
585 GPR_ASSERT(old > n);
586 }
587}
588
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700589/* Increment refcount by two to avoid changing the orphan bit */
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700590#ifdef GRPC_FD_REF_COUNT_DEBUG
591static void fd_ref(grpc_fd *fd, const char *reason, const char *file,
592 int line) {
593 ref_by(fd, 2, reason, file, line);
594}
595
596static void fd_unref(grpc_fd *fd, const char *reason, const char *file,
597 int line) {
598 unref_by(fd, 2, reason, file, line);
599}
600#else
601static void fd_ref(grpc_fd *fd) { ref_by(fd, 2); }
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700602static void fd_unref(grpc_fd *fd) { unref_by(fd, 2); }
603#endif
604
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700605static void fd_global_init(void) { gpr_mu_init(&fd_freelist_mu); }
606
607static void fd_global_shutdown(void) {
608 gpr_mu_lock(&fd_freelist_mu);
609 gpr_mu_unlock(&fd_freelist_mu);
610 while (fd_freelist != NULL) {
611 grpc_fd *fd = fd_freelist;
612 fd_freelist = fd_freelist->freelist_next;
613 gpr_mu_destroy(&fd->mu);
614 gpr_free(fd);
615 }
616 gpr_mu_destroy(&fd_freelist_mu);
617}
618
619static grpc_fd *fd_create(int fd, const char *name) {
620 grpc_fd *new_fd = NULL;
621
622 gpr_mu_lock(&fd_freelist_mu);
623 if (fd_freelist != NULL) {
624 new_fd = fd_freelist;
625 fd_freelist = fd_freelist->freelist_next;
626 }
627 gpr_mu_unlock(&fd_freelist_mu);
628
629 if (new_fd == NULL) {
630 new_fd = gpr_malloc(sizeof(grpc_fd));
631 gpr_mu_init(&new_fd->mu);
632 gpr_mu_init(&new_fd->pi_mu);
633 }
634
635 /* Note: It is not really needed to get the new_fd->mu lock here. If this is a
636 newly created fd (or an fd we got from the freelist), no one else would be
637 holding a lock to it anyway. */
638 gpr_mu_lock(&new_fd->mu);
639
640 gpr_atm_rel_store(&new_fd->refst, 1);
641 new_fd->shutdown = false;
642 new_fd->read_closure = CLOSURE_NOT_READY;
643 new_fd->write_closure = CLOSURE_NOT_READY;
644 new_fd->fd = fd;
645 new_fd->polling_island = NULL;
646 new_fd->freelist_next = NULL;
647 new_fd->on_done_closure = NULL;
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700648 new_fd->orphaned = false;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700649
650 gpr_mu_unlock(&new_fd->mu);
651
652 char *fd_name;
653 gpr_asprintf(&fd_name, "%s fd=%d", name, fd);
654 grpc_iomgr_register_object(&new_fd->iomgr_object, fd_name);
655 gpr_free(fd_name);
656#ifdef GRPC_FD_REF_COUNT_DEBUG
657 gpr_log(GPR_DEBUG, "FD %d %p create %s", fd, r, fd_name);
658#endif
659 return new_fd;
660}
661
662static bool fd_is_orphaned(grpc_fd *fd) {
663 return (gpr_atm_acq_load(&fd->refst) & 1) == 0;
664}
665
666static int fd_wrapped_fd(grpc_fd *fd) {
667 int ret_fd = -1;
668 gpr_mu_lock(&fd->mu);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700669 if (!fd->orphaned) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700670 ret_fd = fd->fd;
671 }
672 gpr_mu_unlock(&fd->mu);
673
674 return ret_fd;
675}
676
677static void fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
678 grpc_closure *on_done, int *release_fd,
679 const char *reason) {
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700680 bool is_fd_closed = false;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700681 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 Kuchibhotla79a62332016-06-04 14:01:03 -0700686 if (release_fd != NULL) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700687 *release_fd = fd->fd;
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700688 } else {
689 close(fd->fd);
690 is_fd_closed = true;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700691 }
692
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700693 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 Kuchibhotla0bcbd792016-06-01 15:43:03 -0700698
699 /* Remove the fd from the polling island:
700 - Update the fd->polling_island to point to the latest polling island
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700701 - Remove the fd from the polling island.
702 - Remove a ref to the polling island and set fd->polling_island to NULL */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700703 gpr_mu_lock(&fd->pi_mu);
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700704 if (fd->polling_island != NULL) {
705 fd->polling_island =
706 polling_island_update_and_lock(fd->polling_island, 1, 0);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700707 polling_island_remove_fd_locked(fd->polling_island, fd, is_fd_closed);
708
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700709 polling_island_unref_and_unlock(fd->polling_island, 1);
710 fd->polling_island = NULL;
711 }
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700712 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 Kuchibhotlaf448c342016-05-19 10:51:24 -0700720static 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 */
739static 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 Kuchibhotlaf448c342016-05-19 10:51:24 -0700756static void fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
757 gpr_mu_lock(&fd->mu);
758 GPR_ASSERT(!fd->shutdown);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700759 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 Kuchibhotlaf448c342016-05-19 10:51:24 -0700763 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
768static 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
775static 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 Kuchibhotla0bcbd792016-06-01 15:43:03 -0700783 * Pollset Definitions
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700784 */
785
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700786static void sig_handler(int sig_num) {
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -0700787#ifdef GPRC_EPOLL_DEBUG
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700788 gpr_log(GPR_INFO, "Received signal %d", sig_num);
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -0700789#endif
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700790}
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700791
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700792/* Global state management */
793static void pollset_global_init(void) {
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700794 grpc_wakeup_fd_init(&grpc_global_wakeup_fd);
795 signal(SIGUSR1, sig_handler);
796}
797
798static void pollset_global_shutdown(void) {
799 grpc_wakeup_fd_destroy(&grpc_global_wakeup_fd);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700800}
801
802/* Return 1 if the pollset has active threads in pollset_work (pollset must
803 * be locked) */
804static int pollset_has_workers(grpc_pollset *p) {
805 return p->root_worker.next != &p->root_worker;
806}
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700807
808static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
809 worker->prev->next = worker->next;
810 worker->next->prev = worker->prev;
811}
812
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700813static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) {
814 if (pollset_has_workers(p)) {
815 grpc_pollset_worker *w = p->root_worker.next;
816 remove_worker(p, w);
817 return w;
818 } else {
819 return NULL;
820 }
821}
822
823static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
824 worker->next = &p->root_worker;
825 worker->prev = worker->next->prev;
826 worker->prev->next = worker->next->prev = worker;
827}
828
829static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
830 worker->prev = &p->root_worker;
831 worker->next = worker->prev->next;
832 worker->prev->next = worker->next->prev = worker;
833}
834
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700835/* p->mu must be held before calling this function */
836static void pollset_kick(grpc_pollset *p,
837 grpc_pollset_worker *specific_worker) {
838 GPR_TIMER_BEGIN("pollset_kick", 0);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700839
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700840 grpc_pollset_worker *worker = specific_worker;
841 if (worker != NULL) {
842 if (worker == GRPC_POLLSET_KICK_BROADCAST) {
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700843 gpr_log(GPR_DEBUG, "pollset_kick: broadcast!");
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700844 if (pollset_has_workers(p)) {
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700845 GPR_TIMER_BEGIN("pollset_kick.broadcast", 0);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700846 for (worker = p->root_worker.next; worker != &p->root_worker;
847 worker = worker->next) {
848 pthread_kill(worker->pt_id, SIGUSR1);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700849 }
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700850 } else {
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700851 gpr_log(GPR_DEBUG, "pollset_kick: (broadcast) Kicked without pollers");
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700852 p->kicked_without_pollers = true;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700853 }
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700854 GPR_TIMER_END("pollset_kick.broadcast", 0);
855 } else {
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700856 gpr_log(GPR_DEBUG, "pollset_kick: kicked kicked_specifically");
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700857 GPR_TIMER_MARK("kicked_specifically", 0);
858 worker->kicked_specifically = true;
859 pthread_kill(worker->pt_id, SIGUSR1);
860 }
861 } else {
862 GPR_TIMER_MARK("kick_anonymous", 0);
863 worker = pop_front_worker(p);
864 if (worker != NULL) {
865 GPR_TIMER_MARK("finally_kick", 0);
866 push_back_worker(p, worker);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700867 gpr_log(GPR_DEBUG, "pollset_kick: anonymous kick");
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700868 pthread_kill(worker->pt_id, SIGUSR1);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700869 } else {
870 GPR_TIMER_MARK("kicked_no_pollers", 0);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700871 gpr_log(GPR_DEBUG, "pollset_kick: kicked without pollers");
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700872 p->kicked_without_pollers = true;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700873 }
874 }
875
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700876 GPR_TIMER_END("pollset_kick", 0);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700877}
878
879static void kick_poller(void) { grpc_wakeup_fd_wakeup(&grpc_global_wakeup_fd); }
880
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700881static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
882 gpr_mu_init(&pollset->mu);
883 *mu = &pollset->mu;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700884
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700885 pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700886 pollset->kicked_without_pollers = false;
887
888 pollset->shutting_down = false;
889 pollset->finish_shutdown_called = false;
890 pollset->shutdown_done = NULL;
891
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700892 gpr_mu_init(&pollset->pi_mu);
893 pollset->polling_island = NULL;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700894}
895
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700896/* Convert a timespec to milliseconds:
897 - Very small or negative poll times are clamped to zero to do a non-blocking
898 poll (which becomes spin polling)
899 - Other small values are rounded up to one millisecond
900 - Longer than a millisecond polls are rounded up to the next nearest
901 millisecond to avoid spinning
902 - Infinite timeouts are converted to -1 */
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700903static int poll_deadline_to_millis_timeout(gpr_timespec deadline,
904 gpr_timespec now) {
905 gpr_timespec timeout;
906 static const int64_t max_spin_polling_us = 10;
907 if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) == 0) {
908 return -1;
909 }
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700910
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700911 if (gpr_time_cmp(deadline, gpr_time_add(now, gpr_time_from_micros(
912 max_spin_polling_us,
913 GPR_TIMESPAN))) <= 0) {
914 return 0;
915 }
916 timeout = gpr_time_sub(deadline, now);
917 return gpr_time_to_millis(gpr_time_add(
918 timeout, gpr_time_from_nanos(GPR_NS_PER_MS - 1, GPR_TIMESPAN)));
919}
920
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -0700921static void set_ready(grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_closure **st) {
922 /* only one set_ready can be active at once (but there may be a racing
923 notify_on) */
924 gpr_mu_lock(&fd->mu);
925 set_ready_locked(exec_ctx, fd, st);
926 gpr_mu_unlock(&fd->mu);
927}
928
929static void fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
930 set_ready(exec_ctx, fd, &fd->read_closure);
931}
932
933static void fd_become_writable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
934 set_ready(exec_ctx, fd, &fd->write_closure);
935}
936
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700937#define GRPC_EPOLL_MAX_EVENTS 1000
938static void pollset_work_and_unlock(grpc_exec_ctx *exec_ctx,
939 grpc_pollset *pollset, int timeout_ms,
940 sigset_t *sig_mask) {
941 struct epoll_event ep_ev[GRPC_EPOLL_MAX_EVENTS];
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700942 int epoll_fd = -1;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700943 int ep_rv;
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700944 gpr_log(GPR_DEBUG, "pollset_work_and_unlock: Entering..");
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700945 GPR_TIMER_BEGIN("pollset_work_and_unlock", 0);
946
947 /* We need to get the epoll_fd to wait on. The epoll_fd is in inside the
948 polling island pointed by pollset->polling_island.
949 Acquire the following locks:
950 - pollset->mu (which we already have)
951 - pollset->pi_mu
952 - pollset->polling_island->mu */
953 gpr_mu_lock(&pollset->pi_mu);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700954
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700955 if (pollset->polling_island != NULL) {
956 pollset->polling_island =
957 polling_island_update_and_lock(pollset->polling_island, 1, 0);
958 epoll_fd = pollset->polling_island->epoll_fd;
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700959 if (pollset->polling_island->fd_cnt == 0) {
960 gpr_log(GPR_DEBUG, "pollset_work_and_unlock: epoll_fd: %d, No other fds",
961 epoll_fd);
962 }
963 for (size_t i = 0; i < pollset->polling_island->fd_cnt; i++) {
964 gpr_log(GPR_DEBUG,
965 "pollset_work_and_unlock: epoll_fd: %d, fd_count: %d, fd[%d]: %d",
966 epoll_fd, pollset->polling_island->fd_cnt, i,
967 pollset->polling_island->fds[i]->fd);
968 }
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700969 gpr_mu_unlock(&pollset->polling_island->mu);
970 }
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700971
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700972 gpr_mu_unlock(&pollset->pi_mu);
973 gpr_mu_unlock(&pollset->mu);
974
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700975 /* If epoll_fd == -1, this is a blank pollset and does not have any fds yet */
976 if (epoll_fd != -1) {
977 do {
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700978 gpr_timespec before_epoll = gpr_now(GPR_CLOCK_PRECISE);
979 gpr_log(GPR_DEBUG, "pollset_work_and_unlock: epoll_wait()....");
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700980 ep_rv = epoll_pwait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, timeout_ms,
981 sig_mask);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700982 gpr_timespec after_epoll = gpr_now(GPR_CLOCK_PRECISE);
983 int dur = gpr_time_to_millis(gpr_time_sub(after_epoll, before_epoll));
984 gpr_log(GPR_DEBUG,
985 "pollset_work_and_unlock: DONE epoll_wait() : %d ms, ep_rv: %d",
986 dur, ep_rv);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -0700987
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700988 if (ep_rv < 0) {
989 if (errno != EINTR) {
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -0700990 /* 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) */
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700994 gpr_log(GPR_ERROR, "epoll_pwait() failed: %s", strerror(errno));
Sree Kuchibhotla79a62332016-06-04 14:01:03 -0700995 } else {
996 gpr_log(GPR_DEBUG, "pollset_work_and_unlock: 0-timeout epoll_wait()");
997 ep_rv = epoll_wait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, 0);
998 gpr_log(GPR_DEBUG, "pollset_work_and_unlock: ep_rv: %d", ep_rv);
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -0700999 }
Sree Kuchibhotla79a62332016-06-04 14:01:03 -07001000 }
1001
1002 int i;
1003 for (i = 0; i < ep_rv; ++i) {
1004 grpc_fd *fd = ep_ev[i].data.ptr;
1005 int cancel = ep_ev[i].events & (EPOLLERR | EPOLLHUP);
1006 int read_ev = ep_ev[i].events & (EPOLLIN | EPOLLPRI);
1007 int write_ev = ep_ev[i].events & EPOLLOUT;
1008 if (fd == NULL) {
1009 grpc_wakeup_fd_consume_wakeup(&grpc_global_wakeup_fd);
1010 } else {
1011 if (read_ev || cancel) {
1012 fd_become_readable(exec_ctx, fd);
1013 }
1014 if (write_ev || cancel) {
1015 fd_become_writable(exec_ctx, fd);
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001016 }
1017 }
1018 }
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -07001019 } while (ep_rv == GRPC_EPOLL_MAX_EVENTS);
1020 }
Sree Kuchibhotla79a62332016-06-04 14:01:03 -07001021 gpr_log(GPR_DEBUG, "pollset_work_and_unlock: Leaving..");
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001022 GPR_TIMER_END("pollset_work_and_unlock", 0);
1023}
1024
1025/* Release the reference to pollset->polling_island and set it to NULL.
1026 pollset->mu must be held */
1027static void pollset_release_polling_island_locked(grpc_pollset *pollset) {
1028 gpr_mu_lock(&pollset->pi_mu);
1029 if (pollset->polling_island) {
1030 pollset->polling_island =
1031 polling_island_update_and_lock(pollset->polling_island, 1, 0);
1032 polling_island_unref_and_unlock(pollset->polling_island, 1);
1033 pollset->polling_island = NULL;
1034 }
1035 gpr_mu_unlock(&pollset->pi_mu);
1036}
1037
1038static void finish_shutdown_locked(grpc_exec_ctx *exec_ctx,
1039 grpc_pollset *pollset) {
1040 /* The pollset cannot have any workers if we are at this stage */
1041 GPR_ASSERT(!pollset_has_workers(pollset));
1042
1043 pollset->finish_shutdown_called = true;
1044 pollset_release_polling_island_locked(pollset);
1045
1046 grpc_exec_ctx_enqueue(exec_ctx, pollset->shutdown_done, true, NULL);
1047}
1048
1049/* pollset->mu lock must be held by the caller before calling this */
1050static void pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1051 grpc_closure *closure) {
1052 GPR_TIMER_BEGIN("pollset_shutdown", 0);
1053 GPR_ASSERT(!pollset->shutting_down);
1054 pollset->shutting_down = true;
1055 pollset->shutdown_done = closure;
1056 pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
1057
1058 /* If the pollset has any workers, we cannot call finish_shutdown_locked()
1059 because it would release the underlying polling island. In such a case, we
1060 let the last worker call finish_shutdown_locked() from pollset_work() */
1061 if (!pollset_has_workers(pollset)) {
1062 GPR_ASSERT(!pollset->finish_shutdown_called);
1063 GPR_TIMER_MARK("pollset_shutdown.finish_shutdown_locked", 0);
1064 finish_shutdown_locked(exec_ctx, pollset);
1065 }
1066 GPR_TIMER_END("pollset_shutdown", 0);
1067}
1068
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -07001069/* pollset_shutdown is guaranteed to be called before pollset_destroy. So other
1070 * than destroying the mutexes, there is nothing special that needs to be done
1071 * here */
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001072static void pollset_destroy(grpc_pollset *pollset) {
1073 GPR_ASSERT(!pollset_has_workers(pollset));
1074 gpr_mu_destroy(&pollset->pi_mu);
1075 gpr_mu_destroy(&pollset->mu);
1076}
1077
1078static void pollset_reset(grpc_pollset *pollset) {
1079 GPR_ASSERT(pollset->shutting_down);
1080 GPR_ASSERT(!pollset_has_workers(pollset));
1081 pollset->shutting_down = false;
1082 pollset->finish_shutdown_called = false;
1083 pollset->kicked_without_pollers = false;
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -07001084 pollset->shutdown_done = NULL;
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001085 pollset_release_polling_island_locked(pollset);
1086}
1087
1088/* pollset->mu lock must be held by the caller before calling this.
1089 The function pollset_work() may temporarily release the lock (pollset->mu)
1090 during the course of its execution but it will always re-acquire the lock and
1091 ensure that it is held by the time the function returns */
1092static void pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1093 grpc_pollset_worker **worker_hdl, gpr_timespec now,
1094 gpr_timespec deadline) {
1095 GPR_TIMER_BEGIN("pollset_work", 0);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -07001096 gpr_log(GPR_DEBUG, "pollset_work: enter");
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001097 int timeout_ms = poll_deadline_to_millis_timeout(deadline, now);
1098
1099 sigset_t new_mask;
1100 sigset_t orig_mask;
1101
1102 grpc_pollset_worker worker;
1103 worker.next = worker.prev = NULL;
1104 worker.kicked_specifically = 0;
1105 worker.pt_id = pthread_self();
1106
1107 *worker_hdl = &worker;
1108
1109 if (pollset->kicked_without_pollers) {
1110 /* If the pollset was kicked without pollers, pretend that the current
1111 worker got the kick and skip polling. A kick indicates that there is some
1112 work that needs attention like an event on the completion queue or an
1113 alarm */
1114 GPR_TIMER_MARK("pollset_work.kicked_without_pollers", 0);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -07001115 gpr_log(GPR_INFO, "pollset_work: kicked without pollers..");
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001116 pollset->kicked_without_pollers = 0;
1117 } else if (!pollset->shutting_down) {
1118 sigemptyset(&new_mask);
1119 sigaddset(&new_mask, SIGUSR1);
1120 pthread_sigmask(SIG_BLOCK, &new_mask, &orig_mask);
1121 sigdelset(&orig_mask, SIGUSR1);
1122
1123 push_front_worker(pollset, &worker);
1124
1125 pollset_work_and_unlock(exec_ctx, pollset, timeout_ms, &orig_mask);
1126 grpc_exec_ctx_flush(exec_ctx);
1127
1128 gpr_mu_lock(&pollset->mu);
1129 remove_worker(pollset, &worker);
1130 }
1131
1132 /* If we are the last worker on the pollset (i.e pollset_has_workers() is
1133 false at this point) and the pollset is shutting down, we may have to
1134 finish the shutdown process by calling finish_shutdown_locked().
1135 See pollset_shutdown() for more details.
1136
1137 Note: Continuing to access pollset here is safe; it is the caller's
1138 responsibility to not destroy a pollset when it has outstanding calls to
1139 pollset_work() */
1140 if (pollset->shutting_down && !pollset_has_workers(pollset) &&
1141 !pollset->finish_shutdown_called) {
1142 GPR_TIMER_MARK("pollset_work.finish_shutdown_locked", 0);
1143 finish_shutdown_locked(exec_ctx, pollset);
1144
1145 gpr_mu_unlock(&pollset->mu);
1146 grpc_exec_ctx_flush(exec_ctx);
1147 gpr_mu_lock(&pollset->mu);
1148 }
1149
Sree Kuchibhotla79a62332016-06-04 14:01:03 -07001150 gpr_log(GPR_DEBUG, "pollset_work(): leaving");
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001151 *worker_hdl = NULL;
1152 GPR_TIMER_END("pollset_work", 0);
1153}
1154
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001155static void pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1156 grpc_fd *fd) {
Sree Kuchibhotla79a62332016-06-04 14:01:03 -07001157 gpr_log(GPR_DEBUG, "pollset_add_fd: pollset: %p, fd: %d", pollset, fd->fd);
Sree Kuchibhotla9bc3d2d2016-06-06 10:27:56 -07001158 /* TODO sreek - Double check if we need to get a pollset->mu lock here */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001159 gpr_mu_lock(&pollset->pi_mu);
1160 gpr_mu_lock(&fd->pi_mu);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001161
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001162 polling_island *pi_new = NULL;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001163
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001164 /* 1) If fd->polling_island and pollset->polling_island are both non-NULL and
1165 * equal, do nothing.
1166 * 2) If fd->polling_island and pollset->polling_island are both NULL, create
1167 * a new polling island (with a refcount of 2) and make the polling_island
1168 * fields in both fd and pollset to point to the new island
1169 * 3) If one of fd->polling_island or pollset->polling_island is NULL, update
1170 * the NULL polling_island field to point to the non-NULL polling_island
1171 * field (ensure that the refcount on the polling island is incremented by
1172 * 1 to account for the newly added reference)
1173 * 4) Finally, if fd->polling_island and pollset->polling_island are non-NULL
1174 * and different, merge both the polling islands and update the
1175 * polling_island fields in both fd and pollset to point to the merged
1176 * polling island.
1177 */
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001178 if (fd->polling_island == pollset->polling_island) {
1179 pi_new = fd->polling_island;
1180 if (pi_new == NULL) {
1181 pi_new = polling_island_create(fd, 2);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001182 }
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001183 } else if (fd->polling_island == NULL) {
1184 pi_new = polling_island_update_and_lock(pollset->polling_island, 1, 1);
Sree Kuchibhotla79a62332016-06-04 14:01:03 -07001185 polling_island_add_fds_locked(pollset->polling_island, &fd, 1, true);
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -07001186 gpr_mu_unlock(&pi_new->mu);
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001187 } else if (pollset->polling_island == NULL) {
1188 pi_new = polling_island_update_and_lock(fd->polling_island, 1, 1);
Sree Kuchibhotla88ee12f2016-06-03 19:26:48 -07001189 gpr_mu_unlock(&pi_new->mu);
Sree Kuchibhotla5098f912016-05-31 10:58:17 -07001190 } else {
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001191 pi_new = polling_island_merge(fd->polling_island, pollset->polling_island);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001192 }
1193
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001194 fd->polling_island = pollset->polling_island = pi_new;
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001195
Sree Kuchibhotla9442bab2016-05-20 17:54:06 -07001196 gpr_mu_unlock(&fd->pi_mu);
1197 gpr_mu_unlock(&pollset->pi_mu);
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001198}
1199
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001200/*******************************************************************************
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001201 * Pollset-set Definitions
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001202 */
1203
1204static grpc_pollset_set *pollset_set_create(void) {
1205 grpc_pollset_set *pollset_set = gpr_malloc(sizeof(*pollset_set));
1206 memset(pollset_set, 0, sizeof(*pollset_set));
1207 gpr_mu_init(&pollset_set->mu);
1208 return pollset_set;
1209}
1210
1211static void pollset_set_destroy(grpc_pollset_set *pollset_set) {
1212 size_t i;
1213 gpr_mu_destroy(&pollset_set->mu);
1214 for (i = 0; i < pollset_set->fd_count; i++) {
1215 GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set");
1216 }
1217 gpr_free(pollset_set->pollsets);
1218 gpr_free(pollset_set->pollset_sets);
1219 gpr_free(pollset_set->fds);
1220 gpr_free(pollset_set);
1221}
1222
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001223static void pollset_set_add_fd(grpc_exec_ctx *exec_ctx,
1224 grpc_pollset_set *pollset_set, grpc_fd *fd) {
1225 size_t i;
1226 gpr_mu_lock(&pollset_set->mu);
1227 if (pollset_set->fd_count == pollset_set->fd_capacity) {
1228 pollset_set->fd_capacity = GPR_MAX(8, 2 * pollset_set->fd_capacity);
1229 pollset_set->fds = gpr_realloc(
1230 pollset_set->fds, pollset_set->fd_capacity * sizeof(*pollset_set->fds));
1231 }
1232 GRPC_FD_REF(fd, "pollset_set");
1233 pollset_set->fds[pollset_set->fd_count++] = fd;
1234 for (i = 0; i < pollset_set->pollset_count; i++) {
1235 pollset_add_fd(exec_ctx, pollset_set->pollsets[i], fd);
1236 }
1237 for (i = 0; i < pollset_set->pollset_set_count; i++) {
1238 pollset_set_add_fd(exec_ctx, pollset_set->pollset_sets[i], fd);
1239 }
1240 gpr_mu_unlock(&pollset_set->mu);
1241}
1242
1243static void pollset_set_del_fd(grpc_exec_ctx *exec_ctx,
1244 grpc_pollset_set *pollset_set, grpc_fd *fd) {
1245 size_t i;
1246 gpr_mu_lock(&pollset_set->mu);
1247 for (i = 0; i < pollset_set->fd_count; i++) {
1248 if (pollset_set->fds[i] == fd) {
1249 pollset_set->fd_count--;
1250 GPR_SWAP(grpc_fd *, pollset_set->fds[i],
1251 pollset_set->fds[pollset_set->fd_count]);
1252 GRPC_FD_UNREF(fd, "pollset_set");
1253 break;
1254 }
1255 }
1256 for (i = 0; i < pollset_set->pollset_set_count; i++) {
1257 pollset_set_del_fd(exec_ctx, pollset_set->pollset_sets[i], fd);
1258 }
1259 gpr_mu_unlock(&pollset_set->mu);
1260}
1261
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001262static void pollset_set_add_pollset(grpc_exec_ctx *exec_ctx,
1263 grpc_pollset_set *pollset_set,
1264 grpc_pollset *pollset) {
1265 size_t i, j;
1266 gpr_mu_lock(&pollset_set->mu);
1267 if (pollset_set->pollset_count == pollset_set->pollset_capacity) {
1268 pollset_set->pollset_capacity =
1269 GPR_MAX(8, 2 * pollset_set->pollset_capacity);
1270 pollset_set->pollsets =
1271 gpr_realloc(pollset_set->pollsets, pollset_set->pollset_capacity *
1272 sizeof(*pollset_set->pollsets));
1273 }
1274 pollset_set->pollsets[pollset_set->pollset_count++] = pollset;
1275 for (i = 0, j = 0; i < pollset_set->fd_count; i++) {
1276 if (fd_is_orphaned(pollset_set->fds[i])) {
1277 GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set");
1278 } else {
1279 pollset_add_fd(exec_ctx, pollset, pollset_set->fds[i]);
1280 pollset_set->fds[j++] = pollset_set->fds[i];
1281 }
1282 }
1283 pollset_set->fd_count = j;
1284 gpr_mu_unlock(&pollset_set->mu);
1285}
1286
1287static void pollset_set_del_pollset(grpc_exec_ctx *exec_ctx,
1288 grpc_pollset_set *pollset_set,
1289 grpc_pollset *pollset) {
1290 size_t i;
1291 gpr_mu_lock(&pollset_set->mu);
1292 for (i = 0; i < pollset_set->pollset_count; i++) {
1293 if (pollset_set->pollsets[i] == pollset) {
1294 pollset_set->pollset_count--;
1295 GPR_SWAP(grpc_pollset *, pollset_set->pollsets[i],
1296 pollset_set->pollsets[pollset_set->pollset_count]);
1297 break;
1298 }
1299 }
1300 gpr_mu_unlock(&pollset_set->mu);
1301}
1302
1303static void pollset_set_add_pollset_set(grpc_exec_ctx *exec_ctx,
1304 grpc_pollset_set *bag,
1305 grpc_pollset_set *item) {
1306 size_t i, j;
1307 gpr_mu_lock(&bag->mu);
1308 if (bag->pollset_set_count == bag->pollset_set_capacity) {
1309 bag->pollset_set_capacity = GPR_MAX(8, 2 * bag->pollset_set_capacity);
1310 bag->pollset_sets =
1311 gpr_realloc(bag->pollset_sets,
1312 bag->pollset_set_capacity * sizeof(*bag->pollset_sets));
1313 }
1314 bag->pollset_sets[bag->pollset_set_count++] = item;
1315 for (i = 0, j = 0; i < bag->fd_count; i++) {
1316 if (fd_is_orphaned(bag->fds[i])) {
1317 GRPC_FD_UNREF(bag->fds[i], "pollset_set");
1318 } else {
1319 pollset_set_add_fd(exec_ctx, item, bag->fds[i]);
1320 bag->fds[j++] = bag->fds[i];
1321 }
1322 }
1323 bag->fd_count = j;
1324 gpr_mu_unlock(&bag->mu);
1325}
1326
1327static void pollset_set_del_pollset_set(grpc_exec_ctx *exec_ctx,
1328 grpc_pollset_set *bag,
1329 grpc_pollset_set *item) {
1330 size_t i;
1331 gpr_mu_lock(&bag->mu);
1332 for (i = 0; i < bag->pollset_set_count; i++) {
1333 if (bag->pollset_sets[i] == item) {
1334 bag->pollset_set_count--;
1335 GPR_SWAP(grpc_pollset_set *, bag->pollset_sets[i],
1336 bag->pollset_sets[bag->pollset_set_count]);
1337 break;
1338 }
1339 }
1340 gpr_mu_unlock(&bag->mu);
1341}
1342
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001343/*******************************************************************************
Sree Kuchibhotla0bcbd792016-06-01 15:43:03 -07001344 * Event engine binding
Sree Kuchibhotlaf448c342016-05-19 10:51:24 -07001345 */
1346
1347static void shutdown_engine(void) {
1348 fd_global_shutdown();
1349 pollset_global_shutdown();
1350}
1351
1352static const grpc_event_engine_vtable vtable = {
1353 .pollset_size = sizeof(grpc_pollset),
1354
1355 .fd_create = fd_create,
1356 .fd_wrapped_fd = fd_wrapped_fd,
1357 .fd_orphan = fd_orphan,
1358 .fd_shutdown = fd_shutdown,
1359 .fd_notify_on_read = fd_notify_on_read,
1360 .fd_notify_on_write = fd_notify_on_write,
1361
1362 .pollset_init = pollset_init,
1363 .pollset_shutdown = pollset_shutdown,
1364 .pollset_reset = pollset_reset,
1365 .pollset_destroy = pollset_destroy,
1366 .pollset_work = pollset_work,
1367 .pollset_kick = pollset_kick,
1368 .pollset_add_fd = pollset_add_fd,
1369
1370 .pollset_set_create = pollset_set_create,
1371 .pollset_set_destroy = pollset_set_destroy,
1372 .pollset_set_add_pollset = pollset_set_add_pollset,
1373 .pollset_set_del_pollset = pollset_set_del_pollset,
1374 .pollset_set_add_pollset_set = pollset_set_add_pollset_set,
1375 .pollset_set_del_pollset_set = pollset_set_del_pollset_set,
1376 .pollset_set_add_fd = pollset_set_add_fd,
1377 .pollset_set_del_fd = pollset_set_del_fd,
1378
1379 .kick_poller = kick_poller,
1380
1381 .shutdown_engine = shutdown_engine,
1382};
1383
1384const grpc_event_engine_vtable *grpc_init_epoll_linux(void) {
1385 fd_global_init();
1386 pollset_global_init();
1387 polling_island_global_init();
1388 return &vtable;
1389}
1390
1391#endif