ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, 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 | |
Craig Tiller | d14a1a5 | 2015-01-21 15:26:29 -0800 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
| 35 | |
| 36 | #ifdef GPR_POSIX_SOCKET |
| 37 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 38 | #include "src/core/iomgr/fd_posix.h" |
| 39 | |
| 40 | #include <assert.h> |
David Klempner | c6bccc2 | 2015-02-24 17:33:05 -0800 | [diff] [blame] | 41 | #include <sys/socket.h> |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 42 | #include <unistd.h> |
| 43 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 44 | #include <grpc/support/alloc.h> |
| 45 | #include <grpc/support/log.h> |
| 46 | #include <grpc/support/useful.h> |
| 47 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 48 | enum descriptor_state |
| 49 | { |
Craig Tiller | f95e37f | 2015-02-18 15:15:29 -0800 | [diff] [blame] | 50 | NOT_READY = 0, |
| 51 | READY = 1 |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 52 | }; /* or a pointer to a closure to call */ |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 53 | |
David Klempner | d178524 | 2015-01-28 17:00:21 -0800 | [diff] [blame] | 54 | /* We need to keep a freelist not because of any concerns of malloc performance |
| 55 | * but instead so that implementations with multiple threads in (for example) |
| 56 | * epoll_wait deal with the race between pollset removal and incoming poll |
| 57 | * notifications. |
| 58 | * |
| 59 | * The problem is that the poller ultimately holds a reference to this |
| 60 | * object, so it is very difficult to know when is safe to free it, at least |
| 61 | * without some expensive synchronization. |
| 62 | * |
| 63 | * If we keep the object freelisted, in the worst case losing this race just |
| 64 | * becomes a spurious read notification on a reused fd. |
| 65 | */ |
| 66 | /* TODO(klempner): We could use some form of polling generation count to know |
| 67 | * when these are safe to free. */ |
| 68 | /* TODO(klempner): Consider disabling freelisting if we don't have multiple |
| 69 | * threads in poll on the same fd */ |
| 70 | /* TODO(klempner): Batch these allocations to reduce fragmentation */ |
| 71 | static grpc_fd *fd_freelist = NULL; |
| 72 | static gpr_mu fd_freelist_mu; |
| 73 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 74 | static void |
| 75 | freelist_fd (grpc_fd * fd) |
| 76 | { |
| 77 | gpr_mu_lock (&fd_freelist_mu); |
David Klempner | d178524 | 2015-01-28 17:00:21 -0800 | [diff] [blame] | 78 | fd->freelist_next = fd_freelist; |
| 79 | fd_freelist = fd; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 80 | grpc_iomgr_unregister_object (&fd->iomgr_object); |
| 81 | gpr_mu_unlock (&fd_freelist_mu); |
David Klempner | d178524 | 2015-01-28 17:00:21 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 84 | static grpc_fd * |
| 85 | alloc_fd (int fd) |
| 86 | { |
David Klempner | d178524 | 2015-01-28 17:00:21 -0800 | [diff] [blame] | 87 | grpc_fd *r = NULL; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 88 | gpr_mu_lock (&fd_freelist_mu); |
| 89 | if (fd_freelist != NULL) |
| 90 | { |
| 91 | r = fd_freelist; |
| 92 | fd_freelist = fd_freelist->freelist_next; |
| 93 | } |
| 94 | gpr_mu_unlock (&fd_freelist_mu); |
| 95 | if (r == NULL) |
| 96 | { |
| 97 | r = gpr_malloc (sizeof (grpc_fd)); |
| 98 | gpr_mu_init (&r->set_state_mu); |
| 99 | gpr_mu_init (&r->watcher_mu); |
| 100 | } |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 101 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 102 | gpr_atm_rel_store (&r->refst, 1); |
| 103 | gpr_atm_rel_store (&r->readst, NOT_READY); |
| 104 | gpr_atm_rel_store (&r->writest, NOT_READY); |
| 105 | gpr_atm_rel_store (&r->shutdown, 0); |
David Klempner | d178524 | 2015-01-28 17:00:21 -0800 | [diff] [blame] | 106 | r->fd = fd; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 107 | r->inactive_watcher_root.next = r->inactive_watcher_root.prev = &r->inactive_watcher_root; |
David Klempner | d178524 | 2015-01-28 17:00:21 -0800 | [diff] [blame] | 108 | r->freelist_next = NULL; |
Craig Tiller | 886d7ec | 2015-05-14 16:18:42 -0700 | [diff] [blame] | 109 | r->read_watcher = r->write_watcher = NULL; |
Craig Tiller | 8b6cb8d | 2015-06-26 08:08:35 -0700 | [diff] [blame] | 110 | r->on_done_closure = NULL; |
Craig Tiller | 0613e58 | 2015-07-30 11:55:43 -0700 | [diff] [blame] | 111 | r->closed = 0; |
David Klempner | d178524 | 2015-01-28 17:00:21 -0800 | [diff] [blame] | 112 | return r; |
| 113 | } |
| 114 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 115 | static void |
| 116 | destroy (grpc_fd * fd) |
| 117 | { |
| 118 | gpr_mu_destroy (&fd->set_state_mu); |
| 119 | gpr_mu_destroy (&fd->watcher_mu); |
| 120 | gpr_free (fd); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 123 | #ifdef GRPC_FD_REF_COUNT_DEBUG |
| 124 | #define REF_BY(fd, n, reason) ref_by(fd, n, reason, __FILE__, __LINE__) |
| 125 | #define UNREF_BY(fd, n, reason) unref_by(fd, n, reason, __FILE__, __LINE__) |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 126 | static void |
| 127 | ref_by (grpc_fd * fd, int n, const char *reason, const char *file, int line) |
| 128 | { |
| 129 | gpr_log (GPR_DEBUG, "FD %d %p ref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n, gpr_atm_no_barrier_load (&fd->refst), gpr_atm_no_barrier_load (&fd->refst) + n, reason, file, line); |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 130 | #else |
| 131 | #define REF_BY(fd, n, reason) ref_by(fd, n) |
| 132 | #define UNREF_BY(fd, n, reason) unref_by(fd, n) |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 133 | static void |
| 134 | ref_by (grpc_fd * fd, int n) |
| 135 | { |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 136 | #endif |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 137 | GPR_ASSERT (gpr_atm_no_barrier_fetch_add (&fd->refst, n) > 0); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 140 | #ifdef GRPC_FD_REF_COUNT_DEBUG |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 141 | static void |
| 142 | unref_by (grpc_fd * fd, int n, const char *reason, const char *file, int line) |
| 143 | { |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 144 | gpr_atm old; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 145 | gpr_log (GPR_DEBUG, "FD %d %p unref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n, gpr_atm_no_barrier_load (&fd->refst), gpr_atm_no_barrier_load (&fd->refst) - n, reason, file, line); |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 146 | #else |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 147 | static void |
| 148 | unref_by (grpc_fd * fd, int n) |
| 149 | { |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 150 | gpr_atm old; |
| 151 | #endif |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 152 | old = gpr_atm_full_fetch_add (&fd->refst, -n); |
| 153 | if (old == n) |
| 154 | { |
| 155 | freelist_fd (fd); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | GPR_ASSERT (old > n); |
| 160 | } |
David Klempner | d178524 | 2015-01-28 17:00:21 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 163 | void |
| 164 | grpc_fd_global_init (void) |
| 165 | { |
| 166 | gpr_mu_init (&fd_freelist_mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 169 | void |
| 170 | grpc_fd_global_shutdown (void) |
| 171 | { |
| 172 | gpr_mu_lock (&fd_freelist_mu); |
| 173 | gpr_mu_unlock (&fd_freelist_mu); |
| 174 | while (fd_freelist != NULL) |
| 175 | { |
| 176 | grpc_fd *fd = fd_freelist; |
| 177 | fd_freelist = fd_freelist->freelist_next; |
| 178 | destroy (fd); |
| 179 | } |
| 180 | gpr_mu_destroy (&fd_freelist_mu); |
| 181 | } |
| 182 | |
| 183 | grpc_fd * |
| 184 | grpc_fd_create (int fd, const char *name) |
| 185 | { |
| 186 | grpc_fd *r = alloc_fd (fd); |
| 187 | grpc_iomgr_register_object (&r->iomgr_object, name); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 188 | return r; |
| 189 | } |
| 190 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 191 | int |
| 192 | grpc_fd_is_orphaned (grpc_fd * fd) |
| 193 | { |
| 194 | return (gpr_atm_acq_load (&fd->refst) & 1) == 0; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 197 | static void |
| 198 | pollset_kick_locked (grpc_pollset * pollset) |
| 199 | { |
| 200 | gpr_mu_lock (GRPC_POLLSET_MU (pollset)); |
| 201 | grpc_pollset_kick (pollset, NULL); |
| 202 | gpr_mu_unlock (GRPC_POLLSET_MU (pollset)); |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 205 | static void |
| 206 | maybe_wake_one_watcher_locked (grpc_fd * fd) |
| 207 | { |
| 208 | if (fd->inactive_watcher_root.next != &fd->inactive_watcher_root) |
| 209 | { |
| 210 | pollset_kick_locked (fd->inactive_watcher_root.next->pollset); |
| 211 | } |
| 212 | else if (fd->read_watcher) |
| 213 | { |
| 214 | pollset_kick_locked (fd->read_watcher->pollset); |
| 215 | } |
| 216 | else if (fd->write_watcher) |
| 217 | { |
| 218 | pollset_kick_locked (fd->write_watcher->pollset); |
| 219 | } |
Craig Tiller | 886d7ec | 2015-05-14 16:18:42 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 222 | static void |
| 223 | maybe_wake_one_watcher (grpc_fd * fd) |
| 224 | { |
| 225 | gpr_mu_lock (&fd->watcher_mu); |
| 226 | maybe_wake_one_watcher_locked (fd); |
| 227 | gpr_mu_unlock (&fd->watcher_mu); |
Craig Tiller | 886d7ec | 2015-05-14 16:18:42 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 230 | static void |
| 231 | wake_all_watchers_locked (grpc_fd * fd) |
| 232 | { |
Craig Tiller | 886d7ec | 2015-05-14 16:18:42 -0700 | [diff] [blame] | 233 | grpc_fd_watcher *watcher; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 234 | for (watcher = fd->inactive_watcher_root.next; watcher != &fd->inactive_watcher_root; watcher = watcher->next) |
| 235 | { |
| 236 | pollset_kick_locked (watcher->pollset); |
| 237 | } |
| 238 | if (fd->read_watcher) |
| 239 | { |
| 240 | pollset_kick_locked (fd->read_watcher->pollset); |
| 241 | } |
| 242 | if (fd->write_watcher && fd->write_watcher != fd->read_watcher) |
| 243 | { |
| 244 | pollset_kick_locked (fd->write_watcher->pollset); |
| 245 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 248 | static int |
| 249 | has_watchers (grpc_fd * fd) |
| 250 | { |
| 251 | return fd->read_watcher != NULL || fd->write_watcher != NULL || fd->inactive_watcher_root.next != &fd->inactive_watcher_root; |
Craig Tiller | 8b6cb8d | 2015-06-26 08:08:35 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 254 | void |
| 255 | grpc_fd_orphan (grpc_fd * fd, grpc_closure * on_done, const char *reason, grpc_closure_list * closure_list) |
| 256 | { |
Craig Tiller | 0317b3d | 2015-06-01 21:57:03 -0700 | [diff] [blame] | 257 | fd->on_done_closure = on_done; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 258 | shutdown (fd->fd, SHUT_RDWR); |
| 259 | gpr_mu_lock (&fd->watcher_mu); |
| 260 | REF_BY (fd, 1, reason); /* remove active status, but keep referenced */ |
| 261 | if (!has_watchers (fd)) |
| 262 | { |
| 263 | fd->closed = 1; |
| 264 | close (fd->fd); |
| 265 | grpc_closure_list_add (closure_list, fd->on_done_closure, 1); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | wake_all_watchers_locked (fd); |
| 270 | } |
| 271 | gpr_mu_unlock (&fd->watcher_mu); |
| 272 | UNREF_BY (fd, 2, reason); /* drop the reference */ |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* increment refcount by two to avoid changing the orphan bit */ |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 276 | #ifdef GRPC_FD_REF_COUNT_DEBUG |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 277 | void |
| 278 | grpc_fd_ref (grpc_fd * fd, const char *reason, const char *file, int line) |
| 279 | { |
| 280 | ref_by (fd, 2, reason, file, line); |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 281 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 282 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 283 | void |
| 284 | grpc_fd_unref (grpc_fd * fd, const char *reason, const char *file, int line) |
| 285 | { |
| 286 | unref_by (fd, 2, reason, file, line); |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 287 | } |
| 288 | #else |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 289 | void |
| 290 | grpc_fd_ref (grpc_fd * fd) |
| 291 | { |
| 292 | ref_by (fd, 2); |
| 293 | } |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 294 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 295 | void |
| 296 | grpc_fd_unref (grpc_fd * fd) |
| 297 | { |
| 298 | unref_by (fd, 2); |
| 299 | } |
Craig Tiller | 9ae7697 | 2015-05-31 13:58:24 -0700 | [diff] [blame] | 300 | #endif |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 301 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 302 | static void |
| 303 | notify_on (grpc_fd * fd, gpr_atm * st, grpc_closure * closure, grpc_closure_list * closure_list) |
| 304 | { |
| 305 | switch (gpr_atm_acq_load (st)) |
| 306 | { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 307 | case NOT_READY: |
| 308 | /* There is no race if the descriptor is already ready, so we skip |
| 309 | the interlocked op in that case. As long as the app doesn't |
| 310 | try to set the same upcall twice (which it shouldn't) then |
| 311 | oldval should never be anything other than READY or NOT_READY. We |
| 312 | don't |
| 313 | check for user error on the fast path. */ |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 314 | if (gpr_atm_rel_cas (st, NOT_READY, (gpr_intptr) closure)) |
| 315 | { |
| 316 | /* swap was successful -- the closure will run after the next |
| 317 | set_ready call. NOTE: we don't have an ABA problem here, |
| 318 | since we should never have concurrent calls to the same |
| 319 | notify_on function. */ |
| 320 | maybe_wake_one_watcher (fd); |
| 321 | return; |
| 322 | } |
| 323 | /* swap was unsuccessful due to an intervening set_ready call. |
| 324 | Fall through to the READY code below */ |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 325 | case READY: |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 326 | GPR_ASSERT (gpr_atm_no_barrier_load (st) == READY); |
| 327 | gpr_atm_rel_store (st, NOT_READY); |
| 328 | grpc_closure_list_add (closure_list, closure, !gpr_atm_acq_load (&fd->shutdown)); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 329 | return; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 330 | default: /* WAITING */ |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 331 | /* upcallptr was set to a different closure. This is an error! */ |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 332 | gpr_log (GPR_ERROR, "User called a notify_on function with a previous callback still " "pending"); |
| 333 | abort (); |
| 334 | } |
| 335 | gpr_log (GPR_ERROR, "Corrupt memory in &st->state"); |
| 336 | abort (); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 339 | static void |
| 340 | set_ready_locked (grpc_fd * fd, gpr_atm * st, grpc_closure_list * closure_list) |
| 341 | { |
| 342 | gpr_intptr state = gpr_atm_acq_load (st); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 343 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 344 | switch (state) |
| 345 | { |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 346 | case READY: |
| 347 | /* duplicate ready, ignore */ |
| 348 | return; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 349 | case NOT_READY: |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 350 | if (gpr_atm_rel_cas (st, NOT_READY, READY)) |
| 351 | { |
| 352 | /* swap was successful -- the closure will run after the next |
| 353 | notify_on call. */ |
| 354 | return; |
| 355 | } |
Craig Tiller | f95e37f | 2015-02-18 15:15:29 -0800 | [diff] [blame] | 356 | /* swap was unsuccessful due to an intervening set_ready call. |
| 357 | Fall through to the WAITING code below */ |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 358 | state = gpr_atm_acq_load (st); |
| 359 | default: /* waiting */ |
| 360 | GPR_ASSERT (gpr_atm_no_barrier_load (st) != READY && gpr_atm_no_barrier_load (st) != NOT_READY); |
| 361 | grpc_closure_list_add (closure_list, (grpc_closure *) state, !gpr_atm_acq_load (&fd->shutdown)); |
| 362 | gpr_atm_rel_store (st, NOT_READY); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 363 | return; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 364 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 367 | static void |
| 368 | set_ready (grpc_fd * fd, gpr_atm * st, grpc_closure_list * closure_list) |
| 369 | { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 370 | /* only one set_ready can be active at once (but there may be a racing |
| 371 | notify_on) */ |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 372 | gpr_mu_lock (&fd->set_state_mu); |
| 373 | set_ready_locked (fd, st, closure_list); |
| 374 | gpr_mu_unlock (&fd->set_state_mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 377 | void |
| 378 | grpc_fd_shutdown (grpc_fd * fd, grpc_closure_list * closure_list) |
| 379 | { |
| 380 | gpr_mu_lock (&fd->set_state_mu); |
| 381 | GPR_ASSERT (!gpr_atm_no_barrier_load (&fd->shutdown)); |
| 382 | gpr_atm_rel_store (&fd->shutdown, 1); |
| 383 | set_ready_locked (fd, &fd->readst, closure_list); |
| 384 | set_ready_locked (fd, &fd->writest, closure_list); |
| 385 | gpr_mu_unlock (&fd->set_state_mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 388 | void |
| 389 | grpc_fd_notify_on_read (grpc_fd * fd, grpc_closure * closure, grpc_closure_list * closure_list) |
| 390 | { |
| 391 | notify_on (fd, &fd->readst, closure, closure_list); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 394 | void |
| 395 | grpc_fd_notify_on_write (grpc_fd * fd, grpc_closure * closure, grpc_closure_list * closure_list) |
| 396 | { |
| 397 | notify_on (fd, &fd->writest, closure, closure_list); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 400 | gpr_uint32 |
| 401 | grpc_fd_begin_poll (grpc_fd * fd, grpc_pollset * pollset, gpr_uint32 read_mask, gpr_uint32 write_mask, grpc_fd_watcher * watcher) |
| 402 | { |
Craig Tiller | 886d7ec | 2015-05-14 16:18:42 -0700 | [diff] [blame] | 403 | gpr_uint32 mask = 0; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 404 | /* keep track of pollers that have requested our events, in case they change |
| 405 | */ |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 406 | GRPC_FD_REF (fd, "poll"); |
Craig Tiller | 59ea16f | 2015-02-18 16:18:08 -0800 | [diff] [blame] | 407 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 408 | gpr_mu_lock (&fd->watcher_mu); |
Craig Tiller | 8b6cb8d | 2015-06-26 08:08:35 -0700 | [diff] [blame] | 409 | /* if we are shutdown, then don't add to the watcher set */ |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 410 | if (gpr_atm_no_barrier_load (&fd->shutdown)) |
| 411 | { |
| 412 | watcher->fd = NULL; |
| 413 | watcher->pollset = NULL; |
| 414 | gpr_mu_unlock (&fd->watcher_mu); |
| 415 | GRPC_FD_UNREF (fd, "poll"); |
| 416 | return 0; |
| 417 | } |
Craig Tiller | 886d7ec | 2015-05-14 16:18:42 -0700 | [diff] [blame] | 418 | /* if there is nobody polling for read, but we need to, then start doing so */ |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 419 | if (read_mask && !fd->read_watcher && (gpr_uintptr) gpr_atm_acq_load (&fd->readst) > READY) |
| 420 | { |
| 421 | fd->read_watcher = watcher; |
| 422 | mask |= read_mask; |
| 423 | } |
Craig Tiller | 8e50fe9 | 2015-05-18 10:45:04 -0700 | [diff] [blame] | 424 | /* if there is nobody polling for write, but we need to, then start doing so |
| 425 | */ |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 426 | if (write_mask && !fd->write_watcher && (gpr_uintptr) gpr_atm_acq_load (&fd->writest) > READY) |
| 427 | { |
| 428 | fd->write_watcher = watcher; |
| 429 | mask |= write_mask; |
| 430 | } |
Craig Tiller | 886d7ec | 2015-05-14 16:18:42 -0700 | [diff] [blame] | 431 | /* if not polling, remember this watcher in case we need someone to later */ |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 432 | if (mask == 0) |
| 433 | { |
| 434 | watcher->next = &fd->inactive_watcher_root; |
| 435 | watcher->prev = watcher->next->prev; |
| 436 | watcher->next->prev = watcher->prev->next = watcher; |
| 437 | } |
Craig Tiller | 7d41321 | 2015-02-09 08:00:02 -0800 | [diff] [blame] | 438 | watcher->pollset = pollset; |
| 439 | watcher->fd = fd; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 440 | gpr_mu_unlock (&fd->watcher_mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 441 | |
Craig Tiller | 886d7ec | 2015-05-14 16:18:42 -0700 | [diff] [blame] | 442 | return mask; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 445 | void |
| 446 | grpc_fd_end_poll (grpc_fd_watcher * watcher, int got_read, int got_write, grpc_closure_list * closure_list) |
| 447 | { |
Craig Tiller | 886d7ec | 2015-05-14 16:18:42 -0700 | [diff] [blame] | 448 | int was_polling = 0; |
| 449 | int kick = 0; |
| 450 | grpc_fd *fd = watcher->fd; |
Craig Tiller | 59ea16f | 2015-02-18 16:18:08 -0800 | [diff] [blame] | 451 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 452 | if (fd == NULL) |
| 453 | { |
| 454 | return; |
| 455 | } |
Craig Tiller | 8b6cb8d | 2015-06-26 08:08:35 -0700 | [diff] [blame] | 456 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 457 | gpr_mu_lock (&fd->watcher_mu); |
| 458 | if (watcher == fd->read_watcher) |
| 459 | { |
| 460 | /* remove read watcher, kick if we still need a read */ |
| 461 | was_polling = 1; |
| 462 | kick = kick || !got_read; |
| 463 | fd->read_watcher = NULL; |
| 464 | } |
| 465 | if (watcher == fd->write_watcher) |
| 466 | { |
| 467 | /* remove write watcher, kick if we still need a write */ |
| 468 | was_polling = 1; |
| 469 | kick = kick || !got_write; |
| 470 | fd->write_watcher = NULL; |
| 471 | } |
| 472 | if (!was_polling) |
| 473 | { |
| 474 | /* remove from inactive list */ |
| 475 | watcher->next->prev = watcher->prev; |
| 476 | watcher->prev->next = watcher->next; |
| 477 | } |
| 478 | if (kick) |
| 479 | { |
| 480 | maybe_wake_one_watcher_locked (fd); |
| 481 | } |
| 482 | if (grpc_fd_is_orphaned (fd) && !has_watchers (fd) && !fd->closed) |
| 483 | { |
| 484 | fd->closed = 1; |
| 485 | close (fd->fd); |
| 486 | grpc_closure_list_add (closure_list, fd->on_done_closure, 1); |
| 487 | } |
| 488 | gpr_mu_unlock (&fd->watcher_mu); |
Craig Tiller | 886d7ec | 2015-05-14 16:18:42 -0700 | [diff] [blame] | 489 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 490 | GRPC_FD_UNREF (fd, "poll"); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 493 | void |
| 494 | grpc_fd_become_readable (grpc_fd * fd, grpc_closure_list * closure_list) |
| 495 | { |
| 496 | set_ready (fd, &fd->readst, closure_list); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame^] | 499 | void |
| 500 | grpc_fd_become_writable (grpc_fd * fd, grpc_closure_list * closure_list) |
| 501 | { |
| 502 | set_ready (fd, &fd->writest, closure_list); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 503 | } |
Craig Tiller | d14a1a5 | 2015-01-21 15:26:29 -0800 | [diff] [blame] | 504 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 505 | #endif |