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 | f37ba94 | 2018-10-17 11:25:54 +0200 | [diff] [blame] | 31 | #include <common/ticks.h> |
| 32 | #include <common/time.h> |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 33 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | #include <types/fd.h> |
Willy Tarreau | f37ba94 | 2018-10-17 11:25:54 +0200 | [diff] [blame] | 35 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 37 | /* public variables */ |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 38 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 39 | extern volatile struct fdlist fd_cache; |
| 40 | extern volatile struct fdlist fd_cache_local[MAX_THREADS]; |
| 41 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 42 | extern volatile struct fdlist update_list; |
| 43 | |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 44 | extern unsigned long *polled_mask; |
| 45 | |
Christopher Faulet | 69553fe | 2018-01-15 11:57:03 +0100 | [diff] [blame] | 46 | extern unsigned long fd_cache_mask; // Mask of threads with events in the cache |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 47 | |
| 48 | extern THREAD_LOCAL int *fd_updt; // FD updates list |
| 49 | extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list |
| 50 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 51 | extern int poller_wr_pipe[MAX_THREADS]; |
| 52 | |
Willy Tarreau | 8b94969 | 2017-11-26 11:07:34 +0100 | [diff] [blame] | 53 | __decl_hathreads(extern HA_RWLOCK_T __attribute__((aligned(64))) fdcache_lock); /* global lock to protect fd_cache array */ |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 54 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 55 | /* Deletes an FD from the fdsets. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | * The file descriptor is also closed. |
| 57 | */ |
| 58 | void fd_delete(int fd); |
| 59 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 60 | /* Deletes an FD from the fdsets. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 61 | * The file descriptor is kept open. |
| 62 | */ |
| 63 | void fd_remove(int fd); |
| 64 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 65 | /* disable the specified poller */ |
| 66 | void disable_poller(const char *poller_name); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 68 | void poller_pipe_io_handler(int fd); |
| 69 | |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 70 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 71 | * Initialize the pollers till the best one is found. |
| 72 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 73 | * The pollers register themselves just before main() is called. |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 74 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 75 | int init_pollers(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 76 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 77 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 78 | * Deinitialize the pollers. |
| 79 | */ |
| 80 | void deinit_pollers(); |
| 81 | |
| 82 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 83 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 84 | * to create initialize part of them again. Returns 0 in case of failure, |
| 85 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 86 | * the the current poller is destroyed and the caller is responsible for trying |
| 87 | * another one by calling init_pollers() again. |
| 88 | */ |
| 89 | int fork_poller(); |
| 90 | |
| 91 | /* |
| 92 | * Lists the known pollers on <out>. |
| 93 | * Should be performed only before initialization. |
| 94 | */ |
| 95 | int list_pollers(FILE *out); |
| 96 | |
| 97 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 98 | * Runs the polling loop |
| 99 | */ |
| 100 | void run_poller(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 101 | |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 102 | /* Scan and process the cached events. This should be called right after |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 103 | * the poller. |
| 104 | */ |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 105 | void fd_process_cached_events(); |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 106 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 107 | void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off); |
| 108 | void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 109 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 110 | /* Mark fd <fd> as updated for polling and allocate an entry in the update list |
| 111 | * 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] | 112 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 113 | static inline void updt_fd_polling(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 114 | { |
Olivier Houchard | 6aab737 | 2018-08-17 13:37:59 +0200 | [diff] [blame] | 115 | if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) { |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 116 | unsigned int oldupdt; |
| 117 | |
| 118 | /* note: we don't have a test-and-set yet in hathreads */ |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 119 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 120 | if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid)) |
| 121 | return; |
| 122 | |
| 123 | oldupdt = HA_ATOMIC_ADD(&fd_nbupdt, 1) - 1; |
| 124 | fd_updt[oldupdt] = fd; |
| 125 | } else { |
| 126 | unsigned long update_mask = fdtab[fd].update_mask; |
| 127 | do { |
| 128 | if (update_mask == fdtab[fd].thread_mask) |
| 129 | return; |
| 130 | } while (!HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask, |
| 131 | fdtab[fd].thread_mask)); |
| 132 | fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update)); |
| 133 | } |
| 134 | |
| 135 | } |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 136 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 137 | /* Called from the poller to acknoledge we read an entry from the global |
| 138 | * update list, to remove our bit from the update_mask, and remove it from |
| 139 | * the list if we were the last one. |
| 140 | */ |
| 141 | static inline void done_update_polling(int fd) |
| 142 | { |
| 143 | unsigned long update_mask; |
| 144 | |
| 145 | update_mask = HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit); |
| 146 | while ((update_mask & all_threads_mask)== 0) { |
| 147 | /* If we were the last one that had to update that entry, remove it from the list */ |
| 148 | fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update)); |
| 149 | if (update_list.first == fd) |
| 150 | abort(); |
| 151 | update_mask = (volatile unsigned long)fdtab[fd].update_mask; |
| 152 | if ((update_mask & all_threads_mask) != 0) { |
| 153 | /* Maybe it's been re-updated in the meanwhile, and we |
| 154 | * wrongly removed it from the list, if so, re-add it |
| 155 | */ |
| 156 | fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update)); |
| 157 | update_mask = (volatile unsigned long)(fdtab[fd].update_mask); |
| 158 | /* And then check again, just in case after all it |
| 159 | * should be removed, even if it's very unlikely, given |
| 160 | * the current thread wouldn't have been able to take |
| 161 | * care of it yet */ |
| 162 | } else |
| 163 | break; |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 164 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 165 | } |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 168 | /* Allocates a cache entry for a file descriptor if it does not yet have one. |
| 169 | * This can be done at any time. |
| 170 | */ |
| 171 | static inline void fd_alloc_cache_entry(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 172 | { |
Willy Tarreau | 26fb5d8 | 2018-03-20 19:06:52 +0100 | [diff] [blame] | 173 | HA_ATOMIC_OR(&fd_cache_mask, fdtab[fd].thread_mask); |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 174 | if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1))) |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 175 | fd_add_to_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd, offsetof(struct fdtab, cache)); |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 176 | else |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 177 | fd_add_to_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache)); |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 180 | /* Removes entry used by fd <fd> from the FD cache and replaces it with the |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 181 | * last one. |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 182 | * If the fd has no entry assigned, return immediately. |
| 183 | */ |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 184 | static inline void fd_release_cache_entry(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 185 | { |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 186 | if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1))) |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 187 | fd_rm_from_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd, offsetof(struct fdtab, cache)); |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 188 | else |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 189 | fd_rm_from_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache)); |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 190 | } |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 191 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 192 | /* This function automatically enables/disables caching for an entry depending |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 193 | * on its state. It is only called on state changes. |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 194 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 195 | static inline void fd_update_cache(int fd) |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 196 | { |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 197 | /* only READY and ACTIVE states (the two with both flags set) require a cache entry */ |
| 198 | if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) || |
| 199 | ((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] | 200 | fd_alloc_cache_entry(fd); |
| 201 | } |
| 202 | else { |
| 203 | fd_release_cache_entry(fd); |
| 204 | } |
| 205 | } |
| 206 | |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 207 | /* |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 208 | * returns the FD's recv state (FD_EV_*) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 209 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 210 | static inline int fd_recv_state(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 211 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 212 | return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 215 | /* |
| 216 | * returns true if the FD is active for recv |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 217 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 218 | static inline int fd_recv_active(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 219 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 220 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R; |
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 recv |
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_recv_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_R; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * returns true if the FD is polled for recv |
| 233 | */ |
| 234 | static inline int fd_recv_polled(const int fd) |
| 235 | { |
| 236 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_R; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * returns the FD's send state (FD_EV_*) |
| 241 | */ |
| 242 | static inline int fd_send_state(const int fd) |
| 243 | { |
| 244 | return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * returns true if the FD is active for send |
| 249 | */ |
| 250 | static inline int fd_send_active(const int fd) |
| 251 | { |
| 252 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 255 | /* |
| 256 | * returns true if the FD is ready for send |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 257 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 258 | static inline int fd_send_ready(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 259 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 260 | return (unsigned)fdtab[fd].state & FD_EV_READY_W; |
| 261 | } |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 262 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 263 | /* |
| 264 | * returns true if the FD is polled for send |
| 265 | */ |
| 266 | static inline int fd_send_polled(const int fd) |
| 267 | { |
| 268 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_W; |
| 269 | } |
| 270 | |
Christopher Faulet | 8db2fdf | 2017-08-30 09:59:38 +0200 | [diff] [blame] | 271 | /* |
| 272 | * returns true if the FD is active for recv or send |
| 273 | */ |
| 274 | static inline int fd_active(const int fd) |
| 275 | { |
| 276 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW; |
| 277 | } |
| 278 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 279 | /* Disable processing recv events on fd <fd> */ |
| 280 | static inline void fd_stop_recv(int fd) |
| 281 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 282 | unsigned char old, new; |
| 283 | |
| 284 | old = fdtab[fd].state; |
| 285 | do { |
| 286 | if (!(old & FD_EV_ACTIVE_R)) |
| 287 | return; |
| 288 | new = old & ~FD_EV_ACTIVE_R; |
| 289 | new &= ~FD_EV_POLLED_R; |
| 290 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 291 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 292 | if ((old ^ new) & FD_EV_POLLED_R) |
| 293 | updt_fd_polling(fd); |
| 294 | |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 295 | if (atleast2(fdtab[fd].thread_mask)) |
| 296 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 297 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 298 | if (atleast2(fdtab[fd].thread_mask)) |
| 299 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 302 | /* Disable processing send events on fd <fd> */ |
| 303 | static inline void fd_stop_send(int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 304 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 305 | unsigned char old, new; |
| 306 | |
| 307 | old = fdtab[fd].state; |
| 308 | do { |
| 309 | if (!(old & FD_EV_ACTIVE_W)) |
| 310 | return; |
| 311 | new = old & ~FD_EV_ACTIVE_W; |
| 312 | new &= ~FD_EV_POLLED_W; |
| 313 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 314 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 315 | if ((old ^ new) & FD_EV_POLLED_W) |
| 316 | updt_fd_polling(fd); |
| 317 | |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 318 | if (atleast2(fdtab[fd].thread_mask)) |
| 319 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 320 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 321 | if (atleast2(fdtab[fd].thread_mask)) |
| 322 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 325 | /* Disable processing of events on fd <fd> for both directions. */ |
| 326 | static inline void fd_stop_both(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 327 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 328 | unsigned char old, new; |
| 329 | |
| 330 | old = fdtab[fd].state; |
| 331 | do { |
| 332 | if (!(old & FD_EV_ACTIVE_RW)) |
| 333 | return; |
| 334 | new = old & ~FD_EV_ACTIVE_RW; |
| 335 | new &= ~FD_EV_POLLED_RW; |
| 336 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 337 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 338 | if ((old ^ new) & FD_EV_POLLED_RW) |
| 339 | updt_fd_polling(fd); |
| 340 | |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 341 | if (atleast2(fdtab[fd].thread_mask)) |
| 342 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 343 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 344 | if (atleast2(fdtab[fd].thread_mask)) |
| 345 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 348 | /* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */ |
| 349 | static inline void fd_cant_recv(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 350 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 351 | unsigned char old, new; |
| 352 | |
| 353 | old = fdtab[fd].state; |
| 354 | do { |
| 355 | if (!(old & FD_EV_READY_R)) |
| 356 | return; |
| 357 | new = old & ~FD_EV_READY_R; |
| 358 | if (new & FD_EV_ACTIVE_R) |
| 359 | new |= FD_EV_POLLED_R; |
| 360 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 361 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 362 | if ((old ^ new) & FD_EV_POLLED_R) |
| 363 | updt_fd_polling(fd); |
| 364 | |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 365 | if (atleast2(fdtab[fd].thread_mask)) |
| 366 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 367 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 368 | if (atleast2(fdtab[fd].thread_mask)) |
| 369 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 372 | /* Report that FD <fd> can receive anymore without polling. */ |
| 373 | static inline void fd_may_recv(const int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 374 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 375 | /* marking ready never changes polled status */ |
| 376 | HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_R); |
| 377 | |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 378 | if (atleast2(fdtab[fd].thread_mask)) |
| 379 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 380 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 381 | if (atleast2(fdtab[fd].thread_mask)) |
| 382 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 385 | /* Disable readiness when polled. This is useful to interrupt reading when it |
| 386 | * is suspected that the end of data might have been reached (eg: short read). |
| 387 | * This can only be done using level-triggered pollers, so if any edge-triggered |
| 388 | * is ever implemented, a test will have to be added here. |
| 389 | */ |
| 390 | static inline void fd_done_recv(const int fd) |
| 391 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 392 | unsigned char old, new; |
| 393 | |
| 394 | old = fdtab[fd].state; |
| 395 | do { |
| 396 | if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R)) |
| 397 | return; |
| 398 | new = old & ~FD_EV_READY_R; |
| 399 | if (new & FD_EV_ACTIVE_R) |
| 400 | new |= FD_EV_POLLED_R; |
| 401 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 402 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 403 | if ((old ^ new) & FD_EV_POLLED_R) |
| 404 | updt_fd_polling(fd); |
| 405 | |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 406 | if (atleast2(fdtab[fd].thread_mask)) |
| 407 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 408 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 409 | if (atleast2(fdtab[fd].thread_mask)) |
| 410 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 413 | /* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */ |
| 414 | static inline void fd_cant_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 415 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 416 | unsigned char old, new; |
| 417 | |
| 418 | old = fdtab[fd].state; |
| 419 | do { |
| 420 | if (!(old & FD_EV_READY_W)) |
| 421 | return; |
| 422 | new = old & ~FD_EV_READY_W; |
| 423 | if (new & FD_EV_ACTIVE_W) |
| 424 | new |= FD_EV_POLLED_W; |
| 425 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 426 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 427 | if ((old ^ new) & FD_EV_POLLED_W) |
| 428 | updt_fd_polling(fd); |
| 429 | |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 430 | if (atleast2(fdtab[fd].thread_mask)) |
| 431 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 432 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 433 | if (atleast2(fdtab[fd].thread_mask)) |
| 434 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 437 | /* Report that FD <fd> can send anymore without polling (EAGAIN detected). */ |
| 438 | static inline void fd_may_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 439 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 440 | /* marking ready never changes polled status */ |
| 441 | HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W); |
| 442 | |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 443 | if (atleast2(fdtab[fd].thread_mask)) |
| 444 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 445 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 446 | if (atleast2(fdtab[fd].thread_mask)) |
| 447 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 448 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 449 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 450 | /* Prepare FD <fd> to try to receive */ |
| 451 | static inline void fd_want_recv(int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 452 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 453 | unsigned char old, new; |
| 454 | |
| 455 | old = fdtab[fd].state; |
| 456 | do { |
| 457 | if (old & FD_EV_ACTIVE_R) |
| 458 | return; |
| 459 | new = old | FD_EV_ACTIVE_R; |
| 460 | if (!(new & FD_EV_READY_R)) |
| 461 | new |= FD_EV_POLLED_R; |
| 462 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 463 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 464 | if ((old ^ new) & FD_EV_POLLED_R) |
| 465 | updt_fd_polling(fd); |
| 466 | |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 467 | if (atleast2(fdtab[fd].thread_mask)) |
| 468 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 469 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 470 | if (atleast2(fdtab[fd].thread_mask)) |
| 471 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 474 | /* Prepare FD <fd> to try to send */ |
| 475 | static inline void fd_want_send(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 476 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 477 | unsigned char old, new; |
| 478 | |
| 479 | old = fdtab[fd].state; |
| 480 | do { |
| 481 | if (old & FD_EV_ACTIVE_W) |
| 482 | return; |
| 483 | new = old | FD_EV_ACTIVE_W; |
| 484 | if (!(new & FD_EV_READY_W)) |
| 485 | new |= FD_EV_POLLED_W; |
| 486 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 487 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 488 | if ((old ^ new) & FD_EV_POLLED_W) |
| 489 | updt_fd_polling(fd); |
| 490 | |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 491 | if (atleast2(fdtab[fd].thread_mask)) |
| 492 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 493 | fd_update_cache(fd); /* need an update entry to change the state */ |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 494 | if (atleast2(fdtab[fd].thread_mask)) |
| 495 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 496 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 497 | |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 498 | /* Update events seen for FD <fd> and its state if needed. This should be called |
| 499 | * by the poller to set FD_POLL_* flags. */ |
| 500 | static inline void fd_update_events(int fd, int evts) |
| 501 | { |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 502 | if (atleast2(fdtab[fd].thread_mask)) |
| 503 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 504 | fdtab[fd].ev &= FD_POLL_STICKY; |
| 505 | fdtab[fd].ev |= evts; |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 506 | if (atleast2(fdtab[fd].thread_mask)) |
| 507 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 508 | |
| 509 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
| 510 | fd_may_recv(fd); |
| 511 | |
| 512 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) |
| 513 | fd_may_send(fd); |
| 514 | } |
| 515 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 516 | /* Prepares <fd> for being polled */ |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 517 | static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 518 | { |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 519 | if (atleast2(thread_mask)) |
| 520 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 521 | fdtab[fd].owner = owner; |
| 522 | fdtab[fd].iocb = iocb; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 523 | fdtab[fd].ev = 0; |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 524 | fdtab[fd].linger_risk = 0; |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 525 | fdtab[fd].cloned = 0; |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 526 | fdtab[fd].thread_mask = thread_mask; |
Willy Tarreau | c9c8378 | 2018-01-17 18:44:46 +0100 | [diff] [blame] | 527 | /* note: do not reset polled_mask here as it indicates which poller |
| 528 | * still knows this FD from a possible previous round. |
| 529 | */ |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 530 | if (atleast2(thread_mask)) |
| 531 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 532 | } |
| 533 | |
Willy Tarreau | f37ba94 | 2018-10-17 11:25:54 +0200 | [diff] [blame] | 534 | /* Computes the bounded poll() timeout based on the next expiration timer <next> |
| 535 | * by bounding it to MAX_DELAY_MS. <next> may equal TICK_ETERNITY. The pollers |
| 536 | * just needs to call this function right before polling to get their timeout |
| 537 | * value. Timeouts that are already expired (possibly due to a pending event) |
| 538 | * are accounted for in activity.poll_exp. |
| 539 | */ |
| 540 | static inline int compute_poll_timeout(int next) |
| 541 | { |
| 542 | int wait_time; |
| 543 | |
| 544 | if (!tick_isset(next)) |
| 545 | wait_time = MAX_DELAY_MS; |
| 546 | else if (tick_is_expired(next, now_ms)) { |
| 547 | activity[tid].poll_exp++; |
| 548 | wait_time = 0; |
| 549 | } |
| 550 | else { |
| 551 | wait_time = TICKS_TO_MS(tick_remain(now_ms, next)) + 1; |
| 552 | if (wait_time > MAX_DELAY_MS) |
| 553 | wait_time = MAX_DELAY_MS; |
| 554 | } |
| 555 | return wait_time; |
| 556 | } |
| 557 | |
Willy Tarreau | 322e6c7 | 2018-01-25 16:37:04 +0100 | [diff] [blame] | 558 | /* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */ |
| 559 | static inline void hap_fd_set(int fd, unsigned int *evts) |
| 560 | { |
Willy Tarreau | 82b37d7 | 2018-01-25 16:59:09 +0100 | [diff] [blame] | 561 | HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1))); |
Willy Tarreau | 322e6c7 | 2018-01-25 16:37:04 +0100 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static inline void hap_fd_clr(int fd, unsigned int *evts) |
| 565 | { |
Willy Tarreau | 82b37d7 | 2018-01-25 16:59:09 +0100 | [diff] [blame] | 566 | HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1)))); |
Willy Tarreau | 322e6c7 | 2018-01-25 16:37:04 +0100 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | static inline unsigned int hap_fd_isset(int fd, unsigned int *evts) |
| 570 | { |
| 571 | return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1))); |
| 572 | } |
| 573 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 574 | static inline void wake_thread(int tid) |
| 575 | { |
| 576 | char c = 'c'; |
| 577 | |
| 578 | shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1)); |
| 579 | } |
| 580 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 581 | |
| 582 | #endif /* _PROTO_FD_H */ |
| 583 | |
| 584 | /* |
| 585 | * Local variables: |
| 586 | * c-indent-level: 8 |
| 587 | * c-basic-offset: 8 |
| 588 | * End: |
| 589 | */ |