blob: 9f70a26c64357f6617ce863a79c17ca86ef25532 [file] [log] [blame]
ctiller58393c22015-01-07 14:03:30 -08001/*
2 *
3 * Copyright 2014, 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
Craig Tillerd14a1a52015-01-21 15:26:29 -080034#include <grpc/support/port_platform.h>
35
36#ifdef GPR_POSIX_SOCKET
37
ctiller58393c22015-01-07 14:03:30 -080038#include "src/core/iomgr/fd_posix.h"
39
40#include <assert.h>
41#include <unistd.h>
42
43#include "src/core/iomgr/iomgr_internal.h"
44#include <grpc/support/alloc.h>
45#include <grpc/support/log.h>
46#include <grpc/support/useful.h>
47
48enum descriptor_state { NOT_READY, READY, WAITING };
49
50static void destroy(grpc_fd *fd) {
51 grpc_iomgr_add_callback(fd->on_done, fd->on_done_user_data);
52 gpr_mu_destroy(&fd->set_state_mu);
53 gpr_free(fd->watchers);
54 gpr_free(fd);
55 grpc_iomgr_unref();
56}
57
58static void ref_by(grpc_fd *fd, int n) {
59 gpr_atm_no_barrier_fetch_add(&fd->refst, n);
60}
61
62static void unref_by(grpc_fd *fd, int n) {
63 if (gpr_atm_full_fetch_add(&fd->refst, -n) == n) {
64 destroy(fd);
65 }
66}
67
68static void do_nothing(void *ignored, int success) {}
69
70grpc_fd *grpc_fd_create(int fd) {
71 grpc_fd *r = gpr_malloc(sizeof(grpc_fd));
72 grpc_iomgr_ref();
73 gpr_atm_rel_store(&r->refst, 1);
74 gpr_atm_rel_store(&r->readst.state, NOT_READY);
75 gpr_atm_rel_store(&r->writest.state, NOT_READY);
76 gpr_mu_init(&r->set_state_mu);
77 gpr_mu_init(&r->watcher_mu);
78 gpr_atm_rel_store(&r->shutdown, 0);
79 r->fd = fd;
80 r->watchers = NULL;
81 r->watcher_count = 0;
82 r->watcher_capacity = 0;
83 grpc_pollset_add_fd(grpc_backup_pollset(), r);
84 return r;
85}
86
87int grpc_fd_is_orphaned(grpc_fd *fd) {
88 return (gpr_atm_acq_load(&fd->refst) & 1) == 0;
89}
90
91static void wake_watchers(grpc_fd *fd) {
92 size_t i, n;
93 gpr_mu_lock(&fd->watcher_mu);
94 n = fd->watcher_count;
95 for (i = 0; i < n; i++) {
96 grpc_pollset_force_kick(fd->watchers[i]);
97 }
98 gpr_mu_unlock(&fd->watcher_mu);
99}
100
101void grpc_fd_orphan(grpc_fd *fd, grpc_iomgr_cb_func on_done, void *user_data) {
102 fd->on_done = on_done ? on_done : do_nothing;
103 fd->on_done_user_data = user_data;
104 ref_by(fd, 1); /* remove active status, but keep referenced */
105 wake_watchers(fd);
106 close(fd->fd);
107 unref_by(fd, 2); /* drop the reference */
108}
109
110/* increment refcount by two to avoid changing the orphan bit */
111void grpc_fd_ref(grpc_fd *fd) { ref_by(fd, 2); }
112
113void grpc_fd_unref(grpc_fd *fd) { unref_by(fd, 2); }
114
115typedef struct {
116 grpc_iomgr_cb_func cb;
117 void *arg;
118} callback;
119
120static void make_callback(grpc_iomgr_cb_func cb, void *arg, int success,
121 int allow_synchronous_callback) {
122 if (allow_synchronous_callback) {
123 cb(arg, success);
124 } else {
125 grpc_iomgr_add_delayed_callback(cb, arg, success);
126 }
127}
128
129static void make_callbacks(callback *callbacks, size_t n, int success,
130 int allow_synchronous_callback) {
131 size_t i;
132 for (i = 0; i < n; i++) {
133 make_callback(callbacks[i].cb, callbacks[i].arg, success,
134 allow_synchronous_callback);
135 }
136}
137
138static void notify_on(grpc_fd *fd, grpc_fd_state *st, grpc_iomgr_cb_func cb,
139 void *arg, int allow_synchronous_callback) {
140 switch ((enum descriptor_state)gpr_atm_acq_load(&st->state)) {
141 case NOT_READY:
142 /* There is no race if the descriptor is already ready, so we skip
143 the interlocked op in that case. As long as the app doesn't
144 try to set the same upcall twice (which it shouldn't) then
145 oldval should never be anything other than READY or NOT_READY. We
146 don't
147 check for user error on the fast path. */
148 st->cb = cb;
149 st->cb_arg = arg;
150 if (gpr_atm_rel_cas(&st->state, NOT_READY, WAITING)) {
151 /* swap was successful -- the closure will run after the next
152 set_ready call. NOTE: we don't have an ABA problem here,
153 since we should never have concurrent calls to the same
154 notify_on function. */
155 wake_watchers(fd);
156 return;
157 }
158 /* swap was unsuccessful due to an intervening set_ready call.
159 Fall through to the READY code below */
160 case READY:
161 assert(gpr_atm_acq_load(&st->state) == READY);
162 gpr_atm_rel_store(&st->state, NOT_READY);
163 make_callback(cb, arg, !gpr_atm_acq_load(&fd->shutdown),
164 allow_synchronous_callback);
165 return;
166 case WAITING:
167 /* upcallptr was set to a different closure. This is an error! */
168 gpr_log(GPR_ERROR,
169 "User called a notify_on function with a previous callback still "
170 "pending");
171 abort();
172 }
173 gpr_log(GPR_ERROR, "Corrupt memory in &st->state");
174 abort();
175}
176
177static void set_ready_locked(grpc_fd_state *st, callback *callbacks,
178 size_t *ncallbacks) {
179 callback *c;
180
181 switch ((enum descriptor_state)gpr_atm_acq_load(&st->state)) {
182 case NOT_READY:
183 if (gpr_atm_rel_cas(&st->state, NOT_READY, READY)) {
184 /* swap was successful -- the closure will run after the next
185 notify_on call. */
186 return;
187 }
188 /* swap was unsuccessful due to an intervening set_ready call.
189 Fall through to the WAITING code below */
190 case WAITING:
191 assert(gpr_atm_acq_load(&st->state) == WAITING);
192 c = &callbacks[(*ncallbacks)++];
193 c->cb = st->cb;
194 c->arg = st->cb_arg;
195 gpr_atm_rel_store(&st->state, NOT_READY);
196 return;
197 case READY:
198 /* duplicate ready, ignore */
199 return;
200 }
201}
202
203static void set_ready(grpc_fd *fd, grpc_fd_state *st,
204 int allow_synchronous_callback) {
205 /* only one set_ready can be active at once (but there may be a racing
206 notify_on) */
207 int success;
208 callback cb;
209 size_t ncb = 0;
210 gpr_mu_lock(&fd->set_state_mu);
211 set_ready_locked(st, &cb, &ncb);
212 gpr_mu_unlock(&fd->set_state_mu);
213 success = !gpr_atm_acq_load(&fd->shutdown);
214 make_callbacks(&cb, ncb, success, allow_synchronous_callback);
215}
216
217void grpc_fd_shutdown(grpc_fd *fd) {
218 callback cb[2];
219 size_t ncb = 0;
220 gpr_mu_lock(&fd->set_state_mu);
221 GPR_ASSERT(!gpr_atm_acq_load(&fd->shutdown));
222 gpr_atm_rel_store(&fd->shutdown, 1);
223 set_ready_locked(&fd->readst, cb, &ncb);
224 set_ready_locked(&fd->writest, cb, &ncb);
225 gpr_mu_unlock(&fd->set_state_mu);
226 make_callbacks(cb, ncb, 0, 0);
227}
228
229void grpc_fd_notify_on_read(grpc_fd *fd, grpc_iomgr_cb_func read_cb,
230 void *read_cb_arg) {
231 notify_on(fd, &fd->readst, read_cb, read_cb_arg, 0);
232}
233
234void grpc_fd_notify_on_write(grpc_fd *fd, grpc_iomgr_cb_func write_cb,
235 void *write_cb_arg) {
236 notify_on(fd, &fd->writest, write_cb, write_cb_arg, 0);
237}
238
239gpr_uint32 grpc_fd_begin_poll(grpc_fd *fd, grpc_pollset *pollset,
240 gpr_uint32 read_mask, gpr_uint32 write_mask) {
241 /* keep track of pollers that have requested our events, in case they change
242 */
243 gpr_mu_lock(&fd->watcher_mu);
244 if (fd->watcher_capacity == fd->watcher_count) {
245 fd->watcher_capacity =
246 GPR_MAX(fd->watcher_capacity + 8, fd->watcher_capacity * 3 / 2);
247 fd->watchers = gpr_realloc(fd->watchers,
248 fd->watcher_capacity * sizeof(grpc_pollset *));
249 }
250 fd->watchers[fd->watcher_count++] = pollset;
251 gpr_mu_unlock(&fd->watcher_mu);
252
253 return (gpr_atm_acq_load(&fd->readst.state) != READY ? read_mask : 0) |
254 (gpr_atm_acq_load(&fd->writest.state) != READY ? write_mask : 0);
255}
256
257void grpc_fd_end_poll(grpc_fd *fd, grpc_pollset *pollset) {
258 size_t r, w, n;
259
260 gpr_mu_lock(&fd->watcher_mu);
261 n = fd->watcher_count;
262 for (r = 0, w = 0; r < n; r++) {
263 if (fd->watchers[r] == pollset) {
264 fd->watcher_count--;
265 continue;
266 }
267 fd->watchers[w++] = fd->watchers[r];
268 }
269 gpr_mu_unlock(&fd->watcher_mu);
270}
271
272void grpc_fd_become_readable(grpc_fd *fd, int allow_synchronous_callback) {
273 set_ready(fd, &fd->readst, allow_synchronous_callback);
274}
275
276void grpc_fd_become_writable(grpc_fd *fd, int allow_synchronous_callback) {
277 set_ready(fd, &fd->writest, allow_synchronous_callback);
278}
Craig Tillerd14a1a52015-01-21 15:26:29 -0800279
280#endif