Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 2 | * include/proto/fd.h |
| 3 | * File descriptors states. |
| 4 | * |
| 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _PROTO_FD_H |
| 23 | #define _PROTO_FD_H |
| 24 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 30 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <types/fd.h> |
| 32 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 33 | /* public variables */ |
| 34 | extern int fd_nbspec; // number of speculative events in the list |
| 35 | extern int fd_nbupdt; // number of updates in the list |
| 36 | extern unsigned int *fd_spec; // speculative I/O list |
| 37 | extern unsigned int *fd_updt; // FD updates list |
| 38 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 40 | * The file descriptor is also closed. |
| 41 | */ |
| 42 | void fd_delete(int fd); |
| 43 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 44 | /* disable the specified poller */ |
| 45 | void disable_poller(const char *poller_name); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 47 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 48 | * Initialize the pollers till the best one is found. |
| 49 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 50 | * The pollers register themselves just before main() is called. |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 51 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 52 | int init_pollers(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 54 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 55 | * Deinitialize the pollers. |
| 56 | */ |
| 57 | void deinit_pollers(); |
| 58 | |
| 59 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 60 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 61 | * to create initialize part of them again. Returns 0 in case of failure, |
| 62 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 63 | * the the current poller is destroyed and the caller is responsible for trying |
| 64 | * another one by calling init_pollers() again. |
| 65 | */ |
| 66 | int fork_poller(); |
| 67 | |
| 68 | /* |
| 69 | * Lists the known pollers on <out>. |
| 70 | * Should be performed only before initialization. |
| 71 | */ |
| 72 | int list_pollers(FILE *out); |
| 73 | |
| 74 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 75 | * Runs the polling loop |
| 76 | */ |
| 77 | void run_poller(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 78 | |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 79 | /* Scan and process the speculative events. This should be called right after |
| 80 | * the poller. |
| 81 | */ |
| 82 | void fd_process_spec_events(); |
| 83 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 84 | /* Mark fd <fd> as updated and allocate an entry in the update list for this if |
| 85 | * it was not already there. This can be done at any time. |
| 86 | */ |
| 87 | static inline void updt_fd(const int fd) |
| 88 | { |
| 89 | if (fdtab[fd].updated) |
| 90 | /* already scheduled for update */ |
| 91 | return; |
| 92 | fd_updt[fd_nbupdt++] = fd; |
| 93 | fdtab[fd].updated = 1; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /* allocate an entry for a speculative event. This can be done at any time. */ |
| 98 | static inline void alloc_spec_entry(const int fd) |
| 99 | { |
| 100 | if (fdtab[fd].spec_p) |
| 101 | /* FD already in speculative I/O list */ |
| 102 | return; |
| 103 | fd_spec[fd_nbspec++] = fd; |
| 104 | fdtab[fd].spec_p = fd_nbspec; |
| 105 | } |
| 106 | |
| 107 | /* Removes entry used by fd <fd> from the spec list and replaces it with the |
| 108 | * last one. The fdtab.spec is adjusted to match the back reference if needed. |
| 109 | * If the fd has no entry assigned, return immediately. |
| 110 | */ |
| 111 | static inline void release_spec_entry(int fd) |
| 112 | { |
| 113 | unsigned int pos; |
| 114 | |
| 115 | pos = fdtab[fd].spec_p; |
| 116 | if (!pos) |
| 117 | return; |
| 118 | fdtab[fd].spec_p = 0; |
| 119 | fd_nbspec--; |
| 120 | if (pos <= fd_nbspec) { |
| 121 | /* was not the last entry */ |
| 122 | fd = fd_spec[fd_nbspec]; |
| 123 | fd_spec[pos - 1] = fd; |
| 124 | fdtab[fd].spec_p = pos; |
| 125 | } |
| 126 | } |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 127 | |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 128 | /* |
| 129 | * Returns non-zero if <fd> is already monitored for events in direction <dir>. |
| 130 | */ |
| 131 | static inline int fd_ev_is_set(const int fd, int dir) |
| 132 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 133 | return ((unsigned)fdtab[fd].spec_e >> dir) & FD_EV_STATUS; |
| 134 | } |
| 135 | |
| 136 | /* Disable processing of events on fd <fd> for direction <dir>. Note: this |
| 137 | * function was optimized to be used with a constant for <dir>. |
| 138 | */ |
| 139 | static inline void fd_ev_clr(const int fd, int dir) |
| 140 | { |
| 141 | unsigned int i = ((unsigned int)fdtab[fd].spec_e) & (FD_EV_STATUS << dir); |
| 142 | if (i == 0) |
| 143 | return; /* already disabled */ |
| 144 | fdtab[fd].spec_e ^= i; |
| 145 | updt_fd(fd); /* need an update entry to change the state */ |
| 146 | } |
| 147 | |
| 148 | /* Enable polling for events on fd <fd> for direction <dir>. Note: this |
| 149 | * function was optimized to be used with a constant for <dir>. |
| 150 | */ |
| 151 | static inline void fd_ev_wai(const int fd, int dir) |
| 152 | { |
| 153 | unsigned int i = ((unsigned int)fdtab[fd].spec_e) & (FD_EV_STATUS << dir); |
| 154 | if (i == (FD_EV_POLLED << dir)) |
| 155 | return; /* already in desired state */ |
| 156 | fdtab[fd].spec_e ^= i ^ (FD_EV_POLLED << dir); |
| 157 | updt_fd(fd); /* need an update entry to change the state */ |
| 158 | } |
| 159 | |
| 160 | /* Enable processing of events on fd <fd> for direction <dir>. Note: this |
| 161 | * function was optimized to be used with a constant for <dir>. |
| 162 | */ |
| 163 | static inline void fd_ev_set(int fd, int dir) |
| 164 | { |
| 165 | unsigned int i = ((unsigned int)fdtab[fd].spec_e) & (FD_EV_STATUS << dir); |
| 166 | |
| 167 | /* note that we don't care about disabling the polled state when |
| 168 | * enabling the active state, since it brings no benefit but costs |
| 169 | * some syscalls. |
| 170 | */ |
| 171 | if (i & (FD_EV_ACTIVE << dir)) |
| 172 | return; /* already in desired state */ |
| 173 | fdtab[fd].spec_e |= (FD_EV_ACTIVE << dir); |
| 174 | updt_fd(fd); /* need an update entry to change the state */ |
| 175 | } |
| 176 | |
| 177 | /* Disable processing of events on fd <fd> for both directions. */ |
| 178 | static inline void fd_ev_rem(const int fd) |
| 179 | { |
| 180 | unsigned int i = ((unsigned int)fdtab[fd].spec_e) & FD_EV_CURR_MASK; |
| 181 | if (i == 0) |
| 182 | return; /* already disabled */ |
| 183 | fdtab[fd].spec_e ^= i; |
| 184 | updt_fd(fd); /* need an update entry to change the state */ |
| 185 | } |
| 186 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 187 | /* event manipulation primitives for use by I/O callbacks */ |
| 188 | static inline void fd_want_recv(int fd) |
| 189 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 190 | return fd_ev_set(fd, DIR_RD); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static inline void fd_stop_recv(int fd) |
| 194 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 195 | return fd_ev_clr(fd, DIR_RD); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 198 | static inline void fd_poll_recv(int fd) |
| 199 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 200 | return fd_ev_wai(fd, DIR_RD); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 203 | static inline void fd_want_send(int fd) |
| 204 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 205 | return fd_ev_set(fd, DIR_WR); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static inline void fd_stop_send(int fd) |
| 209 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 210 | return fd_ev_clr(fd, DIR_WR); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 211 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 212 | |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 213 | static inline void fd_poll_send(int fd) |
| 214 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 215 | return fd_ev_wai(fd, DIR_WR); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 218 | static inline void fd_stop_both(int fd) |
| 219 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 220 | return fd_ev_rem(fd); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 221 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 222 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 223 | /* Prepares <fd> for being polled */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 224 | static inline void fd_insert(int fd) |
| 225 | { |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 226 | fdtab[fd].ev = 0; |
Willy Tarreau | 037d2c1 | 2012-11-06 02:34:46 +0100 | [diff] [blame] | 227 | fdtab[fd].new = 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 228 | if (fd + 1 > maxfd) |
| 229 | maxfd = fd + 1; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | #endif /* _PROTO_FD_H */ |
| 234 | |
| 235 | /* |
| 236 | * Local variables: |
| 237 | * c-indent-level: 8 |
| 238 | * c-basic-offset: 8 |
| 239 | * End: |
| 240 | */ |