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