blob: 7123377f994acd6c940635c3e222ade063babf31 [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
Olivier Houchard6b96f722018-04-25 16:58:25 +020039extern volatile struct fdlist update_list;
40
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020041extern unsigned long *polled_mask;
42
Christopher Faulet69553fe2018-01-15 11:57:03 +010043extern unsigned long fd_cache_mask; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +020044
45extern THREAD_LOCAL int *fd_updt; // FD updates list
46extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
47
Olivier Houchard79321b92018-07-26 17:55:11 +020048extern int poller_wr_pipe[MAX_THREADS];
49
Willy Tarreau8b949692017-11-26 11:07:34 +010050__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 +010051
Willy Tarreau173d9952018-01-26 21:48:23 +010052/* Deletes an FD from the fdsets.
Willy Tarreaubaaee002006-06-26 02:48:02 +020053 * The file descriptor is also closed.
54 */
55void fd_delete(int fd);
56
Willy Tarreau173d9952018-01-26 21:48:23 +010057/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +020058 * The file descriptor is kept open.
59 */
60void fd_remove(int fd);
61
Willy Tarreau4f60f162007-04-08 16:39:58 +020062/* disable the specified poller */
63void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020064
Olivier Houchard79321b92018-07-26 17:55:11 +020065void poller_pipe_io_handler(int fd);
66
Willy Tarreau2a429502006-10-15 14:52:29 +020067/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020068 * Initialize the pollers till the best one is found.
69 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020070 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020071 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020072int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020073
Willy Tarreau4f60f162007-04-08 16:39:58 +020074/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020075 * Deinitialize the pollers.
76 */
77void deinit_pollers();
78
79/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020080 * Some pollers may lose their connection after a fork(). It may be necessary
81 * to create initialize part of them again. Returns 0 in case of failure,
82 * otherwise 1. The fork() function may be NULL if unused. In case of error,
83 * the the current poller is destroyed and the caller is responsible for trying
84 * another one by calling init_pollers() again.
85 */
86int fork_poller();
87
88/*
89 * Lists the known pollers on <out>.
90 * Should be performed only before initialization.
91 */
92int list_pollers(FILE *out);
93
94/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020095 * Runs the polling loop
96 */
97void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +020098
Willy Tarreau033cd9d2014-01-25 19:24:15 +010099/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +0100100 * the poller.
101 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100102void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +0100103
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200104void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off);
105void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100106
Willy Tarreau5be2f352014-11-19 19:43:05 +0100107/* Mark fd <fd> as updated for polling and allocate an entry in the update list
108 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +0100109 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100110static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100111{
Olivier Houchard6aab7372018-08-17 13:37:59 +0200112 if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) {
Olivier Houchard6b96f722018-04-25 16:58:25 +0200113 unsigned int oldupdt;
114
115 /* note: we don't have a test-and-set yet in hathreads */
Willy Tarreau4d841862018-01-17 22:57:54 +0100116
Olivier Houchard6b96f722018-04-25 16:58:25 +0200117 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
118 return;
119
120 oldupdt = HA_ATOMIC_ADD(&fd_nbupdt, 1) - 1;
121 fd_updt[oldupdt] = fd;
122 } else {
123 unsigned long update_mask = fdtab[fd].update_mask;
124 do {
125 if (update_mask == fdtab[fd].thread_mask)
126 return;
127 } while (!HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask,
128 fdtab[fd].thread_mask));
129 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
130 }
131
132}
Willy Tarreau4d841862018-01-17 22:57:54 +0100133
Olivier Houchard6b96f722018-04-25 16:58:25 +0200134/* Called from the poller to acknoledge we read an entry from the global
135 * update list, to remove our bit from the update_mask, and remove it from
136 * the list if we were the last one.
137 */
138static inline void done_update_polling(int fd)
139{
140 unsigned long update_mask;
141
142 update_mask = HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
143 while ((update_mask & all_threads_mask)== 0) {
144 /* If we were the last one that had to update that entry, remove it from the list */
145 fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update));
146 if (update_list.first == fd)
147 abort();
148 update_mask = (volatile unsigned long)fdtab[fd].update_mask;
149 if ((update_mask & all_threads_mask) != 0) {
150 /* Maybe it's been re-updated in the meanwhile, and we
151 * wrongly removed it from the list, if so, re-add it
152 */
153 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
154 update_mask = (volatile unsigned long)(fdtab[fd].update_mask);
155 /* And then check again, just in case after all it
156 * should be removed, even if it's very unlikely, given
157 * the current thread wouldn't have been able to take
158 * care of it yet */
159 } else
160 break;
Willy Tarreau4d841862018-01-17 22:57:54 +0100161
Olivier Houchard6b96f722018-04-25 16:58:25 +0200162 }
Willy Tarreau7be79a42012-11-11 15:02:54 +0100163}
164
Willy Tarreau899d9572014-01-25 19:20:35 +0100165/* Allocates a cache entry for a file descriptor if it does not yet have one.
166 * This can be done at any time.
167 */
168static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100169{
Willy Tarreau26fb5d82018-03-20 19:06:52 +0100170 HA_ATOMIC_OR(&fd_cache_mask, fdtab[fd].thread_mask);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100171 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200172 fd_add_to_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd, offsetof(struct fdtab, cache));
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100173 else
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200174 fd_add_to_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache));
Willy Tarreau7be79a42012-11-11 15:02:54 +0100175}
176
Willy Tarreau899d9572014-01-25 19:20:35 +0100177/* Removes entry used by fd <fd> from the FD cache and replaces it with the
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100178 * last one.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100179 * If the fd has no entry assigned, return immediately.
180 */
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100181static inline void fd_release_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100182{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100183 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200184 fd_rm_from_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd, offsetof(struct fdtab, cache));
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100185 else
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200186 fd_rm_from_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache));
Willy Tarreau7be79a42012-11-11 15:02:54 +0100187}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200188
Willy Tarreau5be2f352014-11-19 19:43:05 +0100189/* This function automatically enables/disables caching for an entry depending
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100190 * on its state. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100191 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100192static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100193{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100194 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
195 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
196 ((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 +0100197 fd_alloc_cache_entry(fd);
198 }
199 else {
200 fd_release_cache_entry(fd);
201 }
202}
203
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100204/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100205 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100206 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100207static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100208{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100209 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100210}
211
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100212/*
213 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100214 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100215static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100216{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100217 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100218}
219
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100220/*
221 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100222 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100223static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100224{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100225 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
226}
227
228/*
229 * returns true if the FD is polled for recv
230 */
231static inline int fd_recv_polled(const int fd)
232{
233 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
234}
235
236/*
237 * returns the FD's send state (FD_EV_*)
238 */
239static inline int fd_send_state(const int fd)
240{
241 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
242}
243
244/*
245 * returns true if the FD is active for send
246 */
247static inline int fd_send_active(const int fd)
248{
249 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100250}
251
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100252/*
253 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100254 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100255static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100256{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100257 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
258}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100259
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100260/*
261 * returns true if the FD is polled for send
262 */
263static inline int fd_send_polled(const int fd)
264{
265 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
266}
267
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200268/*
269 * returns true if the FD is active for recv or send
270 */
271static inline int fd_active(const int fd)
272{
273 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
274}
275
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100276/* Disable processing recv events on fd <fd> */
277static inline void fd_stop_recv(int fd)
278{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100279 unsigned char old, new;
280
281 old = fdtab[fd].state;
282 do {
283 if (!(old & FD_EV_ACTIVE_R))
284 return;
285 new = old & ~FD_EV_ACTIVE_R;
286 new &= ~FD_EV_POLLED_R;
287 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
288
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100289 if ((old ^ new) & FD_EV_POLLED_R)
290 updt_fd_polling(fd);
291
Willy Tarreau87d54a92018-10-15 09:44:46 +0200292 if (atleast2(fdtab[fd].thread_mask))
293 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100294 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200295 if (atleast2(fdtab[fd].thread_mask))
296 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100297}
298
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100299/* Disable processing send events on fd <fd> */
300static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100301{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100302 unsigned char old, new;
303
304 old = fdtab[fd].state;
305 do {
306 if (!(old & FD_EV_ACTIVE_W))
307 return;
308 new = old & ~FD_EV_ACTIVE_W;
309 new &= ~FD_EV_POLLED_W;
310 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
311
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100312 if ((old ^ new) & FD_EV_POLLED_W)
313 updt_fd_polling(fd);
314
Willy Tarreau87d54a92018-10-15 09:44:46 +0200315 if (atleast2(fdtab[fd].thread_mask))
316 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 */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200318 if (atleast2(fdtab[fd].thread_mask))
319 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100320}
321
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100322/* Disable processing of events on fd <fd> for both directions. */
323static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200324{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100325 unsigned char old, new;
326
327 old = fdtab[fd].state;
328 do {
329 if (!(old & FD_EV_ACTIVE_RW))
330 return;
331 new = old & ~FD_EV_ACTIVE_RW;
332 new &= ~FD_EV_POLLED_RW;
333 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
334
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100335 if ((old ^ new) & FD_EV_POLLED_RW)
336 updt_fd_polling(fd);
337
Willy Tarreau87d54a92018-10-15 09:44:46 +0200338 if (atleast2(fdtab[fd].thread_mask))
339 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100340 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200341 if (atleast2(fdtab[fd].thread_mask))
342 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200343}
344
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100345/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
346static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200347{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100348 unsigned char old, new;
349
350 old = fdtab[fd].state;
351 do {
352 if (!(old & FD_EV_READY_R))
353 return;
354 new = old & ~FD_EV_READY_R;
355 if (new & FD_EV_ACTIVE_R)
356 new |= FD_EV_POLLED_R;
357 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
358
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100359 if ((old ^ new) & FD_EV_POLLED_R)
360 updt_fd_polling(fd);
361
Willy Tarreau87d54a92018-10-15 09:44:46 +0200362 if (atleast2(fdtab[fd].thread_mask))
363 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100364 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200365 if (atleast2(fdtab[fd].thread_mask))
366 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 receive anymore without polling. */
370static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +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_R);
374
Willy Tarreau87d54a92018-10-15 09:44:46 +0200375 if (atleast2(fdtab[fd].thread_mask))
376 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100377 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200378 if (atleast2(fdtab[fd].thread_mask))
379 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200380}
381
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100382/* Disable readiness when polled. This is useful to interrupt reading when it
383 * is suspected that the end of data might have been reached (eg: short read).
384 * This can only be done using level-triggered pollers, so if any edge-triggered
385 * is ever implemented, a test will have to be added here.
386 */
387static inline void fd_done_recv(const int fd)
388{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100389 unsigned char old, new;
390
391 old = fdtab[fd].state;
392 do {
393 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
394 return;
395 new = old & ~FD_EV_READY_R;
396 if (new & FD_EV_ACTIVE_R)
397 new |= FD_EV_POLLED_R;
398 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
399
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100400 if ((old ^ new) & FD_EV_POLLED_R)
401 updt_fd_polling(fd);
402
Willy Tarreau87d54a92018-10-15 09:44:46 +0200403 if (atleast2(fdtab[fd].thread_mask))
404 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100405 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200406 if (atleast2(fdtab[fd].thread_mask))
407 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100408}
409
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100410/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
411static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200412{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100413 unsigned char old, new;
414
415 old = fdtab[fd].state;
416 do {
417 if (!(old & FD_EV_READY_W))
418 return;
419 new = old & ~FD_EV_READY_W;
420 if (new & FD_EV_ACTIVE_W)
421 new |= FD_EV_POLLED_W;
422 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
423
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100424 if ((old ^ new) & FD_EV_POLLED_W)
425 updt_fd_polling(fd);
426
Willy Tarreau87d54a92018-10-15 09:44:46 +0200427 if (atleast2(fdtab[fd].thread_mask))
428 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100429 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200430 if (atleast2(fdtab[fd].thread_mask))
431 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200432}
433
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100434/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
435static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200436{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100437 /* marking ready never changes polled status */
438 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W);
439
Willy Tarreau87d54a92018-10-15 09:44:46 +0200440 if (atleast2(fdtab[fd].thread_mask))
441 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100442 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200443 if (atleast2(fdtab[fd].thread_mask))
444 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200445}
Willy Tarreau2a429502006-10-15 14:52:29 +0200446
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100447/* Prepare FD <fd> to try to receive */
448static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200449{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100450 unsigned char old, new;
451
452 old = fdtab[fd].state;
453 do {
454 if (old & FD_EV_ACTIVE_R)
455 return;
456 new = old | FD_EV_ACTIVE_R;
457 if (!(new & FD_EV_READY_R))
458 new |= FD_EV_POLLED_R;
459 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
460
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100461 if ((old ^ new) & FD_EV_POLLED_R)
462 updt_fd_polling(fd);
463
Willy Tarreau87d54a92018-10-15 09:44:46 +0200464 if (atleast2(fdtab[fd].thread_mask))
465 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100466 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200467 if (atleast2(fdtab[fd].thread_mask))
468 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200469}
470
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100471/* Prepare FD <fd> to try to send */
472static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200473{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100474 unsigned char old, new;
475
476 old = fdtab[fd].state;
477 do {
478 if (old & FD_EV_ACTIVE_W)
479 return;
480 new = old | FD_EV_ACTIVE_W;
481 if (!(new & FD_EV_READY_W))
482 new |= FD_EV_POLLED_W;
483 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
484
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100485 if ((old ^ new) & FD_EV_POLLED_W)
486 updt_fd_polling(fd);
487
Willy Tarreau87d54a92018-10-15 09:44:46 +0200488 if (atleast2(fdtab[fd].thread_mask))
489 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100490 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200491 if (atleast2(fdtab[fd].thread_mask))
492 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200493}
Willy Tarreau2a429502006-10-15 14:52:29 +0200494
Christopher Faulet21e92672017-08-30 10:30:04 +0200495/* Update events seen for FD <fd> and its state if needed. This should be called
496 * by the poller to set FD_POLL_* flags. */
497static inline void fd_update_events(int fd, int evts)
498{
Willy Tarreau87d54a92018-10-15 09:44:46 +0200499 if (atleast2(fdtab[fd].thread_mask))
500 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200501 fdtab[fd].ev &= FD_POLL_STICKY;
502 fdtab[fd].ev |= evts;
Willy Tarreau87d54a92018-10-15 09:44:46 +0200503 if (atleast2(fdtab[fd].thread_mask))
504 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200505
506 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
507 fd_may_recv(fd);
508
509 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
510 fd_may_send(fd);
511}
512
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100513/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100514static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515{
Willy Tarreau87d54a92018-10-15 09:44:46 +0200516 if (atleast2(thread_mask))
517 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100518 fdtab[fd].owner = owner;
519 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100520 fdtab[fd].ev = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100521 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200522 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100523 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100524 /* note: do not reset polled_mask here as it indicates which poller
525 * still knows this FD from a possible previous round.
526 */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200527 if (atleast2(thread_mask))
528 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529}
530
Willy Tarreau322e6c72018-01-25 16:37:04 +0100531/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
532static inline void hap_fd_set(int fd, unsigned int *evts)
533{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100534 HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100535}
536
537static inline void hap_fd_clr(int fd, unsigned int *evts)
538{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100539 HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100540}
541
542static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
543{
544 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
545}
546
Olivier Houchard79321b92018-07-26 17:55:11 +0200547static inline void wake_thread(int tid)
548{
549 char c = 'c';
550
551 shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1));
552}
553
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554
555#endif /* _PROTO_FD_H */
556
557/*
558 * Local variables:
559 * c-indent-level: 8
560 * c-basic-offset: 8
561 * End:
562 */