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