blob: 8cc191f75da2b4096e53d834492a5d05b6346a5d [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 Fauletd4604ad2017-05-29 10:40:41 +020038
39extern THREAD_LOCAL int *fd_updt; // FD updates list
40extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
41
Willy Tarreau8b949692017-11-26 11:07:34 +010042__decl_hathreads(extern HA_SPINLOCK_T __attribute__((aligned(64))) fdtab_lock); /* global lock to protect fdtab array */
43__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 Tarreaubaaee002006-06-26 02:48:02 +020046/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
47 * The file descriptor is also closed.
48 */
49void fd_delete(int fd);
50
Olivier Houchard1fc05162017-04-06 01:05:05 +020051/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
52 * 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{
101 if (fdtab[fd].updated)
102 /* already scheduled for update */
103 return;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100104 fdtab[fd].updated = 1;
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++;
118 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 */
Christopher Faulet36716a72017-05-30 11:07:16 +0200396static inline void fd_insert(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 Tarreaud6f087e2008-01-18 17:20:13 +0100399 fdtab[fd].ev = 0;
Willy Tarreau037d2c12012-11-06 02:34:46 +0100400 fdtab[fd].new = 1;
Christopher Fauletd531f882017-06-01 16:55:03 +0200401 fdtab[fd].updated = 0;
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;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100406 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200407
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100408 HA_SPIN_LOCK(FDTAB_LOCK, &fdtab_lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409 if (fd + 1 > maxfd)
410 maxfd = fd + 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100411 HA_SPIN_UNLOCK(FDTAB_LOCK, &fdtab_lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412}
413
414
415#endif /* _PROTO_FD_H */
416
417/*
418 * Local variables:
419 * c-indent-level: 8
420 * c-basic-offset: 8
421 * End:
422 */