blob: a7e70b7fd4655822c02faddcab5a686be2330edf [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>
Christopher Fauletd4604ad2017-05-29 10:40:41 +020031
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <types/fd.h>
33
Willy Tarreau7be79a42012-11-11 15:02:54 +010034/* public variables */
Christopher Fauletd4604ad2017-05-29 10:40:41 +020035
Willy Tarreau16f649c2014-01-25 19:10:48 +010036extern unsigned int *fd_cache; // FD events cache
Willy Tarreau16f649c2014-01-25 19:10:48 +010037extern int fd_cache_num; // number of events in the cache
Christopher Faulet69553fe2018-01-15 11:57:03 +010038extern unsigned long fd_cache_mask; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +020039
40extern THREAD_LOCAL int *fd_updt; // FD updates list
41extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
42
Willy Tarreau8b949692017-11-26 11:07:34 +010043__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 +010044
Willy Tarreau173d9952018-01-26 21:48:23 +010045/* Deletes an FD from the fdsets.
Willy Tarreaubaaee002006-06-26 02:48:02 +020046 * The file descriptor is also closed.
47 */
48void fd_delete(int fd);
49
Willy Tarreau173d9952018-01-26 21:48:23 +010050/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +020051 * The file descriptor is kept open.
52 */
53void fd_remove(int fd);
54
Willy Tarreau4f60f162007-04-08 16:39:58 +020055/* disable the specified poller */
56void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020057
Willy Tarreau2a429502006-10-15 14:52:29 +020058/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020059 * Initialize the pollers till the best one is found.
60 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020061 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020062 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020063int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020064
Willy Tarreau4f60f162007-04-08 16:39:58 +020065/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020066 * Deinitialize the pollers.
67 */
68void deinit_pollers();
69
70/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020071 * Some pollers may lose their connection after a fork(). It may be necessary
72 * to create initialize part of them again. Returns 0 in case of failure,
73 * otherwise 1. The fork() function may be NULL if unused. In case of error,
74 * the the current poller is destroyed and the caller is responsible for trying
75 * another one by calling init_pollers() again.
76 */
77int fork_poller();
78
79/*
80 * Lists the known pollers on <out>.
81 * Should be performed only before initialization.
82 */
83int list_pollers(FILE *out);
84
85/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020086 * Runs the polling loop
87 */
88void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +020089
Willy Tarreau033cd9d2014-01-25 19:24:15 +010090/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +010091 * the poller.
92 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +010093void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +010094
Willy Tarreau5be2f352014-11-19 19:43:05 +010095/* Mark fd <fd> as updated for polling and allocate an entry in the update list
96 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +010097 */
Willy Tarreau5be2f352014-11-19 19:43:05 +010098static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +010099{
Willy Tarreauebc78d72018-01-20 23:53:50 +0100100 if (fdtab[fd].update_mask & tid_bit)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100101 /* already scheduled for update */
102 return;
Willy Tarreauebc78d72018-01-20 23:53:50 +0100103 fdtab[fd].update_mask |= tid_bit;
Willy Tarreau4a291442012-12-13 23:34:18 +0100104 fd_updt[fd_nbupdt++] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100105}
106
107
Willy Tarreau899d9572014-01-25 19:20:35 +0100108/* Allocates a cache entry for a file descriptor if it does not yet have one.
109 * This can be done at any time.
110 */
111static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100112{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100113 HA_RWLOCK_WRLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100114 if (fdtab[fd].cache)
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200115 goto end;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100116 fd_cache_num++;
Christopher Faulet69553fe2018-01-15 11:57:03 +0100117 fd_cache_mask |= fdtab[fd].thread_mask;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100118 fdtab[fd].cache = fd_cache_num;
119 fd_cache[fd_cache_num-1] = fd;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200120 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100121 HA_RWLOCK_WRUNLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100122}
123
Willy Tarreau899d9572014-01-25 19:20:35 +0100124/* Removes entry used by fd <fd> from the FD cache and replaces it with the
125 * last one. The fdtab.cache is adjusted to match the back reference if needed.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100126 * If the fd has no entry assigned, return immediately.
127 */
Willy Tarreau899d9572014-01-25 19:20:35 +0100128static inline void fd_release_cache_entry(int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100129{
130 unsigned int pos;
131
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100132 HA_RWLOCK_WRLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100133 pos = fdtab[fd].cache;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100134 if (!pos)
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200135 goto end;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100136 fdtab[fd].cache = 0;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100137 fd_cache_num--;
138 if (likely(pos <= fd_cache_num)) {
Willy Tarreau7be79a42012-11-11 15:02:54 +0100139 /* was not the last entry */
Willy Tarreau16f649c2014-01-25 19:10:48 +0100140 fd = fd_cache[fd_cache_num];
141 fd_cache[pos - 1] = fd;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100142 fdtab[fd].cache = pos;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100143 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200144 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100145 HA_RWLOCK_WRUNLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100146}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200147
Willy Tarreau25002d22014-01-25 10:32:56 +0100148/* Computes the new polled status based on the active and ready statuses, for
149 * each direction. This is meant to be used by pollers while processing updates.
150 */
151static inline int fd_compute_new_polled_status(int state)
152{
153 if (state & FD_EV_ACTIVE_R) {
154 if (!(state & FD_EV_READY_R))
155 state |= FD_EV_POLLED_R;
156 }
157 else
158 state &= ~FD_EV_POLLED_R;
159
160 if (state & FD_EV_ACTIVE_W) {
161 if (!(state & FD_EV_READY_W))
162 state |= FD_EV_POLLED_W;
163 }
164 else
165 state &= ~FD_EV_POLLED_W;
166
167 return state;
168}
169
Willy Tarreau5be2f352014-11-19 19:43:05 +0100170/* This function automatically enables/disables caching for an entry depending
171 * on its state, and also possibly creates an update entry so that the poller
172 * does its job as well. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100173 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100174static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100175{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100176 /* 3 states for each direction require a polling update */
177 if ((fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_ACTIVE_R)) == FD_EV_POLLED_R ||
178 (fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_READY_R | FD_EV_ACTIVE_R)) == FD_EV_ACTIVE_R ||
179 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_ACTIVE_W)) == FD_EV_POLLED_W ||
180 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_READY_W | FD_EV_ACTIVE_W)) == FD_EV_ACTIVE_W)
181 updt_fd_polling(fd);
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100182
Willy Tarreau5be2f352014-11-19 19:43:05 +0100183 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
184 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
185 ((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 +0100186 fd_alloc_cache_entry(fd);
187 }
188 else {
189 fd_release_cache_entry(fd);
190 }
191}
192
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100193/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100194 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100195 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100196static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100197{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100198 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100199}
200
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100201/*
202 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100203 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100204static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100205{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100206 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100207}
208
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100209/*
210 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100211 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100212static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100213{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100214 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
215}
216
217/*
218 * returns true if the FD is polled for recv
219 */
220static inline int fd_recv_polled(const int fd)
221{
222 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
223}
224
225/*
226 * returns the FD's send state (FD_EV_*)
227 */
228static inline int fd_send_state(const int fd)
229{
230 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
231}
232
233/*
234 * returns true if the FD is active for send
235 */
236static inline int fd_send_active(const int fd)
237{
238 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100239}
240
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100241/*
242 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100243 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100244static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100245{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100246 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
247}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100248
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100249/*
250 * returns true if the FD is polled for send
251 */
252static inline int fd_send_polled(const int fd)
253{
254 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
255}
256
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200257/*
258 * returns true if the FD is active for recv or send
259 */
260static inline int fd_active(const int fd)
261{
262 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
263}
264
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100265/* Disable processing recv events on fd <fd> */
266static inline void fd_stop_recv(int fd)
267{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100268 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200269 if (fd_recv_active(fd)) {
270 fdtab[fd].state &= ~FD_EV_ACTIVE_R;
271 fd_update_cache(fd); /* need an update entry to change the state */
272 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100273 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100274}
275
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100276/* Disable processing send events on fd <fd> */
277static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100278{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100279 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200280 if (fd_send_active(fd)) {
281 fdtab[fd].state &= ~FD_EV_ACTIVE_W;
282 fd_update_cache(fd); /* need an update entry to change the state */
283 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100284 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100285}
286
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100287/* Disable processing of events on fd <fd> for both directions. */
288static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200289{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100290 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200291 if (fd_active(fd)) {
292 fdtab[fd].state &= ~FD_EV_ACTIVE_RW;
293 fd_update_cache(fd); /* need an update entry to change the state */
294 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100295 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200296}
297
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100298/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
299static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200300{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100301 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200302 if (fd_recv_ready(fd)) {
303 fdtab[fd].state &= ~FD_EV_READY_R;
304 fd_update_cache(fd); /* need an update entry to change the state */
305 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100306 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200307}
308
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100309/* Report that FD <fd> can receive anymore without polling. */
310static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200311{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100312 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200313 if (!fd_recv_ready(fd)) {
314 fdtab[fd].state |= FD_EV_READY_R;
315 fd_update_cache(fd); /* need an update entry to change the state */
316 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100317 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200318}
319
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100320/* Disable readiness when polled. This is useful to interrupt reading when it
321 * is suspected that the end of data might have been reached (eg: short read).
322 * This can only be done using level-triggered pollers, so if any edge-triggered
323 * is ever implemented, a test will have to be added here.
324 */
325static inline void fd_done_recv(const int fd)
326{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100327 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200328 if (fd_recv_polled(fd) && fd_recv_ready(fd)) {
329 fdtab[fd].state &= ~FD_EV_READY_R;
330 fd_update_cache(fd); /* need an update entry to change the state */
331 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100333}
334
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100335/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
336static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200337{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100338 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200339 if (fd_send_ready(fd)) {
340 fdtab[fd].state &= ~FD_EV_READY_W;
341 fd_update_cache(fd); /* need an update entry to change the state */
342 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100343 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200344}
345
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100346/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
347static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200348{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100349 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200350 if (!fd_send_ready(fd)) {
351 fdtab[fd].state |= FD_EV_READY_W;
352 fd_update_cache(fd); /* need an update entry to change the state */
353 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100354 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200355}
Willy Tarreau2a429502006-10-15 14:52:29 +0200356
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100357/* Prepare FD <fd> to try to receive */
358static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200359{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100360 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200361 if (!fd_recv_active(fd)) {
362 fdtab[fd].state |= FD_EV_ACTIVE_R;
363 fd_update_cache(fd); /* need an update entry to change the state */
364 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100365 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200366}
367
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100368/* Prepare FD <fd> to try to send */
369static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200370{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100371 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200372 if (!fd_send_active(fd)) {
373 fdtab[fd].state |= FD_EV_ACTIVE_W;
374 fd_update_cache(fd); /* need an update entry to change the state */
375 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100376 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200377}
Willy Tarreau2a429502006-10-15 14:52:29 +0200378
Christopher Faulet21e92672017-08-30 10:30:04 +0200379/* Update events seen for FD <fd> and its state if needed. This should be called
380 * by the poller to set FD_POLL_* flags. */
381static inline void fd_update_events(int fd, int evts)
382{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100383 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200384 fdtab[fd].ev &= FD_POLL_STICKY;
385 fdtab[fd].ev |= evts;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100386 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200387
388 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
389 fd_may_recv(fd);
390
391 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
392 fd_may_send(fd);
393}
394
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100395/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100396static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200397{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100398 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100399 fdtab[fd].owner = owner;
400 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100401 fdtab[fd].ev = 0;
Willy Tarreauebc78d72018-01-20 23:53:50 +0100402 fdtab[fd].update_mask &= ~tid_bit;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100403 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200404 fdtab[fd].cloned = 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200405 fdtab[fd].cache = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100406 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100407 /* note: do not reset polled_mask here as it indicates which poller
408 * still knows this FD from a possible previous round.
409 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100410 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411}
412
Willy Tarreau322e6c72018-01-25 16:37:04 +0100413/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
414static inline void hap_fd_set(int fd, unsigned int *evts)
415{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100416 HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100417}
418
419static inline void hap_fd_clr(int fd, unsigned int *evts)
420{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100421 HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100422}
423
424static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
425{
426 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
427}
428
Willy Tarreaubaaee002006-06-26 02:48:02 +0200429
430#endif /* _PROTO_FD_H */
431
432/*
433 * Local variables:
434 * c-indent-level: 8
435 * c-basic-offset: 8
436 * End:
437 */