blob: c5a03f775b95fd2627f0dc68e1b8f6d215040341 [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
Olivier Houchard4815c8c2018-01-24 18:17:56 +010036extern volatile struct fdlist fd_cache;
37extern volatile struct fdlist fd_cache_local[MAX_THREADS];
38
Olivier Houchard6b96f722018-04-25 16:58:25 +020039extern volatile struct fdlist update_list;
40
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020041extern unsigned long *polled_mask;
42
Christopher Faulet69553fe2018-01-15 11:57:03 +010043extern unsigned long fd_cache_mask; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +020044
45extern THREAD_LOCAL int *fd_updt; // FD updates list
46extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
47
Willy Tarreau8b949692017-11-26 11:07:34 +010048__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 +010049
Willy Tarreau173d9952018-01-26 21:48:23 +010050/* Deletes an FD from the fdsets.
Willy Tarreaubaaee002006-06-26 02:48:02 +020051 * The file descriptor is also closed.
52 */
53void fd_delete(int fd);
54
Willy Tarreau173d9952018-01-26 21:48:23 +010055/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +020056 * The file descriptor is kept open.
57 */
58void fd_remove(int fd);
59
Willy Tarreau4f60f162007-04-08 16:39:58 +020060/* disable the specified poller */
61void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Willy Tarreau2a429502006-10-15 14:52:29 +020063/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020064 * Initialize the pollers till the best one is found.
65 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020066 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020067 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020068int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020069
Willy Tarreau4f60f162007-04-08 16:39:58 +020070/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020071 * Deinitialize the pollers.
72 */
73void deinit_pollers();
74
75/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020076 * Some pollers may lose their connection after a fork(). It may be necessary
77 * to create initialize part of them again. Returns 0 in case of failure,
78 * otherwise 1. The fork() function may be NULL if unused. In case of error,
79 * the the current poller is destroyed and the caller is responsible for trying
80 * another one by calling init_pollers() again.
81 */
82int fork_poller();
83
84/*
85 * Lists the known pollers on <out>.
86 * Should be performed only before initialization.
87 */
88int list_pollers(FILE *out);
89
90/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020091 * Runs the polling loop
92 */
93void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +020094
Willy Tarreau033cd9d2014-01-25 19:24:15 +010095/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +010096 * the poller.
97 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +010098void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +010099
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200100void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off);
101void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100102
Willy Tarreau5be2f352014-11-19 19:43:05 +0100103/* Mark fd <fd> as updated for polling and allocate an entry in the update list
104 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +0100105 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100106static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100107{
Olivier Houchard6b96f722018-04-25 16:58:25 +0200108 if (fdtab[fd].thread_mask == tid_bit) {
109 unsigned int oldupdt;
110
111 /* note: we don't have a test-and-set yet in hathreads */
Willy Tarreau4d841862018-01-17 22:57:54 +0100112
Olivier Houchard6b96f722018-04-25 16:58:25 +0200113 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
114 return;
115
116 oldupdt = HA_ATOMIC_ADD(&fd_nbupdt, 1) - 1;
117 fd_updt[oldupdt] = fd;
118 } else {
119 unsigned long update_mask = fdtab[fd].update_mask;
120 do {
121 if (update_mask == fdtab[fd].thread_mask)
122 return;
123 } while (!HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask,
124 fdtab[fd].thread_mask));
125 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
126 }
127
128}
Willy Tarreau4d841862018-01-17 22:57:54 +0100129
Olivier Houchard6b96f722018-04-25 16:58:25 +0200130/* Called from the poller to acknoledge we read an entry from the global
131 * update list, to remove our bit from the update_mask, and remove it from
132 * the list if we were the last one.
133 */
134static inline void done_update_polling(int fd)
135{
136 unsigned long update_mask;
137
138 update_mask = HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
139 while ((update_mask & all_threads_mask)== 0) {
140 /* If we were the last one that had to update that entry, remove it from the list */
141 fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update));
142 if (update_list.first == fd)
143 abort();
144 update_mask = (volatile unsigned long)fdtab[fd].update_mask;
145 if ((update_mask & all_threads_mask) != 0) {
146 /* Maybe it's been re-updated in the meanwhile, and we
147 * wrongly removed it from the list, if so, re-add it
148 */
149 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
150 update_mask = (volatile unsigned long)(fdtab[fd].update_mask);
151 /* And then check again, just in case after all it
152 * should be removed, even if it's very unlikely, given
153 * the current thread wouldn't have been able to take
154 * care of it yet */
155 } else
156 break;
Willy Tarreau4d841862018-01-17 22:57:54 +0100157
Olivier Houchard6b96f722018-04-25 16:58:25 +0200158 }
Willy Tarreau7be79a42012-11-11 15:02:54 +0100159}
160
Willy Tarreau899d9572014-01-25 19:20:35 +0100161/* Allocates a cache entry for a file descriptor if it does not yet have one.
162 * This can be done at any time.
163 */
164static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100165{
Willy Tarreau26fb5d82018-03-20 19:06:52 +0100166 HA_ATOMIC_OR(&fd_cache_mask, fdtab[fd].thread_mask);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100167 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200168 fd_add_to_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd, offsetof(struct fdtab, cache));
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100169 else
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200170 fd_add_to_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache));
Willy Tarreau7be79a42012-11-11 15:02:54 +0100171}
172
Willy Tarreau899d9572014-01-25 19:20:35 +0100173/* Removes entry used by fd <fd> from the FD cache and replaces it with the
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100174 * last one.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100175 * If the fd has no entry assigned, return immediately.
176 */
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100177static inline void fd_release_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100178{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100179 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200180 fd_rm_from_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd, offsetof(struct fdtab, cache));
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100181 else
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200182 fd_rm_from_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache));
Willy Tarreau7be79a42012-11-11 15:02:54 +0100183}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200184
Willy Tarreau5be2f352014-11-19 19:43:05 +0100185/* This function automatically enables/disables caching for an entry depending
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100186 * on its state. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100187 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100188static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100189{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100190 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
191 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
192 ((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 +0100193 fd_alloc_cache_entry(fd);
194 }
195 else {
196 fd_release_cache_entry(fd);
197 }
198}
199
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100200/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100201 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100202 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100203static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100204{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100205 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100206}
207
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100208/*
209 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100210 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100211static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100212{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100213 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100214}
215
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100216/*
217 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100218 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100219static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100220{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100221 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
222}
223
224/*
225 * returns true if the FD is polled for recv
226 */
227static inline int fd_recv_polled(const int fd)
228{
229 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
230}
231
232/*
233 * returns the FD's send state (FD_EV_*)
234 */
235static inline int fd_send_state(const int fd)
236{
237 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
238}
239
240/*
241 * returns true if the FD is active for send
242 */
243static inline int fd_send_active(const int fd)
244{
245 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100246}
247
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100248/*
249 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100250 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100251static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100252{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100253 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
254}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100255
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100256/*
257 * returns true if the FD is polled for send
258 */
259static inline int fd_send_polled(const int fd)
260{
261 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
262}
263
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200264/*
265 * returns true if the FD is active for recv or send
266 */
267static inline int fd_active(const int fd)
268{
269 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
270}
271
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100272/* Disable processing recv events on fd <fd> */
273static inline void fd_stop_recv(int fd)
274{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100275 unsigned char old, new;
276
277 old = fdtab[fd].state;
278 do {
279 if (!(old & FD_EV_ACTIVE_R))
280 return;
281 new = old & ~FD_EV_ACTIVE_R;
282 new &= ~FD_EV_POLLED_R;
283 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
284
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100285 if ((old ^ new) & FD_EV_POLLED_R)
286 updt_fd_polling(fd);
287
Willy Tarreau4d841862018-01-17 22:57:54 +0100288 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100289 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100290 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100291}
292
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100293/* Disable processing send events on fd <fd> */
294static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100295{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100296 unsigned char old, new;
297
298 old = fdtab[fd].state;
299 do {
300 if (!(old & FD_EV_ACTIVE_W))
301 return;
302 new = old & ~FD_EV_ACTIVE_W;
303 new &= ~FD_EV_POLLED_W;
304 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
305
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100306 if ((old ^ new) & FD_EV_POLLED_W)
307 updt_fd_polling(fd);
308
Willy Tarreau4d841862018-01-17 22:57:54 +0100309 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100310 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100311 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100312}
313
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100314/* Disable processing of events on fd <fd> for both directions. */
315static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200316{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100317 unsigned char old, new;
318
319 old = fdtab[fd].state;
320 do {
321 if (!(old & FD_EV_ACTIVE_RW))
322 return;
323 new = old & ~FD_EV_ACTIVE_RW;
324 new &= ~FD_EV_POLLED_RW;
325 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
326
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100327 if ((old ^ new) & FD_EV_POLLED_RW)
328 updt_fd_polling(fd);
329
Willy Tarreau4d841862018-01-17 22:57:54 +0100330 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100331 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200333}
334
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100335/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
336static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200337{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100338 unsigned char old, new;
339
340 old = fdtab[fd].state;
341 do {
342 if (!(old & FD_EV_READY_R))
343 return;
344 new = old & ~FD_EV_READY_R;
345 if (new & FD_EV_ACTIVE_R)
346 new |= FD_EV_POLLED_R;
347 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
348
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100349 if ((old ^ new) & FD_EV_POLLED_R)
350 updt_fd_polling(fd);
351
Willy Tarreau4d841862018-01-17 22:57:54 +0100352 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100353 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100354 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200355}
356
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100357/* Report that FD <fd> can receive anymore without polling. */
358static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200359{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100360 /* marking ready never changes polled status */
361 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_R);
362
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100363 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100364 fd_update_cache(fd); /* need an update entry to change the state */
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 Tarreau6c11bd22014-01-24 00:54:27 +0100368/* Disable readiness when polled. This is useful to interrupt reading when it
369 * is suspected that the end of data might have been reached (eg: short read).
370 * This can only be done using level-triggered pollers, so if any edge-triggered
371 * is ever implemented, a test will have to be added here.
372 */
373static inline void fd_done_recv(const int fd)
374{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100375 unsigned char old, new;
376
377 old = fdtab[fd].state;
378 do {
379 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
380 return;
381 new = old & ~FD_EV_READY_R;
382 if (new & FD_EV_ACTIVE_R)
383 new |= FD_EV_POLLED_R;
384 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
385
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100386 if ((old ^ new) & FD_EV_POLLED_R)
387 updt_fd_polling(fd);
388
Willy Tarreau4d841862018-01-17 22:57:54 +0100389 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100390 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100391 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100392}
393
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100394/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
395static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200396{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100397 unsigned char old, new;
398
399 old = fdtab[fd].state;
400 do {
401 if (!(old & FD_EV_READY_W))
402 return;
403 new = old & ~FD_EV_READY_W;
404 if (new & FD_EV_ACTIVE_W)
405 new |= FD_EV_POLLED_W;
406 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
407
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100408 if ((old ^ new) & FD_EV_POLLED_W)
409 updt_fd_polling(fd);
410
Willy Tarreau4d841862018-01-17 22:57:54 +0100411 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100412 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100413 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200414}
415
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100416/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
417static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200418{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100419 /* marking ready never changes polled status */
420 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W);
421
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100422 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100423 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100424 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200425}
Willy Tarreau2a429502006-10-15 14:52:29 +0200426
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100427/* Prepare FD <fd> to try to receive */
428static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200429{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100430 unsigned char old, new;
431
432 old = fdtab[fd].state;
433 do {
434 if (old & FD_EV_ACTIVE_R)
435 return;
436 new = old | FD_EV_ACTIVE_R;
437 if (!(new & FD_EV_READY_R))
438 new |= FD_EV_POLLED_R;
439 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
440
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100441 if ((old ^ new) & FD_EV_POLLED_R)
442 updt_fd_polling(fd);
443
Willy Tarreau4d841862018-01-17 22:57:54 +0100444 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100445 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100446 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200447}
448
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100449/* Prepare FD <fd> to try to send */
450static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200451{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100452 unsigned char old, new;
453
454 old = fdtab[fd].state;
455 do {
456 if (old & FD_EV_ACTIVE_W)
457 return;
458 new = old | FD_EV_ACTIVE_W;
459 if (!(new & FD_EV_READY_W))
460 new |= FD_EV_POLLED_W;
461 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
462
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100463 if ((old ^ new) & FD_EV_POLLED_W)
464 updt_fd_polling(fd);
465
Willy Tarreau4d841862018-01-17 22:57:54 +0100466 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100467 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100468 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200469}
Willy Tarreau2a429502006-10-15 14:52:29 +0200470
Christopher Faulet21e92672017-08-30 10:30:04 +0200471/* Update events seen for FD <fd> and its state if needed. This should be called
472 * by the poller to set FD_POLL_* flags. */
473static inline void fd_update_events(int fd, int evts)
474{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100475 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200476 fdtab[fd].ev &= FD_POLL_STICKY;
477 fdtab[fd].ev |= evts;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100478 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200479
480 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
481 fd_may_recv(fd);
482
483 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
484 fd_may_send(fd);
485}
486
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100487/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100488static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100490 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100491 fdtab[fd].owner = owner;
492 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100493 fdtab[fd].ev = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100494 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200495 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100496 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100497 /* note: do not reset polled_mask here as it indicates which poller
498 * still knows this FD from a possible previous round.
499 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100500 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501}
502
Willy Tarreau322e6c72018-01-25 16:37:04 +0100503/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
504static inline void hap_fd_set(int fd, unsigned int *evts)
505{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100506 HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100507}
508
509static inline void hap_fd_clr(int fd, unsigned int *evts)
510{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100511 HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100512}
513
514static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
515{
516 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
517}
518
Willy Tarreaubaaee002006-06-26 02:48:02 +0200519
520#endif /* _PROTO_FD_H */
521
522/*
523 * Local variables:
524 * c-indent-level: 8
525 * c-basic-offset: 8
526 * End:
527 */