ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | 3466c4b | 2016-01-12 10:26:04 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 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 | #include <grpc/support/port_platform.h> |
| 35 | |
Craig Tiller | 1a6f755 | 2015-05-29 13:56:44 -0700 | [diff] [blame] | 36 | #ifdef GPR_POSIX_SOCKET |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 37 | |
| 38 | #include "src/core/iomgr/pollset_posix.h" |
| 39 | |
| 40 | #include <errno.h> |
| 41 | #include <poll.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
| 44 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 45 | #include <grpc/support/alloc.h> |
| 46 | #include <grpc/support/log.h> |
| 47 | #include <grpc/support/useful.h> |
| 48 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 49 | #include "src/core/iomgr/fd_posix.h" |
| 50 | #include "src/core/iomgr/iomgr_internal.h" |
| 51 | #include "src/core/iomgr/pollset_posix.h" |
| 52 | #include "src/core/support/block_annotate.h" |
| 53 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 54 | typedef struct { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 55 | /* all polled fds */ |
| 56 | size_t fd_count; |
| 57 | size_t fd_capacity; |
| 58 | grpc_fd **fds; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 59 | /* fds that have been removed from the pollset explicitly */ |
| 60 | size_t del_count; |
| 61 | size_t del_capacity; |
| 62 | grpc_fd **dels; |
| 63 | } pollset_hdr; |
| 64 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 65 | static void multipoll_with_poll_pollset_add_fd(grpc_exec_ctx *exec_ctx, |
| 66 | grpc_pollset *pollset, |
| 67 | grpc_fd *fd, |
| 68 | int and_unlock_pollset) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 69 | size_t i; |
| 70 | pollset_hdr *h = pollset->data.ptr; |
| 71 | /* TODO(ctiller): this is O(num_fds^2); maybe switch to a hash set here */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | for (i = 0; i < h->fd_count; i++) { |
| 73 | if (h->fds[i] == fd) goto exit; |
| 74 | } |
| 75 | if (h->fd_count == h->fd_capacity) { |
| 76 | h->fd_capacity = GPR_MAX(h->fd_capacity + 8, h->fd_count * 3 / 2); |
| 77 | h->fds = gpr_realloc(h->fds, sizeof(grpc_fd *) * h->fd_capacity); |
| 78 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 79 | h->fds[h->fd_count++] = fd; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | GRPC_FD_REF(fd, "multipoller"); |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 81 | exit: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | if (and_unlock_pollset) { |
Craig Tiller | 85371a2 | 2016-02-25 13:55:13 -0800 | [diff] [blame] | 83 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 84 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 87 | static void multipoll_with_poll_pollset_maybe_work_and_unlock( |
| 88 | grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, grpc_pollset_worker *worker, |
| 89 | gpr_timespec deadline, gpr_timespec now) { |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 90 | #define POLLOUT_CHECK (POLLOUT | POLLHUP | POLLERR) |
Craig Tiller | 57f79d6 | 2015-10-02 14:00:12 -0700 | [diff] [blame] | 91 | #define POLLIN_CHECK (POLLIN | POLLHUP | POLLERR) |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 92 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 93 | int timeout; |
| 94 | int r; |
Craig Tiller | ebc7ef2 | 2015-09-10 22:19:25 -0700 | [diff] [blame] | 95 | size_t i, j, fd_count; |
| 96 | nfds_t pfd_count; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 97 | pollset_hdr *h; |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 98 | /* TODO(ctiller): inline some elements to avoid an allocation */ |
| 99 | grpc_fd_watcher *watchers; |
| 100 | struct pollfd *pfds; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 101 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 102 | h = pollset->data.ptr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 103 | timeout = grpc_poll_deadline_to_millis_timeout(deadline, now); |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 104 | /* TODO(ctiller): perform just one malloc here if we exceed the inline case */ |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 105 | pfds = gpr_malloc(sizeof(*pfds) * (h->fd_count + 2)); |
| 106 | watchers = gpr_malloc(sizeof(*watchers) * (h->fd_count + 2)); |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 107 | fd_count = 0; |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 108 | pfd_count = 2; |
| 109 | pfds[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd); |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 110 | pfds[0].events = POLLIN; |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 111 | pfds[0].revents = 0; |
Craig Tiller | e8b5f62 | 2015-11-02 14:15:03 -0800 | [diff] [blame] | 112 | pfds[1].fd = GRPC_WAKEUP_FD_GET_READ_FD(&worker->wakeup_fd->fd); |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 113 | pfds[1].events = POLLIN; |
| 114 | pfds[1].revents = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 115 | for (i = 0; i < h->fd_count; i++) { |
| 116 | int remove = grpc_fd_is_orphaned(h->fds[i]); |
| 117 | for (j = 0; !remove && j < h->del_count; j++) { |
| 118 | if (h->fds[i] == h->dels[j]) remove = 1; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 119 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 120 | if (remove) { |
| 121 | GRPC_FD_UNREF(h->fds[i], "multipoller"); |
| 122 | } else { |
| 123 | h->fds[fd_count++] = h->fds[i]; |
| 124 | watchers[pfd_count].fd = h->fds[i]; |
Craig Tiller | b3320fc | 2016-03-04 14:52:06 -0800 | [diff] [blame] | 125 | GRPC_FD_REF(watchers[pfd_count].fd, "multipoller_start"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 126 | pfds[pfd_count].fd = h->fds[i]->fd; |
| 127 | pfds[pfd_count].revents = 0; |
| 128 | pfd_count++; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 129 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | } |
| 131 | for (j = 0; j < h->del_count; j++) { |
| 132 | GRPC_FD_UNREF(h->dels[j], "multipoller_del"); |
| 133 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 134 | h->del_count = 0; |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 135 | h->fd_count = fd_count; |
Craig Tiller | 85371a2 | 2016-02-25 13:55:13 -0800 | [diff] [blame] | 136 | gpr_mu_unlock(&pollset->mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 137 | |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 138 | for (i = 2; i < pfd_count; i++) { |
Craig Tiller | b3320fc | 2016-03-04 14:52:06 -0800 | [diff] [blame] | 139 | grpc_fd *fd = watchers[i].fd; |
Craig Tiller | f1b6d61 | 2016-03-04 15:00:02 -0800 | [diff] [blame] | 140 | pfds[i].events = (short)grpc_fd_begin_poll(fd, pollset, worker, POLLIN, |
| 141 | POLLOUT, &watchers[i]); |
Craig Tiller | b3320fc | 2016-03-04 14:52:06 -0800 | [diff] [blame] | 142 | GRPC_FD_UNREF(fd, "multipoller_start"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 143 | } |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 144 | |
Vijay Pai | 8c7665e | 2015-09-25 21:40:19 -0700 | [diff] [blame] | 145 | /* TODO(vpai): Consider first doing a 0 timeout poll here to avoid |
| 146 | even going into the blocking annotation if possible */ |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 147 | GRPC_SCHEDULING_START_BLOCKING_REGION; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 148 | r = grpc_poll_function(pfds, pfd_count, timeout); |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 149 | GRPC_SCHEDULING_END_BLOCKING_REGION; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 150 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 151 | if (r < 0) { |
Craig Tiller | 4c219e6 | 2016-01-06 16:52:28 -0800 | [diff] [blame] | 152 | if (errno != EINTR) { |
| 153 | gpr_log(GPR_ERROR, "poll() failed: %s", strerror(errno)); |
| 154 | } |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 155 | for (i = 2; i < pfd_count; i++) { |
| 156 | grpc_fd_end_poll(exec_ctx, &watchers[i], 0, 0); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 157 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 158 | } else if (r == 0) { |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 159 | for (i = 2; i < pfd_count; i++) { |
| 160 | grpc_fd_end_poll(exec_ctx, &watchers[i], 0, 0); |
| 161 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | } else { |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 163 | if (pfds[0].revents & POLLIN_CHECK) { |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 164 | grpc_wakeup_fd_consume_wakeup(&grpc_global_wakeup_fd); |
| 165 | } |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 166 | if (pfds[1].revents & POLLIN_CHECK) { |
Craig Tiller | e8b5f62 | 2015-11-02 14:15:03 -0800 | [diff] [blame] | 167 | grpc_wakeup_fd_consume_wakeup(&worker->wakeup_fd->fd); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 168 | } |
Craig Tiller | 8afeec8 | 2015-09-28 17:03:34 -0700 | [diff] [blame] | 169 | for (i = 2; i < pfd_count; i++) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 170 | if (watchers[i].fd == NULL) { |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 171 | grpc_fd_end_poll(exec_ctx, &watchers[i], 0, 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 172 | continue; |
| 173 | } |
Craig Tiller | 58d05a6 | 2015-10-02 13:59:31 -0700 | [diff] [blame] | 174 | grpc_fd_end_poll(exec_ctx, &watchers[i], pfds[i].revents & POLLIN_CHECK, |
| 175 | pfds[i].revents & POLLOUT_CHECK); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 176 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 177 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 178 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 179 | gpr_free(pfds); |
| 180 | gpr_free(watchers); |
David Klempner | baced4d | 2015-02-10 17:10:15 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 183 | static void multipoll_with_poll_pollset_finish_shutdown(grpc_pollset *pollset) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 184 | size_t i; |
| 185 | pollset_hdr *h = pollset->data.ptr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | for (i = 0; i < h->fd_count; i++) { |
| 187 | GRPC_FD_UNREF(h->fds[i], "multipoller"); |
| 188 | } |
| 189 | for (i = 0; i < h->del_count; i++) { |
| 190 | GRPC_FD_UNREF(h->dels[i], "multipoller_del"); |
| 191 | } |
Craig Tiller | 5ef2754 | 2015-06-01 08:24:35 -0700 | [diff] [blame] | 192 | h->fd_count = 0; |
| 193 | h->del_count = 0; |
| 194 | } |
| 195 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 196 | static void multipoll_with_poll_pollset_destroy(grpc_pollset *pollset) { |
Craig Tiller | 5ef2754 | 2015-06-01 08:24:35 -0700 | [diff] [blame] | 197 | pollset_hdr *h = pollset->data.ptr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 198 | multipoll_with_poll_pollset_finish_shutdown(pollset); |
| 199 | gpr_free(h->fds); |
| 200 | gpr_free(h->dels); |
| 201 | gpr_free(h); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static const grpc_pollset_vtable multipoll_with_poll_pollset = { |
Craig Tiller | 88a33ef | 2015-12-11 13:17:40 -0800 | [diff] [blame] | 205 | multipoll_with_poll_pollset_add_fd, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | multipoll_with_poll_pollset_maybe_work_and_unlock, |
| 207 | multipoll_with_poll_pollset_finish_shutdown, |
| 208 | multipoll_with_poll_pollset_destroy}; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 209 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 210 | void grpc_poll_become_multipoller(grpc_exec_ctx *exec_ctx, |
| 211 | grpc_pollset *pollset, grpc_fd **fds, |
| 212 | size_t nfds) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 213 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 214 | pollset_hdr *h = gpr_malloc(sizeof(pollset_hdr)); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 215 | pollset->vtable = &multipoll_with_poll_pollset; |
| 216 | pollset->data.ptr = h; |
| 217 | h->fd_count = nfds; |
| 218 | h->fd_capacity = nfds; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 219 | h->fds = gpr_malloc(nfds * sizeof(grpc_fd *)); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 220 | h->del_count = 0; |
| 221 | h->del_capacity = 0; |
| 222 | h->dels = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | for (i = 0; i < nfds; i++) { |
| 224 | h->fds[i] = fds[i]; |
| 225 | GRPC_FD_REF(fds[i], "multipoller"); |
| 226 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Craig Tiller | 1a6f755 | 2015-05-29 13:56:44 -0700 | [diff] [blame] | 229 | #endif /* GPR_POSIX_SOCKET */ |
| 230 | |
| 231 | #ifdef GPR_POSIX_MULTIPOLL_WITH_POLL |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 232 | grpc_platform_become_multipoller_type grpc_platform_become_multipoller = |
| 233 | grpc_poll_become_multipoller; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 234 | #endif |