blob: e240f5c12c6bba42e5de5e58cd345508efc98e75 [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
42#ifdef USE_THREAD
43HA_SPINLOCK_T fdtab_lock; /* global lock to protect fdtab array */
44HA_RWLOCK_T fdcache_lock; /* global lock to protect fd_cache array */
45HA_SPINLOCK_T poll_lock; /* global lock to protect poll info */
46#endif
Willy Tarreau7be79a42012-11-11 15:02:54 +010047
Willy Tarreaubaaee002006-06-26 02:48:02 +020048/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
49 * The file descriptor is also closed.
50 */
51void fd_delete(int fd);
52
Olivier Houchard1fc05162017-04-06 01:05:05 +020053/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
54 * The file descriptor is kept open.
55 */
56void fd_remove(int fd);
57
Willy Tarreau4f60f162007-04-08 16:39:58 +020058/* disable the specified poller */
59void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
Willy Tarreau2a429502006-10-15 14:52:29 +020061/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020062 * Initialize the pollers till the best one is found.
63 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020064 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020065 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020066int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020067
Willy Tarreau4f60f162007-04-08 16:39:58 +020068/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020069 * Deinitialize the pollers.
70 */
71void deinit_pollers();
72
73/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020074 * Some pollers may lose their connection after a fork(). It may be necessary
75 * to create initialize part of them again. Returns 0 in case of failure,
76 * otherwise 1. The fork() function may be NULL if unused. In case of error,
77 * the the current poller is destroyed and the caller is responsible for trying
78 * another one by calling init_pollers() again.
79 */
80int fork_poller();
81
82/*
83 * Lists the known pollers on <out>.
84 * Should be performed only before initialization.
85 */
86int list_pollers(FILE *out);
87
88/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020089 * Runs the polling loop
90 */
91void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +020092
Willy Tarreau033cd9d2014-01-25 19:24:15 +010093/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +010094 * the poller.
95 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +010096void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +010097
Willy Tarreau5be2f352014-11-19 19:43:05 +010098/* Mark fd <fd> as updated for polling and allocate an entry in the update list
99 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +0100100 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100101static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100102{
103 if (fdtab[fd].updated)
104 /* already scheduled for update */
105 return;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100106 fdtab[fd].updated = 1;
Willy Tarreau4a291442012-12-13 23:34:18 +0100107 fd_updt[fd_nbupdt++] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100108}
109
110
Willy Tarreau899d9572014-01-25 19:20:35 +0100111/* Allocates a cache entry for a file descriptor if it does not yet have one.
112 * This can be done at any time.
113 */
114static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100115{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200116 RWLOCK_WRLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100117 if (fdtab[fd].cache)
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200118 goto end;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100119 fd_cache_num++;
120 fdtab[fd].cache = fd_cache_num;
121 fd_cache[fd_cache_num-1] = fd;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200122 end:
123 RWLOCK_WRUNLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100124}
125
Willy Tarreau899d9572014-01-25 19:20:35 +0100126/* Removes entry used by fd <fd> from the FD cache and replaces it with the
127 * last one. The fdtab.cache is adjusted to match the back reference if needed.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100128 * If the fd has no entry assigned, return immediately.
129 */
Willy Tarreau899d9572014-01-25 19:20:35 +0100130static inline void fd_release_cache_entry(int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100131{
132 unsigned int pos;
133
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200134 RWLOCK_WRLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100135 pos = fdtab[fd].cache;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100136 if (!pos)
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200137 goto end;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100138 fdtab[fd].cache = 0;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100139 fd_cache_num--;
140 if (likely(pos <= fd_cache_num)) {
Willy Tarreau7be79a42012-11-11 15:02:54 +0100141 /* was not the last entry */
Willy Tarreau16f649c2014-01-25 19:10:48 +0100142 fd = fd_cache[fd_cache_num];
143 fd_cache[pos - 1] = fd;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100144 fdtab[fd].cache = pos;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100145 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200146 end:
147 RWLOCK_WRUNLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100148}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200149
Willy Tarreau25002d22014-01-25 10:32:56 +0100150/* Computes the new polled status based on the active and ready statuses, for
151 * each direction. This is meant to be used by pollers while processing updates.
152 */
153static inline int fd_compute_new_polled_status(int state)
154{
155 if (state & FD_EV_ACTIVE_R) {
156 if (!(state & FD_EV_READY_R))
157 state |= FD_EV_POLLED_R;
158 }
159 else
160 state &= ~FD_EV_POLLED_R;
161
162 if (state & FD_EV_ACTIVE_W) {
163 if (!(state & FD_EV_READY_W))
164 state |= FD_EV_POLLED_W;
165 }
166 else
167 state &= ~FD_EV_POLLED_W;
168
169 return state;
170}
171
Willy Tarreau5be2f352014-11-19 19:43:05 +0100172/* This function automatically enables/disables caching for an entry depending
173 * on its state, and also possibly creates an update entry so that the poller
174 * does its job as well. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100175 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100176static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100177{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100178 /* 3 states for each direction require a polling update */
179 if ((fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_ACTIVE_R)) == FD_EV_POLLED_R ||
180 (fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_READY_R | FD_EV_ACTIVE_R)) == FD_EV_ACTIVE_R ||
181 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_ACTIVE_W)) == FD_EV_POLLED_W ||
182 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_READY_W | FD_EV_ACTIVE_W)) == FD_EV_ACTIVE_W)
183 updt_fd_polling(fd);
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100184
Willy Tarreau5be2f352014-11-19 19:43:05 +0100185 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
186 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
187 ((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 +0100188 fd_alloc_cache_entry(fd);
189 }
190 else {
191 fd_release_cache_entry(fd);
192 }
193}
194
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100195/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100196 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100197 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100198static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100199{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100200 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100201}
202
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100203/*
204 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100205 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100206static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100207{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100208 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100209}
210
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100211/*
212 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100213 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100214static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100215{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100216 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
217}
218
219/*
220 * returns true if the FD is polled for recv
221 */
222static inline int fd_recv_polled(const int fd)
223{
224 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
225}
226
227/*
228 * returns the FD's send state (FD_EV_*)
229 */
230static inline int fd_send_state(const int fd)
231{
232 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
233}
234
235/*
236 * returns true if the FD is active for send
237 */
238static inline int fd_send_active(const int fd)
239{
240 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100241}
242
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100243/*
244 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100245 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100246static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100247{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100248 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
249}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100250
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100251/*
252 * returns true if the FD is polled for send
253 */
254static inline int fd_send_polled(const int fd)
255{
256 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
257}
258
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200259/*
260 * returns true if the FD is active for recv or send
261 */
262static inline int fd_active(const int fd)
263{
264 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
265}
266
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100267/* Disable processing recv events on fd <fd> */
268static inline void fd_stop_recv(int fd)
269{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200270 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200271 if (fd_recv_active(fd)) {
272 fdtab[fd].state &= ~FD_EV_ACTIVE_R;
273 fd_update_cache(fd); /* need an update entry to change the state */
274 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200275 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100276}
277
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100278/* Disable processing send events on fd <fd> */
279static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100280{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200281 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200282 if (fd_send_active(fd)) {
283 fdtab[fd].state &= ~FD_EV_ACTIVE_W;
284 fd_update_cache(fd); /* need an update entry to change the state */
285 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200286 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100287}
288
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100289/* Disable processing of events on fd <fd> for both directions. */
290static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200291{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200292 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200293 if (fd_active(fd)) {
294 fdtab[fd].state &= ~FD_EV_ACTIVE_RW;
295 fd_update_cache(fd); /* need an update entry to change the state */
296 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200297 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200298}
299
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100300/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
301static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200302{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200303 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200304 if (fd_recv_ready(fd)) {
305 fdtab[fd].state &= ~FD_EV_READY_R;
306 fd_update_cache(fd); /* need an update entry to change the state */
307 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200308 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200309}
310
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100311/* Report that FD <fd> can receive anymore without polling. */
312static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200313{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200314 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200315 if (!fd_recv_ready(fd)) {
316 fdtab[fd].state |= FD_EV_READY_R;
317 fd_update_cache(fd); /* need an update entry to change the state */
318 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200319 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200320}
321
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100322/* Disable readiness when polled. This is useful to interrupt reading when it
323 * is suspected that the end of data might have been reached (eg: short read).
324 * This can only be done using level-triggered pollers, so if any edge-triggered
325 * is ever implemented, a test will have to be added here.
326 */
327static inline void fd_done_recv(const int fd)
328{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200329 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200330 if (fd_recv_polled(fd) && fd_recv_ready(fd)) {
331 fdtab[fd].state &= ~FD_EV_READY_R;
332 fd_update_cache(fd); /* need an update entry to change the state */
333 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200334 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100335}
336
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100337/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
338static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200339{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200340 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200341 if (fd_send_ready(fd)) {
342 fdtab[fd].state &= ~FD_EV_READY_W;
343 fd_update_cache(fd); /* need an update entry to change the state */
344 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200345 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200346}
347
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100348/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
349static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200350{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200351 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200352 if (!fd_send_ready(fd)) {
353 fdtab[fd].state |= FD_EV_READY_W;
354 fd_update_cache(fd); /* need an update entry to change the state */
355 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200356 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200357}
Willy Tarreau2a429502006-10-15 14:52:29 +0200358
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100359/* Prepare FD <fd> to try to receive */
360static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200361{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200362 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200363 if (!fd_recv_active(fd)) {
364 fdtab[fd].state |= FD_EV_ACTIVE_R;
365 fd_update_cache(fd); /* need an update entry to change the state */
366 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200367 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200368}
369
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100370/* Prepare FD <fd> to try to send */
371static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200372{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200373 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200374 if (!fd_send_active(fd)) {
375 fdtab[fd].state |= FD_EV_ACTIVE_W;
376 fd_update_cache(fd); /* need an update entry to change the state */
377 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200378 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200379}
Willy Tarreau2a429502006-10-15 14:52:29 +0200380
Christopher Faulet21e92672017-08-30 10:30:04 +0200381/* Update events seen for FD <fd> and its state if needed. This should be called
382 * by the poller to set FD_POLL_* flags. */
383static inline void fd_update_events(int fd, int evts)
384{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200385 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200386 fdtab[fd].ev &= FD_POLL_STICKY;
387 fdtab[fd].ev |= evts;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200388 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200389
390 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
391 fd_may_recv(fd);
392
393 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
394 fd_may_send(fd);
395}
396
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100397/* Prepares <fd> for being polled */
Christopher Faulet36716a72017-05-30 11:07:16 +0200398static inline void fd_insert(int fd, unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200400 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100401 fdtab[fd].ev = 0;
Willy Tarreau037d2c12012-11-06 02:34:46 +0100402 fdtab[fd].new = 1;
Christopher Fauletd531f882017-06-01 16:55:03 +0200403 fdtab[fd].updated = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100404 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200405 fdtab[fd].cloned = 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200406 fdtab[fd].cache = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100407 fdtab[fd].thread_mask = thread_mask;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200408 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
409
410 SPIN_LOCK(FDTAB_LOCK, &fdtab_lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411 if (fd + 1 > maxfd)
412 maxfd = fd + 1;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200413 SPIN_UNLOCK(FDTAB_LOCK, &fdtab_lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414}
415
416
417#endif /* _PROTO_FD_H */
418
419/*
420 * Local variables:
421 * c-indent-level: 8
422 * c-basic-offset: 8
423 * End:
424 */