blob: 03e318284e9bbed071a70adeccbb622940868c09 [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>
Willy Tarreauf37ba942018-10-17 11:25:54 +020031#include <common/ticks.h>
32#include <common/time.h>
Christopher Fauletd4604ad2017-05-29 10:40:41 +020033
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <types/fd.h>
Willy Tarreauf37ba942018-10-17 11:25:54 +020035#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreau7be79a42012-11-11 15:02:54 +010037/* public variables */
Christopher Fauletd4604ad2017-05-29 10:40:41 +020038
Olivier Houchard4815c8c2018-01-24 18:17:56 +010039extern volatile struct fdlist fd_cache;
40extern volatile struct fdlist fd_cache_local[MAX_THREADS];
41
Olivier Houchard6b96f722018-04-25 16:58:25 +020042extern volatile struct fdlist update_list;
43
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020044extern unsigned long *polled_mask;
45
Christopher Faulet69553fe2018-01-15 11:57:03 +010046extern unsigned long fd_cache_mask; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +020047
48extern THREAD_LOCAL int *fd_updt; // FD updates list
49extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
50
Olivier Houchard79321b92018-07-26 17:55:11 +020051extern int poller_wr_pipe[MAX_THREADS];
52
Willy Tarreau8b949692017-11-26 11:07:34 +010053__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 +010054
Willy Tarreau173d9952018-01-26 21:48:23 +010055/* Deletes an FD from the fdsets.
Willy Tarreaubaaee002006-06-26 02:48:02 +020056 * The file descriptor is also closed.
57 */
58void fd_delete(int fd);
59
Willy Tarreau173d9952018-01-26 21:48:23 +010060/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +020061 * The file descriptor is kept open.
62 */
63void fd_remove(int fd);
64
Willy Tarreau4f60f162007-04-08 16:39:58 +020065/* disable the specified poller */
66void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020067
Olivier Houchard79321b92018-07-26 17:55:11 +020068void poller_pipe_io_handler(int fd);
69
Willy Tarreau2a429502006-10-15 14:52:29 +020070/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020071 * Initialize the pollers till the best one is found.
72 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020073 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020074 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020075int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020076
Willy Tarreau4f60f162007-04-08 16:39:58 +020077/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020078 * Deinitialize the pollers.
79 */
80void deinit_pollers();
81
82/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020083 * Some pollers may lose their connection after a fork(). It may be necessary
84 * to create initialize part of them again. Returns 0 in case of failure,
85 * otherwise 1. The fork() function may be NULL if unused. In case of error,
86 * the the current poller is destroyed and the caller is responsible for trying
87 * another one by calling init_pollers() again.
88 */
89int fork_poller();
90
91/*
92 * Lists the known pollers on <out>.
93 * Should be performed only before initialization.
94 */
95int list_pollers(FILE *out);
96
97/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020098 * Runs the polling loop
99 */
100void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +0200101
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100102/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +0100103 * the poller.
104 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100105void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +0100106
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200107void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off);
108void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100109
Willy Tarreau5be2f352014-11-19 19:43:05 +0100110/* Mark fd <fd> as updated for polling and allocate an entry in the update list
111 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +0100112 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100113static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100114{
Olivier Houchard6aab7372018-08-17 13:37:59 +0200115 if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) {
Olivier Houchard6b96f722018-04-25 16:58:25 +0200116 unsigned int oldupdt;
117
118 /* note: we don't have a test-and-set yet in hathreads */
Willy Tarreau4d841862018-01-17 22:57:54 +0100119
Olivier Houchard6b96f722018-04-25 16:58:25 +0200120 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
121 return;
122
123 oldupdt = HA_ATOMIC_ADD(&fd_nbupdt, 1) - 1;
124 fd_updt[oldupdt] = fd;
125 } else {
126 unsigned long update_mask = fdtab[fd].update_mask;
127 do {
128 if (update_mask == fdtab[fd].thread_mask)
129 return;
130 } while (!HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask,
131 fdtab[fd].thread_mask));
132 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
133 }
134
135}
Willy Tarreau4d841862018-01-17 22:57:54 +0100136
Olivier Houchard6b96f722018-04-25 16:58:25 +0200137/* Called from the poller to acknoledge we read an entry from the global
138 * update list, to remove our bit from the update_mask, and remove it from
139 * the list if we were the last one.
140 */
141static inline void done_update_polling(int fd)
142{
143 unsigned long update_mask;
144
145 update_mask = HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
146 while ((update_mask & all_threads_mask)== 0) {
147 /* If we were the last one that had to update that entry, remove it from the list */
148 fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update));
149 if (update_list.first == fd)
150 abort();
151 update_mask = (volatile unsigned long)fdtab[fd].update_mask;
152 if ((update_mask & all_threads_mask) != 0) {
153 /* Maybe it's been re-updated in the meanwhile, and we
154 * wrongly removed it from the list, if so, re-add it
155 */
156 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
157 update_mask = (volatile unsigned long)(fdtab[fd].update_mask);
158 /* And then check again, just in case after all it
159 * should be removed, even if it's very unlikely, given
160 * the current thread wouldn't have been able to take
161 * care of it yet */
162 } else
163 break;
Willy Tarreau4d841862018-01-17 22:57:54 +0100164
Olivier Houchard6b96f722018-04-25 16:58:25 +0200165 }
Willy Tarreau7be79a42012-11-11 15:02:54 +0100166}
167
Willy Tarreau899d9572014-01-25 19:20:35 +0100168/* Allocates a cache entry for a file descriptor if it does not yet have one.
169 * This can be done at any time.
170 */
171static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100172{
Willy Tarreau26fb5d82018-03-20 19:06:52 +0100173 HA_ATOMIC_OR(&fd_cache_mask, fdtab[fd].thread_mask);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100174 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200175 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 +0100176 else
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200177 fd_add_to_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache));
Willy Tarreau7be79a42012-11-11 15:02:54 +0100178}
179
Willy Tarreau899d9572014-01-25 19:20:35 +0100180/* Removes entry used by fd <fd> from the FD cache and replaces it with the
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100181 * last one.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100182 * If the fd has no entry assigned, return immediately.
183 */
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100184static inline void fd_release_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100185{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100186 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200187 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 +0100188 else
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200189 fd_rm_from_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache));
Willy Tarreau7be79a42012-11-11 15:02:54 +0100190}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200191
Willy Tarreau5be2f352014-11-19 19:43:05 +0100192/* This function automatically enables/disables caching for an entry depending
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100193 * on its state. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100194 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100195static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100196{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100197 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
198 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
199 ((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 +0100200 fd_alloc_cache_entry(fd);
201 }
202 else {
203 fd_release_cache_entry(fd);
204 }
205}
206
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100207/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100208 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100209 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100210static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100211{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100212 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100213}
214
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100215/*
216 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100217 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100218static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100219{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100220 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100221}
222
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100223/*
224 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100225 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100226static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100227{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100228 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
229}
230
231/*
232 * returns true if the FD is polled for recv
233 */
234static inline int fd_recv_polled(const int fd)
235{
236 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
237}
238
239/*
240 * returns the FD's send state (FD_EV_*)
241 */
242static inline int fd_send_state(const int fd)
243{
244 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
245}
246
247/*
248 * returns true if the FD is active for send
249 */
250static inline int fd_send_active(const int fd)
251{
252 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100253}
254
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100255/*
256 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100257 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100258static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100259{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100260 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
261}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100262
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100263/*
264 * returns true if the FD is polled for send
265 */
266static inline int fd_send_polled(const int fd)
267{
268 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
269}
270
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200271/*
272 * returns true if the FD is active for recv or send
273 */
274static inline int fd_active(const int fd)
275{
276 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
277}
278
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100279/* Disable processing recv events on fd <fd> */
280static inline void fd_stop_recv(int fd)
281{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100282 unsigned char old, new;
283
284 old = fdtab[fd].state;
285 do {
286 if (!(old & FD_EV_ACTIVE_R))
287 return;
288 new = old & ~FD_EV_ACTIVE_R;
289 new &= ~FD_EV_POLLED_R;
290 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
291
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100292 if ((old ^ new) & FD_EV_POLLED_R)
293 updt_fd_polling(fd);
294
Willy Tarreau87d54a92018-10-15 09:44:46 +0200295 if (atleast2(fdtab[fd].thread_mask))
296 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100297 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200298 if (atleast2(fdtab[fd].thread_mask))
299 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100300}
301
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100302/* Disable processing send events on fd <fd> */
303static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100304{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100305 unsigned char old, new;
306
307 old = fdtab[fd].state;
308 do {
309 if (!(old & FD_EV_ACTIVE_W))
310 return;
311 new = old & ~FD_EV_ACTIVE_W;
312 new &= ~FD_EV_POLLED_W;
313 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
314
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100315 if ((old ^ new) & FD_EV_POLLED_W)
316 updt_fd_polling(fd);
317
Willy Tarreau87d54a92018-10-15 09:44:46 +0200318 if (atleast2(fdtab[fd].thread_mask))
319 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100320 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200321 if (atleast2(fdtab[fd].thread_mask))
322 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100323}
324
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100325/* Disable processing of events on fd <fd> for both directions. */
326static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200327{
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_ACTIVE_RW))
333 return;
334 new = old & ~FD_EV_ACTIVE_RW;
335 new &= ~FD_EV_POLLED_RW;
336 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
337
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100338 if ((old ^ new) & FD_EV_POLLED_RW)
339 updt_fd_polling(fd);
340
Willy Tarreau87d54a92018-10-15 09:44:46 +0200341 if (atleast2(fdtab[fd].thread_mask))
342 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 */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200344 if (atleast2(fdtab[fd].thread_mask))
345 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200346}
347
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100348/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
349static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200350{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100351 unsigned char old, new;
352
353 old = fdtab[fd].state;
354 do {
355 if (!(old & FD_EV_READY_R))
356 return;
357 new = old & ~FD_EV_READY_R;
358 if (new & FD_EV_ACTIVE_R)
359 new |= FD_EV_POLLED_R;
360 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
361
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100362 if ((old ^ new) & FD_EV_POLLED_R)
363 updt_fd_polling(fd);
364
Willy Tarreau87d54a92018-10-15 09:44:46 +0200365 if (atleast2(fdtab[fd].thread_mask))
366 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100367 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200368 if (atleast2(fdtab[fd].thread_mask))
369 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200370}
371
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100372/* Report that FD <fd> can receive anymore without polling. */
373static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200374{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100375 /* marking ready never changes polled status */
376 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_R);
377
Willy Tarreau87d54a92018-10-15 09:44:46 +0200378 if (atleast2(fdtab[fd].thread_mask))
379 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100380 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200381 if (atleast2(fdtab[fd].thread_mask))
382 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200383}
384
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100385/* Disable readiness when polled. This is useful to interrupt reading when it
386 * is suspected that the end of data might have been reached (eg: short read).
387 * This can only be done using level-triggered pollers, so if any edge-triggered
388 * is ever implemented, a test will have to be added here.
389 */
390static inline void fd_done_recv(const int fd)
391{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100392 unsigned char old, new;
393
394 old = fdtab[fd].state;
395 do {
396 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
397 return;
398 new = old & ~FD_EV_READY_R;
399 if (new & FD_EV_ACTIVE_R)
400 new |= FD_EV_POLLED_R;
401 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
402
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100403 if ((old ^ new) & FD_EV_POLLED_R)
404 updt_fd_polling(fd);
405
Willy Tarreau87d54a92018-10-15 09:44:46 +0200406 if (atleast2(fdtab[fd].thread_mask))
407 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100408 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200409 if (atleast2(fdtab[fd].thread_mask))
410 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100411}
412
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100413/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
414static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200415{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100416 unsigned char old, new;
417
418 old = fdtab[fd].state;
419 do {
420 if (!(old & FD_EV_READY_W))
421 return;
422 new = old & ~FD_EV_READY_W;
423 if (new & FD_EV_ACTIVE_W)
424 new |= FD_EV_POLLED_W;
425 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
426
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100427 if ((old ^ new) & FD_EV_POLLED_W)
428 updt_fd_polling(fd);
429
Willy Tarreau87d54a92018-10-15 09:44:46 +0200430 if (atleast2(fdtab[fd].thread_mask))
431 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100432 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200433 if (atleast2(fdtab[fd].thread_mask))
434 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200435}
436
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100437/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
438static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200439{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100440 /* marking ready never changes polled status */
441 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W);
442
Willy Tarreau87d54a92018-10-15 09:44:46 +0200443 if (atleast2(fdtab[fd].thread_mask))
444 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100445 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200446 if (atleast2(fdtab[fd].thread_mask))
447 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200448}
Willy Tarreau2a429502006-10-15 14:52:29 +0200449
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100450/* Prepare FD <fd> to try to receive */
451static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200452{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100453 unsigned char old, new;
454
455 old = fdtab[fd].state;
456 do {
457 if (old & FD_EV_ACTIVE_R)
458 return;
459 new = old | FD_EV_ACTIVE_R;
460 if (!(new & FD_EV_READY_R))
461 new |= FD_EV_POLLED_R;
462 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
463
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100464 if ((old ^ new) & FD_EV_POLLED_R)
465 updt_fd_polling(fd);
466
Willy Tarreau87d54a92018-10-15 09:44:46 +0200467 if (atleast2(fdtab[fd].thread_mask))
468 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100469 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200470 if (atleast2(fdtab[fd].thread_mask))
471 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200472}
473
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100474/* Prepare FD <fd> to try to send */
475static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200476{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100477 unsigned char old, new;
478
479 old = fdtab[fd].state;
480 do {
481 if (old & FD_EV_ACTIVE_W)
482 return;
483 new = old | FD_EV_ACTIVE_W;
484 if (!(new & FD_EV_READY_W))
485 new |= FD_EV_POLLED_W;
486 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
487
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100488 if ((old ^ new) & FD_EV_POLLED_W)
489 updt_fd_polling(fd);
490
Willy Tarreau87d54a92018-10-15 09:44:46 +0200491 if (atleast2(fdtab[fd].thread_mask))
492 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100493 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200494 if (atleast2(fdtab[fd].thread_mask))
495 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200496}
Willy Tarreau2a429502006-10-15 14:52:29 +0200497
Christopher Faulet21e92672017-08-30 10:30:04 +0200498/* Update events seen for FD <fd> and its state if needed. This should be called
499 * by the poller to set FD_POLL_* flags. */
500static inline void fd_update_events(int fd, int evts)
501{
Willy Tarreau87d54a92018-10-15 09:44:46 +0200502 if (atleast2(fdtab[fd].thread_mask))
503 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200504 fdtab[fd].ev &= FD_POLL_STICKY;
505 fdtab[fd].ev |= evts;
Willy Tarreau87d54a92018-10-15 09:44:46 +0200506 if (atleast2(fdtab[fd].thread_mask))
507 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200508
509 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
510 fd_may_recv(fd);
511
512 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
513 fd_may_send(fd);
514}
515
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100516/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100517static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200518{
Willy Tarreau87d54a92018-10-15 09:44:46 +0200519 if (atleast2(thread_mask))
520 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100521 fdtab[fd].owner = owner;
522 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100523 fdtab[fd].ev = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100524 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200525 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100526 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100527 /* note: do not reset polled_mask here as it indicates which poller
528 * still knows this FD from a possible previous round.
529 */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200530 if (atleast2(thread_mask))
531 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200532}
533
Willy Tarreauf37ba942018-10-17 11:25:54 +0200534/* Computes the bounded poll() timeout based on the next expiration timer <next>
535 * by bounding it to MAX_DELAY_MS. <next> may equal TICK_ETERNITY. The pollers
536 * just needs to call this function right before polling to get their timeout
537 * value. Timeouts that are already expired (possibly due to a pending event)
538 * are accounted for in activity.poll_exp.
539 */
540static inline int compute_poll_timeout(int next)
541{
542 int wait_time;
543
544 if (!tick_isset(next))
545 wait_time = MAX_DELAY_MS;
546 else if (tick_is_expired(next, now_ms)) {
547 activity[tid].poll_exp++;
548 wait_time = 0;
549 }
550 else {
551 wait_time = TICKS_TO_MS(tick_remain(now_ms, next)) + 1;
552 if (wait_time > MAX_DELAY_MS)
553 wait_time = MAX_DELAY_MS;
554 }
555 return wait_time;
556}
557
Willy Tarreau322e6c72018-01-25 16:37:04 +0100558/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
559static inline void hap_fd_set(int fd, unsigned int *evts)
560{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100561 HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100562}
563
564static inline void hap_fd_clr(int fd, unsigned int *evts)
565{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100566 HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100567}
568
569static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
570{
571 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
572}
573
Olivier Houchard79321b92018-07-26 17:55:11 +0200574static inline void wake_thread(int tid)
575{
576 char c = 'c';
577
578 shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1));
579}
580
Willy Tarreaubaaee002006-06-26 02:48:02 +0200581
582#endif /* _PROTO_FD_H */
583
584/*
585 * Local variables:
586 * c-indent-level: 8
587 * c-basic-offset: 8
588 * End:
589 */