blob: a3ec5e8544284781e8c621d856eb395cd85619c8 [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 Tarreau4d841862018-01-17 22:57:54 +0100292 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100293 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100294 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100295}
296
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100297/* Disable processing send events on fd <fd> */
298static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100299{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100300 unsigned char old, new;
301
302 old = fdtab[fd].state;
303 do {
304 if (!(old & FD_EV_ACTIVE_W))
305 return;
306 new = old & ~FD_EV_ACTIVE_W;
307 new &= ~FD_EV_POLLED_W;
308 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
309
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100310 if ((old ^ new) & FD_EV_POLLED_W)
311 updt_fd_polling(fd);
312
Willy Tarreau4d841862018-01-17 22:57:54 +0100313 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100314 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100315 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100316}
317
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100318/* Disable processing of events on fd <fd> for both directions. */
319static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200320{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100321 unsigned char old, new;
322
323 old = fdtab[fd].state;
324 do {
325 if (!(old & FD_EV_ACTIVE_RW))
326 return;
327 new = old & ~FD_EV_ACTIVE_RW;
328 new &= ~FD_EV_POLLED_RW;
329 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
330
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100331 if ((old ^ new) & FD_EV_POLLED_RW)
332 updt_fd_polling(fd);
333
Willy Tarreau4d841862018-01-17 22:57:54 +0100334 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100335 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100336 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200337}
338
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100339/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
340static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200341{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100342 unsigned char old, new;
343
344 old = fdtab[fd].state;
345 do {
346 if (!(old & FD_EV_READY_R))
347 return;
348 new = old & ~FD_EV_READY_R;
349 if (new & FD_EV_ACTIVE_R)
350 new |= FD_EV_POLLED_R;
351 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
352
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100353 if ((old ^ new) & FD_EV_POLLED_R)
354 updt_fd_polling(fd);
355
Willy Tarreau4d841862018-01-17 22:57:54 +0100356 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100357 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100358 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200359}
360
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100361/* Report that FD <fd> can receive anymore without polling. */
362static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200363{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100364 /* marking ready never changes polled status */
365 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_R);
366
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100367 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100368 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100369 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200370}
371
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100372/* Disable readiness when polled. This is useful to interrupt reading when it
373 * is suspected that the end of data might have been reached (eg: short read).
374 * This can only be done using level-triggered pollers, so if any edge-triggered
375 * is ever implemented, a test will have to be added here.
376 */
377static inline void fd_done_recv(const int fd)
378{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100379 unsigned char old, new;
380
381 old = fdtab[fd].state;
382 do {
383 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
384 return;
385 new = old & ~FD_EV_READY_R;
386 if (new & FD_EV_ACTIVE_R)
387 new |= FD_EV_POLLED_R;
388 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
389
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100390 if ((old ^ new) & FD_EV_POLLED_R)
391 updt_fd_polling(fd);
392
Willy Tarreau4d841862018-01-17 22:57:54 +0100393 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100394 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100395 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100396}
397
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100398/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
399static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200400{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100401 unsigned char old, new;
402
403 old = fdtab[fd].state;
404 do {
405 if (!(old & FD_EV_READY_W))
406 return;
407 new = old & ~FD_EV_READY_W;
408 if (new & FD_EV_ACTIVE_W)
409 new |= FD_EV_POLLED_W;
410 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
411
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100412 if ((old ^ new) & FD_EV_POLLED_W)
413 updt_fd_polling(fd);
414
Willy Tarreau4d841862018-01-17 22:57:54 +0100415 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100416 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100417 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200418}
419
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100420/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
421static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200422{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100423 /* marking ready never changes polled status */
424 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W);
425
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100426 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100427 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100428 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200429}
Willy Tarreau2a429502006-10-15 14:52:29 +0200430
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100431/* Prepare FD <fd> to try to receive */
432static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200433{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100434 unsigned char old, new;
435
436 old = fdtab[fd].state;
437 do {
438 if (old & FD_EV_ACTIVE_R)
439 return;
440 new = old | FD_EV_ACTIVE_R;
441 if (!(new & FD_EV_READY_R))
442 new |= FD_EV_POLLED_R;
443 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
444
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100445 if ((old ^ new) & FD_EV_POLLED_R)
446 updt_fd_polling(fd);
447
Willy Tarreau4d841862018-01-17 22:57:54 +0100448 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100449 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100450 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200451}
452
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100453/* Prepare FD <fd> to try to send */
454static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200455{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100456 unsigned char old, new;
457
458 old = fdtab[fd].state;
459 do {
460 if (old & FD_EV_ACTIVE_W)
461 return;
462 new = old | FD_EV_ACTIVE_W;
463 if (!(new & FD_EV_READY_W))
464 new |= FD_EV_POLLED_W;
465 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
466
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100467 if ((old ^ new) & FD_EV_POLLED_W)
468 updt_fd_polling(fd);
469
Willy Tarreau4d841862018-01-17 22:57:54 +0100470 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100471 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100472 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200473}
Willy Tarreau2a429502006-10-15 14:52:29 +0200474
Christopher Faulet21e92672017-08-30 10:30:04 +0200475/* Update events seen for FD <fd> and its state if needed. This should be called
476 * by the poller to set FD_POLL_* flags. */
477static inline void fd_update_events(int fd, int evts)
478{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100479 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200480 fdtab[fd].ev &= FD_POLL_STICKY;
481 fdtab[fd].ev |= evts;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100482 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200483
484 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
485 fd_may_recv(fd);
486
487 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
488 fd_may_send(fd);
489}
490
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100491/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100492static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100494 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100495 fdtab[fd].owner = owner;
496 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100497 fdtab[fd].ev = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100498 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200499 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100500 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100501 /* note: do not reset polled_mask here as it indicates which poller
502 * still knows this FD from a possible previous round.
503 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100504 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200505}
506
Willy Tarreau322e6c72018-01-25 16:37:04 +0100507/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
508static inline void hap_fd_set(int fd, unsigned int *evts)
509{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100510 HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100511}
512
513static inline void hap_fd_clr(int fd, unsigned int *evts)
514{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100515 HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100516}
517
518static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
519{
520 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
521}
522
Olivier Houchard79321b92018-07-26 17:55:11 +0200523static inline void wake_thread(int tid)
524{
525 char c = 'c';
526
527 shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1));
528}
529
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530
531#endif /* _PROTO_FD_H */
532
533/*
534 * Local variables:
535 * c-indent-level: 8
536 * c-basic-offset: 8
537 * End:
538 */