blob: 5885d0cc9c1308e3510659bff36cf8817346eb79 [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 */
44__decl_hathreads(extern HA_SPINLOCK_T __attribute__((aligned(64))) poll_lock); /* global lock to protect poll info */
Willy Tarreau7be79a42012-11-11 15:02:54 +010045
Willy Tarreau173d9952018-01-26 21:48:23 +010046/* Deletes an FD from the fdsets.
Willy Tarreaubaaee002006-06-26 02:48:02 +020047 * The file descriptor is also closed.
48 */
49void fd_delete(int fd);
50
Willy Tarreau173d9952018-01-26 21:48:23 +010051/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +020052 * The file descriptor is kept open.
53 */
54void fd_remove(int fd);
55
Willy Tarreau4f60f162007-04-08 16:39:58 +020056/* disable the specified poller */
57void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau2a429502006-10-15 14:52:29 +020059/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020060 * Initialize the pollers till the best one is found.
61 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020062 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020063 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020064int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020065
Willy Tarreau4f60f162007-04-08 16:39:58 +020066/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020067 * Deinitialize the pollers.
68 */
69void deinit_pollers();
70
71/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020072 * Some pollers may lose their connection after a fork(). It may be necessary
73 * to create initialize part of them again. Returns 0 in case of failure,
74 * otherwise 1. The fork() function may be NULL if unused. In case of error,
75 * the the current poller is destroyed and the caller is responsible for trying
76 * another one by calling init_pollers() again.
77 */
78int fork_poller();
79
80/*
81 * Lists the known pollers on <out>.
82 * Should be performed only before initialization.
83 */
84int list_pollers(FILE *out);
85
86/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020087 * Runs the polling loop
88 */
89void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +020090
Willy Tarreau033cd9d2014-01-25 19:24:15 +010091/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +010092 * the poller.
93 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +010094void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +010095
Willy Tarreau5be2f352014-11-19 19:43:05 +010096/* Mark fd <fd> as updated for polling and allocate an entry in the update list
97 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +010098 */
Willy Tarreau5be2f352014-11-19 19:43:05 +010099static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100100{
Willy Tarreauebc78d72018-01-20 23:53:50 +0100101 if (fdtab[fd].update_mask & tid_bit)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100102 /* already scheduled for update */
103 return;
Willy Tarreauebc78d72018-01-20 23:53:50 +0100104 fdtab[fd].update_mask |= tid_bit;
Willy Tarreau4a291442012-12-13 23:34:18 +0100105 fd_updt[fd_nbupdt++] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100106}
107
108
Willy Tarreau899d9572014-01-25 19:20:35 +0100109/* Allocates a cache entry for a file descriptor if it does not yet have one.
110 * This can be done at any time.
111 */
112static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100113{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100114 HA_RWLOCK_WRLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100115 if (fdtab[fd].cache)
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200116 goto end;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100117 fd_cache_num++;
Christopher Faulet69553fe2018-01-15 11:57:03 +0100118 fd_cache_mask |= fdtab[fd].thread_mask;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100119 fdtab[fd].cache = fd_cache_num;
120 fd_cache[fd_cache_num-1] = fd;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200121 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100122 HA_RWLOCK_WRUNLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100123}
124
Willy Tarreau899d9572014-01-25 19:20:35 +0100125/* Removes entry used by fd <fd> from the FD cache and replaces it with the
126 * last one. The fdtab.cache is adjusted to match the back reference if needed.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100127 * If the fd has no entry assigned, return immediately.
128 */
Willy Tarreau899d9572014-01-25 19:20:35 +0100129static inline void fd_release_cache_entry(int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100130{
131 unsigned int pos;
132
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100133 HA_RWLOCK_WRLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100134 pos = fdtab[fd].cache;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100135 if (!pos)
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200136 goto end;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100137 fdtab[fd].cache = 0;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100138 fd_cache_num--;
139 if (likely(pos <= fd_cache_num)) {
Willy Tarreau7be79a42012-11-11 15:02:54 +0100140 /* was not the last entry */
Willy Tarreau16f649c2014-01-25 19:10:48 +0100141 fd = fd_cache[fd_cache_num];
142 fd_cache[pos - 1] = fd;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100143 fdtab[fd].cache = pos;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100144 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200145 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100146 HA_RWLOCK_WRUNLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100147}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200148
Willy Tarreau25002d22014-01-25 10:32:56 +0100149/* Computes the new polled status based on the active and ready statuses, for
150 * each direction. This is meant to be used by pollers while processing updates.
151 */
152static inline int fd_compute_new_polled_status(int state)
153{
154 if (state & FD_EV_ACTIVE_R) {
155 if (!(state & FD_EV_READY_R))
156 state |= FD_EV_POLLED_R;
157 }
158 else
159 state &= ~FD_EV_POLLED_R;
160
161 if (state & FD_EV_ACTIVE_W) {
162 if (!(state & FD_EV_READY_W))
163 state |= FD_EV_POLLED_W;
164 }
165 else
166 state &= ~FD_EV_POLLED_W;
167
168 return state;
169}
170
Willy Tarreau5be2f352014-11-19 19:43:05 +0100171/* This function automatically enables/disables caching for an entry depending
172 * on its state, and also possibly creates an update entry so that the poller
173 * does its job as well. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100174 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100175static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100176{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100177 /* 3 states for each direction require a polling update */
178 if ((fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_ACTIVE_R)) == FD_EV_POLLED_R ||
179 (fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_READY_R | FD_EV_ACTIVE_R)) == FD_EV_ACTIVE_R ||
180 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_ACTIVE_W)) == FD_EV_POLLED_W ||
181 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_READY_W | FD_EV_ACTIVE_W)) == FD_EV_ACTIVE_W)
182 updt_fd_polling(fd);
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100183
Willy Tarreau5be2f352014-11-19 19:43:05 +0100184 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
185 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
186 ((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 +0100187 fd_alloc_cache_entry(fd);
188 }
189 else {
190 fd_release_cache_entry(fd);
191 }
192}
193
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100194/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100195 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100196 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100197static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100198{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100199 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100200}
201
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100202/*
203 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100204 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100205static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100206{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100207 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
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 recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100212 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100213static inline int fd_recv_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_R;
216}
217
218/*
219 * returns true if the FD is polled for recv
220 */
221static inline int fd_recv_polled(const int fd)
222{
223 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
224}
225
226/*
227 * returns the FD's send state (FD_EV_*)
228 */
229static inline int fd_send_state(const int fd)
230{
231 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
232}
233
234/*
235 * returns true if the FD is active for send
236 */
237static inline int fd_send_active(const int fd)
238{
239 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100240}
241
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100242/*
243 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100244 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100245static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100246{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100247 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
248}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100249
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100250/*
251 * returns true if the FD is polled for send
252 */
253static inline int fd_send_polled(const int fd)
254{
255 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
256}
257
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200258/*
259 * returns true if the FD is active for recv or send
260 */
261static inline int fd_active(const int fd)
262{
263 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
264}
265
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100266/* Disable processing recv events on fd <fd> */
267static inline void fd_stop_recv(int fd)
268{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100269 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200270 if (fd_recv_active(fd)) {
271 fdtab[fd].state &= ~FD_EV_ACTIVE_R;
272 fd_update_cache(fd); /* need an update entry to change the state */
273 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100274 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100275}
276
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100277/* Disable processing send events on fd <fd> */
278static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100279{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100280 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200281 if (fd_send_active(fd)) {
282 fdtab[fd].state &= ~FD_EV_ACTIVE_W;
283 fd_update_cache(fd); /* need an update entry to change the state */
284 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100285 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100286}
287
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100288/* Disable processing of events on fd <fd> for both directions. */
289static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200290{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100291 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200292 if (fd_active(fd)) {
293 fdtab[fd].state &= ~FD_EV_ACTIVE_RW;
294 fd_update_cache(fd); /* need an update entry to change the state */
295 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100296 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200297}
298
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100299/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
300static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200301{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100302 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200303 if (fd_recv_ready(fd)) {
304 fdtab[fd].state &= ~FD_EV_READY_R;
305 fd_update_cache(fd); /* need an update entry to change the state */
306 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100307 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200308}
309
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100310/* Report that FD <fd> can receive anymore without polling. */
311static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200312{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100313 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200314 if (!fd_recv_ready(fd)) {
315 fdtab[fd].state |= FD_EV_READY_R;
316 fd_update_cache(fd); /* need an update entry to change the state */
317 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100318 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200319}
320
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100321/* Disable readiness when polled. This is useful to interrupt reading when it
322 * is suspected that the end of data might have been reached (eg: short read).
323 * This can only be done using level-triggered pollers, so if any edge-triggered
324 * is ever implemented, a test will have to be added here.
325 */
326static inline void fd_done_recv(const int fd)
327{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100328 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200329 if (fd_recv_polled(fd) && fd_recv_ready(fd)) {
330 fdtab[fd].state &= ~FD_EV_READY_R;
331 fd_update_cache(fd); /* need an update entry to change the state */
332 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100333 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100334}
335
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100336/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
337static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200338{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100339 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200340 if (fd_send_ready(fd)) {
341 fdtab[fd].state &= ~FD_EV_READY_W;
342 fd_update_cache(fd); /* need an update entry to change the state */
343 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100344 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200345}
346
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100347/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
348static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200349{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100350 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200351 if (!fd_send_ready(fd)) {
352 fdtab[fd].state |= FD_EV_READY_W;
353 fd_update_cache(fd); /* need an update entry to change the state */
354 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100355 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200356}
Willy Tarreau2a429502006-10-15 14:52:29 +0200357
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100358/* Prepare FD <fd> to try to receive */
359static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200360{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100361 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200362 if (!fd_recv_active(fd)) {
363 fdtab[fd].state |= FD_EV_ACTIVE_R;
364 fd_update_cache(fd); /* need an update entry to change the state */
365 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100366 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200367}
368
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100369/* Prepare FD <fd> to try to send */
370static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200371{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100372 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200373 if (!fd_send_active(fd)) {
374 fdtab[fd].state |= FD_EV_ACTIVE_W;
375 fd_update_cache(fd); /* need an update entry to change the state */
376 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100377 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200378}
Willy Tarreau2a429502006-10-15 14:52:29 +0200379
Christopher Faulet21e92672017-08-30 10:30:04 +0200380/* Update events seen for FD <fd> and its state if needed. This should be called
381 * by the poller to set FD_POLL_* flags. */
382static inline void fd_update_events(int fd, int evts)
383{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100384 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200385 fdtab[fd].ev &= FD_POLL_STICKY;
386 fdtab[fd].ev |= evts;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100387 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200388
389 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
390 fd_may_recv(fd);
391
392 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
393 fd_may_send(fd);
394}
395
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100396/* Prepares <fd> for being polled */
Christopher Faulet36716a72017-05-30 11:07:16 +0200397static inline void fd_insert(int fd, unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200398{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100399 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100400 fdtab[fd].ev = 0;
Willy Tarreauebc78d72018-01-20 23:53:50 +0100401 fdtab[fd].update_mask &= ~tid_bit;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100402 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200403 fdtab[fd].cloned = 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200404 fdtab[fd].cache = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100405 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100406 /* note: do not reset polled_mask here as it indicates which poller
407 * still knows this FD from a possible previous round.
408 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100409 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200410}
411
412
413#endif /* _PROTO_FD_H */
414
415/*
416 * Local variables:
417 * c-indent-level: 8
418 * c-basic-offset: 8
419 * End:
420 */