blob: c4d1977a0b6b1e52db897e8088dbd555fae53e00 [file] [log] [blame]
ctiller58393c22015-01-07 14:03:30 -08001/*
2 *
murgatroid993466c4b2016-01-12 10:26:04 -08003 * Copyright 2015-2016, Google Inc.
ctiller58393c22015-01-07 14:03:30 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_IOMGR_POLLSET_POSIX_H
35#define GRPC_CORE_LIB_IOMGR_POLLSET_POSIX_H
ctiller58393c22015-01-07 14:03:30 -080036
Craig Tiller69f90e62015-08-06 08:32:35 -070037#include <poll.h>
38
ctiller58393c22015-01-07 14:03:30 -080039#include <grpc/support/sync.h>
Craig Tiller69b093b2016-02-25 19:04:07 -080040
Craig Tiller298751c2015-09-22 09:41:05 -070041#include "src/core/iomgr/exec_ctx.h"
Craig Tiller000cd8f2015-09-18 07:20:29 -070042#include "src/core/iomgr/iomgr.h"
Craig Tiller69b093b2016-02-25 19:04:07 -080043#include "src/core/iomgr/pollset.h"
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070044#include "src/core/iomgr/wakeup_fd_posix.h"
David Klempner7f3ed1e2015-01-16 15:35:56 -080045
ctiller58393c22015-01-07 14:03:30 -080046typedef struct grpc_pollset_vtable grpc_pollset_vtable;
47
48/* forward declare only in this file to avoid leaking impl details via
49 pollset.h; real users of grpc_fd should always include 'fd_posix.h' and not
50 use the struct tag */
51struct grpc_fd;
52
Craig Tillere8b5f622015-11-02 14:15:03 -080053typedef struct grpc_cached_wakeup_fd {
54 grpc_wakeup_fd fd;
55 struct grpc_cached_wakeup_fd *next;
56} grpc_cached_wakeup_fd;
57
Craig Tiller69b093b2016-02-25 19:04:07 -080058struct grpc_pollset_worker {
Craig Tillere8b5f622015-11-02 14:15:03 -080059 grpc_cached_wakeup_fd *wakeup_fd;
Craig Tiller988e37f2015-10-01 07:53:56 -070060 int reevaluate_polling_on_wakeup;
Craig Tiller66197ca2015-11-02 08:04:10 -080061 int kicked_specifically;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070062 struct grpc_pollset_worker *next;
63 struct grpc_pollset_worker *prev;
Craig Tiller69b093b2016-02-25 19:04:07 -080064};
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070065
Craig Tiller69b093b2016-02-25 19:04:07 -080066struct grpc_pollset {
ctiller58393c22015-01-07 14:03:30 -080067 /* pollsets under posix can mutate representation as fds are added and
68 removed.
69 For example, we may choose a poll() based implementation on linux for
70 few fds, and an epoll() based implementation for many fds */
71 const grpc_pollset_vtable *vtable;
Craig Tiller85371a22016-02-25 13:55:13 -080072 gpr_mu mu;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070073 grpc_pollset_worker root_worker;
David Klempnerb5056612015-02-24 14:22:50 -080074 int in_flight_cbs;
75 int shutting_down;
Jan Tattermuschd6ca8b42015-06-10 15:58:39 -070076 int called_shutdown;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070077 int kicked_without_pollers;
Craig Tillerd1bec032015-09-18 17:29:00 -070078 grpc_closure *shutdown_done;
Craig Tillerd9ccbbf2015-09-22 09:30:00 -070079 grpc_closure_list idle_jobs;
Craig Tillera82950e2015-09-22 12:33:20 -070080 union {
ctiller58393c22015-01-07 14:03:30 -080081 int fd;
82 void *ptr;
83 } data;
Craig Tillere8b5f622015-11-02 14:15:03 -080084 /* Local cache of eventfds for workers */
85 grpc_cached_wakeup_fd *local_wakeup_cache;
Craig Tiller69b093b2016-02-25 19:04:07 -080086};
ctiller58393c22015-01-07 14:03:30 -080087
Craig Tillera82950e2015-09-22 12:33:20 -070088struct grpc_pollset_vtable {
89 void (*add_fd)(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
90 struct grpc_fd *fd, int and_unlock_pollset);
Craig Tillera82950e2015-09-22 12:33:20 -070091 void (*maybe_work_and_unlock)(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
92 grpc_pollset_worker *worker,
93 gpr_timespec deadline, gpr_timespec now);
94 void (*finish_shutdown)(grpc_pollset *pollset);
95 void (*destroy)(grpc_pollset *pollset);
ctiller58393c22015-01-07 14:03:30 -080096};
97
ctiller58393c22015-01-07 14:03:30 -080098/* Add an fd to a pollset */
Craig Tillera82950e2015-09-22 12:33:20 -070099void grpc_pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
100 struct grpc_fd *fd);
ctiller58393c22015-01-07 14:03:30 -0800101
ctiller58393c22015-01-07 14:03:30 -0800102/* Returns the fd to listen on for kicks */
Craig Tillera82950e2015-09-22 12:33:20 -0700103int grpc_kick_read_fd(grpc_pollset *p);
ctiller58393c22015-01-07 14:03:30 -0800104/* Call after polling has been kicked to leave the kicked state */
Craig Tillera82950e2015-09-22 12:33:20 -0700105void grpc_kick_drain(grpc_pollset *p);
ctiller58393c22015-01-07 14:03:30 -0800106
Craig Tiller6174b9a2015-06-18 08:13:05 -0700107/* Convert a timespec to milliseconds:
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700108 - very small or negative poll times are clamped to zero to do a
Craig Tiller6174b9a2015-06-18 08:13:05 -0700109 non-blocking poll (which becomes spin polling)
110 - other small values are rounded up to one millisecond
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700111 - longer than a millisecond polls are rounded up to the next nearest
Craig Tiller6174b9a2015-06-18 08:13:05 -0700112 millisecond to avoid spinning
113 - infinite timeouts are converted to -1 */
Craig Tillera82950e2015-09-22 12:33:20 -0700114int grpc_poll_deadline_to_millis_timeout(gpr_timespec deadline,
115 gpr_timespec now);
Craig Tiller6174b9a2015-06-18 08:13:05 -0700116
Craig Tiller548735e2015-10-02 16:17:10 -0700117/* Allow kick to wakeup the currently polling worker */
Craig Tiller988e37f2015-10-01 07:53:56 -0700118#define GRPC_POLLSET_CAN_KICK_SELF 1
Craig Tiller548735e2015-10-02 16:17:10 -0700119/* Force the wakee to repoll when awoken */
Craig Tiller988e37f2015-10-01 07:53:56 -0700120#define GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP 2
Craig Tiller548735e2015-10-02 16:17:10 -0700121/* As per grpc_pollset_kick, with an extended set of flags (defined above)
122 -- mostly for fd_posix's use. */
Craig Tillerd0a00002015-10-06 11:30:37 -0700123void grpc_pollset_kick_ext(grpc_pollset *p,
124 grpc_pollset_worker *specific_worker,
Craig Tiller7536af02015-12-22 13:49:30 -0800125 uint32_t flags);
Craig Tiller988e37f2015-10-01 07:53:56 -0700126
ctiller58393c22015-01-07 14:03:30 -0800127/* turn a pollset into a multipoller: platform specific */
Craig Tillera82950e2015-09-22 12:33:20 -0700128typedef void (*grpc_platform_become_multipoller_type)(grpc_exec_ctx *exec_ctx,
129 grpc_pollset *pollset,
130 struct grpc_fd **fds,
131 size_t fd_count);
Craig Tiller1a6f7552015-05-29 13:56:44 -0700132extern grpc_platform_become_multipoller_type grpc_platform_become_multipoller;
133
Craig Tillera82950e2015-09-22 12:33:20 -0700134void grpc_poll_become_multipoller(grpc_exec_ctx *exec_ctx,
135 grpc_pollset *pollset, struct grpc_fd **fds,
136 size_t fd_count);
ctiller58393c22015-01-07 14:03:30 -0800137
Craig Tillere97c9b42015-07-30 14:10:03 -0700138/* Return 1 if the pollset has active threads in grpc_pollset_work (pollset must
139 * be locked) */
Craig Tillera82950e2015-09-22 12:33:20 -0700140int grpc_pollset_has_workers(grpc_pollset *pollset);
Craig Tillere97c9b42015-07-30 14:10:03 -0700141
yang-g8fefe372016-01-07 16:24:55 -0800142void grpc_remove_fd_from_all_epoll_sets(int fd);
143
Craig Tiller69f90e62015-08-06 08:32:35 -0700144/* override to allow tests to hook poll() usage */
vjpaicf4daeb2016-02-15 02:33:54 -0800145/* NOTE: Any changes to grpc_poll_function must take place when the gRPC
146 is certainly not doing any polling anywhere.
147 Otherwise, there might be a race between changing the variable and actually
148 doing a polling operation */
Craig Tillera82950e2015-09-22 12:33:20 -0700149typedef int (*grpc_poll_function_type)(struct pollfd *, nfds_t, int);
Craig Tiller69f90e62015-08-06 08:32:35 -0700150extern grpc_poll_function_type grpc_poll_function;
Craig Tiller8afeec82015-09-28 17:03:34 -0700151extern grpc_wakeup_fd grpc_global_wakeup_fd;
Craig Tiller69f90e62015-08-06 08:32:35 -0700152
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700153#endif /* GRPC_CORE_LIB_IOMGR_POLLSET_POSIX_H */