| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 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 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include <grpc/support/sync.h> |
| 22 |
|
| 23 |
#include "src/core/lib/iomgr/port.h" |
| 24 |
|
| 25 |
#ifdef GRPC_POSIX_SOCKET_EV_POLL |
| 26 |
|
| 27 |
#include <assert.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <limits.h> |
| 30 |
#include <poll.h> |
| 31 |
#include <string.h> |
| 32 |
#include <sys/socket.h> |
| 33 |
#include <unistd.h> |
| 34 |
|
| 35 |
#include <string> |
| 36 |
|
| 37 |
#include "absl/strings/str_cat.h" |
| 38 |
|
| 39 |
#include <grpc/support/alloc.h> |
| 40 |
#include <grpc/support/log.h> |
| 41 |
|
| 42 |
#include "src/core/lib/debug/stats.h" |
| 43 |
#include "src/core/lib/debug/stats_data.h" |
| 44 |
#include "src/core/lib/gpr/useful.h" |
| 45 |
#include "src/core/lib/gprpp/thd.h" |
| 46 |
#include "src/core/lib/iomgr/block_annotate.h" |
| 47 |
#include "src/core/lib/iomgr/ev_poll_posix.h" |
| 48 |
#include "src/core/lib/iomgr/iomgr_internal.h" |
| 49 |
#include "src/core/lib/iomgr/wakeup_fd_posix.h" |
| 50 |
|
| 51 |
#define GRPC_POLLSET_KICK_BROADCAST ((grpc_pollset_worker*)1) |
| 52 |
|
| 53 |
/******************************************************************************* |
| 54 |
* FD declarations |
| 55 |
*/ |
| 56 |
typedef struct grpc_fd_watcher { |
| 57 |
struct grpc_fd_watcher* next; |
| 58 |
struct grpc_fd_watcher* prev; |
| 59 |
grpc_pollset* pollset; |
| 60 |
grpc_pollset_worker* worker; |
| 61 |
grpc_fd* fd; |
| 62 |
} grpc_fd_watcher; |
| 63 |
|
| 64 |
typedef struct grpc_cached_wakeup_fd grpc_cached_wakeup_fd; |
| 65 |
|
| 66 |
/* Only used when GRPC_ENABLE_FORK_SUPPORT=1 */ |
| 67 |
struct grpc_fork_fd_list { |
| 68 |
/* Only one of fd or cached_wakeup_fd will be set. The unused field will be |
| 69 |
set to nullptr. */ |
| 70 |
grpc_fd* fd; |
| 71 |
grpc_cached_wakeup_fd* cached_wakeup_fd; |
| 72 |
|
| 73 |
grpc_fork_fd_list* next; |
| 74 |
grpc_fork_fd_list* prev; |
| 75 |
}; |
| 76 |
|
| 77 |
struct grpc_fd { |
| 78 |
int fd; |
| 79 |
/* refst format: |
| 80 |
bit0: 1=active/0=orphaned |
| 81 |
bit1-n: refcount |
| 82 |
meaning that mostly we ref by two to avoid altering the orphaned bit, |
| 83 |
and just unref by 1 when we're ready to flag the object as orphaned */ |
| 84 |
gpr_atm refst; |
| 85 |
|
| 86 |
gpr_mu mu; |
| 87 |
int shutdown; |
| 88 |
int closed; |
| 89 |
int released; |
| 90 |
gpr_atm pollhup; |
| 91 |
grpc_error_handle shutdown_error; |
| 92 |
|
| 93 |
/* The watcher list. |
| 94 |
|
| 95 |
The following watcher related fields are protected by watcher_mu. |
| 96 |
|
| 97 |
An fd_watcher is an ephemeral object created when an fd wants to |
| 98 |
begin polling, and destroyed after the poll. |
| 99 |
|
| 100 |
It denotes the fd's interest in whether to read poll or write poll |
| 101 |
or both or neither on this fd. |
| 102 |
|
| 103 |
If a watcher is asked to poll for reads or writes, the read_watcher |
| 104 |
or write_watcher fields are set respectively. A watcher may be asked |
| 105 |
to poll for both, in which case both fields will be set. |
| 106 |
|
| 107 |
read_watcher and write_watcher may be NULL if no watcher has been |
| 108 |
asked to poll for reads or writes. |
| 109 |
|
| 110 |
If an fd_watcher is not asked to poll for reads or writes, it's added |
| 111 |
to a linked list of inactive watchers, rooted at inactive_watcher_root. |
| 112 |
If at a later time there becomes need of a poller to poll, one of |
| 113 |
the inactive pollers may be kicked out of their poll loops to take |
| 114 |
that responsibility. */ |
| 115 |
grpc_fd_watcher inactive_watcher_root; |
| 116 |
grpc_fd_watcher* read_watcher; |
| 117 |
grpc_fd_watcher* write_watcher; |
| 118 |
|
| 119 |
grpc_closure* read_closure; |
| 120 |
grpc_closure* write_closure; |
| 121 |
|
| 122 |
grpc_closure* on_done_closure; |
| 123 |
|
| 124 |
grpc_iomgr_object iomgr_object; |
| 125 |
|
| 126 |
/* Only used when GRPC_ENABLE_FORK_SUPPORT=1 */ |
| 127 |
grpc_fork_fd_list* fork_fd_list; |
| 128 |
}; |
| 129 |
|
| 130 |
/* True when GRPC_ENABLE_FORK_SUPPORT=1. */ |
| 131 |
static bool track_fds_for_fork = false; |
| 132 |
|
| 133 |
/* Only used when GRPC_ENABLE_FORK_SUPPORT=1 */ |
| 134 |
static grpc_fork_fd_list* fork_fd_list_head = nullptr; |
| 135 |
static gpr_mu fork_fd_list_mu; |
| 136 |
|
| 137 |
/* Begin polling on an fd. |
| 138 |
Registers that the given pollset is interested in this fd - so that if read |
| 139 |
or writability interest changes, the pollset can be kicked to pick up that |
| 140 |
new interest. |
| 141 |
Return value is: |
| 142 |
(fd_needs_read? read_mask : 0) | (fd_needs_write? write_mask : 0) |
| 143 |
i.e. a combination of read_mask and write_mask determined by the fd's current |
| 144 |
interest in said events. |
| 145 |
Polling strategies that do not need to alter their behavior depending on the |
| 146 |
fd's current interest (such as epoll) do not need to call this function. |
| 147 |
MUST NOT be called with a pollset lock taken */ |
| 148 |
static uint32_t fd_begin_poll(grpc_fd* fd, grpc_pollset* pollset, |
| 149 |
grpc_pollset_worker* worker, uint32_t read_mask, |
| 150 |
uint32_t write_mask, grpc_fd_watcher* watcher); |
| 151 |
/* Complete polling previously started with fd_begin_poll |
| 152 |
MUST NOT be called with a pollset lock taken |
| 153 |
if got_read or got_write are 1, also does the become_{readable,writable} as |
| 154 |
appropriate. */ |
| 155 |
static void fd_end_poll(grpc_fd_watcher* watcher, int got_read, int got_write); |
| 156 |
|
| 157 |
/* Return 1 if this fd is orphaned, 0 otherwise */ |
| 158 |
static bool fd_is_orphaned(grpc_fd* fd); |
| 159 |
|
| 160 |
#ifndef NDEBUG |
| 161 |
static void fd_ref(grpc_fd* fd, const char* reason, const char* file, int line); |
| 162 |
static void fd_unref(grpc_fd* fd, const char* reason, const char* file, |
| 163 |
int line); |
| 164 |
#define GRPC_FD_REF(fd, reason) fd_ref(fd, reason, __FILE__, __LINE__) |
| 165 |
#define GRPC_FD_UNREF(fd, reason) fd_unref(fd, reason, __FILE__, __LINE__) |
| 166 |
#else |
| 167 |
static void fd_ref(grpc_fd* fd); |
| 168 |
static void fd_unref(grpc_fd* fd); |
| 169 |
#define GRPC_FD_REF(fd, reason) fd_ref(fd) |
| 170 |
#define GRPC_FD_UNREF(fd, reason) fd_unref(fd) |
| 171 |
#endif |
| 172 |
|
| 173 |
#define CLOSURE_NOT_READY ((grpc_closure*)0) |
| 174 |
#define CLOSURE_READY ((grpc_closure*)1) |
| 175 |
|
| 176 |
/******************************************************************************* |
| 177 |
* pollset declarations |
| 178 |
*/ |
| 179 |
|
| 180 |
typedef struct grpc_cached_wakeup_fd { |
| 181 |
grpc_wakeup_fd fd; |
| 182 |
struct grpc_cached_wakeup_fd* next; |
| 183 |
|
| 184 |
/* Only used when GRPC_ENABLE_FORK_SUPPORT=1 */ |
| 185 |
grpc_fork_fd_list* fork_fd_list; |
| 186 |
} grpc_cached_wakeup_fd; |
| 187 |
|
| 188 |
struct grpc_pollset_worker { |
| 189 |
grpc_cached_wakeup_fd* wakeup_fd; |
| 190 |
int reevaluate_polling_on_wakeup; |
| 191 |
int kicked_specifically; |
| 192 |
struct grpc_pollset_worker* next; |
| 193 |
struct grpc_pollset_worker* prev; |
| 194 |
}; |
| 195 |
|
| 196 |
struct grpc_pollset { |
| 197 |
gpr_mu mu; |
| 198 |
grpc_pollset_worker root_worker; |
| 199 |
int shutting_down; |
| 200 |
int called_shutdown; |
| 201 |
int kicked_without_pollers; |
| 202 |
grpc_closure* shutdown_done; |
| 203 |
int pollset_set_count; |
| 204 |
/* all polled fds */ |
| 205 |
size_t fd_count; |
| 206 |
size_t fd_capacity; |
| 207 |
grpc_fd** fds; |
| 208 |
/* Local cache of eventfds for workers */ |
| 209 |
grpc_cached_wakeup_fd* local_wakeup_cache; |
| 210 |
}; |
| 211 |
|
| 212 |
/* Add an fd to a pollset */ |
| 213 |
static void pollset_add_fd(grpc_pollset* pollset, struct grpc_fd* fd); |
| 214 |
|
| 215 |
static void pollset_set_add_fd(grpc_pollset_set* pollset_set, grpc_fd* fd); |
| 216 |
|
| 217 |
/* Convert a timespec to milliseconds: |
| 218 |
- very small or negative poll times are clamped to zero to do a |
| 219 |
non-blocking poll (which becomes spin polling) |
| 220 |
- other small values are rounded up to one millisecond |
| 221 |
- longer than a millisecond polls are rounded up to the next nearest |
| 222 |
millisecond to avoid spinning |
| 223 |
- infinite timeouts are converted to -1 */ |
| 224 |
static int poll_deadline_to_millis_timeout(grpc_core::Timestamp deadline); |
| 225 |
|
| 226 |
/* Allow kick to wakeup the currently polling worker */ |
| 227 |
#define GRPC_POLLSET_CAN_KICK_SELF 1 |
| 228 |
/* Force the wakee to repoll when awoken */ |
| 229 |
#define GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP 2 |
| 230 |
/* As per pollset_kick, with an extended set of flags (defined above) |
| 231 |
-- mostly for fd_posix's use. */ |
| 232 |
static grpc_error_handle pollset_kick_ext(grpc_pollset* p, |
| 233 |
grpc_pollset_worker* specific_worker, |
| 234 |
uint32_t flags) GRPC_MUST_USE_RESULT; |
| 235 |
|
| 236 |
/* Return 1 if the pollset has active threads in pollset_work (pollset must |
| 237 |
* be locked) */ |
| 238 |
static bool pollset_has_workers(grpc_pollset* pollset); |
| 239 |
|
| 240 |
/******************************************************************************* |
| 241 |
* pollset_set definitions |
| 242 |
*/ |
| 243 |
|
| 244 |
struct grpc_pollset_set { |
| 245 |
gpr_mu mu; |
| 246 |
|
| 247 |
size_t pollset_count; |
| 248 |
size_t pollset_capacity; |
| 249 |
grpc_pollset** pollsets; |
| 250 |
|
| 251 |
size_t pollset_set_count; |
| 252 |
size_t pollset_set_capacity; |
| 253 |
struct grpc_pollset_set** pollset_sets; |
| 254 |
|
| 255 |
size_t fd_count; |
| 256 |
size_t fd_capacity; |
| 257 |
grpc_fd** fds; |
| 258 |
}; |
| 259 |
|
| 260 |
/******************************************************************************* |
| 261 |
* functions to track opened fds. No-ops unless track_fds_for_fork is true. |
| 262 |
*/ |
| 263 |
|
| 264 |
static void fork_fd_list_remove_node(grpc_fork_fd_list* node) { |
| 265 |
if (track_fds_for_fork) { |
| 266 |
gpr_mu_lock(&fork_fd_list_mu); |
| 267 |
if (fork_fd_list_head == node) { |
| 268 |
fork_fd_list_head = node->next; |
| 269 |
} |
| 270 |
if (node->prev != nullptr) { |
| 271 |
node->prev->next = node->next; |
| 272 |
} |
| 273 |
if (node->next != nullptr) { |
| 274 |
node->next->prev = node->prev; |
| 275 |
} |
| 276 |
gpr_free(node); |
| 277 |
gpr_mu_unlock(&fork_fd_list_mu); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
static void fork_fd_list_add_node(grpc_fork_fd_list* node) { |
| 282 |
gpr_mu_lock(&fork_fd_list_mu); |
| 283 |
node->next = fork_fd_list_head; |
| 284 |
node->prev = nullptr; |
| 285 |
if (fork_fd_list_head != nullptr) { |
| 286 |
fork_fd_list_head->prev = node; |
| 287 |
} |
| 288 |
fork_fd_list_head = node; |
| 289 |
gpr_mu_unlock(&fork_fd_list_mu); |
| 290 |
} |
| 291 |
|
| 292 |
static void fork_fd_list_add_grpc_fd(grpc_fd* fd) { |
| 293 |
if (track_fds_for_fork) { |
| 294 |
fd->fork_fd_list = |
| 295 |
static_cast<grpc_fork_fd_list*>(gpr_malloc(sizeof(grpc_fork_fd_list))); |
| 296 |
fd->fork_fd_list->fd = fd; |
| 297 |
fd->fork_fd_list->cached_wakeup_fd = nullptr; |
| 298 |
fork_fd_list_add_node(fd->fork_fd_list); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
static void fork_fd_list_add_wakeup_fd(grpc_cached_wakeup_fd* fd) { |
| 303 |
if (track_fds_for_fork) { |
| 304 |
fd->fork_fd_list = |
| 305 |
static_cast<grpc_fork_fd_list*>(gpr_malloc(sizeof(grpc_fork_fd_list))); |
| 306 |
fd->fork_fd_list->cached_wakeup_fd = fd; |
| 307 |
fd->fork_fd_list->fd = nullptr; |
| 308 |
fork_fd_list_add_node(fd->fork_fd_list); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/******************************************************************************* |
| 313 |
* fd_posix.c |
| 314 |
*/ |
| 315 |
|
| 316 |
#ifndef NDEBUG |
| 317 |
#define REF_BY(fd, n, reason) ref_by(fd, n, reason, __FILE__, __LINE__) |
| 318 |
#define UNREF_BY(fd, n, reason) unref_by(fd, n, reason, __FILE__, __LINE__) |
| 319 |
static void ref_by(grpc_fd* fd, int n, const char* reason, const char* file, |
| 320 |
int line) { |
| 321 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_trace_fd_refcount)) { |
| 322 |
gpr_log(GPR_DEBUG, |
| 323 |
"FD %d %p ref %d %" PRIdPTR " -> %" PRIdPTR " [%s; %s:%d]", |
| 324 |
fd->fd, fd, n, gpr_atm_no_barrier_load(&fd->refst), |
| 325 |
gpr_atm_no_barrier_load(&fd->refst) + n, reason, file, line); |
| 326 |
} |
| 327 |
#else |
| 328 |
#define REF_BY(fd, n, reason) \ |
| 329 |
do { \ |
| 330 |
ref_by(fd, n); \ |
| 331 |
(void)(reason); \ |
| 332 |
} while (0) |
| 333 |
#define UNREF_BY(fd, n, reason) \ |
| 334 |
do { \ |
| 335 |
unref_by(fd, n); \ |
| 336 |
(void)(reason); \ |
| 337 |
} while (0) |
| 338 |
static void ref_by(grpc_fd* fd, int n) { |
| 339 |
#endif |
| 340 |
GPR_ASSERT(gpr_atm_no_barrier_fetch_add(&fd->refst, n) > 0); |
| 341 |
} |
| 342 |
|
| 343 |
#ifndef NDEBUG |
| 344 |
static void unref_by(grpc_fd* fd, int n, const char* reason, const char* file, |
| 345 |
int line) { |
| 346 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_trace_fd_refcount)) { |
| 347 |
gpr_log(GPR_DEBUG, |
| 348 |
"FD %d %p unref %d %" PRIdPTR " -> %" PRIdPTR " [%s; %s:%d]", |
| 349 |
fd->fd, fd, n, gpr_atm_no_barrier_load(&fd->refst), |
| 350 |
gpr_atm_no_barrier_load(&fd->refst) - n, reason, file, line); |
| 351 |
} |
| 352 |
#else |
| 353 |
static void unref_by(grpc_fd* fd, int n) { |
| 354 |
#endif |
| 355 |
gpr_atm old = gpr_atm_full_fetch_add(&fd->refst, -n); |
| 356 |
if (old == n) { |
| 357 |
gpr_mu_destroy(&fd->mu); |
| 358 |
grpc_iomgr_unregister_object(&fd->iomgr_object); |
| 359 |
fork_fd_list_remove_node(fd->fork_fd_list); |
| 360 |
if (fd->shutdown) { |
| 361 |
} |
| 362 |
fd->shutdown_error.~Status(); |
| 363 |
gpr_free(fd); |
| 364 |
} else { |
| 365 |
GPR_ASSERT(old > n); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
static grpc_fd* fd_create(int fd, const char* name, bool track_err) { |
| 370 |
// Avoid unused-parameter warning for debug-only parameter |
| 371 |
(void)track_err; |
| 372 |
GPR_DEBUG_ASSERT(track_err == false); |
| 373 |
grpc_fd* r = static_cast<grpc_fd*>(gpr_malloc(sizeof(*r))); |
| 374 |
gpr_mu_init(&r->mu); |
| 375 |
gpr_atm_rel_store(&r->refst, 1); |
| 376 |
r->shutdown = 0; |
| 377 |
new (&r->shutdown_error) absl::Status(); |
| 378 |
r->read_closure = CLOSURE_NOT_READY; |
| 379 |
r->write_closure = CLOSURE_NOT_READY; |
| 380 |
r->fd = fd; |
| 381 |
r->inactive_watcher_root.next = r->inactive_watcher_root.prev = |
| 382 |
&r->inactive_watcher_root; |
| 383 |
r->read_watcher = r->write_watcher = nullptr; |
| 384 |
r->on_done_closure = nullptr; |
| 385 |
r->closed = 0; |
| 386 |
r->released = 0; |
| 387 |
gpr_atm_no_barrier_store(&r->pollhup, 0); |
| 388 |
|
| 389 |
std::string name2 = absl::StrCat(name, " fd=", fd); |
| 390 |
grpc_iomgr_register_object(&r->iomgr_object, name2.c_str()); |
| 391 |
fork_fd_list_add_grpc_fd(r); |
| 392 |
return r; |
| 393 |
} |
| 394 |
|
| 395 |
static bool fd_is_orphaned(grpc_fd* fd) { |
| 396 |
return (gpr_atm_acq_load(&fd->refst) & 1) == 0; |
| 397 |
} |
| 398 |
|
| 399 |
static grpc_error_handle pollset_kick_locked(grpc_fd_watcher* watcher) { |
| 400 |
gpr_mu_lock(&watcher->pollset->mu); |
| 401 |
GPR_ASSERT(watcher->worker); |
| 402 |
grpc_error_handle err = |
| 403 |
pollset_kick_ext(watcher->pollset, watcher->worker, |
| 404 |
GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP); |
| 405 |
gpr_mu_unlock(&watcher->pollset->mu); |
| 406 |
return err; |
| 407 |
} |
| 408 |
|
| 409 |
static void maybe_wake_one_watcher_locked(grpc_fd* fd) { |
| 410 |
if (fd->inactive_watcher_root.next != &fd->inactive_watcher_root) { |
| 411 |
(void)pollset_kick_locked(fd->inactive_watcher_root.next); |
| 412 |
} else if (fd->read_watcher) { |
| 413 |
(void)pollset_kick_locked(fd->read_watcher); |
| 414 |
} else if (fd->write_watcher) { |
| 415 |
(void)pollset_kick_locked(fd->write_watcher); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
static void wake_all_watchers_locked(grpc_fd* fd) { |
| 420 |
grpc_fd_watcher* watcher; |
| 421 |
for (watcher = fd->inactive_watcher_root.next; |
| 422 |
watcher != &fd->inactive_watcher_root; watcher = watcher->next) { |
| 423 |
(void)pollset_kick_locked(watcher); |
| 424 |
} |
| 425 |
if (fd->read_watcher) { |
| 426 |
(void)pollset_kick_locked(fd->read_watcher); |
| 427 |
} |
| 428 |
if (fd->write_watcher && fd->write_watcher != fd->read_watcher) { |
| 429 |
(void)pollset_kick_locked(fd->write_watcher); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
static int has_watchers(grpc_fd* fd) { |
| 434 |
return fd->read_watcher != nullptr || fd->write_watcher != nullptr || |
| 435 |
fd->inactive_watcher_root.next != &fd->inactive_watcher_root; |
| 436 |
} |
| 437 |
|
| 438 |
static void close_fd_locked(grpc_fd* fd) { |
| 439 |
fd->closed = 1; |
| 440 |
if (!fd->released) { |
| 441 |
close(fd->fd); |
| 442 |
} |
| 443 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, fd->on_done_closure, |
| 444 |
absl::OkStatus()); |
| 445 |
} |
| 446 |
|
| 447 |
static int fd_wrapped_fd(grpc_fd* fd) { |
| 448 |
if (fd->released || fd->closed) { |
| 449 |
return -1; |
| 450 |
} else { |
| 451 |
return fd->fd; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
static void fd_orphan(grpc_fd* fd, grpc_closure* on_done, int* release_fd, |
| 456 |
const char* reason) { |
| 457 |
fd->on_done_closure = on_done; |
| 458 |
fd->released = release_fd != nullptr; |
| 459 |
if (release_fd != nullptr) { |
| 460 |
*release_fd = fd->fd; |
| 461 |
fd->released = true; |
| 462 |
} |
| 463 |
gpr_mu_lock(&fd->mu); |
| 464 |
REF_BY(fd, 1, reason); /* remove active status, but keep referenced */ |
| 465 |
if (!has_watchers(fd)) { |
| 466 |
close_fd_locked(fd); |
| 467 |
} else { |
| 468 |
wake_all_watchers_locked(fd); |
| 469 |
} |
| 470 |
gpr_mu_unlock(&fd->mu); |
| 471 |
UNREF_BY(fd, 2, reason); /* drop the reference */ |
| 472 |
} |
| 473 |
|
| 474 |
/* increment refcount by two to avoid changing the orphan bit */ |
| 475 |
#ifndef NDEBUG |
| 476 |
static void fd_ref(grpc_fd* fd, const char* reason, const char* file, |
| 477 |
int line) { |
| 478 |
ref_by(fd, 2, reason, file, line); |
| 479 |
} |
| 480 |
|
| 481 |
static void fd_unref(grpc_fd* fd, const char* reason, const char* file, |
| 482 |
int line) { |
| 483 |
unref_by(fd, 2, reason, file, line); |
| 484 |
} |
| 485 |
#else |
| 486 |
static void fd_ref(grpc_fd* fd) { ref_by(fd, 2); } |
| 487 |
|
| 488 |
static void fd_unref(grpc_fd* fd) { unref_by(fd, 2); } |
| 489 |
#endif |
| 490 |
|
| 491 |
static grpc_error_handle fd_shutdown_error(grpc_fd* fd) { |
| 492 |
if (!fd->shutdown) { |
| 493 |
return absl::OkStatus(); |
| 494 |
} else { |
| 495 |
return grpc_error_set_int( |
| 496 |
GRPC_ERROR_CREATE_REFERENCING("FD shutdown", &fd->shutdown_error, 1), |
| 497 |
grpc_core::StatusIntProperty::kRpcStatus, GRPC_STATUS_UNAVAILABLE); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
static void notify_on_locked(grpc_fd* fd, grpc_closure** st, |
| 502 |
grpc_closure* closure) { |
| 503 |
if (fd->shutdown || gpr_atm_no_barrier_load(&fd->pollhup)) { |
| 504 |
grpc_core::ExecCtx::Run( |
| 505 |
DEBUG_LOCATION, closure, |
| 506 |
grpc_error_set_int(GRPC_ERROR_CREATE("FD shutdown"), |
| 507 |
grpc_core::StatusIntProperty::kRpcStatus, |
| 508 |
GRPC_STATUS_UNAVAILABLE)); |
| 509 |
} else if (*st == CLOSURE_NOT_READY) { |
| 510 |
/* not ready ==> switch to a waiting state by setting the closure */ |
| 511 |
*st = closure; |
| 512 |
} else if (*st == CLOSURE_READY) { |
| 513 |
/* already ready ==> queue the closure to run immediately */ |
| 514 |
*st = CLOSURE_NOT_READY; |
| 515 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, fd_shutdown_error(fd)); |
| 516 |
maybe_wake_one_watcher_locked(fd); |
| 517 |
} else { |
| 518 |
/* upcallptr was set to a different closure. This is an error! */ |
| 519 |
gpr_log(GPR_ERROR, |
| 520 |
"User called a notify_on function with a previous callback still " |
| 521 |
"pending"); |
| 522 |
abort(); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
/* returns 1 if state becomes not ready */ |
| 527 |
static int set_ready_locked(grpc_fd* fd, grpc_closure** st) { |
| 528 |
if (*st == CLOSURE_READY) { |
| 529 |
/* duplicate ready ==> ignore */ |
| 530 |
return 0; |
| 531 |
} else if (*st == CLOSURE_NOT_READY) { |
| 532 |
/* not ready, and not waiting ==> flag ready */ |
| 533 |
*st = CLOSURE_READY; |
| 534 |
return 0; |
| 535 |
} else { |
| 536 |
/* waiting ==> queue closure */ |
| 537 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, *st, fd_shutdown_error(fd)); |
| 538 |
*st = CLOSURE_NOT_READY; |
| 539 |
return 1; |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
static void fd_shutdown(grpc_fd* fd, grpc_error_handle why) { |
| 544 |
gpr_mu_lock(&fd->mu); |
| 545 |
/* only shutdown once */ |
| 546 |
if (!fd->shutdown) { |
| 547 |
fd->shutdown = 1; |
| 548 |
fd->shutdown_error = why; |
| 549 |
/* signal read/write closed to OS so that future operations fail */ |
| 550 |
shutdown(fd->fd, SHUT_RDWR); |
| 551 |
set_ready_locked(fd, &fd->read_closure); |
| 552 |
set_ready_locked(fd, &fd->write_closure); |
| 553 |
} |
| 554 |
gpr_mu_unlock(&fd->mu); |
| 555 |
} |
| 556 |
|
| 557 |
static bool fd_is_shutdown(grpc_fd* fd) { |
| 558 |
gpr_mu_lock(&fd->mu); |
| 559 |
bool r = fd->shutdown; |
| 560 |
gpr_mu_unlock(&fd->mu); |
| 561 |
return r; |
| 562 |
} |
| 563 |
|
| 564 |
static void fd_notify_on_read(grpc_fd* fd, grpc_closure* closure) { |
| 565 |
gpr_mu_lock(&fd->mu); |
| 566 |
notify_on_locked(fd, &fd->read_closure, closure); |
| 567 |
gpr_mu_unlock(&fd->mu); |
| 568 |
} |
| 569 |
|
| 570 |
static void fd_notify_on_write(grpc_fd* fd, grpc_closure* closure) { |
| 571 |
gpr_mu_lock(&fd->mu); |
| 572 |
notify_on_locked(fd, &fd->write_closure, closure); |
| 573 |
gpr_mu_unlock(&fd->mu); |
| 574 |
} |
| 575 |
|
| 576 |
static void fd_notify_on_error(grpc_fd* /*fd*/, grpc_closure* closure) { |
| 577 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_polling_trace)) { |
| 578 |
gpr_log(GPR_ERROR, "Polling engine does not support tracking errors."); |
| 579 |
} |
| 580 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, absl::CancelledError()); |
| 581 |
} |
| 582 |
|
| 583 |
static void fd_set_readable(grpc_fd* fd) { |
| 584 |
gpr_mu_lock(&fd->mu); |
| 585 |
set_ready_locked(fd, &fd->read_closure); |
| 586 |
gpr_mu_unlock(&fd->mu); |
| 587 |
} |
| 588 |
|
| 589 |
static void fd_set_writable(grpc_fd* fd) { |
| 590 |
gpr_mu_lock(&fd->mu); |
| 591 |
set_ready_locked(fd, &fd->write_closure); |
| 592 |
gpr_mu_unlock(&fd->mu); |
| 593 |
} |
| 594 |
|
| 595 |
static void fd_set_error(grpc_fd* /*fd*/) { |
| 596 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_polling_trace)) { |
| 597 |
gpr_log(GPR_ERROR, "Polling engine does not support tracking errors."); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
static uint32_t fd_begin_poll(grpc_fd* fd, grpc_pollset* pollset, |
| 602 |
grpc_pollset_worker* worker, uint32_t read_mask, |
| 603 |
uint32_t write_mask, grpc_fd_watcher* watcher) { |
| 604 |
uint32_t mask = 0; |
| 605 |
grpc_closure* cur; |
| 606 |
int requested; |
| 607 |
/* keep track of pollers that have requested our events, in case they change |
| 608 |
*/ |
| 609 |
GRPC_FD_REF(fd, "poll"); |
| 610 |
|
| 611 |
gpr_mu_lock(&fd->mu); |
| 612 |
|
| 613 |
/* if we are shutdown, then don't add to the watcher set */ |
| 614 |
if (fd->shutdown) { |
| 615 |
watcher->pollset = nullptr; |
| 616 |
watcher->worker = nullptr; |
| 617 |
gpr_mu_unlock(&fd->mu); |
| 618 |
GRPC_FD_UNREF(fd, "poll"); |
| 619 |
return 0; |
| 620 |
} |
| 621 |
|
| 622 |
/* if there is nobody polling for read, but we need to, then start doing so */ |
| 623 |
cur = fd->read_closure; |
| 624 |
requested = cur != CLOSURE_READY; |
| 625 |
if (read_mask && fd->read_watcher == nullptr && requested) { |
| 626 |
fd->read_watcher = watcher; |
| 627 |
mask |= read_mask; |
| 628 |
} |
| 629 |
/* if there is nobody polling for write, but we need to, then start doing so |
| 630 |
*/ |
| 631 |
cur = fd->write_closure; |
| 632 |
requested = cur != CLOSURE_READY; |
| 633 |
if (write_mask && fd->write_watcher == nullptr && requested) { |
| 634 |
fd->write_watcher = watcher; |
| 635 |
mask |= write_mask; |
| 636 |
} |
| 637 |
/* if not polling, remember this watcher in case we need someone to later */ |
| 638 |
if (mask == 0 && worker != nullptr) { |
| 639 |
watcher->next = &fd->inactive_watcher_root; |
| 640 |
watcher->prev = watcher->next->prev; |
| 641 |
watcher->next->prev = watcher->prev->next = watcher; |
| 642 |
} |
| 643 |
watcher->pollset = pollset; |
| 644 |
watcher->worker = worker; |
| 645 |
watcher->fd = fd; |
| 646 |
gpr_mu_unlock(&fd->mu); |
| 647 |
|
| 648 |
return mask; |
| 649 |
} |
| 650 |
|
| 651 |
static void fd_end_poll(grpc_fd_watcher* watcher, int got_read, int got_write) { |
| 652 |
int was_polling = 0; |
| 653 |
int kick = 0; |
| 654 |
grpc_fd* fd = watcher->fd; |
| 655 |
if (fd == nullptr) { |
| 656 |
return; |
| 657 |
} |
| 658 |
|
| 659 |
gpr_mu_lock(&fd->mu); |
| 660 |
if (watcher->pollset == nullptr) { |
| 661 |
watcher->fd = nullptr; |
| 662 |
gpr_mu_unlock(&fd->mu); |
| 663 |
GRPC_FD_UNREF(fd, "multipoller_start"); |
| 664 |
return; |
| 665 |
} |
| 666 |
|
| 667 |
if (watcher == fd->read_watcher) { |
| 668 |
/* remove read watcher, kick if we still need a read */ |
| 669 |
was_polling = 1; |
| 670 |
if (!got_read) { |
| 671 |
kick = 1; |
| 672 |
} |
| 673 |
fd->read_watcher = nullptr; |
| 674 |
} |
| 675 |
if (watcher == fd->write_watcher) { |
| 676 |
/* remove write watcher, kick if we still need a write */ |
| 677 |
was_polling = 1; |
| 678 |
if (!got_write) { |
| 679 |
kick = 1; |
| 680 |
} |
| 681 |
fd->write_watcher = nullptr; |
| 682 |
} |
| 683 |
if (!was_polling && watcher->worker != nullptr) { |
| 684 |
/* remove from inactive list */ |
| 685 |
watcher->next->prev = watcher->prev; |
| 686 |
watcher->prev->next = watcher->next; |
| 687 |
} |
| 688 |
if (got_read) { |
| 689 |
if (set_ready_locked(fd, &fd->read_closure)) { |
| 690 |
kick = 1; |
| 691 |
} |
| 692 |
} |
| 693 |
if (got_write) { |
| 694 |
if (set_ready_locked(fd, &fd->write_closure)) { |
| 695 |
kick = 1; |
| 696 |
} |
| 697 |
} |
| 698 |
if (kick) { |
| 699 |
maybe_wake_one_watcher_locked(fd); |
| 700 |
} |
| 701 |
if (fd_is_orphaned(fd) && !has_watchers(fd) && !fd->closed) { |
| 702 |
close_fd_locked(fd); |
| 703 |
} |
| 704 |
gpr_mu_unlock(&fd->mu); |
| 705 |
|
| 706 |
GRPC_FD_UNREF(fd, "poll"); |
| 707 |
} |
| 708 |
|
| 709 |
/******************************************************************************* |
| 710 |
* pollset_posix.c |
| 711 |
*/ |
| 712 |
|
| 713 |
static thread_local grpc_pollset* g_current_thread_poller; |
| 714 |
static thread_local grpc_pollset_worker* g_current_thread_worker; |
| 715 |
|
| 716 |
static void remove_worker(grpc_pollset* /*p*/, grpc_pollset_worker* worker) { |
| 717 |
worker->prev->next = worker->next; |
| 718 |
worker->next->prev = worker->prev; |
| 719 |
} |
| 720 |
|
| 721 |
static bool pollset_has_workers(grpc_pollset* p) { |
| 722 |
return p->root_worker.next != &p->root_worker; |
| 723 |
} |
| 724 |
|
| 725 |
static bool pollset_in_pollset_sets(grpc_pollset* p) { |
| 726 |
return p->pollset_set_count; |
| 727 |
} |
| 728 |
|
| 729 |
static bool pollset_has_observers(grpc_pollset* p) { |
| 730 |
return pollset_has_workers(p) || pollset_in_pollset_sets(p); |
| 731 |
} |
| 732 |
|
| 733 |
static grpc_pollset_worker* pop_front_worker(grpc_pollset* p) { |
| 734 |
if (pollset_has_workers(p)) { |
| 735 |
grpc_pollset_worker* w = p->root_worker.next; |
| 736 |
remove_worker(p, w); |
| 737 |
return w; |
| 738 |
} else { |
| 739 |
return nullptr; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
static void push_back_worker(grpc_pollset* p, grpc_pollset_worker* worker) { |
| 744 |
worker->next = &p->root_worker; |
| 745 |
worker->prev = worker->next->prev; |
| 746 |
worker->prev->next = worker->next->prev = worker; |
| 747 |
} |
| 748 |
|
| 749 |
static void push_front_worker(grpc_pollset* p, grpc_pollset_worker* worker) { |
| 750 |
worker->prev = &p->root_worker; |
| 751 |
worker->next = worker->prev->next; |
| 752 |
worker->prev->next = worker->next->prev = worker; |
| 753 |
} |
| 754 |
|
| 755 |
static void kick_append_error(grpc_error_handle* composite, |
| 756 |
grpc_error_handle error) { |
| 757 |
if (error.ok()) return; |
| 758 |
if (composite->ok()) { |
| 759 |
*composite = GRPC_ERROR_CREATE("Kick Failure"); |
| 760 |
} |
| 761 |
*composite = grpc_error_add_child(*composite, error); |
| 762 |
} |
| 763 |
|
| 764 |
static grpc_error_handle pollset_kick_ext(grpc_pollset* p, |
| 765 |
grpc_pollset_worker* specific_worker, |
| 766 |
uint32_t flags) { |
| 767 |
grpc_error_handle error; |
| 768 |
|
| 769 |
/* pollset->mu already held */ |
| 770 |
if (specific_worker != nullptr) { |
| 771 |
if (specific_worker == GRPC_POLLSET_KICK_BROADCAST) { |
| 772 |
GPR_ASSERT((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) == 0); |
| 773 |
for (specific_worker = p->root_worker.next; |
| 774 |
specific_worker != &p->root_worker; |
| 775 |
specific_worker = specific_worker->next) { |
| 776 |
kick_append_error( |
| 777 |
&error, grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd)); |
| 778 |
} |
| 779 |
p->kicked_without_pollers = true; |
| 780 |
} else if (g_current_thread_worker != specific_worker) { |
| 781 |
if ((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) != 0) { |
| 782 |
specific_worker->reevaluate_polling_on_wakeup = true; |
| 783 |
} |
| 784 |
specific_worker->kicked_specifically = true; |
| 785 |
kick_append_error(&error, |
| 786 |
grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd)); |
| 787 |
} else if ((flags & GRPC_POLLSET_CAN_KICK_SELF) != 0) { |
| 788 |
if ((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) != 0) { |
| 789 |
specific_worker->reevaluate_polling_on_wakeup = true; |
| 790 |
} |
| 791 |
specific_worker->kicked_specifically = true; |
| 792 |
kick_append_error(&error, |
| 793 |
grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd)); |
| 794 |
} |
| 795 |
} else if (g_current_thread_poller != p) { |
| 796 |
GPR_ASSERT((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) == 0); |
| 797 |
specific_worker = pop_front_worker(p); |
| 798 |
if (specific_worker != nullptr) { |
| 799 |
if (g_current_thread_worker == specific_worker) { |
| 800 |
push_back_worker(p, specific_worker); |
| 801 |
specific_worker = pop_front_worker(p); |
| 802 |
if ((flags & GRPC_POLLSET_CAN_KICK_SELF) == 0 && |
| 803 |
g_current_thread_worker == specific_worker) { |
| 804 |
push_back_worker(p, specific_worker); |
| 805 |
specific_worker = nullptr; |
| 806 |
} |
| 807 |
} |
| 808 |
if (specific_worker != nullptr) { |
| 809 |
push_back_worker(p, specific_worker); |
| 810 |
kick_append_error( |
| 811 |
&error, grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd)); |
| 812 |
} |
| 813 |
} else { |
| 814 |
p->kicked_without_pollers = true; |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
GRPC_LOG_IF_ERROR("pollset_kick_ext", error); |
| 819 |
return error; |
| 820 |
} |
| 821 |
|
| 822 |
static grpc_error_handle pollset_kick(grpc_pollset* p, |
| 823 |
grpc_pollset_worker* specific_worker) { |
| 824 |
return pollset_kick_ext(p, specific_worker, 0); |
| 825 |
} |
| 826 |
|
| 827 |
/* global state management */ |
| 828 |
|
| 829 |
static grpc_error_handle pollset_global_init(void) { return absl::OkStatus(); } |
| 830 |
|
| 831 |
/* main interface */ |
| 832 |
|
| 833 |
static void pollset_init(grpc_pollset* pollset, gpr_mu** mu) { |
| 834 |
gpr_mu_init(&pollset->mu); |
| 835 |
*mu = &pollset->mu; |
| 836 |
pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker; |
| 837 |
pollset->shutting_down = 0; |
| 838 |
pollset->called_shutdown = 0; |
| 839 |
pollset->kicked_without_pollers = 0; |
| 840 |
pollset->local_wakeup_cache = nullptr; |
| 841 |
pollset->kicked_without_pollers = 0; |
| 842 |
pollset->fd_count = 0; |
| 843 |
pollset->fd_capacity = 0; |
| 844 |
pollset->fds = nullptr; |
| 845 |
pollset->pollset_set_count = 0; |
| 846 |
} |
| 847 |
|
| 848 |
static void pollset_destroy(grpc_pollset* pollset) { |
| 849 |
GPR_ASSERT(!pollset_has_workers(pollset)); |
| 850 |
while (pollset->local_wakeup_cache) { |
| 851 |
grpc_cached_wakeup_fd* next = pollset->local_wakeup_cache->next; |
| 852 |
fork_fd_list_remove_node(pollset->local_wakeup_cache->fork_fd_list); |
| 853 |
grpc_wakeup_fd_destroy(&pollset->local_wakeup_cache->fd); |
| 854 |
gpr_free(pollset->local_wakeup_cache); |
| 855 |
pollset->local_wakeup_cache = next; |
| 856 |
} |
| 857 |
gpr_free(pollset->fds); |
| 858 |
gpr_mu_destroy(&pollset->mu); |
| 859 |
} |
| 860 |
|
| 861 |
static void pollset_add_fd(grpc_pollset* pollset, grpc_fd* fd) { |
| 862 |
gpr_mu_lock(&pollset->mu); |
| 863 |
size_t i; |
| 864 |
/* TODO(ctiller): this is O(num_fds^2); maybe switch to a hash set here */ |
| 865 |
for (i = 0; i < pollset->fd_count; i++) { |
| 866 |
if (pollset->fds[i] == fd) goto exit; |
| 867 |
} |
| 868 |
if (pollset->fd_count == pollset->fd_capacity) { |
| 869 |
pollset->fd_capacity = |
| 870 |
std::max(pollset->fd_capacity + 8, pollset->fd_count * 3 / 2); |
| 871 |
pollset->fds = static_cast<grpc_fd**>( |
| 872 |
gpr_realloc(pollset->fds, sizeof(grpc_fd*) * pollset->fd_capacity)); |
| 873 |
} |
| 874 |
pollset->fds[pollset->fd_count++] = fd; |
| 875 |
GRPC_FD_REF(fd, "multipoller"); |
| 876 |
(void)pollset_kick(pollset, nullptr); |
| 877 |
exit: |
| 878 |
gpr_mu_unlock(&pollset->mu); |
| 879 |
} |
| 880 |
|
| 881 |
static void finish_shutdown(grpc_pollset* pollset) { |
| 882 |
size_t i; |
| 883 |
for (i = 0; i < pollset->fd_count; i++) { |
| 884 |
GRPC_FD_UNREF(pollset->fds[i], "multipoller"); |
| 885 |
} |
| 886 |
pollset->fd_count = 0; |
| 887 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, pollset->shutdown_done, |
| 888 |
absl::OkStatus()); |
| 889 |
} |
| 890 |
|
| 891 |
static void work_combine_error(grpc_error_handle* composite, |
| 892 |
grpc_error_handle error) { |
| 893 |
if (error.ok()) return; |
| 894 |
if (composite->ok()) { |
| 895 |
*composite = GRPC_ERROR_CREATE("pollset_work"); |
| 896 |
} |
| 897 |
*composite = grpc_error_add_child(*composite, error); |
| 898 |
} |
| 899 |
|
| 900 |
static grpc_error_handle pollset_work(grpc_pollset* pollset, |
| 901 |
grpc_pollset_worker** worker_hdl, |
| 902 |
grpc_core::Timestamp deadline) { |
| 903 |
grpc_pollset_worker worker; |
| 904 |
if (worker_hdl) *worker_hdl = &worker; |
| 905 |
grpc_error_handle error; |
| 906 |
|
| 907 |
/* Avoid malloc for small number of elements. */ |
| 908 |
enum { inline_elements = 96 }; |
| 909 |
struct pollfd pollfd_space[inline_elements]; |
| 910 |
struct grpc_fd_watcher watcher_space[inline_elements]; |
| 911 |
|
| 912 |
/* pollset->mu already held */ |
| 913 |
int added_worker = 0; |
| 914 |
int locked = 1; |
| 915 |
int queued_work = 0; |
| 916 |
int keep_polling = 0; |
| 917 |
/* this must happen before we (potentially) drop pollset->mu */ |
| 918 |
worker.next = worker.prev = nullptr; |
| 919 |
worker.reevaluate_polling_on_wakeup = 0; |
| 920 |
if (pollset->local_wakeup_cache != nullptr) { |
| 921 |
worker.wakeup_fd = pollset->local_wakeup_cache; |
| 922 |
pollset->local_wakeup_cache = worker.wakeup_fd->next; |
| 923 |
} else { |
| 924 |
worker.wakeup_fd = static_cast<grpc_cached_wakeup_fd*>( |
| 925 |
gpr_malloc(sizeof(*worker.wakeup_fd))); |
| 926 |
error = grpc_wakeup_fd_init(&worker.wakeup_fd->fd); |
| 927 |
fork_fd_list_add_wakeup_fd(worker.wakeup_fd); |
| 928 |
if (!error.ok()) { |
| 929 |
GRPC_LOG_IF_ERROR("pollset_work", error); |
| 930 |
return error; |
| 931 |
} |
| 932 |
} |
| 933 |
worker.kicked_specifically = 0; |
| 934 |
/* If we're shutting down then we don't execute any extended work */ |
| 935 |
if (pollset->shutting_down) { |
| 936 |
goto done; |
| 937 |
} |
| 938 |
/* Start polling, and keep doing so while we're being asked to |
| 939 |
re-evaluate our pollers (this allows poll() based pollers to |
| 940 |
ensure they don't miss wakeups) */ |
| 941 |
keep_polling = 1; |
| 942 |
g_current_thread_poller = pollset; |
| 943 |
while (keep_polling) { |
| 944 |
keep_polling = 0; |
| 945 |
if (!pollset->kicked_without_pollers || |
| 946 |
deadline <= grpc_core::Timestamp::Now()) { |
| 947 |
if (!added_worker) { |
| 948 |
push_front_worker(pollset, &worker); |
| 949 |
added_worker = 1; |
| 950 |
g_current_thread_worker = &worker; |
| 951 |
} |
| 952 |
#define POLLOUT_CHECK (POLLOUT | POLLHUP | POLLERR) |
| 953 |
#define POLLIN_CHECK (POLLIN | POLLHUP | POLLERR) |
| 954 |
|
| 955 |
int timeout; |
| 956 |
int r; |
| 957 |
size_t i, fd_count; |
| 958 |
nfds_t pfd_count; |
| 959 |
grpc_fd_watcher* watchers; |
| 960 |
struct pollfd* pfds; |
| 961 |
|
| 962 |
timeout = poll_deadline_to_millis_timeout(deadline); |
| 963 |
|
| 964 |
if (pollset->fd_count + 2 <= inline_elements) { |
| 965 |
pfds = pollfd_space; |
| 966 |
watchers = watcher_space; |
| 967 |
} else { |
| 968 |
/* Allocate one buffer to hold both pfds and watchers arrays */ |
| 969 |
const size_t pfd_size = sizeof(*pfds) * (pollset->fd_count + 2); |
| 970 |
const size_t watch_size = sizeof(*watchers) * (pollset->fd_count + 2); |
| 971 |
void* buf = gpr_malloc(pfd_size + watch_size); |
| 972 |
pfds = static_cast<struct pollfd*>(buf); |
| 973 |
watchers = static_cast<grpc_fd_watcher*>( |
| 974 |
static_cast<void*>((static_cast<char*>(buf) + pfd_size))); |
| 975 |
} |
| 976 |
|
| 977 |
fd_count = 0; |
| 978 |
pfd_count = 1; |
| 979 |
pfds[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&worker.wakeup_fd->fd); |
| 980 |
pfds[0].events = POLLIN; |
| 981 |
pfds[0].revents = 0; |
| 982 |
for (i = 0; i < pollset->fd_count; i++) { |
| 983 |
if (fd_is_orphaned(pollset->fds[i]) || |
| 984 |
gpr_atm_no_barrier_load(&pollset->fds[i]->pollhup) == 1) { |
| 985 |
GRPC_FD_UNREF(pollset->fds[i], "multipoller"); |
| 986 |
} else { |
| 987 |
pollset->fds[fd_count++] = pollset->fds[i]; |
| 988 |
watchers[pfd_count].fd = pollset->fds[i]; |
| 989 |
GRPC_FD_REF(watchers[pfd_count].fd, "multipoller_start"); |
| 990 |
pfds[pfd_count].fd = pollset->fds[i]->fd; |
| 991 |
pfds[pfd_count].revents = 0; |
| 992 |
pfd_count++; |
| 993 |
} |
| 994 |
} |
| 995 |
pollset->fd_count = fd_count; |
| 996 |
gpr_mu_unlock(&pollset->mu); |
| 997 |
|
| 998 |
for (i = 1; i < pfd_count; i++) { |
| 999 |
grpc_fd* fd = watchers[i].fd; |
| 1000 |
pfds[i].events = static_cast<short>( |
| 1001 |
fd_begin_poll(fd, pollset, &worker, POLLIN, POLLOUT, &watchers[i])); |
| 1002 |
if (watchers[i].pollset != nullptr) { |
| 1003 |
GRPC_FD_UNREF(fd, "multipoller_start"); |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
/* TODO(vpai): Consider first doing a 0 timeout poll here to avoid |
| 1008 |
even going into the blocking annotation if possible */ |
| 1009 |
GRPC_SCHEDULING_START_BLOCKING_REGION; |
| 1010 |
r = grpc_poll_function(pfds, pfd_count, timeout); |
| 1011 |
GRPC_SCHEDULING_END_BLOCKING_REGION; |
| 1012 |
|
| 1013 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_polling_trace)) { |
| 1014 |
gpr_log(GPR_INFO, "%p poll=%d", pollset, r); |
| 1015 |
} |
| 1016 |
|
| 1017 |
if (r < 0) { |
| 1018 |
if (errno != EINTR) { |
| 1019 |
work_combine_error(&error, GRPC_OS_ERROR(errno, "poll")); |
| 1020 |
} |
| 1021 |
|
| 1022 |
for (i = 1; i < pfd_count; i++) { |
| 1023 |
if (watchers[i].pollset == nullptr) { |
| 1024 |
fd_end_poll(&watchers[i], 0, 0); |
| 1025 |
} else { |
| 1026 |
// Wake up all the file descriptors, if we have an invalid one |
| 1027 |
// we can identify it on the next pollset_work() |
| 1028 |
fd_end_poll(&watchers[i], 1, 1); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
} else if (r == 0) { |
| 1032 |
for (i = 1; i < pfd_count; i++) { |
| 1033 |
fd_end_poll(&watchers[i], 0, 0); |
| 1034 |
} |
| 1035 |
} else { |
| 1036 |
if (pfds[0].revents & POLLIN_CHECK) { |
| 1037 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_polling_trace)) { |
| 1038 |
gpr_log(GPR_INFO, "%p: got_wakeup", pollset); |
| 1039 |
} |
| 1040 |
work_combine_error( |
| 1041 |
&error, grpc_wakeup_fd_consume_wakeup(&worker.wakeup_fd->fd)); |
| 1042 |
} |
| 1043 |
for (i = 1; i < pfd_count; i++) { |
| 1044 |
if (watchers[i].pollset == nullptr) { |
| 1045 |
grpc_fd* fd = watchers[i].fd; |
| 1046 |
if (pfds[i].revents & POLLHUP) { |
| 1047 |
gpr_atm_no_barrier_store(&fd->pollhup, 1); |
| 1048 |
} |
| 1049 |
fd_end_poll(&watchers[i], 0, 0); |
| 1050 |
} else { |
| 1051 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_polling_trace)) { |
| 1052 |
gpr_log(GPR_INFO, "%p got_event: %d r:%d w:%d [%d]", pollset, |
| 1053 |
pfds[i].fd, (pfds[i].revents & POLLIN_CHECK) != 0, |
| 1054 |
(pfds[i].revents & POLLOUT_CHECK) != 0, pfds[i].revents); |
| 1055 |
} |
| 1056 |
/* This is a mitigation to prevent poll() from spinning on a |
| 1057 |
** POLLHUP https://github.com/grpc/grpc/pull/13665 |
| 1058 |
*/ |
| 1059 |
if (pfds[i].revents & POLLHUP) { |
| 1060 |
gpr_atm_no_barrier_store(&watchers[i].fd->pollhup, 1); |
| 1061 |
} |
| 1062 |
fd_end_poll(&watchers[i], pfds[i].revents & POLLIN_CHECK, |
| 1063 |
pfds[i].revents & POLLOUT_CHECK); |
| 1064 |
} |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
if (pfds != pollfd_space) { |
| 1069 |
/* pfds and watchers are in the same memory block pointed to by pfds */ |
| 1070 |
gpr_free(pfds); |
| 1071 |
} |
| 1072 |
|
| 1073 |
locked = 0; |
| 1074 |
} else { |
| 1075 |
pollset->kicked_without_pollers = 0; |
| 1076 |
} |
| 1077 |
/* Finished execution - start cleaning up. |
| 1078 |
Note that we may arrive here from outside the enclosing while() loop. |
| 1079 |
In that case we won't loop though as we haven't added worker to the |
| 1080 |
worker list, which means nobody could ask us to re-evaluate polling). */ |
| 1081 |
done: |
| 1082 |
if (!locked) { |
| 1083 |
queued_work |= grpc_core::ExecCtx::Get()->Flush(); |
| 1084 |
gpr_mu_lock(&pollset->mu); |
| 1085 |
locked = 1; |
| 1086 |
} |
| 1087 |
/* If we're forced to re-evaluate polling (via pollset_kick with |
| 1088 |
GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) then we land here and force |
| 1089 |
a loop */ |
| 1090 |
if (worker.reevaluate_polling_on_wakeup && error.ok()) { |
| 1091 |
worker.reevaluate_polling_on_wakeup = 0; |
| 1092 |
pollset->kicked_without_pollers = 0; |
| 1093 |
if (queued_work || worker.kicked_specifically) { |
| 1094 |
/* If there's queued work on the list, then set the deadline to be |
| 1095 |
immediate so we get back out of the polling loop quickly */ |
| 1096 |
deadline = grpc_core::Timestamp(); |
| 1097 |
} |
| 1098 |
keep_polling = 1; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
g_current_thread_poller = nullptr; |
| 1102 |
if (added_worker) { |
| 1103 |
remove_worker(pollset, &worker); |
| 1104 |
g_current_thread_worker = nullptr; |
| 1105 |
} |
| 1106 |
/* release wakeup fd to the local pool */ |
| 1107 |
worker.wakeup_fd->next = pollset->local_wakeup_cache; |
| 1108 |
pollset->local_wakeup_cache = worker.wakeup_fd; |
| 1109 |
/* check shutdown conditions */ |
| 1110 |
if (pollset->shutting_down) { |
| 1111 |
if (pollset_has_workers(pollset)) { |
| 1112 |
(void)pollset_kick(pollset, nullptr); |
| 1113 |
} else if (!pollset->called_shutdown && !pollset_has_observers(pollset)) { |
| 1114 |
pollset->called_shutdown = 1; |
| 1115 |
gpr_mu_unlock(&pollset->mu); |
| 1116 |
finish_shutdown(pollset); |
| 1117 |
grpc_core::ExecCtx::Get()->Flush(); |
| 1118 |
/* Continuing to access pollset here is safe -- it is the caller's |
| 1119 |
* responsibility to not destroy when it has outstanding calls to |
| 1120 |
* pollset_work. |
| 1121 |
* TODO(dklempner): Can we refactor the shutdown logic to avoid this? */ |
| 1122 |
gpr_mu_lock(&pollset->mu); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
if (worker_hdl) *worker_hdl = nullptr; |
| 1126 |
GRPC_LOG_IF_ERROR("pollset_work", error); |
| 1127 |
return error; |
| 1128 |
} |
| 1129 |
|
| 1130 |
static void pollset_shutdown(grpc_pollset* pollset, grpc_closure* closure) { |
| 1131 |
GPR_ASSERT(!pollset->shutting_down); |
| 1132 |
pollset->shutting_down = 1; |
| 1133 |
pollset->shutdown_done = closure; |
| 1134 |
(void)pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST); |
| 1135 |
if (!pollset->called_shutdown && !pollset_has_observers(pollset)) { |
| 1136 |
pollset->called_shutdown = 1; |
| 1137 |
finish_shutdown(pollset); |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
static int poll_deadline_to_millis_timeout(grpc_core::Timestamp deadline) { |
| 1142 |
if (deadline == grpc_core::Timestamp::InfFuture()) return -1; |
| 1143 |
if (deadline.is_process_epoch()) return 0; |
| 1144 |
int64_t n = (deadline - grpc_core::Timestamp::Now()).millis(); |
| 1145 |
if (n < 0) return 0; |
| 1146 |
if (n > INT_MAX) return -1; |
| 1147 |
return static_cast<int>(n); |
| 1148 |
} |
| 1149 |
|
| 1150 |
/******************************************************************************* |
| 1151 |
* pollset_set_posix.c |
| 1152 |
*/ |
| 1153 |
|
| 1154 |
static grpc_pollset_set* pollset_set_create(void) { |
| 1155 |
grpc_pollset_set* pollset_set = |
| 1156 |
static_cast<grpc_pollset_set*>(gpr_zalloc(sizeof(*pollset_set))); |
| 1157 |
gpr_mu_init(&pollset_set->mu); |
| 1158 |
return pollset_set; |
| 1159 |
} |
| 1160 |
|
| 1161 |
static void pollset_set_destroy(grpc_pollset_set* pollset_set) { |
| 1162 |
size_t i; |
| 1163 |
gpr_mu_destroy(&pollset_set->mu); |
| 1164 |
for (i = 0; i < pollset_set->fd_count; i++) { |
| 1165 |
GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set"); |
| 1166 |
} |
| 1167 |
for (i = 0; i < pollset_set->pollset_count; i++) { |
| 1168 |
grpc_pollset* pollset = pollset_set->pollsets[i]; |
| 1169 |
gpr_mu_lock(&pollset->mu); |
| 1170 |
pollset->pollset_set_count--; |
| 1171 |
/* check shutdown */ |
| 1172 |
if (pollset->shutting_down && !pollset->called_shutdown && |
| 1173 |
!pollset_has_observers(pollset)) { |
| 1174 |
pollset->called_shutdown = 1; |
| 1175 |
gpr_mu_unlock(&pollset->mu); |
| 1176 |
finish_shutdown(pollset); |
| 1177 |
} else { |
| 1178 |
gpr_mu_unlock(&pollset->mu); |
| 1179 |
} |
| 1180 |
} |
| 1181 |
gpr_free(pollset_set->pollsets); |
| 1182 |
gpr_free(pollset_set->pollset_sets); |
| 1183 |
gpr_free(pollset_set->fds); |
| 1184 |
gpr_free(pollset_set); |
| 1185 |
} |
| 1186 |
|
| 1187 |
static void pollset_set_add_pollset(grpc_pollset_set* pollset_set, |
| 1188 |
grpc_pollset* pollset) { |
| 1189 |
size_t i, j; |
| 1190 |
gpr_mu_lock(&pollset->mu); |
| 1191 |
pollset->pollset_set_count++; |
| 1192 |
gpr_mu_unlock(&pollset->mu); |
| 1193 |
gpr_mu_lock(&pollset_set->mu); |
| 1194 |
if (pollset_set->pollset_count == pollset_set->pollset_capacity) { |
| 1195 |
pollset_set->pollset_capacity = |
| 1196 |
std::max(size_t(8), 2 * pollset_set->pollset_capacity); |
| 1197 |
pollset_set->pollsets = static_cast<grpc_pollset**>(gpr_realloc( |
| 1198 |
pollset_set->pollsets, |
| 1199 |
pollset_set->pollset_capacity * sizeof(*pollset_set->pollsets))); |
| 1200 |
} |
| 1201 |
pollset_set->pollsets[pollset_set->pollset_count++] = pollset; |
| 1202 |
for (i = 0, j = 0; i < pollset_set->fd_count; i++) { |
| 1203 |
if (fd_is_orphaned(pollset_set->fds[i])) { |
| 1204 |
GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set"); |
| 1205 |
} else { |
| 1206 |
pollset_add_fd(pollset, pollset_set->fds[i]); |
| 1207 |
pollset_set->fds[j++] = pollset_set->fds[i]; |
| 1208 |
} |
| 1209 |
} |
| 1210 |
pollset_set->fd_count = j; |
| 1211 |
gpr_mu_unlock(&pollset_set->mu); |
| 1212 |
} |
| 1213 |
|
| 1214 |
static void pollset_set_del_pollset(grpc_pollset_set* pollset_set, |
| 1215 |
grpc_pollset* pollset) { |
| 1216 |
size_t i; |
| 1217 |
gpr_mu_lock(&pollset_set->mu); |
| 1218 |
for (i = 0; i < pollset_set->pollset_count; i++) { |
| 1219 |
if (pollset_set->pollsets[i] == pollset) { |
| 1220 |
pollset_set->pollset_count--; |
| 1221 |
std::swap(pollset_set->pollsets[i], |
| 1222 |
pollset_set->pollsets[pollset_set->pollset_count]); |
| 1223 |
break; |
| 1224 |
} |
| 1225 |
} |
| 1226 |
gpr_mu_unlock(&pollset_set->mu); |
| 1227 |
gpr_mu_lock(&pollset->mu); |
| 1228 |
pollset->pollset_set_count--; |
| 1229 |
/* check shutdown */ |
| 1230 |
if (pollset->shutting_down && !pollset->called_shutdown && |
| 1231 |
!pollset_has_observers(pollset)) { |
| 1232 |
pollset->called_shutdown = 1; |
| 1233 |
gpr_mu_unlock(&pollset->mu); |
| 1234 |
finish_shutdown(pollset); |
| 1235 |
} else { |
| 1236 |
gpr_mu_unlock(&pollset->mu); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
|
| 1240 |
static void pollset_set_add_pollset_set(grpc_pollset_set* bag, |
| 1241 |
grpc_pollset_set* item) { |
| 1242 |
size_t i, j; |
| 1243 |
gpr_mu_lock(&bag->mu); |
| 1244 |
if (bag->pollset_set_count == bag->pollset_set_capacity) { |
| 1245 |
bag->pollset_set_capacity = |
| 1246 |
std::max(size_t(8), 2 * bag->pollset_set_capacity); |
| 1247 |
bag->pollset_sets = static_cast<grpc_pollset_set**>( |
| 1248 |
gpr_realloc(bag->pollset_sets, |
| 1249 |
bag->pollset_set_capacity * sizeof(*bag->pollset_sets))); |
| 1250 |
} |
| 1251 |
bag->pollset_sets[bag->pollset_set_count++] = item; |
| 1252 |
for (i = 0, j = 0; i < bag->fd_count; i++) { |
| 1253 |
if (fd_is_orphaned(bag->fds[i])) { |
| 1254 |
GRPC_FD_UNREF(bag->fds[i], "pollset_set"); |
| 1255 |
} else { |
| 1256 |
pollset_set_add_fd(item, bag->fds[i]); |
| 1257 |
bag->fds[j++] = bag->fds[i]; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
bag->fd_count = j; |
| 1261 |
gpr_mu_unlock(&bag->mu); |
| 1262 |
} |
| 1263 |
|
| 1264 |
static void pollset_set_del_pollset_set(grpc_pollset_set* bag, |
| 1265 |
grpc_pollset_set* item) { |
| 1266 |
size_t i; |
| 1267 |
gpr_mu_lock(&bag->mu); |
| 1268 |
for (i = 0; i < bag->pollset_set_count; i++) { |
| 1269 |
if (bag->pollset_sets[i] == item) { |
| 1270 |
bag->pollset_set_count--; |
| 1271 |
std::swap(bag->pollset_sets[i], |
| 1272 |
bag->pollset_sets[bag->pollset_set_count]); |
| 1273 |
break; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
gpr_mu_unlock(&bag->mu); |
| 1277 |
} |
| 1278 |
|
| 1279 |
static void pollset_set_add_fd(grpc_pollset_set* pollset_set, grpc_fd* fd) { |
| 1280 |
size_t i; |
| 1281 |
gpr_mu_lock(&pollset_set->mu); |
| 1282 |
if (pollset_set->fd_count == pollset_set->fd_capacity) { |
| 1283 |
pollset_set->fd_capacity = |
| 1284 |
std::max(size_t(8), 2 * pollset_set->fd_capacity); |
| 1285 |
pollset_set->fds = static_cast<grpc_fd**>( |
| 1286 |
gpr_realloc(pollset_set->fds, |
| 1287 |
pollset_set->fd_capacity * sizeof(*pollset_set->fds))); |
| 1288 |
} |
| 1289 |
GRPC_FD_REF(fd, "pollset_set"); |
| 1290 |
pollset_set->fds[pollset_set->fd_count++] = fd; |
| 1291 |
for (i = 0; i < pollset_set->pollset_count; i++) { |
| 1292 |
pollset_add_fd(pollset_set->pollsets[i], fd); |
| 1293 |
} |
| 1294 |
for (i = 0; i < pollset_set->pollset_set_count; i++) { |
| 1295 |
pollset_set_add_fd(pollset_set->pollset_sets[i], fd); |
| 1296 |
} |
| 1297 |
gpr_mu_unlock(&pollset_set->mu); |
| 1298 |
} |
| 1299 |
|
| 1300 |
static void pollset_set_del_fd(grpc_pollset_set* pollset_set, grpc_fd* fd) { |
| 1301 |
size_t i; |
| 1302 |
gpr_mu_lock(&pollset_set->mu); |
| 1303 |
for (i = 0; i < pollset_set->fd_count; i++) { |
| 1304 |
if (pollset_set->fds[i] == fd) { |
| 1305 |
pollset_set->fd_count--; |
| 1306 |
std::swap(pollset_set->fds[i], pollset_set->fds[pollset_set->fd_count]); |
| 1307 |
GRPC_FD_UNREF(fd, "pollset_set"); |
| 1308 |
break; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
for (i = 0; i < pollset_set->pollset_set_count; i++) { |
| 1312 |
pollset_set_del_fd(pollset_set->pollset_sets[i], fd); |
| 1313 |
} |
| 1314 |
gpr_mu_unlock(&pollset_set->mu); |
| 1315 |
} |
| 1316 |
|
| 1317 |
/******************************************************************************* |
| 1318 |
* event engine binding |
| 1319 |
*/ |
| 1320 |
|
| 1321 |
static bool is_any_background_poller_thread(void) { return false; } |
| 1322 |
|
| 1323 |
static void shutdown_background_closure(void) {} |
| 1324 |
|
| 1325 |
static bool add_closure_to_background_poller(grpc_closure* /*closure*/, |
| 1326 |
grpc_error_handle /*error*/) { |
| 1327 |
return false; |
| 1328 |
} |
| 1329 |
|
| 1330 |
/* Called by the child process's post-fork handler to close open fds, including |
| 1331 |
* worker wakeup fds. This allows gRPC to shutdown in the child process without |
| 1332 |
* interfering with connections or RPCs ongoing in the parent. */ |
| 1333 |
static void reset_event_manager_on_fork() { |
| 1334 |
gpr_mu_lock(&fork_fd_list_mu); |
| 1335 |
while (fork_fd_list_head != nullptr) { |
| 1336 |
if (fork_fd_list_head->fd != nullptr) { |
| 1337 |
if (!fork_fd_list_head->fd->closed) { |
| 1338 |
close(fork_fd_list_head->fd->fd); |
| 1339 |
} |
| 1340 |
fork_fd_list_head->fd->fd = -1; |
| 1341 |
} else { |
| 1342 |
close(fork_fd_list_head->cached_wakeup_fd->fd.read_fd); |
| 1343 |
fork_fd_list_head->cached_wakeup_fd->fd.read_fd = -1; |
| 1344 |
close(fork_fd_list_head->cached_wakeup_fd->fd.write_fd); |
| 1345 |
fork_fd_list_head->cached_wakeup_fd->fd.write_fd = -1; |
| 1346 |
} |
| 1347 |
fork_fd_list_head = fork_fd_list_head->next; |
| 1348 |
} |
| 1349 |
gpr_mu_unlock(&fork_fd_list_mu); |
| 1350 |
} |
| 1351 |
|
| 1352 |
const grpc_event_engine_vtable grpc_ev_poll_posix = { |
| 1353 |
sizeof(grpc_pollset), |
| 1354 |
false, |
| 1355 |
false, |
| 1356 |
|
| 1357 |
fd_create, |
| 1358 |
fd_wrapped_fd, |
| 1359 |
fd_orphan, |
| 1360 |
fd_shutdown, |
| 1361 |
fd_notify_on_read, |
| 1362 |
fd_notify_on_write, |
| 1363 |
fd_notify_on_error, |
| 1364 |
fd_set_readable, |
| 1365 |
fd_set_writable, |
| 1366 |
fd_set_error, |
| 1367 |
fd_is_shutdown, |
| 1368 |
|
| 1369 |
pollset_init, |
| 1370 |
pollset_shutdown, |
| 1371 |
pollset_destroy, |
| 1372 |
pollset_work, |
| 1373 |
pollset_kick, |
| 1374 |
pollset_add_fd, |
| 1375 |
|
| 1376 |
pollset_set_create, |
| 1377 |
pollset_set_destroy, |
| 1378 |
pollset_set_add_pollset, |
| 1379 |
pollset_set_del_pollset, |
| 1380 |
pollset_set_add_pollset_set, |
| 1381 |
pollset_set_del_pollset_set, |
| 1382 |
pollset_set_add_fd, |
| 1383 |
pollset_set_del_fd, |
| 1384 |
|
| 1385 |
is_any_background_poller_thread, |
| 1386 |
/* name = */ "poll", |
| 1387 |
/* check_engine_available = */ |
| 1388 |
[](bool) { |
| 1389 |
if (!grpc_has_wakeup_fd()) { |
| 1390 |
gpr_log(GPR_ERROR, "Skipping poll because of no wakeup fd."); |
| 1391 |
return false; |
| 1392 |
} |
| 1393 |
if (!GRPC_LOG_IF_ERROR("pollset_global_init", pollset_global_init())) { |
| 1394 |
return false; |
| 1395 |
} |
| 1396 |
if (grpc_core::Fork::Enabled()) { |
| 1397 |
track_fds_for_fork = true; |
| 1398 |
gpr_mu_init(&fork_fd_list_mu); |
| 1399 |
grpc_core::Fork::SetResetChildPollingEngineFunc( |
| 1400 |
reset_event_manager_on_fork); |
| 1401 |
} |
| 1402 |
return true; |
| 1403 |
}, |
| 1404 |
/* init_engine = */ []() {}, |
| 1405 |
/* shutdown_engine = */ shutdown_background_closure, |
| 1406 |
[]() {}, |
| 1407 |
add_closure_to_background_poller, |
| 1408 |
}; |
| 1409 |
|
| 1410 |
namespace { |
| 1411 |
|
| 1412 |
grpc_poll_function_type real_poll_function; |
| 1413 |
|
| 1414 |
int phony_poll(struct pollfd fds[], nfds_t nfds, int timeout) { |
| 1415 |
if (timeout == 0) { |
| 1416 |
return real_poll_function(fds, nfds, 0); |
| 1417 |
} else { |
| 1418 |
gpr_log(GPR_ERROR, "Attempted a blocking poll when declared non-polling."); |
| 1419 |
GPR_ASSERT(false); |
| 1420 |
return -1; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
} // namespace |
| 1425 |
|
| 1426 |
const grpc_event_engine_vtable grpc_ev_none_posix = []() { |
| 1427 |
grpc_event_engine_vtable v = grpc_ev_poll_posix; |
| 1428 |
v.check_engine_available = [](bool explicit_request) { |
| 1429 |
if (!explicit_request) return false; |
| 1430 |
// return the simplest engine as a phony but also override the poller |
| 1431 |
if (!grpc_ev_poll_posix.check_engine_available(explicit_request)) { |
| 1432 |
return false; |
| 1433 |
} |
| 1434 |
real_poll_function = grpc_poll_function; |
| 1435 |
grpc_poll_function = phony_poll; |
| 1436 |
return true; |
| 1437 |
}; |
| 1438 |
v.name = "none"; |
| 1439 |
v.init_engine = []() {}; |
| 1440 |
v.shutdown_engine = []() {}; |
| 1441 |
return v; |
| 1442 |
}(); |
| 1443 |
|
| 1444 |
#endif /* GRPC_POSIX_SOCKET_EV_POLL */ |