blob: e146976b2a2ef990580c500b793be6ae3acaaff9 [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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <types/fd.h>
Willy Tarreau609aad92018-11-22 08:31:09 +010034#include <proto/activity.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreau7be79a42012-11-11 15:02:54 +010036/* public variables */
Christopher Fauletd4604ad2017-05-29 10:40:41 +020037
Olivier Houchard6b96f722018-04-25 16:58:25 +020038extern volatile struct fdlist update_list;
39
Olivier Houchard53055052019-07-25 14:00:18 +000040
41extern struct polled_mask {
42 unsigned long poll_recv;
43 unsigned long poll_send;
44} *polled_mask;
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020045
Christopher Fauletd4604ad2017-05-29 10:40:41 +020046extern THREAD_LOCAL int *fd_updt; // FD updates list
47extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
48
Olivier Houchard79321b92018-07-26 17:55:11 +020049extern int poller_wr_pipe[MAX_THREADS];
50
Olivier Houchard7c49d2e2019-04-16 18:37:05 +020051extern volatile int ha_used_fds; // Number of FDs we're currently using
52
Willy Tarreau173d9952018-01-26 21:48:23 +010053/* Deletes an FD from the fdsets.
Willy Tarreaubaaee002006-06-26 02:48:02 +020054 * The file descriptor is also closed.
55 */
56void fd_delete(int fd);
57
Willy Tarreau173d9952018-01-26 21:48:23 +010058/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +020059 * The file descriptor is kept open.
60 */
61void fd_remove(int fd);
62
Willy Tarreau2d7f81b2019-02-21 22:19:17 +010063/* close all FDs starting from <start> */
64void my_closefrom(int start);
65
Willy Tarreau4f60f162007-04-08 16:39:58 +020066/* disable the specified poller */
67void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Olivier Houchard79321b92018-07-26 17:55:11 +020069void poller_pipe_io_handler(int fd);
70
Willy Tarreau2a429502006-10-15 14:52:29 +020071/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020072 * Initialize the pollers till the best one is found.
73 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020074 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020075 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020076int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020077
Willy Tarreau4f60f162007-04-08 16:39:58 +020078/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020079 * Deinitialize the pollers.
80 */
81void deinit_pollers();
82
83/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020084 * Some pollers may lose their connection after a fork(). It may be necessary
85 * to create initialize part of them again. Returns 0 in case of failure,
86 * otherwise 1. The fork() function may be NULL if unused. In case of error,
87 * the the current poller is destroyed and the caller is responsible for trying
88 * another one by calling init_pollers() again.
89 */
90int fork_poller();
91
92/*
93 * Lists the known pollers on <out>.
94 * Should be performed only before initialization.
95 */
96int list_pollers(FILE *out);
97
98/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020099 * Runs the polling loop
100 */
101void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +0200102
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200103void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off);
104void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100105
Willy Tarreau5be2f352014-11-19 19:43:05 +0100106/* Mark fd <fd> as updated for polling and allocate an entry in the update list
107 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +0100108 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100109static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100110{
Olivier Houchard6aab7372018-08-17 13:37:59 +0200111 if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) {
Olivier Houchard6b96f722018-04-25 16:58:25 +0200112
113 /* note: we don't have a test-and-set yet in hathreads */
Willy Tarreau4d841862018-01-17 22:57:54 +0100114
Olivier Houchard6b96f722018-04-25 16:58:25 +0200115 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
116 return;
117
Olivier Houcharda0fdce32019-06-12 14:31:08 +0200118 fd_updt[fd_nbupdt++] = fd;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200119 } else {
120 unsigned long update_mask = fdtab[fd].update_mask;
121 do {
122 if (update_mask == fdtab[fd].thread_mask)
123 return;
Olivier Houchardd3608792019-03-08 18:47:42 +0100124 } while (!_HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask,
Olivier Houchard6b96f722018-04-25 16:58:25 +0200125 fdtab[fd].thread_mask));
126 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
127 }
128
129}
Willy Tarreau4d841862018-01-17 22:57:54 +0100130
Olivier Houchard6b96f722018-04-25 16:58:25 +0200131/* Called from the poller to acknoledge we read an entry from the global
132 * update list, to remove our bit from the update_mask, and remove it from
133 * the list if we were the last one.
134 */
135static inline void done_update_polling(int fd)
136{
137 unsigned long update_mask;
138
Olivier Houchardd3608792019-03-08 18:47:42 +0100139 update_mask = _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +0200140 while ((update_mask & all_threads_mask)== 0) {
141 /* If we were the last one that had to update that entry, remove it from the list */
142 fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update));
Olivier Houchard6b96f722018-04-25 16:58:25 +0200143 update_mask = (volatile unsigned long)fdtab[fd].update_mask;
144 if ((update_mask & all_threads_mask) != 0) {
145 /* Maybe it's been re-updated in the meanwhile, and we
146 * wrongly removed it from the list, if so, re-add it
147 */
148 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
149 update_mask = (volatile unsigned long)(fdtab[fd].update_mask);
150 /* And then check again, just in case after all it
151 * should be removed, even if it's very unlikely, given
152 * the current thread wouldn't have been able to take
153 * care of it yet */
154 } else
155 break;
Willy Tarreau4d841862018-01-17 22:57:54 +0100156
Olivier Houchard6b96f722018-04-25 16:58:25 +0200157 }
Willy Tarreau7be79a42012-11-11 15:02:54 +0100158}
159
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100160/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100161 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100162 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100163static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100164{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100165 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100166}
167
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100168/*
169 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100170 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100171static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100172{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100173 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100174}
175
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100176/*
177 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100178 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100179static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100180{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100181 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
182}
183
184/*
185 * returns true if the FD is polled for recv
186 */
187static inline int fd_recv_polled(const int fd)
188{
189 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
190}
191
192/*
193 * returns the FD's send state (FD_EV_*)
194 */
195static inline int fd_send_state(const int fd)
196{
197 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
198}
199
200/*
201 * returns true if the FD is active for send
202 */
203static inline int fd_send_active(const int fd)
204{
205 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100206}
207
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100208/*
209 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100210 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100211static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100212{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100213 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
214}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100215
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100216/*
217 * returns true if the FD is polled for send
218 */
219static inline int fd_send_polled(const int fd)
220{
221 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
222}
223
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200224/*
225 * returns true if the FD is active for recv or send
226 */
227static inline int fd_active(const int fd)
228{
229 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
230}
231
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100232/* Disable processing recv events on fd <fd> */
233static inline void fd_stop_recv(int fd)
234{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100235 unsigned char old, new;
236
237 old = fdtab[fd].state;
238 do {
239 if (!(old & FD_EV_ACTIVE_R))
240 return;
241 new = old & ~FD_EV_ACTIVE_R;
242 new &= ~FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100243 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100244
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100245 if ((old ^ new) & FD_EV_POLLED_R)
246 updt_fd_polling(fd);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100247}
248
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100249/* Disable processing send events on fd <fd> */
250static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100251{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100252 unsigned char old, new;
253
254 old = fdtab[fd].state;
255 do {
256 if (!(old & FD_EV_ACTIVE_W))
257 return;
258 new = old & ~FD_EV_ACTIVE_W;
259 new &= ~FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100260 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100261
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100262 if ((old ^ new) & FD_EV_POLLED_W)
263 updt_fd_polling(fd);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100264}
265
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100266/* Disable processing of events on fd <fd> for both directions. */
267static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200268{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100269 unsigned char old, new;
270
271 old = fdtab[fd].state;
272 do {
273 if (!(old & FD_EV_ACTIVE_RW))
274 return;
275 new = old & ~FD_EV_ACTIVE_RW;
276 new &= ~FD_EV_POLLED_RW;
Olivier Houchardd3608792019-03-08 18:47:42 +0100277 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100278
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100279 if ((old ^ new) & FD_EV_POLLED_RW)
280 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200281}
282
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100283/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
284static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200285{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100286 unsigned char old, new;
287
288 old = fdtab[fd].state;
289 do {
290 if (!(old & FD_EV_READY_R))
291 return;
292 new = old & ~FD_EV_READY_R;
293 if (new & FD_EV_ACTIVE_R)
294 new |= FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100295 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100296
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100297 if ((old ^ new) & FD_EV_POLLED_R)
298 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200299}
300
Willy Tarreau1dad3842019-07-08 23:09:03 +0200301/* Report that FD <fd> may receive again without polling. */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100302static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200303{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100304 /* marking ready never changes polled status */
Willy Tarreau1dad3842019-07-08 23:09:03 +0200305 if ((fdtab[fd].state & FD_EV_READY_R) ||
306 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_R_BIT))
307 return;
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200308}
309
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100310/* Disable readiness when polled. This is useful to interrupt reading when it
311 * is suspected that the end of data might have been reached (eg: short read).
312 * This can only be done using level-triggered pollers, so if any edge-triggered
313 * is ever implemented, a test will have to be added here.
314 */
315static inline void fd_done_recv(const int fd)
316{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100317 unsigned char old, new;
318
319 old = fdtab[fd].state;
320 do {
321 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
322 return;
323 new = old & ~FD_EV_READY_R;
324 if (new & FD_EV_ACTIVE_R)
325 new |= FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100326 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100327
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100328 if ((old ^ new) & FD_EV_POLLED_R)
329 updt_fd_polling(fd);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100330}
331
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100332/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
333static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200334{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100335 unsigned char old, new;
336
337 old = fdtab[fd].state;
338 do {
339 if (!(old & FD_EV_READY_W))
340 return;
341 new = old & ~FD_EV_READY_W;
342 if (new & FD_EV_ACTIVE_W)
343 new |= FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100344 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100345
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100346 if ((old ^ new) & FD_EV_POLLED_W)
347 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200348}
349
Willy Tarreau1dad3842019-07-08 23:09:03 +0200350/* Report that FD <fd> may send again without polling (EAGAIN not detected). */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100351static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200352{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100353 /* marking ready never changes polled status */
Willy Tarreau1dad3842019-07-08 23:09:03 +0200354 if ((fdtab[fd].state & FD_EV_READY_W) ||
355 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_W_BIT))
356 return;
Willy Tarreau49b046d2012-08-09 12:11:58 +0200357}
Willy Tarreau2a429502006-10-15 14:52:29 +0200358
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100359/* Prepare FD <fd> to try to receive */
360static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200361{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100362 unsigned char old, new;
363
364 old = fdtab[fd].state;
365 do {
366 if (old & FD_EV_ACTIVE_R)
367 return;
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200368 new = old | FD_EV_ACTIVE_R | FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100369 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100370
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100371 if ((old ^ new) & FD_EV_POLLED_R)
372 updt_fd_polling(fd);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200373}
374
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100375/* Prepare FD <fd> to try to send */
376static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200377{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100378 unsigned char old, new;
379
380 old = fdtab[fd].state;
381 do {
382 if (old & FD_EV_ACTIVE_W)
383 return;
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200384 new = old | FD_EV_ACTIVE_W | FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100385 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100386
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100387 if ((old ^ new) & FD_EV_POLLED_W)
388 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200389}
Willy Tarreau2a429502006-10-15 14:52:29 +0200390
Christopher Faulet21e92672017-08-30 10:30:04 +0200391/* Update events seen for FD <fd> and its state if needed. This should be called
392 * by the poller to set FD_POLL_* flags. */
393static inline void fd_update_events(int fd, int evts)
394{
Richard Russobc9d9842019-02-20 12:43:45 -0800395 unsigned long locked = atleast2(fdtab[fd].thread_mask);
Willy Tarreau1dad3842019-07-08 23:09:03 +0200396 unsigned char old, new;
Richard Russobc9d9842019-02-20 12:43:45 -0800397
Willy Tarreau1dad3842019-07-08 23:09:03 +0200398 old = fdtab[fd].ev;
399 new = (old & FD_POLL_STICKY) | evts;
400
401 if (unlikely(locked)) {
402 /* Locked FDs (those with more than 2 threads) are atomically updated */
403 while (unlikely(new != old && !_HA_ATOMIC_CAS(&fdtab[fd].ev, &old, new)))
404 new = (old & FD_POLL_STICKY) | evts;
405 } else {
406 if (new != old)
407 fdtab[fd].ev = new;
408 }
Christopher Faulet21e92672017-08-30 10:30:04 +0200409
410 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
411 fd_may_recv(fd);
412
413 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
414 fd_may_send(fd);
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200415
416 if (fdtab[fd].iocb)
417 fdtab[fd].iocb(fd);
Willy Tarreauf5cab822019-08-16 16:06:14 +0200418
419 ti->flags &= ~TI_FL_STUCK; // this thread is still running
Christopher Faulet21e92672017-08-30 10:30:04 +0200420}
421
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100422/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100423static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200424{
Richard Russobc9d9842019-02-20 12:43:45 -0800425 unsigned long locked = atleast2(thread_mask);
426
427 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200428 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100429 fdtab[fd].owner = owner;
430 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100431 fdtab[fd].ev = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100432 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200433 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100434 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100435 /* note: do not reset polled_mask here as it indicates which poller
436 * still knows this FD from a possible previous round.
437 */
Richard Russobc9d9842019-02-20 12:43:45 -0800438 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200439 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200440 _HA_ATOMIC_ADD(&ha_used_fds, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441}
442
Willy Tarreauf37ba942018-10-17 11:25:54 +0200443/* Computes the bounded poll() timeout based on the next expiration timer <next>
444 * by bounding it to MAX_DELAY_MS. <next> may equal TICK_ETERNITY. The pollers
445 * just needs to call this function right before polling to get their timeout
446 * value. Timeouts that are already expired (possibly due to a pending event)
447 * are accounted for in activity.poll_exp.
448 */
449static inline int compute_poll_timeout(int next)
450{
451 int wait_time;
452
453 if (!tick_isset(next))
454 wait_time = MAX_DELAY_MS;
455 else if (tick_is_expired(next, now_ms)) {
456 activity[tid].poll_exp++;
457 wait_time = 0;
458 }
459 else {
460 wait_time = TICKS_TO_MS(tick_remain(now_ms, next)) + 1;
461 if (wait_time > MAX_DELAY_MS)
462 wait_time = MAX_DELAY_MS;
463 }
464 return wait_time;
465}
466
Willy Tarreau322e6c72018-01-25 16:37:04 +0100467/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
468static inline void hap_fd_set(int fd, unsigned int *evts)
469{
Olivier Houchardd3608792019-03-08 18:47:42 +0100470 _HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100471}
472
473static inline void hap_fd_clr(int fd, unsigned int *evts)
474{
Olivier Houchardd3608792019-03-08 18:47:42 +0100475 _HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100476}
477
478static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
479{
480 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
481}
482
Olivier Houchard79321b92018-07-26 17:55:11 +0200483static inline void wake_thread(int tid)
484{
485 char c = 'c';
486
487 shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1));
488}
489
Willy Tarreaubaaee002006-06-26 02:48:02 +0200490
491#endif /* _PROTO_FD_H */
492
493/*
494 * Local variables:
495 * c-indent-level: 8
496 * c-basic-offset: 8
497 * End:
498 */