blob: 3c778abc42852dddb7c04d9b53283d25ac25cd5f [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 Tarreau931d8b72019-08-27 11:08:17 +020063ssize_t fd_write_frag_line(int fd, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg, int nl);
64
Willy Tarreau2d7f81b2019-02-21 22:19:17 +010065/* close all FDs starting from <start> */
66void my_closefrom(int start);
67
Willy Tarreau4f60f162007-04-08 16:39:58 +020068/* disable the specified poller */
69void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020070
Olivier Houchard79321b92018-07-26 17:55:11 +020071void poller_pipe_io_handler(int fd);
72
Willy Tarreau2a429502006-10-15 14:52:29 +020073/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020074 * Initialize the pollers till the best one is found.
75 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020076 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020077 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020078int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020079
Willy Tarreau4f60f162007-04-08 16:39:58 +020080/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020081 * Deinitialize the pollers.
82 */
83void deinit_pollers();
84
85/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020086 * Some pollers may lose their connection after a fork(). It may be necessary
87 * to create initialize part of them again. Returns 0 in case of failure,
88 * otherwise 1. The fork() function may be NULL if unused. In case of error,
89 * the the current poller is destroyed and the caller is responsible for trying
90 * another one by calling init_pollers() again.
91 */
92int fork_poller();
93
94/*
95 * Lists the known pollers on <out>.
96 * Should be performed only before initialization.
97 */
98int list_pollers(FILE *out);
99
100/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200101 * Runs the polling loop
102 */
103void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +0200104
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200105void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off);
106void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off);
Willy Tarreaudbe30602019-09-04 13:25:41 +0200107void updt_fd_polling(const int fd);
Willy Tarreau4d841862018-01-17 22:57:54 +0100108
Olivier Houchard6b96f722018-04-25 16:58:25 +0200109/* Called from the poller to acknoledge we read an entry from the global
110 * update list, to remove our bit from the update_mask, and remove it from
111 * the list if we were the last one.
112 */
113static inline void done_update_polling(int fd)
114{
115 unsigned long update_mask;
116
Olivier Houchardd3608792019-03-08 18:47:42 +0100117 update_mask = _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +0200118 while ((update_mask & all_threads_mask)== 0) {
119 /* If we were the last one that had to update that entry, remove it from the list */
120 fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update));
Olivier Houchard6b96f722018-04-25 16:58:25 +0200121 update_mask = (volatile unsigned long)fdtab[fd].update_mask;
122 if ((update_mask & all_threads_mask) != 0) {
123 /* Maybe it's been re-updated in the meanwhile, and we
124 * wrongly removed it from the list, if so, re-add it
125 */
126 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
127 update_mask = (volatile unsigned long)(fdtab[fd].update_mask);
128 /* And then check again, just in case after all it
129 * should be removed, even if it's very unlikely, given
130 * the current thread wouldn't have been able to take
131 * care of it yet */
132 } else
133 break;
Willy Tarreau4d841862018-01-17 22:57:54 +0100134
Olivier Houchard6b96f722018-04-25 16:58:25 +0200135 }
Willy Tarreau7be79a42012-11-11 15:02:54 +0100136}
137
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100138/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100139 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100140 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100141static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100142{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100143 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100144}
145
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100146/*
147 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100148 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100149static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100150{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100151 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
152}
153
154/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100155 * returns true if the FD is active for send
156 */
157static inline int fd_send_active(const int fd)
158{
159 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100160}
161
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100162/*
163 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100164 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100165static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100166{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100167 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
168}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100169
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100170/*
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200171 * returns true if the FD is active for recv or send
172 */
173static inline int fd_active(const int fd)
174{
175 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
176}
177
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100178/* Disable processing recv events on fd <fd> */
179static inline void fd_stop_recv(int fd)
180{
Willy Tarreauf8ecc7f2019-09-04 13:22:50 +0200181 if (!(fdtab[fd].state & FD_EV_ACTIVE_R) ||
182 !HA_ATOMIC_BTR(&fdtab[fd].state, FD_EV_ACTIVE_R_BIT))
183 return;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100184}
185
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100186/* Disable processing send events on fd <fd> */
187static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100188{
Willy Tarreauf8ecc7f2019-09-04 13:22:50 +0200189 if (!(fdtab[fd].state & FD_EV_ACTIVE_W) ||
190 !HA_ATOMIC_BTR(&fdtab[fd].state, FD_EV_ACTIVE_W_BIT))
191 return;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100192}
193
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100194/* Disable processing of events on fd <fd> for both directions. */
195static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200196{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100197 unsigned char old, new;
198
199 old = fdtab[fd].state;
200 do {
201 if (!(old & FD_EV_ACTIVE_RW))
202 return;
203 new = old & ~FD_EV_ACTIVE_RW;
Olivier Houchardd3608792019-03-08 18:47:42 +0100204 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau49b046d2012-08-09 12:11:58 +0200205}
206
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100207/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
208static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200209{
Willy Tarreauf8ecc7f2019-09-04 13:22:50 +0200210 /* marking ready never changes polled status */
211 if (!(fdtab[fd].state & FD_EV_READY_R) ||
212 !HA_ATOMIC_BTR(&fdtab[fd].state, FD_EV_READY_R_BIT))
213 return;
Willy Tarreau49b046d2012-08-09 12:11:58 +0200214}
215
Willy Tarreau1dad3842019-07-08 23:09:03 +0200216/* Report that FD <fd> may receive again without polling. */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100217static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200218{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100219 /* marking ready never changes polled status */
Willy Tarreau1dad3842019-07-08 23:09:03 +0200220 if ((fdtab[fd].state & FD_EV_READY_R) ||
221 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_R_BIT))
222 return;
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200223}
224
Willy Tarreau8f2825f2019-09-05 16:39:21 +0200225/* Report that FD <fd> may receive again without polling but only if its not
226 * active yet. This is in order to speculatively try to enable I/Os when it's
227 * highly likely that these will succeed, but without interfering with polling.
228 */
229static inline void fd_cond_recv(const int fd)
230{
231 if ((fdtab[fd].state & (FD_EV_ACTIVE_R|FD_EV_READY_R)) == 0)
232 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_R_BIT);
233}
234
235/* Report that FD <fd> may send again without polling but only if its not
236 * active yet. This is in order to speculatively try to enable I/Os when it's
237 * highly likely that these will succeed, but without interfering with polling.
238 */
239static inline void fd_cond_send(const int fd)
240{
241 if ((fdtab[fd].state & (FD_EV_ACTIVE_W|FD_EV_READY_W)) == 0)
242 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_W_BIT);
243}
244
Willy Tarreau4ac9d062019-09-05 16:30:39 +0200245/* Report that FD <fd> may receive and send without polling. Used at FD
246 * initialization.
247 */
248static inline void fd_may_both(const int fd)
249{
250 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_RW);
251}
252
Willy Tarreauf8ecc7f2019-09-04 13:22:50 +0200253/* Disable readiness when active. This is useful to interrupt reading when it
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100254 * is suspected that the end of data might have been reached (eg: short read).
255 * This can only be done using level-triggered pollers, so if any edge-triggered
256 * is ever implemented, a test will have to be added here.
257 */
258static inline void fd_done_recv(const int fd)
259{
Willy Tarreauf8ecc7f2019-09-04 13:22:50 +0200260 /* removing ready never changes polled status */
261 if ((fdtab[fd].state & (FD_EV_ACTIVE_R|FD_EV_READY_R)) != (FD_EV_ACTIVE_R|FD_EV_READY_R) ||
262 !HA_ATOMIC_BTR(&fdtab[fd].state, FD_EV_READY_R_BIT))
263 return;
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100264}
265
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100266/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
267static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200268{
Willy Tarreauf8ecc7f2019-09-04 13:22:50 +0200269 /* removing ready never changes polled status */
270 if (!(fdtab[fd].state & FD_EV_READY_W) ||
271 !HA_ATOMIC_BTR(&fdtab[fd].state, FD_EV_READY_W_BIT))
272 return;
Willy Tarreau49b046d2012-08-09 12:11:58 +0200273}
274
Willy Tarreau1dad3842019-07-08 23:09:03 +0200275/* Report that FD <fd> may send again without polling (EAGAIN not detected). */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100276static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200277{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100278 /* marking ready never changes polled status */
Willy Tarreau1dad3842019-07-08 23:09:03 +0200279 if ((fdtab[fd].state & FD_EV_READY_W) ||
280 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_W_BIT))
281 return;
Willy Tarreau49b046d2012-08-09 12:11:58 +0200282}
Willy Tarreau2a429502006-10-15 14:52:29 +0200283
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100284/* Prepare FD <fd> to try to receive */
285static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200286{
Willy Tarreauf8ecc7f2019-09-04 13:22:50 +0200287 if ((fdtab[fd].state & FD_EV_ACTIVE_R) ||
288 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_ACTIVE_R_BIT))
289 return;
Willy Tarreau5bee3e22019-09-04 09:52:57 +0200290 updt_fd_polling(fd);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200291}
292
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100293/* Prepare FD <fd> to try to send */
294static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200295{
Willy Tarreauf8ecc7f2019-09-04 13:22:50 +0200296 if ((fdtab[fd].state & FD_EV_ACTIVE_W) ||
297 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_ACTIVE_W_BIT))
298 return;
Willy Tarreau5bee3e22019-09-04 09:52:57 +0200299 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200300}
Willy Tarreau2a429502006-10-15 14:52:29 +0200301
Willy Tarreau6b308982019-09-06 19:05:50 +0200302/* Update events seen for FD <fd> and its state if needed. This should be
303 * called by the poller, passing FD_EV_*_{R,W,RW} in <evts>. FD_EV_ERR_*
304 * doesn't need to also pass FD_EV_SHUT_*, it's implied. ERR and SHUT are
305 * allowed to be reported regardless of R/W readiness.
306 */
307static inline void fd_update_events(int fd, unsigned char evts)
Christopher Faulet21e92672017-08-30 10:30:04 +0200308{
Richard Russobc9d9842019-02-20 12:43:45 -0800309 unsigned long locked = atleast2(fdtab[fd].thread_mask);
Willy Tarreau1dad3842019-07-08 23:09:03 +0200310 unsigned char old, new;
Willy Tarreaueaf05be2019-12-27 15:52:34 +0100311 int new_flags, must_stop;
Willy Tarreau6b308982019-09-06 19:05:50 +0200312
313 new_flags =
314 ((evts & FD_EV_READY_R) ? FD_POLL_IN : 0) |
315 ((evts & FD_EV_READY_W) ? FD_POLL_OUT : 0) |
316 ((evts & FD_EV_SHUT_R) ? FD_POLL_HUP : 0) |
Willy Tarreau1ed37812020-02-26 16:12:45 +0100317 ((evts & FD_EV_ERR_RW) ? FD_POLL_ERR : 0);
Richard Russobc9d9842019-02-20 12:43:45 -0800318
Willy Tarreau2aaeee32019-10-01 11:46:40 +0200319 /* SHUTW reported while FD was active for writes is an error */
320 if ((fdtab[fd].ev & FD_EV_ACTIVE_W) && (evts & FD_EV_SHUT_W))
321 new_flags |= FD_POLL_ERR;
322
Willy Tarreaueaf05be2019-12-27 15:52:34 +0100323 /* compute the inactive events reported late that must be stopped */
324 must_stop = 0;
325 if (unlikely(!fd_active(fd))) {
326 /* both sides stopped */
327 must_stop = FD_POLL_IN | FD_POLL_OUT;
328 }
Willy Tarreau1ed37812020-02-26 16:12:45 +0100329 else if (unlikely(!fd_recv_active(fd) && (evts & (FD_EV_READY_R | FD_EV_SHUT_R | FD_EV_ERR_RW)))) {
Willy Tarreaueaf05be2019-12-27 15:52:34 +0100330 /* only send remains */
331 must_stop = FD_POLL_IN;
332 }
Willy Tarreau1ed37812020-02-26 16:12:45 +0100333 else if (unlikely(!fd_send_active(fd) && (evts & (FD_EV_READY_W | FD_EV_SHUT_W | FD_EV_ERR_RW)))) {
Willy Tarreaueaf05be2019-12-27 15:52:34 +0100334 /* only recv remains */
335 must_stop = FD_POLL_OUT;
336 }
337
Willy Tarreau1dad3842019-07-08 23:09:03 +0200338 old = fdtab[fd].ev;
Willy Tarreau6b308982019-09-06 19:05:50 +0200339 new = (old & FD_POLL_STICKY) | new_flags;
Willy Tarreau1dad3842019-07-08 23:09:03 +0200340
341 if (unlikely(locked)) {
342 /* Locked FDs (those with more than 2 threads) are atomically updated */
343 while (unlikely(new != old && !_HA_ATOMIC_CAS(&fdtab[fd].ev, &old, new)))
Willy Tarreau6b308982019-09-06 19:05:50 +0200344 new = (old & FD_POLL_STICKY) | new_flags;
Willy Tarreau1dad3842019-07-08 23:09:03 +0200345 } else {
346 if (new != old)
347 fdtab[fd].ev = new;
348 }
Christopher Faulet21e92672017-08-30 10:30:04 +0200349
350 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
351 fd_may_recv(fd);
352
353 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
354 fd_may_send(fd);
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200355
Willy Tarreau19689882019-12-26 11:09:43 +0100356 if (fdtab[fd].iocb && fd_active(fd))
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200357 fdtab[fd].iocb(fd);
Willy Tarreauf5cab822019-08-16 16:06:14 +0200358
Willy Tarreaueaf05be2019-12-27 15:52:34 +0100359 /* we had to stop this FD and it still must be stopped after the I/O
360 * cb's changes, so let's program an update for this.
361 */
362 if (must_stop && !(fdtab[fd].update_mask & tid_bit)) {
363 if (((must_stop & FD_POLL_IN) && !fd_recv_active(fd)) ||
364 ((must_stop & FD_POLL_OUT) && !fd_send_active(fd)))
365 if (!HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
366 fd_updt[fd_nbupdt++] = fd;
367 }
368
Willy Tarreauf5cab822019-08-16 16:06:14 +0200369 ti->flags &= ~TI_FL_STUCK; // this thread is still running
Christopher Faulet21e92672017-08-30 10:30:04 +0200370}
371
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100372/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100373static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200374{
Richard Russobc9d9842019-02-20 12:43:45 -0800375 unsigned long locked = atleast2(thread_mask);
376
377 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200378 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100379 fdtab[fd].owner = owner;
380 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100381 fdtab[fd].ev = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100382 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200383 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100384 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100385 /* note: do not reset polled_mask here as it indicates which poller
386 * still knows this FD from a possible previous round.
387 */
Richard Russobc9d9842019-02-20 12:43:45 -0800388 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200389 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau4ac9d062019-09-05 16:30:39 +0200390 /* the two directions are ready until proven otherwise */
391 fd_may_both(fd);
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200392 _HA_ATOMIC_ADD(&ha_used_fds, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200393}
394
Willy Tarreauf37ba942018-10-17 11:25:54 +0200395/* Computes the bounded poll() timeout based on the next expiration timer <next>
396 * by bounding it to MAX_DELAY_MS. <next> may equal TICK_ETERNITY. The pollers
397 * just needs to call this function right before polling to get their timeout
398 * value. Timeouts that are already expired (possibly due to a pending event)
399 * are accounted for in activity.poll_exp.
400 */
401static inline int compute_poll_timeout(int next)
402{
403 int wait_time;
404
405 if (!tick_isset(next))
406 wait_time = MAX_DELAY_MS;
407 else if (tick_is_expired(next, now_ms)) {
408 activity[tid].poll_exp++;
409 wait_time = 0;
410 }
411 else {
412 wait_time = TICKS_TO_MS(tick_remain(now_ms, next)) + 1;
413 if (wait_time > MAX_DELAY_MS)
414 wait_time = MAX_DELAY_MS;
415 }
416 return wait_time;
417}
418
Willy Tarreau322e6c72018-01-25 16:37:04 +0100419/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
420static inline void hap_fd_set(int fd, unsigned int *evts)
421{
Olivier Houchardd3608792019-03-08 18:47:42 +0100422 _HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100423}
424
425static inline void hap_fd_clr(int fd, unsigned int *evts)
426{
Olivier Houchardd3608792019-03-08 18:47:42 +0100427 _HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100428}
429
430static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
431{
432 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
433}
434
Olivier Houchard79321b92018-07-26 17:55:11 +0200435static inline void wake_thread(int tid)
436{
437 char c = 'c';
438
439 shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1));
440}
441
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442
443#endif /* _PROTO_FD_H */
444
445/*
446 * Local variables:
447 * c-indent-level: 8
448 * c-basic-offset: 8
449 * End:
450 */