OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-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 #ifndef GRPC_INTERNAL_CORE_IOMGR_POLLSET_POSIX_H |
| 35 #define GRPC_INTERNAL_CORE_IOMGR_POLLSET_POSIX_H |
| 36 |
| 37 #include <poll.h> |
| 38 |
| 39 #include <grpc/support/sync.h> |
| 40 |
| 41 #include "src/core/iomgr/exec_ctx.h" |
| 42 #include "src/core/iomgr/iomgr.h" |
| 43 #include "src/core/iomgr/pollset.h" |
| 44 #include "src/core/iomgr/wakeup_fd_posix.h" |
| 45 |
| 46 typedef 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 */ |
| 51 struct grpc_fd; |
| 52 |
| 53 typedef struct grpc_cached_wakeup_fd { |
| 54 grpc_wakeup_fd fd; |
| 55 struct grpc_cached_wakeup_fd *next; |
| 56 } grpc_cached_wakeup_fd; |
| 57 |
| 58 struct grpc_pollset_worker { |
| 59 grpc_cached_wakeup_fd *wakeup_fd; |
| 60 int reevaluate_polling_on_wakeup; |
| 61 int kicked_specifically; |
| 62 struct grpc_pollset_worker *next; |
| 63 struct grpc_pollset_worker *prev; |
| 64 }; |
| 65 |
| 66 struct grpc_pollset { |
| 67 /* 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; |
| 72 gpr_mu mu; |
| 73 grpc_pollset_worker root_worker; |
| 74 int in_flight_cbs; |
| 75 int shutting_down; |
| 76 int called_shutdown; |
| 77 int kicked_without_pollers; |
| 78 grpc_closure *shutdown_done; |
| 79 grpc_closure_list idle_jobs; |
| 80 union { |
| 81 int fd; |
| 82 void *ptr; |
| 83 } data; |
| 84 /* Local cache of eventfds for workers */ |
| 85 grpc_cached_wakeup_fd *local_wakeup_cache; |
| 86 }; |
| 87 |
| 88 struct grpc_pollset_vtable { |
| 89 void (*add_fd)(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 90 struct grpc_fd *fd, int and_unlock_pollset); |
| 91 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); |
| 96 }; |
| 97 |
| 98 /* Add an fd to a pollset */ |
| 99 void grpc_pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 100 struct grpc_fd *fd); |
| 101 |
| 102 /* Returns the fd to listen on for kicks */ |
| 103 int grpc_kick_read_fd(grpc_pollset *p); |
| 104 /* Call after polling has been kicked to leave the kicked state */ |
| 105 void grpc_kick_drain(grpc_pollset *p); |
| 106 |
| 107 /* Convert a timespec to milliseconds: |
| 108 - very small or negative poll times are clamped to zero to do a |
| 109 non-blocking poll (which becomes spin polling) |
| 110 - other small values are rounded up to one millisecond |
| 111 - longer than a millisecond polls are rounded up to the next nearest |
| 112 millisecond to avoid spinning |
| 113 - infinite timeouts are converted to -1 */ |
| 114 int grpc_poll_deadline_to_millis_timeout(gpr_timespec deadline, |
| 115 gpr_timespec now); |
| 116 |
| 117 /* Allow kick to wakeup the currently polling worker */ |
| 118 #define GRPC_POLLSET_CAN_KICK_SELF 1 |
| 119 /* Force the wakee to repoll when awoken */ |
| 120 #define GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP 2 |
| 121 /* As per grpc_pollset_kick, with an extended set of flags (defined above) |
| 122 -- mostly for fd_posix's use. */ |
| 123 void grpc_pollset_kick_ext(grpc_pollset *p, |
| 124 grpc_pollset_worker *specific_worker, |
| 125 uint32_t flags); |
| 126 |
| 127 /* turn a pollset into a multipoller: platform specific */ |
| 128 typedef 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); |
| 132 extern grpc_platform_become_multipoller_type grpc_platform_become_multipoller; |
| 133 |
| 134 void grpc_poll_become_multipoller(grpc_exec_ctx *exec_ctx, |
| 135 grpc_pollset *pollset, struct grpc_fd **fds, |
| 136 size_t fd_count); |
| 137 |
| 138 /* Return 1 if the pollset has active threads in grpc_pollset_work (pollset must |
| 139 * be locked) */ |
| 140 int grpc_pollset_has_workers(grpc_pollset *pollset); |
| 141 |
| 142 void grpc_remove_fd_from_all_epoll_sets(int fd); |
| 143 |
| 144 /* override to allow tests to hook poll() usage */ |
| 145 /* 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 */ |
| 149 typedef int (*grpc_poll_function_type)(struct pollfd *, nfds_t, int); |
| 150 extern grpc_poll_function_type grpc_poll_function; |
| 151 extern grpc_wakeup_fd grpc_global_wakeup_fd; |
| 152 |
| 153 #endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_POSIX_H */ |
OLD | NEW |