blob: 9038be5117c4af57951e69672bdb341553f57f6d [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 Tarreau4cc67a22018-02-05 17:14:55 +0100107
Willy Tarreau5be2f352014-11-19 19:43:05 +0100108/* Mark fd <fd> as updated for polling and allocate an entry in the update list
109 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +0100110 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100111static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100112{
Olivier Houchard6aab7372018-08-17 13:37:59 +0200113 if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) {
Olivier Houchard6b96f722018-04-25 16:58:25 +0200114
115 /* note: we don't have a test-and-set yet in hathreads */
Willy Tarreau4d841862018-01-17 22:57:54 +0100116
Olivier Houchard6b96f722018-04-25 16:58:25 +0200117 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
118 return;
119
Olivier Houcharda0fdce32019-06-12 14:31:08 +0200120 fd_updt[fd_nbupdt++] = fd;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200121 } else {
122 unsigned long update_mask = fdtab[fd].update_mask;
123 do {
124 if (update_mask == fdtab[fd].thread_mask)
125 return;
Olivier Houchardd3608792019-03-08 18:47:42 +0100126 } while (!_HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask,
Olivier Houchard6b96f722018-04-25 16:58:25 +0200127 fdtab[fd].thread_mask));
128 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
129 }
130
131}
Willy Tarreau4d841862018-01-17 22:57:54 +0100132
Olivier Houchard6b96f722018-04-25 16:58:25 +0200133/* Called from the poller to acknoledge we read an entry from the global
134 * update list, to remove our bit from the update_mask, and remove it from
135 * the list if we were the last one.
136 */
137static inline void done_update_polling(int fd)
138{
139 unsigned long update_mask;
140
Olivier Houchardd3608792019-03-08 18:47:42 +0100141 update_mask = _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +0200142 while ((update_mask & all_threads_mask)== 0) {
143 /* If we were the last one that had to update that entry, remove it from the list */
144 fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update));
Olivier Houchard6b96f722018-04-25 16:58:25 +0200145 update_mask = (volatile unsigned long)fdtab[fd].update_mask;
146 if ((update_mask & all_threads_mask) != 0) {
147 /* Maybe it's been re-updated in the meanwhile, and we
148 * wrongly removed it from the list, if so, re-add it
149 */
150 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
151 update_mask = (volatile unsigned long)(fdtab[fd].update_mask);
152 /* And then check again, just in case after all it
153 * should be removed, even if it's very unlikely, given
154 * the current thread wouldn't have been able to take
155 * care of it yet */
156 } else
157 break;
Willy Tarreau4d841862018-01-17 22:57:54 +0100158
Olivier Houchard6b96f722018-04-25 16:58:25 +0200159 }
Willy Tarreau7be79a42012-11-11 15:02:54 +0100160}
161
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100162/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100163 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100164 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100165static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100166{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100167 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100168}
169
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100170/*
171 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100172 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100173static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100174{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100175 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100176}
177
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100178/*
179 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100180 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100181static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100182{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100183 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
184}
185
186/*
187 * returns true if the FD is polled for recv
188 */
189static inline int fd_recv_polled(const int fd)
190{
191 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
192}
193
194/*
195 * returns the FD's send state (FD_EV_*)
196 */
197static inline int fd_send_state(const int fd)
198{
199 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
200}
201
202/*
203 * returns true if the FD is active for send
204 */
205static inline int fd_send_active(const int fd)
206{
207 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100208}
209
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100210/*
211 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100212 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100213static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100214{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100215 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
216}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100217
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100218/*
219 * returns true if the FD is polled for send
220 */
221static inline int fd_send_polled(const int fd)
222{
223 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
224}
225
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200226/*
227 * returns true if the FD is active for recv or send
228 */
229static inline int fd_active(const int fd)
230{
231 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
232}
233
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100234/* Disable processing recv events on fd <fd> */
235static inline void fd_stop_recv(int fd)
236{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100237 unsigned char old, new;
238
239 old = fdtab[fd].state;
240 do {
241 if (!(old & FD_EV_ACTIVE_R))
242 return;
243 new = old & ~FD_EV_ACTIVE_R;
244 new &= ~FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100245 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100246
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100247 if ((old ^ new) & FD_EV_POLLED_R)
248 updt_fd_polling(fd);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100249}
250
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100251/* Disable processing send events on fd <fd> */
252static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100253{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100254 unsigned char old, new;
255
256 old = fdtab[fd].state;
257 do {
258 if (!(old & FD_EV_ACTIVE_W))
259 return;
260 new = old & ~FD_EV_ACTIVE_W;
261 new &= ~FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100262 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100263
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100264 if ((old ^ new) & FD_EV_POLLED_W)
265 updt_fd_polling(fd);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100266}
267
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100268/* Disable processing of events on fd <fd> for both directions. */
269static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200270{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100271 unsigned char old, new;
272
273 old = fdtab[fd].state;
274 do {
275 if (!(old & FD_EV_ACTIVE_RW))
276 return;
277 new = old & ~FD_EV_ACTIVE_RW;
278 new &= ~FD_EV_POLLED_RW;
Olivier Houchardd3608792019-03-08 18:47:42 +0100279 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100280
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100281 if ((old ^ new) & FD_EV_POLLED_RW)
282 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200283}
284
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100285/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
286static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200287{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100288 unsigned char old, new;
289
290 old = fdtab[fd].state;
291 do {
292 if (!(old & FD_EV_READY_R))
293 return;
294 new = old & ~FD_EV_READY_R;
295 if (new & FD_EV_ACTIVE_R)
296 new |= FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100297 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100298
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100299 if ((old ^ new) & FD_EV_POLLED_R)
300 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200301}
302
Willy Tarreau1dad3842019-07-08 23:09:03 +0200303/* Report that FD <fd> may receive again without polling. */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100304static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200305{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100306 /* marking ready never changes polled status */
Willy Tarreau1dad3842019-07-08 23:09:03 +0200307 if ((fdtab[fd].state & FD_EV_READY_R) ||
308 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_R_BIT))
309 return;
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200310}
311
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100312/* Disable readiness when polled. This is useful to interrupt reading when it
313 * is suspected that the end of data might have been reached (eg: short read).
314 * This can only be done using level-triggered pollers, so if any edge-triggered
315 * is ever implemented, a test will have to be added here.
316 */
317static inline void fd_done_recv(const int fd)
318{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100319 unsigned char old, new;
320
321 old = fdtab[fd].state;
322 do {
323 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
324 return;
325 new = old & ~FD_EV_READY_R;
326 if (new & FD_EV_ACTIVE_R)
327 new |= FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100328 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100329
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100330 if ((old ^ new) & FD_EV_POLLED_R)
331 updt_fd_polling(fd);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100332}
333
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100334/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
335static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200336{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100337 unsigned char old, new;
338
339 old = fdtab[fd].state;
340 do {
341 if (!(old & FD_EV_READY_W))
342 return;
343 new = old & ~FD_EV_READY_W;
344 if (new & FD_EV_ACTIVE_W)
345 new |= FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100346 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100347
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100348 if ((old ^ new) & FD_EV_POLLED_W)
349 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200350}
351
Willy Tarreau1dad3842019-07-08 23:09:03 +0200352/* Report that FD <fd> may send again without polling (EAGAIN not detected). */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100353static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200354{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100355 /* marking ready never changes polled status */
Willy Tarreau1dad3842019-07-08 23:09:03 +0200356 if ((fdtab[fd].state & FD_EV_READY_W) ||
357 HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_W_BIT))
358 return;
Willy Tarreau49b046d2012-08-09 12:11:58 +0200359}
Willy Tarreau2a429502006-10-15 14:52:29 +0200360
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100361/* Prepare FD <fd> to try to receive */
362static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200363{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100364 unsigned char old, new;
365
366 old = fdtab[fd].state;
367 do {
368 if (old & FD_EV_ACTIVE_R)
369 return;
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200370 new = old | FD_EV_ACTIVE_R | FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100371 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100372
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100373 if ((old ^ new) & FD_EV_POLLED_R)
374 updt_fd_polling(fd);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200375}
376
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100377/* Prepare FD <fd> to try to send */
378static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200379{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100380 unsigned char old, new;
381
382 old = fdtab[fd].state;
383 do {
384 if (old & FD_EV_ACTIVE_W)
385 return;
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200386 new = old | FD_EV_ACTIVE_W | FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100387 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100388
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100389 if ((old ^ new) & FD_EV_POLLED_W)
390 updt_fd_polling(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200391}
Willy Tarreau2a429502006-10-15 14:52:29 +0200392
Christopher Faulet21e92672017-08-30 10:30:04 +0200393/* Update events seen for FD <fd> and its state if needed. This should be called
394 * by the poller to set FD_POLL_* flags. */
395static inline void fd_update_events(int fd, int evts)
396{
Richard Russobc9d9842019-02-20 12:43:45 -0800397 unsigned long locked = atleast2(fdtab[fd].thread_mask);
Willy Tarreau1dad3842019-07-08 23:09:03 +0200398 unsigned char old, new;
Richard Russobc9d9842019-02-20 12:43:45 -0800399
Willy Tarreau1dad3842019-07-08 23:09:03 +0200400 old = fdtab[fd].ev;
401 new = (old & FD_POLL_STICKY) | evts;
402
403 if (unlikely(locked)) {
404 /* Locked FDs (those with more than 2 threads) are atomically updated */
405 while (unlikely(new != old && !_HA_ATOMIC_CAS(&fdtab[fd].ev, &old, new)))
406 new = (old & FD_POLL_STICKY) | evts;
407 } else {
408 if (new != old)
409 fdtab[fd].ev = new;
410 }
Christopher Faulet21e92672017-08-30 10:30:04 +0200411
412 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
413 fd_may_recv(fd);
414
415 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
416 fd_may_send(fd);
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200417
418 if (fdtab[fd].iocb)
419 fdtab[fd].iocb(fd);
Willy Tarreauf5cab822019-08-16 16:06:14 +0200420
421 ti->flags &= ~TI_FL_STUCK; // this thread is still running
Christopher Faulet21e92672017-08-30 10:30:04 +0200422}
423
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100424/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100425static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426{
Richard Russobc9d9842019-02-20 12:43:45 -0800427 unsigned long locked = atleast2(thread_mask);
428
429 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200430 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100431 fdtab[fd].owner = owner;
432 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100433 fdtab[fd].ev = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100434 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200435 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100436 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100437 /* note: do not reset polled_mask here as it indicates which poller
438 * still knows this FD from a possible previous round.
439 */
Richard Russobc9d9842019-02-20 12:43:45 -0800440 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200441 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200442 _HA_ATOMIC_ADD(&ha_used_fds, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200443}
444
Willy Tarreauf37ba942018-10-17 11:25:54 +0200445/* Computes the bounded poll() timeout based on the next expiration timer <next>
446 * by bounding it to MAX_DELAY_MS. <next> may equal TICK_ETERNITY. The pollers
447 * just needs to call this function right before polling to get their timeout
448 * value. Timeouts that are already expired (possibly due to a pending event)
449 * are accounted for in activity.poll_exp.
450 */
451static inline int compute_poll_timeout(int next)
452{
453 int wait_time;
454
455 if (!tick_isset(next))
456 wait_time = MAX_DELAY_MS;
457 else if (tick_is_expired(next, now_ms)) {
458 activity[tid].poll_exp++;
459 wait_time = 0;
460 }
461 else {
462 wait_time = TICKS_TO_MS(tick_remain(now_ms, next)) + 1;
463 if (wait_time > MAX_DELAY_MS)
464 wait_time = MAX_DELAY_MS;
465 }
466 return wait_time;
467}
468
Willy Tarreau322e6c72018-01-25 16:37:04 +0100469/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
470static inline void hap_fd_set(int fd, unsigned int *evts)
471{
Olivier Houchardd3608792019-03-08 18:47:42 +0100472 _HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100473}
474
475static inline void hap_fd_clr(int fd, unsigned int *evts)
476{
Olivier Houchardd3608792019-03-08 18:47:42 +0100477 _HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100478}
479
480static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
481{
482 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
483}
484
Olivier Houchard79321b92018-07-26 17:55:11 +0200485static inline void wake_thread(int tid)
486{
487 char c = 'c';
488
489 shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1));
490}
491
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492
493#endif /* _PROTO_FD_H */
494
495/*
496 * Local variables:
497 * c-indent-level: 8
498 * c-basic-offset: 8
499 * End:
500 */