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 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 5 | * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 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 */ |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 34 | extern unsigned int *fd_cache; // FD events cache |
| 35 | extern unsigned int *fd_updt; // FD updates list |
| 36 | extern int fd_cache_num; // number of events in the cache |
| 37 | extern int fd_nbupdt; // number of updates in the list |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 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 | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 79 | /* Scan and process the cached events. This should be called right after |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 80 | * the poller. |
| 81 | */ |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 82 | void fd_process_cached_events(); |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 83 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 84 | /* Mark fd <fd> as updated for polling and allocate an entry in the update list |
| 85 | * for this if it was not already there. This can be done at any time. |
Willy Tarreau | e852545 | 2014-01-25 09:58:06 +0100 | [diff] [blame] | 86 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 87 | static inline void updt_fd_polling(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 88 | { |
| 89 | if (fdtab[fd].updated) |
| 90 | /* already scheduled for update */ |
| 91 | return; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 92 | fdtab[fd].updated = 1; |
Willy Tarreau | 4a29144 | 2012-12-13 23:34:18 +0100 | [diff] [blame] | 93 | fd_updt[fd_nbupdt++] = fd; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 97 | /* Allocates a cache entry for a file descriptor if it does not yet have one. |
| 98 | * This can be done at any time. |
| 99 | */ |
| 100 | static inline void fd_alloc_cache_entry(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 101 | { |
Willy Tarreau | 15a4dec | 2014-01-20 11:09:39 +0100 | [diff] [blame] | 102 | if (fdtab[fd].cache) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 103 | return; |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 104 | fd_cache_num++; |
| 105 | fdtab[fd].cache = fd_cache_num; |
| 106 | fd_cache[fd_cache_num-1] = fd; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 109 | /* Removes entry used by fd <fd> from the FD cache and replaces it with the |
| 110 | * last one. The fdtab.cache is adjusted to match the back reference if needed. |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 111 | * If the fd has no entry assigned, return immediately. |
| 112 | */ |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 113 | static inline void fd_release_cache_entry(int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 114 | { |
| 115 | unsigned int pos; |
| 116 | |
Willy Tarreau | 15a4dec | 2014-01-20 11:09:39 +0100 | [diff] [blame] | 117 | pos = fdtab[fd].cache; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 118 | if (!pos) |
| 119 | return; |
Willy Tarreau | 15a4dec | 2014-01-20 11:09:39 +0100 | [diff] [blame] | 120 | fdtab[fd].cache = 0; |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 121 | fd_cache_num--; |
| 122 | if (likely(pos <= fd_cache_num)) { |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 123 | /* was not the last entry */ |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 124 | fd = fd_cache[fd_cache_num]; |
| 125 | fd_cache[pos - 1] = fd; |
Willy Tarreau | 15a4dec | 2014-01-20 11:09:39 +0100 | [diff] [blame] | 126 | fdtab[fd].cache = pos; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 127 | } |
| 128 | } |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 129 | |
Willy Tarreau | 25002d2 | 2014-01-25 10:32:56 +0100 | [diff] [blame] | 130 | /* Computes the new polled status based on the active and ready statuses, for |
| 131 | * each direction. This is meant to be used by pollers while processing updates. |
| 132 | */ |
| 133 | static inline int fd_compute_new_polled_status(int state) |
| 134 | { |
| 135 | if (state & FD_EV_ACTIVE_R) { |
| 136 | if (!(state & FD_EV_READY_R)) |
| 137 | state |= FD_EV_POLLED_R; |
| 138 | } |
| 139 | else |
| 140 | state &= ~FD_EV_POLLED_R; |
| 141 | |
| 142 | if (state & FD_EV_ACTIVE_W) { |
| 143 | if (!(state & FD_EV_READY_W)) |
| 144 | state |= FD_EV_POLLED_W; |
| 145 | } |
| 146 | else |
| 147 | state &= ~FD_EV_POLLED_W; |
| 148 | |
| 149 | return state; |
| 150 | } |
| 151 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 152 | /* This function automatically enables/disables caching for an entry depending |
| 153 | * on its state, and also possibly creates an update entry so that the poller |
| 154 | * does its job as well. It is only called on state changes. |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 155 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 156 | static inline void fd_update_cache(int fd) |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 157 | { |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 158 | /* 3 states for each direction require a polling update */ |
| 159 | if ((fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_ACTIVE_R)) == FD_EV_POLLED_R || |
| 160 | (fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_READY_R | FD_EV_ACTIVE_R)) == FD_EV_ACTIVE_R || |
| 161 | (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_ACTIVE_W)) == FD_EV_POLLED_W || |
| 162 | (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_READY_W | FD_EV_ACTIVE_W)) == FD_EV_ACTIVE_W) |
| 163 | updt_fd_polling(fd); |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 164 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 165 | /* only READY and ACTIVE states (the two with both flags set) require a cache entry */ |
| 166 | if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) || |
| 167 | ((fdtab[fd].state & (FD_EV_READY_W | FD_EV_ACTIVE_W)) == (FD_EV_READY_W | FD_EV_ACTIVE_W))) { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 168 | fd_alloc_cache_entry(fd); |
| 169 | } |
| 170 | else { |
| 171 | fd_release_cache_entry(fd); |
| 172 | } |
| 173 | } |
| 174 | |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 175 | /* |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 176 | * returns the FD's recv state (FD_EV_*) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 177 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 178 | static inline int fd_recv_state(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 179 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 180 | return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 183 | /* |
| 184 | * returns true if the FD is active for recv |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 185 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 186 | static inline int fd_recv_active(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 187 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 188 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 191 | /* |
| 192 | * returns true if the FD is ready for recv |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 193 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 194 | static inline int fd_recv_ready(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 195 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 196 | return (unsigned)fdtab[fd].state & FD_EV_READY_R; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * returns true if the FD is polled for recv |
| 201 | */ |
| 202 | static inline int fd_recv_polled(const int fd) |
| 203 | { |
| 204 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_R; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * returns the FD's send state (FD_EV_*) |
| 209 | */ |
| 210 | static inline int fd_send_state(const int fd) |
| 211 | { |
| 212 | return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * returns true if the FD is active for send |
| 217 | */ |
| 218 | static inline int fd_send_active(const int fd) |
| 219 | { |
| 220 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 223 | /* |
| 224 | * returns true if the FD is ready for send |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 225 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 226 | static inline int fd_send_ready(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 227 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 228 | return (unsigned)fdtab[fd].state & FD_EV_READY_W; |
| 229 | } |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 230 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 231 | /* |
| 232 | * returns true if the FD is polled for send |
| 233 | */ |
| 234 | static inline int fd_send_polled(const int fd) |
| 235 | { |
| 236 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_W; |
| 237 | } |
| 238 | |
| 239 | /* Disable processing recv events on fd <fd> */ |
| 240 | static inline void fd_stop_recv(int fd) |
| 241 | { |
| 242 | if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_R)) |
| 243 | return; /* already disabled */ |
| 244 | fdtab[fd].state &= ~FD_EV_ACTIVE_R; |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 245 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 248 | /* Disable processing send events on fd <fd> */ |
| 249 | static inline void fd_stop_send(int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 250 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 251 | if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_W)) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 252 | return; /* already disabled */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 253 | fdtab[fd].state &= ~FD_EV_ACTIVE_W; |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 254 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 257 | /* Disable processing of events on fd <fd> for both directions. */ |
| 258 | static inline void fd_stop_both(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 259 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 260 | if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_RW)) |
| 261 | return; /* already disabled */ |
| 262 | fdtab[fd].state &= ~FD_EV_ACTIVE_RW; |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 263 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 266 | /* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */ |
| 267 | static inline void fd_cant_recv(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 268 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 269 | if (!(((unsigned int)fdtab[fd].state) & FD_EV_READY_R)) |
| 270 | return; /* already marked as blocked */ |
| 271 | fdtab[fd].state &= ~FD_EV_READY_R; |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 272 | fd_update_cache(fd); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 275 | /* Report that FD <fd> can receive anymore without polling. */ |
| 276 | static inline void fd_may_recv(const int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 277 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 278 | if (((unsigned int)fdtab[fd].state) & FD_EV_READY_R) |
| 279 | return; /* already marked as blocked */ |
| 280 | fdtab[fd].state |= FD_EV_READY_R; |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 281 | fd_update_cache(fd); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 284 | /* Disable readiness when polled. This is useful to interrupt reading when it |
| 285 | * is suspected that the end of data might have been reached (eg: short read). |
| 286 | * This can only be done using level-triggered pollers, so if any edge-triggered |
| 287 | * is ever implemented, a test will have to be added here. |
| 288 | */ |
| 289 | static inline void fd_done_recv(const int fd) |
| 290 | { |
| 291 | if (fd_recv_polled(fd)) |
| 292 | fd_cant_recv(fd); |
| 293 | } |
| 294 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 295 | /* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */ |
| 296 | static inline void fd_cant_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 297 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 298 | if (!(((unsigned int)fdtab[fd].state) & FD_EV_READY_W)) |
| 299 | return; /* already marked as blocked */ |
| 300 | fdtab[fd].state &= ~FD_EV_READY_W; |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 301 | fd_update_cache(fd); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 304 | /* Report that FD <fd> can send anymore without polling (EAGAIN detected). */ |
| 305 | static inline void fd_may_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 306 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 307 | if (((unsigned int)fdtab[fd].state) & FD_EV_READY_W) |
| 308 | return; /* already marked as blocked */ |
| 309 | fdtab[fd].state |= FD_EV_READY_W; |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 310 | fd_update_cache(fd); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 311 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 312 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 313 | /* Prepare FD <fd> to try to receive */ |
| 314 | static inline void fd_want_recv(int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 315 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 316 | if (((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_R)) |
| 317 | return; /* already enabled */ |
| 318 | fdtab[fd].state |= FD_EV_ACTIVE_R; |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 319 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 322 | /* Prepare FD <fd> to try to send */ |
| 323 | static inline void fd_want_send(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 324 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 325 | if (((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_W)) |
| 326 | return; /* already enabled */ |
| 327 | fdtab[fd].state |= FD_EV_ACTIVE_W; |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 328 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 329 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 330 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 331 | /* Prepares <fd> for being polled */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 332 | static inline void fd_insert(int fd) |
| 333 | { |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 334 | fdtab[fd].ev = 0; |
Willy Tarreau | 037d2c1 | 2012-11-06 02:34:46 +0100 | [diff] [blame] | 335 | fdtab[fd].new = 1; |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 336 | fdtab[fd].linger_risk = 0; |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 337 | fdtab[fd].cloned = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 338 | if (fd + 1 > maxfd) |
| 339 | maxfd = fd + 1; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | #endif /* _PROTO_FD_H */ |
| 344 | |
| 345 | /* |
| 346 | * Local variables: |
| 347 | * c-indent-level: 8 |
| 348 | * c-basic-offset: 8 |
| 349 | * End: |
| 350 | */ |