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