Debian Codegraph @

src/core/lib/iomgr/ev_posix.h

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #ifndef GRPC_CORE_LIB_IOMGR_EV_POSIX_H
20 #define GRPC_CORE_LIB_IOMGR_EV_POSIX_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <poll.h>
25
26 #include "src/core/lib/debug/trace.h"
27 #include "src/core/lib/gprpp/global_config.h"
28 #include "src/core/lib/iomgr/exec_ctx.h"
29 #include "src/core/lib/iomgr/pollset.h"
30 #include "src/core/lib/iomgr/pollset_set.h"
31 #include "src/core/lib/iomgr/wakeup_fd_posix.h"
32
33 GPR_GLOBAL_CONFIG_DECLARE_STRING(grpc_poll_strategy);
34
35 extern grpc_core::DebugOnlyTraceFlag grpc_fd_trace; /* Disabled by default */
36 extern grpc_core::DebugOnlyTraceFlag
37 grpc_polling_trace; /* Disabled by default */
38
39 #define GRPC_FD_TRACE(format, ...) \
40 if (GRPC_TRACE_FLAG_ENABLED(grpc_fd_trace)) { \
41 gpr_log(GPR_INFO, "(fd-trace) " format, __VA_ARGS__); \
42 }
43
44 typedef struct grpc_fd grpc_fd;
45
46 typedef struct grpc_event_engine_vtable {
47 size_t pollset_size;
48 bool can_track_err;
49 bool run_in_background;
50
51 grpc_fd* (*fd_create)(int fd, const char* name, bool track_err);
52 int (*fd_wrapped_fd)(grpc_fd* fd);
53 void (*fd_orphan)(grpc_fd* fd, grpc_closure* on_done, int* release_fd,
54 const char* reason);
55 void (*fd_shutdown)(grpc_fd* fd, grpc_error_handle why);
56 void (*fd_notify_on_read)(grpc_fd* fd, grpc_closure* closure);
57 void (*fd_notify_on_write)(grpc_fd* fd, grpc_closure* closure);
58 void (*fd_notify_on_error)(grpc_fd* fd, grpc_closure* closure);
59 void (*fd_set_readable)(grpc_fd* fd);
60 void (*fd_set_writable)(grpc_fd* fd);
61 void (*fd_set_error)(grpc_fd* fd);
62 bool (*fd_is_shutdown)(grpc_fd* fd);
63
64 void (*pollset_init)(grpc_pollset* pollset, gpr_mu** mu);
65 void (*pollset_shutdown)(grpc_pollset* pollset, grpc_closure* closure);
66 void (*pollset_destroy)(grpc_pollset* pollset);
67 grpc_error_handle (*pollset_work)(grpc_pollset* pollset,
68 grpc_pollset_worker** worker,
69 grpc_core::Timestamp deadline);
70 grpc_error_handle (*pollset_kick)(grpc_pollset* pollset,
71 grpc_pollset_worker* specific_worker);
72 void (*pollset_add_fd)(grpc_pollset* pollset, struct grpc_fd* fd);
73
74 grpc_pollset_set* (*pollset_set_create)(void);
75 void (*pollset_set_destroy)(grpc_pollset_set* pollset_set);
76 void (*pollset_set_add_pollset)(grpc_pollset_set* pollset_set,
77 grpc_pollset* pollset);
78 void (*pollset_set_del_pollset)(grpc_pollset_set* pollset_set,
79 grpc_pollset* pollset);
80 void (*pollset_set_add_pollset_set)(grpc_pollset_set* bag,
81 grpc_pollset_set* item);
82 void (*pollset_set_del_pollset_set)(grpc_pollset_set* bag,
83 grpc_pollset_set* item);
84 void (*pollset_set_add_fd)(grpc_pollset_set* pollset_set, grpc_fd* fd);
85 void (*pollset_set_del_fd)(grpc_pollset_set* pollset_set, grpc_fd* fd);
86
87 bool (*is_any_background_poller_thread)(void);
88 const char* name;
89 bool (*check_engine_available)(bool explicit_request);
90 void (*init_engine)();
91 void (*shutdown_background_closure)(void);
92 void (*shutdown_engine)(void);
93 bool (*add_closure_to_background_poller)(grpc_closure* closure,
94 grpc_error_handle error);
95 } grpc_event_engine_vtable;
96
97 /* register a new event engine factory */
98 void grpc_register_event_engine_factory(const grpc_event_engine_vtable* vtable,
99 bool add_at_head);
100
101 void grpc_event_engine_init(void);
102 void grpc_event_engine_shutdown(void);
103
104 /* Return the name of the poll strategy */
105 const char* grpc_get_poll_strategy_name();
106
107 /* Returns true if polling engine can track errors separately, false otherwise.
108 * If this is true, fd can be created with track_err set. After this, error
109 * events will be reported using fd_notify_on_error. If it is not set, errors
110 * will continue to be reported through fd_notify_on_read and
111 * fd_notify_on_write.
112 */
113 bool grpc_event_engine_can_track_errors();
114
115 /* Returns true if polling engine runs in the background, false otherwise.
116 * Currently only 'epollbg' runs in the background.
117 */
118 bool grpc_event_engine_run_in_background();
119
120 /* Create a wrapped file descriptor.
121 Requires fd is a non-blocking file descriptor.
122 \a track_err if true means that error events would be tracked separately
123 using grpc_fd_notify_on_error. Currently, valid only for linux systems.
124 This takes ownership of closing fd. */
125 grpc_fd* grpc_fd_create(int fd, const char* name, bool track_err);
126
127 /* Return the wrapped fd, or -1 if it has been released or closed. */
128 int grpc_fd_wrapped_fd(grpc_fd* fd);
129
130 /* Releases fd to be asynchronously destroyed.
131 on_done is called when the underlying file descriptor is definitely close()d.
132 If on_done is NULL, no callback will be made.
133 If release_fd is not NULL, it's set to fd and fd will not be closed.
134 Requires: *fd initialized; no outstanding notify_on_read or
135 notify_on_write.
136 MUST NOT be called with a pollset lock taken */
137 void grpc_fd_orphan(grpc_fd* fd, grpc_closure* on_done, int* release_fd,
138 const char* reason);
139
140 /* Has grpc_fd_shutdown been called on an fd? */
141 bool grpc_fd_is_shutdown(grpc_fd* fd);
142
143 /* Cause any current and future callbacks to fail. */
144 void grpc_fd_shutdown(grpc_fd* fd, grpc_error_handle why);
145
146 /* Register read interest, causing read_cb to be called once when fd becomes
147 readable, on deadline specified by deadline, or on shutdown triggered by
148 grpc_fd_shutdown.
149 read_cb will be called with read_cb_arg when *fd becomes readable.
150 read_cb is Called with status of GRPC_CALLBACK_SUCCESS if readable,
151 GRPC_CALLBACK_TIMED_OUT if the call timed out,
152 and CANCELLED if the call was cancelled.
153
154 Requires:This method must not be called before the read_cb for any previous
155 call runs. Edge triggered events are used whenever they are supported by the
156 underlying platform. This means that users must drain fd in read_cb before
157 calling notify_on_read again. Users are also expected to handle spurious
158 events, i.e read_cb is called while nothing can be readable from fd */
159 void grpc_fd_notify_on_read(grpc_fd* fd, grpc_closure* closure);
160
161 /* Exactly the same semantics as above, except based on writable events. */
162 void grpc_fd_notify_on_write(grpc_fd* fd, grpc_closure* closure);
163
164 /* Exactly the same semantics as above, except based on error events. track_err
165 * needs to have been set on grpc_fd_create */
166 void grpc_fd_notify_on_error(grpc_fd* fd, grpc_closure* closure);
167
168 /* Forcibly set the fd to be readable, resulting in the closure registered with
169 * grpc_fd_notify_on_read being invoked.
170 */
171 void grpc_fd_set_readable(grpc_fd* fd);
172
173 /* Forcibly set the fd to be writable, resulting in the closure registered with
174 * grpc_fd_notify_on_write being invoked.
175 */
176 void grpc_fd_set_writable(grpc_fd* fd);
177
178 /* Forcibly set the fd to have errored, resulting in the closure registered with
179 * grpc_fd_notify_on_error being invoked.
180 */
181 void grpc_fd_set_error(grpc_fd* fd);
182
183 /* pollset_posix functions */
184
185 /* Add an fd to a pollset */
186 void grpc_pollset_add_fd(grpc_pollset* pollset, struct grpc_fd* fd);
187
188 /* pollset_set_posix functions */
189
190 void grpc_pollset_set_add_fd(grpc_pollset_set* pollset_set, grpc_fd* fd);
191 void grpc_pollset_set_del_fd(grpc_pollset_set* pollset_set, grpc_fd* fd);
192
193 /* Returns true if the caller is a worker thread for any background poller. */
194 bool grpc_is_any_background_poller_thread();
195
196 /* Returns true if the closure is registered into the background poller. Note
197 * that the closure may or may not run yet when this function returns, and the
198 * closure should not be blocking or long-running. */
199 bool grpc_add_closure_to_background_poller(grpc_closure* closure,
200 grpc_error_handle error);
201
202 /* Shut down all the closures registered in the background poller. */
203 void grpc_shutdown_background_closure();
204
205 /* override to allow tests to hook poll() usage */
206 typedef int (*grpc_poll_function_type)(struct pollfd*, nfds_t, int);
207 extern grpc_poll_function_type grpc_poll_function;
208
209 #endif /* GRPC_CORE_LIB_IOMGR_EV_POSIX_H */