blob: b9811fe43e0cfd09540a42412631906b662f5281 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau49b046d2012-08-09 12:11:58 +02002 * include/proto/fd.h
3 * File descriptors states.
4 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +01005 * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu
Willy Tarreau49b046d2012-08-09 12:11:58 +02006 *
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 Tarreaubaaee002006-06-26 02:48:02 +020021
22#ifndef _PROTO_FD_H
23#define _PROTO_FD_H
24
Willy Tarreau2ff76222007-04-09 19:29:56 +020025#include <stdio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <sys/time.h>
27#include <sys/types.h>
28#include <unistd.h>
29
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020030#include <common/config.h>
Christopher Fauletd4604ad2017-05-29 10:40:41 +020031
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <types/fd.h>
33
Willy Tarreau7be79a42012-11-11 15:02:54 +010034/* public variables */
Christopher Fauletd4604ad2017-05-29 10:40:41 +020035
Olivier Houchard4815c8c2018-01-24 18:17:56 +010036extern volatile struct fdlist fd_cache;
37extern volatile struct fdlist fd_cache_local[MAX_THREADS];
38
Christopher Faulet69553fe2018-01-15 11:57:03 +010039extern unsigned long fd_cache_mask; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +020040
41extern THREAD_LOCAL int *fd_updt; // FD updates list
42extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
43
Willy Tarreau8b949692017-11-26 11:07:34 +010044__decl_hathreads(extern HA_RWLOCK_T __attribute__((aligned(64))) fdcache_lock); /* global lock to protect fd_cache array */
Willy Tarreau7be79a42012-11-11 15:02:54 +010045
Willy Tarreau173d9952018-01-26 21:48:23 +010046/* Deletes an FD from the fdsets.
Willy Tarreaubaaee002006-06-26 02:48:02 +020047 * The file descriptor is also closed.
48 */
49void fd_delete(int fd);
50
Willy Tarreau173d9952018-01-26 21:48:23 +010051/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +020052 * The file descriptor is kept open.
53 */
54void fd_remove(int fd);
55
Willy Tarreau4f60f162007-04-08 16:39:58 +020056/* disable the specified poller */
57void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau2a429502006-10-15 14:52:29 +020059/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020060 * Initialize the pollers till the best one is found.
61 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020062 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020063 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020064int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020065
Willy Tarreau4f60f162007-04-08 16:39:58 +020066/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020067 * Deinitialize the pollers.
68 */
69void deinit_pollers();
70
71/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020072 * 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 */
78int fork_poller();
79
80/*
81 * Lists the known pollers on <out>.
82 * Should be performed only before initialization.
83 */
84int list_pollers(FILE *out);
85
86/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020087 * Runs the polling loop
88 */
89void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +020090
Willy Tarreau033cd9d2014-01-25 19:24:15 +010091/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +010092 * the poller.
93 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +010094void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +010095
Willy Tarreau4cc67a22018-02-05 17:14:55 +010096void fd_add_to_fd_list(volatile struct fdlist *list, int fd);
97void fd_rm_from_fd_list(volatile struct fdlist *list, int fd);
98
Willy Tarreau5be2f352014-11-19 19:43:05 +010099/* Mark fd <fd> as updated for polling and allocate an entry in the update list
100 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +0100101 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100102static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100103{
Willy Tarreau4d841862018-01-17 22:57:54 +0100104 unsigned int oldupdt;
105
106 /* note: we don't have a test-and-set yet in hathreads */
107
108 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
Willy Tarreau7be79a42012-11-11 15:02:54 +0100109 return;
Willy Tarreau4d841862018-01-17 22:57:54 +0100110
111 oldupdt = HA_ATOMIC_ADD(&fd_nbupdt, 1) - 1;
112 fd_updt[oldupdt] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100113}
114
Willy Tarreau899d9572014-01-25 19:20:35 +0100115/* Allocates a cache entry for a file descriptor if it does not yet have one.
116 * This can be done at any time.
117 */
118static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100119{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100120 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
121 fd_add_to_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd);
122 else
123 fd_add_to_fd_list(&fd_cache, fd);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100124}
125
Willy Tarreau899d9572014-01-25 19:20:35 +0100126/* Removes entry used by fd <fd> from the FD cache and replaces it with the
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100127 * last one.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100128 * If the fd has no entry assigned, return immediately.
129 */
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100130static inline void fd_release_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100131{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100132 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
133 fd_rm_from_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd);
134 else
135 fd_rm_from_fd_list(&fd_cache, fd);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100136}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200137
Willy Tarreau5be2f352014-11-19 19:43:05 +0100138/* This function automatically enables/disables caching for an entry depending
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100139 * on its state. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100140 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100141static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100142{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100143 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
144 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
145 ((fdtab[fd].state & (FD_EV_READY_W | FD_EV_ACTIVE_W)) == (FD_EV_READY_W | FD_EV_ACTIVE_W))) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100146 fd_alloc_cache_entry(fd);
147 }
148 else {
149 fd_release_cache_entry(fd);
150 }
151}
152
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100153/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100154 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100155 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100156static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100157{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100158 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100159}
160
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100161/*
162 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100163 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100164static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100165{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100166 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100167}
168
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100169/*
170 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100171 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100172static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100173{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100174 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
175}
176
177/*
178 * returns true if the FD is polled for recv
179 */
180static inline int fd_recv_polled(const int fd)
181{
182 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
183}
184
185/*
186 * returns the FD's send state (FD_EV_*)
187 */
188static inline int fd_send_state(const int fd)
189{
190 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
191}
192
193/*
194 * returns true if the FD is active for send
195 */
196static inline int fd_send_active(const int fd)
197{
198 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100199}
200
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100201/*
202 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100203 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100204static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100205{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100206 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
207}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100208
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100209/*
210 * returns true if the FD is polled for send
211 */
212static inline int fd_send_polled(const int fd)
213{
214 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
215}
216
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200217/*
218 * returns true if the FD is active for recv or send
219 */
220static inline int fd_active(const int fd)
221{
222 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
223}
224
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100225/* Disable processing recv events on fd <fd> */
226static inline void fd_stop_recv(int fd)
227{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100228 unsigned char old, new;
229
230 old = fdtab[fd].state;
231 do {
232 if (!(old & FD_EV_ACTIVE_R))
233 return;
234 new = old & ~FD_EV_ACTIVE_R;
235 new &= ~FD_EV_POLLED_R;
236 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
237
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100238 if ((old ^ new) & FD_EV_POLLED_R)
239 updt_fd_polling(fd);
240
Willy Tarreau4d841862018-01-17 22:57:54 +0100241 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100242 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100243 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100244}
245
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100246/* Disable processing send events on fd <fd> */
247static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100248{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100249 unsigned char old, new;
250
251 old = fdtab[fd].state;
252 do {
253 if (!(old & FD_EV_ACTIVE_W))
254 return;
255 new = old & ~FD_EV_ACTIVE_W;
256 new &= ~FD_EV_POLLED_W;
257 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
258
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100259 if ((old ^ new) & FD_EV_POLLED_W)
260 updt_fd_polling(fd);
261
Willy Tarreau4d841862018-01-17 22:57:54 +0100262 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100263 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100264 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100265}
266
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100267/* Disable processing of events on fd <fd> for both directions. */
268static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200269{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100270 unsigned char old, new;
271
272 old = fdtab[fd].state;
273 do {
274 if (!(old & FD_EV_ACTIVE_RW))
275 return;
276 new = old & ~FD_EV_ACTIVE_RW;
277 new &= ~FD_EV_POLLED_RW;
278 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
279
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100280 if ((old ^ new) & FD_EV_POLLED_RW)
281 updt_fd_polling(fd);
282
Willy Tarreau4d841862018-01-17 22:57:54 +0100283 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100284 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100285 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200286}
287
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100288/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
289static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200290{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100291 unsigned char old, new;
292
293 old = fdtab[fd].state;
294 do {
295 if (!(old & FD_EV_READY_R))
296 return;
297 new = old & ~FD_EV_READY_R;
298 if (new & FD_EV_ACTIVE_R)
299 new |= FD_EV_POLLED_R;
300 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
301
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100302 if ((old ^ new) & FD_EV_POLLED_R)
303 updt_fd_polling(fd);
304
Willy Tarreau4d841862018-01-17 22:57:54 +0100305 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100306 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100307 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200308}
309
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100310/* Report that FD <fd> can receive anymore without polling. */
311static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200312{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100313 /* marking ready never changes polled status */
314 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_R);
315
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100316 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100317 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100318 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200319}
320
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100321/* Disable readiness when polled. This is useful to interrupt reading when it
322 * is suspected that the end of data might have been reached (eg: short read).
323 * This can only be done using level-triggered pollers, so if any edge-triggered
324 * is ever implemented, a test will have to be added here.
325 */
326static inline void fd_done_recv(const int fd)
327{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100328 unsigned char old, new;
329
330 old = fdtab[fd].state;
331 do {
332 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
333 return;
334 new = old & ~FD_EV_READY_R;
335 if (new & FD_EV_ACTIVE_R)
336 new |= FD_EV_POLLED_R;
337 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
338
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100339 if ((old ^ new) & FD_EV_POLLED_R)
340 updt_fd_polling(fd);
341
Willy Tarreau4d841862018-01-17 22:57:54 +0100342 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100343 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100344 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100345}
346
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100347/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
348static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200349{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100350 unsigned char old, new;
351
352 old = fdtab[fd].state;
353 do {
354 if (!(old & FD_EV_READY_W))
355 return;
356 new = old & ~FD_EV_READY_W;
357 if (new & FD_EV_ACTIVE_W)
358 new |= FD_EV_POLLED_W;
359 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
360
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100361 if ((old ^ new) & FD_EV_POLLED_W)
362 updt_fd_polling(fd);
363
Willy Tarreau4d841862018-01-17 22:57:54 +0100364 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100365 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100366 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200367}
368
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100369/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
370static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200371{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100372 /* marking ready never changes polled status */
373 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W);
374
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100375 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100376 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100377 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200378}
Willy Tarreau2a429502006-10-15 14:52:29 +0200379
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100380/* Prepare FD <fd> to try to receive */
381static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200382{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100383 unsigned char old, new;
384
385 old = fdtab[fd].state;
386 do {
387 if (old & FD_EV_ACTIVE_R)
388 return;
389 new = old | FD_EV_ACTIVE_R;
390 if (!(new & FD_EV_READY_R))
391 new |= FD_EV_POLLED_R;
392 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
393
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100394 if ((old ^ new) & FD_EV_POLLED_R)
395 updt_fd_polling(fd);
396
Willy Tarreau4d841862018-01-17 22:57:54 +0100397 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100398 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100399 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200400}
401
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100402/* Prepare FD <fd> to try to send */
403static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200404{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100405 unsigned char old, new;
406
407 old = fdtab[fd].state;
408 do {
409 if (old & FD_EV_ACTIVE_W)
410 return;
411 new = old | FD_EV_ACTIVE_W;
412 if (!(new & FD_EV_READY_W))
413 new |= FD_EV_POLLED_W;
414 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
415
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100416 if ((old ^ new) & FD_EV_POLLED_W)
417 updt_fd_polling(fd);
418
Willy Tarreau4d841862018-01-17 22:57:54 +0100419 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100420 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100421 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200422}
Willy Tarreau2a429502006-10-15 14:52:29 +0200423
Christopher Faulet21e92672017-08-30 10:30:04 +0200424/* Update events seen for FD <fd> and its state if needed. This should be called
425 * by the poller to set FD_POLL_* flags. */
426static inline void fd_update_events(int fd, int evts)
427{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100428 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200429 fdtab[fd].ev &= FD_POLL_STICKY;
430 fdtab[fd].ev |= evts;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100431 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200432
433 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
434 fd_may_recv(fd);
435
436 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
437 fd_may_send(fd);
438}
439
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100440/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100441static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100443 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100444 fdtab[fd].owner = owner;
445 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100446 fdtab[fd].ev = 0;
Willy Tarreauebc78d72018-01-20 23:53:50 +0100447 fdtab[fd].update_mask &= ~tid_bit;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100448 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200449 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100450 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100451 /* note: do not reset polled_mask here as it indicates which poller
452 * still knows this FD from a possible previous round.
453 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100454 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455}
456
Willy Tarreau322e6c72018-01-25 16:37:04 +0100457/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
458static inline void hap_fd_set(int fd, unsigned int *evts)
459{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100460 HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100461}
462
463static inline void hap_fd_clr(int fd, unsigned int *evts)
464{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100465 HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100466}
467
468static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
469{
470 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
471}
472
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473
474#endif /* _PROTO_FD_H */
475
476/*
477 * Local variables:
478 * c-indent-level: 8
479 * c-basic-offset: 8
480 * End:
481 */