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> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | #include <types/fd.h> |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 34 | #include <proto/activity.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 36 | /* public variables */ |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 37 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 38 | extern volatile struct fdlist fd_cache; |
| 39 | extern volatile struct fdlist fd_cache_local[MAX_THREADS]; |
| 40 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 41 | extern volatile struct fdlist update_list; |
| 42 | |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 43 | extern unsigned long *polled_mask; |
| 44 | |
Christopher Faulet | 69553fe | 2018-01-15 11:57:03 +0100 | [diff] [blame] | 45 | 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] | 46 | |
| 47 | extern THREAD_LOCAL int *fd_updt; // FD updates list |
| 48 | extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list |
| 49 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 50 | extern int poller_wr_pipe[MAX_THREADS]; |
| 51 | |
Olivier Houchard | 7c49d2e | 2019-04-16 18:37:05 +0200 | [diff] [blame] | 52 | extern volatile int ha_used_fds; // Number of FDs we're currently using |
| 53 | |
Willy Tarreau | 8b94969 | 2017-11-26 11:07:34 +0100 | [diff] [blame] | 54 | __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] | 55 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 56 | /* Deletes an FD from the fdsets. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | * The file descriptor is also closed. |
| 58 | */ |
| 59 | void fd_delete(int fd); |
| 60 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 61 | /* Deletes an FD from the fdsets. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 62 | * The file descriptor is kept open. |
| 63 | */ |
| 64 | void fd_remove(int fd); |
| 65 | |
Willy Tarreau | 2d7f81b | 2019-02-21 22:19:17 +0100 | [diff] [blame] | 66 | /* close all FDs starting from <start> */ |
| 67 | void my_closefrom(int start); |
| 68 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 69 | /* disable the specified poller */ |
| 70 | void disable_poller(const char *poller_name); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 71 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 72 | void poller_pipe_io_handler(int fd); |
| 73 | |
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 | * Initialize the pollers till the best one is found. |
| 76 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 77 | * The pollers register themselves just before main() is called. |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 78 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 79 | int init_pollers(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 80 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 81 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 82 | * Deinitialize the pollers. |
| 83 | */ |
| 84 | void deinit_pollers(); |
| 85 | |
| 86 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 87 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 88 | * to create initialize part of them again. Returns 0 in case of failure, |
| 89 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 90 | * the the current poller is destroyed and the caller is responsible for trying |
| 91 | * another one by calling init_pollers() again. |
| 92 | */ |
| 93 | int fork_poller(); |
| 94 | |
| 95 | /* |
| 96 | * Lists the known pollers on <out>. |
| 97 | * Should be performed only before initialization. |
| 98 | */ |
| 99 | int list_pollers(FILE *out); |
| 100 | |
| 101 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 102 | * Runs the polling loop |
| 103 | */ |
| 104 | void run_poller(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 105 | |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 106 | /* Scan and process the cached events. This should be called right after |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 107 | * the poller. |
| 108 | */ |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 109 | void fd_process_cached_events(); |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 110 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 111 | void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off); |
| 112 | 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] | 113 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 114 | /* Mark fd <fd> as updated for polling and allocate an entry in the update list |
| 115 | * 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] | 116 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 117 | static inline void updt_fd_polling(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 118 | { |
Olivier Houchard | 6aab737 | 2018-08-17 13:37:59 +0200 | [diff] [blame] | 119 | if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) { |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 120 | |
| 121 | /* note: we don't have a test-and-set yet in hathreads */ |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 122 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 123 | if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid)) |
| 124 | return; |
| 125 | |
Olivier Houchard | a0fdce3 | 2019-06-12 14:31:08 +0200 | [diff] [blame^] | 126 | fd_updt[fd_nbupdt++] = fd; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 127 | } else { |
| 128 | unsigned long update_mask = fdtab[fd].update_mask; |
| 129 | do { |
| 130 | if (update_mask == fdtab[fd].thread_mask) |
| 131 | return; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 132 | } while (!_HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask, |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 133 | fdtab[fd].thread_mask)); |
| 134 | fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update)); |
| 135 | } |
| 136 | |
| 137 | } |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 138 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 139 | /* Called from the poller to acknoledge we read an entry from the global |
| 140 | * update list, to remove our bit from the update_mask, and remove it from |
| 141 | * the list if we were the last one. |
| 142 | */ |
| 143 | static inline void done_update_polling(int fd) |
| 144 | { |
| 145 | unsigned long update_mask; |
| 146 | |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 147 | update_mask = _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit); |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 148 | while ((update_mask & all_threads_mask)== 0) { |
| 149 | /* If we were the last one that had to update that entry, remove it from the list */ |
| 150 | fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update)); |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 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 | { |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +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; |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 283 | unsigned long locked; |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 284 | |
| 285 | old = fdtab[fd].state; |
| 286 | do { |
| 287 | if (!(old & FD_EV_ACTIVE_R)) |
| 288 | return; |
| 289 | new = old & ~FD_EV_ACTIVE_R; |
| 290 | new &= ~FD_EV_POLLED_R; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 291 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 292 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 293 | if ((old ^ new) & FD_EV_POLLED_R) |
| 294 | updt_fd_polling(fd); |
| 295 | |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 296 | locked = atleast2(fdtab[fd].thread_mask); |
| 297 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 298 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 299 | fd_update_cache(fd); /* need an update entry to change the state */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 300 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 301 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 302 | } |
| 303 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 304 | /* Disable processing send events on fd <fd> */ |
| 305 | static inline void fd_stop_send(int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 306 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 307 | unsigned char old, new; |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 308 | unsigned long locked; |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 309 | |
| 310 | old = fdtab[fd].state; |
| 311 | do { |
| 312 | if (!(old & FD_EV_ACTIVE_W)) |
| 313 | return; |
| 314 | new = old & ~FD_EV_ACTIVE_W; |
| 315 | new &= ~FD_EV_POLLED_W; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 316 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 317 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 318 | if ((old ^ new) & FD_EV_POLLED_W) |
| 319 | updt_fd_polling(fd); |
| 320 | |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 321 | locked = atleast2(fdtab[fd].thread_mask); |
| 322 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 323 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 324 | fd_update_cache(fd); /* need an update entry to change the state */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 325 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 326 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 329 | /* Disable processing of events on fd <fd> for both directions. */ |
| 330 | static inline void fd_stop_both(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 331 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 332 | unsigned char old, new; |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 333 | unsigned long locked; |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 334 | |
| 335 | old = fdtab[fd].state; |
| 336 | do { |
| 337 | if (!(old & FD_EV_ACTIVE_RW)) |
| 338 | return; |
| 339 | new = old & ~FD_EV_ACTIVE_RW; |
| 340 | new &= ~FD_EV_POLLED_RW; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 341 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 342 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 343 | if ((old ^ new) & FD_EV_POLLED_RW) |
| 344 | updt_fd_polling(fd); |
| 345 | |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 346 | locked = atleast2(fdtab[fd].thread_mask); |
| 347 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 348 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 349 | fd_update_cache(fd); /* need an update entry to change the state */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 350 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 351 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 354 | /* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */ |
| 355 | static inline void fd_cant_recv(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 356 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 357 | unsigned char old, new; |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 358 | unsigned long locked; |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 359 | |
| 360 | old = fdtab[fd].state; |
| 361 | do { |
| 362 | if (!(old & FD_EV_READY_R)) |
| 363 | return; |
| 364 | new = old & ~FD_EV_READY_R; |
| 365 | if (new & FD_EV_ACTIVE_R) |
| 366 | new |= FD_EV_POLLED_R; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 367 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 368 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 369 | if ((old ^ new) & FD_EV_POLLED_R) |
| 370 | updt_fd_polling(fd); |
| 371 | |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 372 | locked = atleast2(fdtab[fd].thread_mask); |
| 373 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 374 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 375 | fd_update_cache(fd); /* need an update entry to change the state */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 376 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 377 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 378 | } |
| 379 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 380 | /* Report that FD <fd> can receive anymore without polling. */ |
| 381 | static inline void fd_may_recv(const int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 382 | { |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 383 | unsigned long locked; |
| 384 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 385 | /* marking ready never changes polled status */ |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 386 | _HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_R); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 387 | |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 388 | locked = atleast2(fdtab[fd].thread_mask); |
| 389 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 390 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 391 | fd_update_cache(fd); /* need an update entry to change the state */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 392 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 393 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 396 | /* Disable readiness when polled. This is useful to interrupt reading when it |
| 397 | * is suspected that the end of data might have been reached (eg: short read). |
| 398 | * This can only be done using level-triggered pollers, so if any edge-triggered |
| 399 | * is ever implemented, a test will have to be added here. |
| 400 | */ |
| 401 | static inline void fd_done_recv(const int fd) |
| 402 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 403 | unsigned char old, new; |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 404 | unsigned long locked; |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 405 | |
| 406 | old = fdtab[fd].state; |
| 407 | do { |
| 408 | if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R)) |
| 409 | return; |
| 410 | new = old & ~FD_EV_READY_R; |
| 411 | if (new & FD_EV_ACTIVE_R) |
| 412 | new |= FD_EV_POLLED_R; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 413 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 414 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 415 | if ((old ^ new) & FD_EV_POLLED_R) |
| 416 | updt_fd_polling(fd); |
| 417 | |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 418 | locked = atleast2(fdtab[fd].thread_mask); |
| 419 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 420 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 421 | fd_update_cache(fd); /* need an update entry to change the state */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 422 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 423 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 426 | /* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */ |
| 427 | static inline void fd_cant_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 428 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 429 | unsigned char old, new; |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 430 | unsigned long locked; |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 431 | |
| 432 | old = fdtab[fd].state; |
| 433 | do { |
| 434 | if (!(old & FD_EV_READY_W)) |
| 435 | return; |
| 436 | new = old & ~FD_EV_READY_W; |
| 437 | if (new & FD_EV_ACTIVE_W) |
| 438 | new |= FD_EV_POLLED_W; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 439 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 440 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 441 | if ((old ^ new) & FD_EV_POLLED_W) |
| 442 | updt_fd_polling(fd); |
| 443 | |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 444 | locked = atleast2(fdtab[fd].thread_mask); |
| 445 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 446 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 447 | fd_update_cache(fd); /* need an update entry to change the state */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 448 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 449 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 452 | /* Report that FD <fd> can send anymore without polling (EAGAIN detected). */ |
| 453 | static inline void fd_may_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 454 | { |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 455 | unsigned long locked; |
| 456 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 457 | /* marking ready never changes polled status */ |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 458 | _HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 459 | |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 460 | locked = atleast2(fdtab[fd].thread_mask); |
| 461 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 462 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 463 | fd_update_cache(fd); /* need an update entry to change the state */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 464 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 465 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 466 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 467 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 468 | /* Prepare FD <fd> to try to receive */ |
| 469 | static inline void fd_want_recv(int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 470 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 471 | unsigned char old, new; |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 472 | unsigned long locked; |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 473 | |
| 474 | old = fdtab[fd].state; |
| 475 | do { |
| 476 | if (old & FD_EV_ACTIVE_R) |
| 477 | return; |
| 478 | new = old | FD_EV_ACTIVE_R; |
| 479 | if (!(new & FD_EV_READY_R)) |
| 480 | new |= FD_EV_POLLED_R; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 481 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 482 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 483 | if ((old ^ new) & FD_EV_POLLED_R) |
| 484 | updt_fd_polling(fd); |
| 485 | |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 486 | locked = atleast2(fdtab[fd].thread_mask); |
| 487 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 488 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 489 | fd_update_cache(fd); /* need an update entry to change the state */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 490 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 491 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 492 | } |
| 493 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 494 | /* Prepare FD <fd> to try to send */ |
| 495 | static inline void fd_want_send(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 496 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 497 | unsigned char old, new; |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 498 | unsigned long locked; |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 499 | |
| 500 | old = fdtab[fd].state; |
| 501 | do { |
| 502 | if (old & FD_EV_ACTIVE_W) |
| 503 | return; |
| 504 | new = old | FD_EV_ACTIVE_W; |
| 505 | if (!(new & FD_EV_READY_W)) |
| 506 | new |= FD_EV_POLLED_W; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 507 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 508 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 509 | if ((old ^ new) & FD_EV_POLLED_W) |
| 510 | updt_fd_polling(fd); |
| 511 | |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 512 | locked = atleast2(fdtab[fd].thread_mask); |
| 513 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 514 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 515 | fd_update_cache(fd); /* need an update entry to change the state */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 516 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 517 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 518 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 519 | |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 520 | /* Update events seen for FD <fd> and its state if needed. This should be called |
| 521 | * by the poller to set FD_POLL_* flags. */ |
| 522 | static inline void fd_update_events(int fd, int evts) |
| 523 | { |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 524 | unsigned long locked = atleast2(fdtab[fd].thread_mask); |
| 525 | |
| 526 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 527 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 528 | fdtab[fd].ev &= FD_POLL_STICKY; |
| 529 | fdtab[fd].ev |= evts; |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 530 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 531 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 532 | |
| 533 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
| 534 | fd_may_recv(fd); |
| 535 | |
| 536 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) |
| 537 | fd_may_send(fd); |
| 538 | } |
| 539 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 540 | /* Prepares <fd> for being polled */ |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 541 | 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] | 542 | { |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 543 | unsigned long locked = atleast2(thread_mask); |
| 544 | |
| 545 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 546 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 547 | fdtab[fd].owner = owner; |
| 548 | fdtab[fd].iocb = iocb; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 549 | fdtab[fd].ev = 0; |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 550 | fdtab[fd].linger_risk = 0; |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 551 | fdtab[fd].cloned = 0; |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 552 | fdtab[fd].thread_mask = thread_mask; |
Willy Tarreau | c9c8378 | 2018-01-17 18:44:46 +0100 | [diff] [blame] | 553 | /* note: do not reset polled_mask here as it indicates which poller |
| 554 | * still knows this FD from a possible previous round. |
| 555 | */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 556 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 557 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Olivier Houchard | 7c49d2e | 2019-04-16 18:37:05 +0200 | [diff] [blame] | 558 | _HA_ATOMIC_ADD(&ha_used_fds, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Willy Tarreau | f37ba94 | 2018-10-17 11:25:54 +0200 | [diff] [blame] | 561 | /* Computes the bounded poll() timeout based on the next expiration timer <next> |
| 562 | * by bounding it to MAX_DELAY_MS. <next> may equal TICK_ETERNITY. The pollers |
| 563 | * just needs to call this function right before polling to get their timeout |
| 564 | * value. Timeouts that are already expired (possibly due to a pending event) |
| 565 | * are accounted for in activity.poll_exp. |
| 566 | */ |
| 567 | static inline int compute_poll_timeout(int next) |
| 568 | { |
| 569 | int wait_time; |
| 570 | |
| 571 | if (!tick_isset(next)) |
| 572 | wait_time = MAX_DELAY_MS; |
| 573 | else if (tick_is_expired(next, now_ms)) { |
| 574 | activity[tid].poll_exp++; |
| 575 | wait_time = 0; |
| 576 | } |
| 577 | else { |
| 578 | wait_time = TICKS_TO_MS(tick_remain(now_ms, next)) + 1; |
| 579 | if (wait_time > MAX_DELAY_MS) |
| 580 | wait_time = MAX_DELAY_MS; |
| 581 | } |
| 582 | return wait_time; |
| 583 | } |
| 584 | |
Willy Tarreau | 322e6c7 | 2018-01-25 16:37:04 +0100 | [diff] [blame] | 585 | /* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */ |
| 586 | static inline void hap_fd_set(int fd, unsigned int *evts) |
| 587 | { |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 588 | _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] | 589 | } |
| 590 | |
| 591 | static inline void hap_fd_clr(int fd, unsigned int *evts) |
| 592 | { |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 593 | _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] | 594 | } |
| 595 | |
| 596 | static inline unsigned int hap_fd_isset(int fd, unsigned int *evts) |
| 597 | { |
| 598 | return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1))); |
| 599 | } |
| 600 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 601 | static inline void wake_thread(int tid) |
| 602 | { |
| 603 | char c = 'c'; |
| 604 | |
| 605 | shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1)); |
| 606 | } |
| 607 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 608 | |
| 609 | #endif /* _PROTO_FD_H */ |
| 610 | |
| 611 | /* |
| 612 | * Local variables: |
| 613 | * c-indent-level: 8 |
| 614 | * c-basic-offset: 8 |
| 615 | * End: |
| 616 | */ |