blob: 673eaae0f50a9c9144bacfd9aeb6037d58146620 [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 Houchardcb92f5c2018-04-26 14:23:07 +020040extern unsigned long *polled_mask;
41
Christopher Fauletd4604ad2017-05-29 10:40:41 +020042extern THREAD_LOCAL int *fd_updt; // FD updates list
43extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
44
Olivier Houchard79321b92018-07-26 17:55:11 +020045extern int poller_wr_pipe[MAX_THREADS];
46
Olivier Houchard7c49d2e2019-04-16 18:37:05 +020047extern volatile int ha_used_fds; // Number of FDs we're currently using
48
Willy Tarreau173d9952018-01-26 21:48:23 +010049/* Deletes an FD from the fdsets.
Willy Tarreaubaaee002006-06-26 02:48:02 +020050 * The file descriptor is also closed.
51 */
52void fd_delete(int fd);
53
Willy Tarreau173d9952018-01-26 21:48:23 +010054/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +020055 * The file descriptor is kept open.
56 */
57void fd_remove(int fd);
58
Willy Tarreau2d7f81b2019-02-21 22:19:17 +010059/* close all FDs starting from <start> */
60void my_closefrom(int start);
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
Olivier Houchard6a2cf872018-04-25 15:10:30 +020099void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off);
100void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100101
Willy Tarreau5be2f352014-11-19 19:43:05 +0100102/* Mark fd <fd> as updated for polling and allocate an entry in the update list
103 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +0100104 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100105static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100106{
Olivier Houchard6aab7372018-08-17 13:37:59 +0200107 if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) {
Olivier Houchard6b96f722018-04-25 16:58:25 +0200108
109 /* note: we don't have a test-and-set yet in hathreads */
Willy Tarreau4d841862018-01-17 22:57:54 +0100110
Olivier Houchard6b96f722018-04-25 16:58:25 +0200111 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
112 return;
113
Olivier Houcharda0fdce32019-06-12 14:31:08 +0200114 fd_updt[fd_nbupdt++] = fd;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200115 } else {
116 unsigned long update_mask = fdtab[fd].update_mask;
117 do {
118 if (update_mask == fdtab[fd].thread_mask)
119 return;
Olivier Houchardd3608792019-03-08 18:47:42 +0100120 } while (!_HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask,
Olivier Houchard6b96f722018-04-25 16:58:25 +0200121 fdtab[fd].thread_mask));
122 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
123 }
124
125}
Willy Tarreau4d841862018-01-17 22:57:54 +0100126
Olivier Houchard6b96f722018-04-25 16:58:25 +0200127/* Called from the poller to acknoledge we read an entry from the global
128 * update list, to remove our bit from the update_mask, and remove it from
129 * the list if we were the last one.
130 */
131static inline void done_update_polling(int fd)
132{
133 unsigned long update_mask;
134
Olivier Houchardd3608792019-03-08 18:47:42 +0100135 update_mask = _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +0200136 while ((update_mask & all_threads_mask)== 0) {
137 /* If we were the last one that had to update that entry, remove it from the list */
138 fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update));
Olivier Houchard6b96f722018-04-25 16:58:25 +0200139 update_mask = (volatile unsigned long)fdtab[fd].update_mask;
140 if ((update_mask & all_threads_mask) != 0) {
141 /* Maybe it's been re-updated in the meanwhile, and we
142 * wrongly removed it from the list, if so, re-add it
143 */
144 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
145 update_mask = (volatile unsigned long)(fdtab[fd].update_mask);
146 /* And then check again, just in case after all it
147 * should be removed, even if it's very unlikely, given
148 * the current thread wouldn't have been able to take
149 * care of it yet */
150 } else
151 break;
Willy Tarreau4d841862018-01-17 22:57:54 +0100152
Olivier Houchard6b96f722018-04-25 16:58:25 +0200153 }
Willy Tarreau7be79a42012-11-11 15:02:54 +0100154}
155
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100156/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100157 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100158 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100159static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100160{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100161 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100162}
163
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100164/*
165 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100166 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100167static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100168{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100169 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100170}
171
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100172/*
173 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100174 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100175static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100176{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100177 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
178}
179
180/*
181 * returns true if the FD is polled for recv
182 */
183static inline int fd_recv_polled(const int fd)
184{
185 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
186}
187
188/*
189 * returns the FD's send state (FD_EV_*)
190 */
191static inline int fd_send_state(const int fd)
192{
193 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
194}
195
196/*
197 * returns true if the FD is active for send
198 */
199static inline int fd_send_active(const int fd)
200{
201 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100202}
203
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100204/*
205 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100206 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100207static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100208{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100209 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
210}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100211
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100212/*
213 * returns true if the FD is polled for send
214 */
215static inline int fd_send_polled(const int fd)
216{
217 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
218}
219
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200220/*
221 * returns true if the FD is active for recv or send
222 */
223static inline int fd_active(const int fd)
224{
225 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
226}
227
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100228/* Disable processing recv events on fd <fd> */
229static inline void fd_stop_recv(int fd)
230{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100231 unsigned char old, new;
232
233 old = fdtab[fd].state;
234 do {
235 if (!(old & FD_EV_ACTIVE_R))
236 return;
237 new = old & ~FD_EV_ACTIVE_R;
238 new &= ~FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100239 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100240
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100241 if ((old ^ new) & FD_EV_POLLED_R)
242 updt_fd_polling(fd);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100243}
244
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100245/* Disable processing send events on fd <fd> */
246static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100247{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100248 unsigned char old, new;
249
250 old = fdtab[fd].state;
251 do {
252 if (!(old & FD_EV_ACTIVE_W))
253 return;
254 new = old & ~FD_EV_ACTIVE_W;
255 new &= ~FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100256 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100257
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100258 if ((old ^ new) & FD_EV_POLLED_W)
259 updt_fd_polling(fd);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100260}
261
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100262/* Disable processing of events on fd <fd> for both directions. */
263static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200264{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100265 unsigned char old, new;
266
267 old = fdtab[fd].state;
268 do {
269 if (!(old & FD_EV_ACTIVE_RW))
270 return;
271 new = old & ~FD_EV_ACTIVE_RW;
272 new &= ~FD_EV_POLLED_RW;
Olivier Houchardd3608792019-03-08 18:47:42 +0100273 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100274
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100275 if ((old ^ new) & FD_EV_POLLED_RW)
276 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200277}
278
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100279/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
280static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200281{
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_READY_R))
287 return;
288 new = old & ~FD_EV_READY_R;
289 if (new & FD_EV_ACTIVE_R)
290 new |= FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100291 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100292
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100293 if ((old ^ new) & FD_EV_POLLED_R)
294 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200295}
296
Willy Tarreau1dad3842019-07-08 23:09:03 +0200297/* Report that FD <fd> may receive again without polling. */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100298static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200299{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100300 /* marking ready never changes polled status */
Willy Tarreau1dad3842019-07-08 23:09:03 +0200301 if ((fdtab[fd].state & FD_EV_READY_R) ||
302 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_R_BIT))
303 return;
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200304}
305
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100306/* Disable readiness when polled. This is useful to interrupt reading when it
307 * is suspected that the end of data might have been reached (eg: short read).
308 * This can only be done using level-triggered pollers, so if any edge-triggered
309 * is ever implemented, a test will have to be added here.
310 */
311static inline void fd_done_recv(const int fd)
312{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100313 unsigned char old, new;
314
315 old = fdtab[fd].state;
316 do {
317 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
318 return;
319 new = old & ~FD_EV_READY_R;
320 if (new & FD_EV_ACTIVE_R)
321 new |= FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100322 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100323
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100324 if ((old ^ new) & FD_EV_POLLED_R)
325 updt_fd_polling(fd);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100326}
327
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100328/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
329static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200330{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100331 unsigned char old, new;
332
333 old = fdtab[fd].state;
334 do {
335 if (!(old & FD_EV_READY_W))
336 return;
337 new = old & ~FD_EV_READY_W;
338 if (new & FD_EV_ACTIVE_W)
339 new |= FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100340 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100341
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100342 if ((old ^ new) & FD_EV_POLLED_W)
343 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200344}
345
Willy Tarreau1dad3842019-07-08 23:09:03 +0200346/* Report that FD <fd> may send again without polling (EAGAIN not detected). */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100347static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200348{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100349 /* marking ready never changes polled status */
Willy Tarreau1dad3842019-07-08 23:09:03 +0200350 if ((fdtab[fd].state & FD_EV_READY_W) ||
351 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_W_BIT))
352 return;
Willy Tarreau49b046d2012-08-09 12:11:58 +0200353}
Willy Tarreau2a429502006-10-15 14:52:29 +0200354
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100355/* Prepare FD <fd> to try to receive */
356static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200357{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100358 unsigned char old, new;
359
360 old = fdtab[fd].state;
361 do {
362 if (old & FD_EV_ACTIVE_R)
363 return;
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200364 new = old | FD_EV_ACTIVE_R | FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100365 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100366
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100367 if ((old ^ new) & FD_EV_POLLED_R)
368 updt_fd_polling(fd);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200369}
370
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100371/* Prepare FD <fd> to try to send */
372static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200373{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100374 unsigned char old, new;
375
376 old = fdtab[fd].state;
377 do {
378 if (old & FD_EV_ACTIVE_W)
379 return;
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200380 new = old | FD_EV_ACTIVE_W | FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100381 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100382
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100383 if ((old ^ new) & FD_EV_POLLED_W)
384 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200385}
Willy Tarreau2a429502006-10-15 14:52:29 +0200386
Christopher Faulet21e92672017-08-30 10:30:04 +0200387/* Update events seen for FD <fd> and its state if needed. This should be called
388 * by the poller to set FD_POLL_* flags. */
389static inline void fd_update_events(int fd, int evts)
390{
Richard Russobc9d9842019-02-20 12:43:45 -0800391 unsigned long locked = atleast2(fdtab[fd].thread_mask);
Willy Tarreau1dad3842019-07-08 23:09:03 +0200392 unsigned char old, new;
Richard Russobc9d9842019-02-20 12:43:45 -0800393
Willy Tarreau1dad3842019-07-08 23:09:03 +0200394 old = fdtab[fd].ev;
395 new = (old & FD_POLL_STICKY) | evts;
396
397 if (unlikely(locked)) {
398 /* Locked FDs (those with more than 2 threads) are atomically updated */
399 while (unlikely(new != old && !_HA_ATOMIC_CAS(&fdtab[fd].ev, &old, new)))
400 new = (old & FD_POLL_STICKY) | evts;
401 } else {
402 if (new != old)
403 fdtab[fd].ev = new;
404 }
Christopher Faulet21e92672017-08-30 10:30:04 +0200405
406 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
407 fd_may_recv(fd);
408
409 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
410 fd_may_send(fd);
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200411
412 if (fdtab[fd].iocb)
413 fdtab[fd].iocb(fd);
Christopher Faulet21e92672017-08-30 10:30:04 +0200414}
415
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100416/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100417static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200418{
Richard Russobc9d9842019-02-20 12:43:45 -0800419 unsigned long locked = atleast2(thread_mask);
420
421 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200422 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100423 fdtab[fd].owner = owner;
424 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100425 fdtab[fd].ev = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100426 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200427 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100428 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100429 /* note: do not reset polled_mask here as it indicates which poller
430 * still knows this FD from a possible previous round.
431 */
Richard Russobc9d9842019-02-20 12:43:45 -0800432 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200433 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200434 _HA_ATOMIC_ADD(&ha_used_fds, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435}
436
Willy Tarreauf37ba942018-10-17 11:25:54 +0200437/* Computes the bounded poll() timeout based on the next expiration timer <next>
438 * by bounding it to MAX_DELAY_MS. <next> may equal TICK_ETERNITY. The pollers
439 * just needs to call this function right before polling to get their timeout
440 * value. Timeouts that are already expired (possibly due to a pending event)
441 * are accounted for in activity.poll_exp.
442 */
443static inline int compute_poll_timeout(int next)
444{
445 int wait_time;
446
447 if (!tick_isset(next))
448 wait_time = MAX_DELAY_MS;
449 else if (tick_is_expired(next, now_ms)) {
450 activity[tid].poll_exp++;
451 wait_time = 0;
452 }
453 else {
454 wait_time = TICKS_TO_MS(tick_remain(now_ms, next)) + 1;
455 if (wait_time > MAX_DELAY_MS)
456 wait_time = MAX_DELAY_MS;
457 }
458 return wait_time;
459}
460
Willy Tarreau322e6c72018-01-25 16:37:04 +0100461/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
462static inline void hap_fd_set(int fd, unsigned int *evts)
463{
Olivier Houchardd3608792019-03-08 18:47:42 +0100464 _HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100465}
466
467static inline void hap_fd_clr(int fd, unsigned int *evts)
468{
Olivier Houchardd3608792019-03-08 18:47:42 +0100469 _HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100470}
471
472static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
473{
474 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
475}
476
Olivier Houchard79321b92018-07-26 17:55:11 +0200477static inline void wake_thread(int tid)
478{
479 char c = 'c';
480
481 shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1));
482}
483
Willy Tarreaubaaee002006-06-26 02:48:02 +0200484
485#endif /* _PROTO_FD_H */
486
487/*
488 * Local variables:
489 * c-indent-level: 8
490 * c-basic-offset: 8
491 * End:
492 */