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_FD_POSIX_H |
| 35 #define GRPC_INTERNAL_CORE_IOMGR_FD_POSIX_H |
| 36 |
| 37 #include "src/core/iomgr/iomgr_internal.h" |
| 38 #include "src/core/iomgr/pollset.h" |
| 39 #include <grpc/support/atm.h> |
| 40 #include <grpc/support/sync.h> |
| 41 #include <grpc/support/time.h> |
| 42 |
| 43 typedef struct grpc_fd grpc_fd; |
| 44 |
| 45 typedef struct grpc_fd_watcher { |
| 46 struct grpc_fd_watcher *next; |
| 47 struct grpc_fd_watcher *prev; |
| 48 grpc_pollset *pollset; |
| 49 grpc_pollset_worker *worker; |
| 50 grpc_fd *fd; |
| 51 } grpc_fd_watcher; |
| 52 |
| 53 struct grpc_fd { |
| 54 int fd; |
| 55 /* refst format: |
| 56 bit0: 1=active/0=orphaned |
| 57 bit1-n: refcount |
| 58 meaning that mostly we ref by two to avoid altering the orphaned bit, |
| 59 and just unref by 1 when we're ready to flag the object as orphaned */ |
| 60 gpr_atm refst; |
| 61 |
| 62 gpr_mu mu; |
| 63 int shutdown; |
| 64 int closed; |
| 65 int released; |
| 66 |
| 67 /* The watcher list. |
| 68 |
| 69 The following watcher related fields are protected by watcher_mu. |
| 70 |
| 71 An fd_watcher is an ephemeral object created when an fd wants to |
| 72 begin polling, and destroyed after the poll. |
| 73 |
| 74 It denotes the fd's interest in whether to read poll or write poll |
| 75 or both or neither on this fd. |
| 76 |
| 77 If a watcher is asked to poll for reads or writes, the read_watcher |
| 78 or write_watcher fields are set respectively. A watcher may be asked |
| 79 to poll for both, in which case both fields will be set. |
| 80 |
| 81 read_watcher and write_watcher may be NULL if no watcher has been |
| 82 asked to poll for reads or writes. |
| 83 |
| 84 If an fd_watcher is not asked to poll for reads or writes, it's added |
| 85 to a linked list of inactive watchers, rooted at inactive_watcher_root. |
| 86 If at a later time there becomes need of a poller to poll, one of |
| 87 the inactive pollers may be kicked out of their poll loops to take |
| 88 that responsibility. */ |
| 89 grpc_fd_watcher inactive_watcher_root; |
| 90 grpc_fd_watcher *read_watcher; |
| 91 grpc_fd_watcher *write_watcher; |
| 92 |
| 93 grpc_closure *read_closure; |
| 94 grpc_closure *write_closure; |
| 95 |
| 96 struct grpc_fd *freelist_next; |
| 97 |
| 98 grpc_closure *on_done_closure; |
| 99 |
| 100 grpc_iomgr_object iomgr_object; |
| 101 }; |
| 102 |
| 103 /* Create a wrapped file descriptor. |
| 104 Requires fd is a non-blocking file descriptor. |
| 105 This takes ownership of closing fd. */ |
| 106 grpc_fd *grpc_fd_create(int fd, const char *name); |
| 107 |
| 108 /* Return the wrapped fd, or -1 if it has been released or closed. */ |
| 109 int grpc_fd_wrapped_fd(grpc_fd *fd); |
| 110 |
| 111 /* Releases fd to be asynchronously destroyed. |
| 112 on_done is called when the underlying file descriptor is definitely close()d. |
| 113 If on_done is NULL, no callback will be made. |
| 114 If release_fd is not NULL, it's set to fd and fd will not be closed. |
| 115 Requires: *fd initialized; no outstanding notify_on_read or |
| 116 notify_on_write. |
| 117 MUST NOT be called with a pollset lock taken */ |
| 118 void grpc_fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_closure *on_done, |
| 119 int *release_fd, const char *reason); |
| 120 |
| 121 /* Begin polling on an fd. |
| 122 Registers that the given pollset is interested in this fd - so that if read |
| 123 or writability interest changes, the pollset can be kicked to pick up that |
| 124 new interest. |
| 125 Return value is: |
| 126 (fd_needs_read? read_mask : 0) | (fd_needs_write? write_mask : 0) |
| 127 i.e. a combination of read_mask and write_mask determined by the fd's current |
| 128 interest in said events. |
| 129 Polling strategies that do not need to alter their behavior depending on the |
| 130 fd's current interest (such as epoll) do not need to call this function. |
| 131 MUST NOT be called with a pollset lock taken */ |
| 132 uint32_t grpc_fd_begin_poll(grpc_fd *fd, grpc_pollset *pollset, |
| 133 grpc_pollset_worker *worker, uint32_t read_mask, |
| 134 uint32_t write_mask, grpc_fd_watcher *rec); |
| 135 /* Complete polling previously started with grpc_fd_begin_poll |
| 136 MUST NOT be called with a pollset lock taken |
| 137 if got_read or got_write are 1, also does the become_{readable,writable} as |
| 138 appropriate. */ |
| 139 void grpc_fd_end_poll(grpc_exec_ctx *exec_ctx, grpc_fd_watcher *rec, |
| 140 int got_read, int got_write); |
| 141 |
| 142 /* Return 1 if this fd is orphaned, 0 otherwise */ |
| 143 int grpc_fd_is_orphaned(grpc_fd *fd); |
| 144 |
| 145 /* Cause any current callbacks to error out with GRPC_CALLBACK_CANCELLED. */ |
| 146 void grpc_fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd); |
| 147 |
| 148 /* Register read interest, causing read_cb to be called once when fd becomes |
| 149 readable, on deadline specified by deadline, or on shutdown triggered by |
| 150 grpc_fd_shutdown. |
| 151 read_cb will be called with read_cb_arg when *fd becomes readable. |
| 152 read_cb is Called with status of GRPC_CALLBACK_SUCCESS if readable, |
| 153 GRPC_CALLBACK_TIMED_OUT if the call timed out, |
| 154 and CANCELLED if the call was cancelled. |
| 155 |
| 156 Requires:This method must not be called before the read_cb for any previous |
| 157 call runs. Edge triggered events are used whenever they are supported by the |
| 158 underlying platform. This means that users must drain fd in read_cb before |
| 159 calling notify_on_read again. Users are also expected to handle spurious |
| 160 events, i.e read_cb is called while nothing can be readable from fd */ |
| 161 void grpc_fd_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
| 162 grpc_closure *closure); |
| 163 |
| 164 /* Exactly the same semantics as above, except based on writable events. */ |
| 165 void grpc_fd_notify_on_write(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
| 166 grpc_closure *closure); |
| 167 |
| 168 /* Notification from the poller to an fd that it has become readable or |
| 169 writable. |
| 170 If allow_synchronous_callback is 1, allow running the fd callback inline |
| 171 in this callstack, otherwise register an asynchronous callback and return */ |
| 172 void grpc_fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd); |
| 173 void grpc_fd_become_writable(grpc_exec_ctx *exec_ctx, grpc_fd *fd); |
| 174 |
| 175 /* Reference counting for fds */ |
| 176 /*#define GRPC_FD_REF_COUNT_DEBUG*/ |
| 177 #ifdef GRPC_FD_REF_COUNT_DEBUG |
| 178 void grpc_fd_ref(grpc_fd *fd, const char *reason, const char *file, int line); |
| 179 void grpc_fd_unref(grpc_fd *fd, const char *reason, const char *file, int line); |
| 180 #define GRPC_FD_REF(fd, reason) grpc_fd_ref(fd, reason, __FILE__, __LINE__) |
| 181 #define GRPC_FD_UNREF(fd, reason) grpc_fd_unref(fd, reason, __FILE__, __LINE__) |
| 182 #else |
| 183 void grpc_fd_ref(grpc_fd *fd); |
| 184 void grpc_fd_unref(grpc_fd *fd); |
| 185 #define GRPC_FD_REF(fd, reason) grpc_fd_ref(fd) |
| 186 #define GRPC_FD_UNREF(fd, reason) grpc_fd_unref(fd) |
| 187 #endif |
| 188 |
| 189 void grpc_fd_global_init(void); |
| 190 void grpc_fd_global_shutdown(void); |
| 191 |
| 192 #endif /* GRPC_INTERNAL_CORE_IOMGR_FD_POSIX_H */ |
OLD | NEW |