blob: 0f59df6617b2e682c2b3d6eef3dc9baa68a88a62 [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
Christopher Faulet69553fe2018-01-15 11:57:03 +010039extern unsigned long fd_cache_mask; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +020040
41extern THREAD_LOCAL int *fd_updt; // FD updates list
42extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
43
Willy Tarreau8b949692017-11-26 11:07:34 +010044__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 +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 Tarreau4cc67a22018-02-05 17:14:55 +010096void fd_add_to_fd_list(volatile struct fdlist *list, int fd);
97void fd_rm_from_fd_list(volatile struct fdlist *list, int fd);
98
Willy Tarreau5be2f352014-11-19 19:43:05 +010099/* Mark fd <fd> as updated for polling and allocate an entry in the update list
100 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +0100101 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100102static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100103{
Willy Tarreau4d841862018-01-17 22:57:54 +0100104 unsigned int oldupdt;
105
106 /* note: we don't have a test-and-set yet in hathreads */
107
108 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
Willy Tarreau7be79a42012-11-11 15:02:54 +0100109 return;
Willy Tarreau4d841862018-01-17 22:57:54 +0100110
111 oldupdt = HA_ATOMIC_ADD(&fd_nbupdt, 1) - 1;
112 fd_updt[oldupdt] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100113}
114
Willy Tarreau899d9572014-01-25 19:20:35 +0100115/* Allocates a cache entry for a file descriptor if it does not yet have one.
116 * This can be done at any time.
117 */
118static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100119{
Willy Tarreau26fb5d82018-03-20 19:06:52 +0100120 HA_ATOMIC_OR(&fd_cache_mask, fdtab[fd].thread_mask);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100121 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
122 fd_add_to_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd);
123 else
124 fd_add_to_fd_list(&fd_cache, fd);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100125}
126
Willy Tarreau899d9572014-01-25 19:20:35 +0100127/* Removes entry used by fd <fd> from the FD cache and replaces it with the
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100128 * last one.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100129 * If the fd has no entry assigned, return immediately.
130 */
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100131static inline void fd_release_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100132{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100133 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
134 fd_rm_from_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd);
135 else
136 fd_rm_from_fd_list(&fd_cache, fd);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100137}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200138
Willy Tarreau5be2f352014-11-19 19:43:05 +0100139/* This function automatically enables/disables caching for an entry depending
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100140 * on its state. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100141 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100142static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100143{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100144 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
145 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
146 ((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 +0100147 fd_alloc_cache_entry(fd);
148 }
149 else {
150 fd_release_cache_entry(fd);
151 }
152}
153
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100154/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100155 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100156 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100157static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100158{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100159 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100160}
161
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100162/*
163 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100164 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100165static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100166{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100167 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100168}
169
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100170/*
171 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100172 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100173static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100174{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100175 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
176}
177
178/*
179 * returns true if the FD is polled for recv
180 */
181static inline int fd_recv_polled(const int fd)
182{
183 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
184}
185
186/*
187 * returns the FD's send state (FD_EV_*)
188 */
189static inline int fd_send_state(const int fd)
190{
191 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
192}
193
194/*
195 * returns true if the FD is active for send
196 */
197static inline int fd_send_active(const int fd)
198{
199 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100200}
201
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100202/*
203 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100204 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100205static inline int fd_send_ready(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_READY_W;
208}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100209
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100210/*
211 * returns true if the FD is polled for send
212 */
213static inline int fd_send_polled(const int fd)
214{
215 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
216}
217
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200218/*
219 * returns true if the FD is active for recv or send
220 */
221static inline int fd_active(const int fd)
222{
223 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
224}
225
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100226/* Disable processing recv events on fd <fd> */
227static inline void fd_stop_recv(int fd)
228{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100229 unsigned char old, new;
230
231 old = fdtab[fd].state;
232 do {
233 if (!(old & FD_EV_ACTIVE_R))
234 return;
235 new = old & ~FD_EV_ACTIVE_R;
236 new &= ~FD_EV_POLLED_R;
237 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
238
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100239 if ((old ^ new) & FD_EV_POLLED_R)
240 updt_fd_polling(fd);
241
Willy Tarreau4d841862018-01-17 22:57:54 +0100242 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100243 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100244 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100245}
246
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100247/* Disable processing send events on fd <fd> */
248static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100249{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100250 unsigned char old, new;
251
252 old = fdtab[fd].state;
253 do {
254 if (!(old & FD_EV_ACTIVE_W))
255 return;
256 new = old & ~FD_EV_ACTIVE_W;
257 new &= ~FD_EV_POLLED_W;
258 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
259
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100260 if ((old ^ new) & FD_EV_POLLED_W)
261 updt_fd_polling(fd);
262
Willy Tarreau4d841862018-01-17 22:57:54 +0100263 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100264 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100265 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100266}
267
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100268/* Disable processing of events on fd <fd> for both directions. */
269static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200270{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100271 unsigned char old, new;
272
273 old = fdtab[fd].state;
274 do {
275 if (!(old & FD_EV_ACTIVE_RW))
276 return;
277 new = old & ~FD_EV_ACTIVE_RW;
278 new &= ~FD_EV_POLLED_RW;
279 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
280
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100281 if ((old ^ new) & FD_EV_POLLED_RW)
282 updt_fd_polling(fd);
283
Willy Tarreau4d841862018-01-17 22:57:54 +0100284 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100285 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100286 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200287}
288
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100289/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
290static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200291{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100292 unsigned char old, new;
293
294 old = fdtab[fd].state;
295 do {
296 if (!(old & FD_EV_READY_R))
297 return;
298 new = old & ~FD_EV_READY_R;
299 if (new & FD_EV_ACTIVE_R)
300 new |= FD_EV_POLLED_R;
301 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
302
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100303 if ((old ^ new) & FD_EV_POLLED_R)
304 updt_fd_polling(fd);
305
Willy Tarreau4d841862018-01-17 22:57:54 +0100306 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100307 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100308 HA_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{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100314 /* marking ready never changes polled status */
315 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_R);
316
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100317 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100318 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100319 HA_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{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100329 unsigned char old, new;
330
331 old = fdtab[fd].state;
332 do {
333 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
334 return;
335 new = old & ~FD_EV_READY_R;
336 if (new & FD_EV_ACTIVE_R)
337 new |= FD_EV_POLLED_R;
338 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
339
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100340 if ((old ^ new) & FD_EV_POLLED_R)
341 updt_fd_polling(fd);
342
Willy Tarreau4d841862018-01-17 22:57:54 +0100343 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100344 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100345 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100346}
347
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100348/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
349static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200350{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100351 unsigned char old, new;
352
353 old = fdtab[fd].state;
354 do {
355 if (!(old & FD_EV_READY_W))
356 return;
357 new = old & ~FD_EV_READY_W;
358 if (new & FD_EV_ACTIVE_W)
359 new |= FD_EV_POLLED_W;
360 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
361
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100362 if ((old ^ new) & FD_EV_POLLED_W)
363 updt_fd_polling(fd);
364
Willy Tarreau4d841862018-01-17 22:57:54 +0100365 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100366 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100367 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200368}
369
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100370/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
371static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200372{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100373 /* marking ready never changes polled status */
374 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W);
375
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100376 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100377 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100378 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200379}
Willy Tarreau2a429502006-10-15 14:52:29 +0200380
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100381/* Prepare FD <fd> to try to receive */
382static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200383{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100384 unsigned char old, new;
385
386 old = fdtab[fd].state;
387 do {
388 if (old & FD_EV_ACTIVE_R)
389 return;
390 new = old | FD_EV_ACTIVE_R;
391 if (!(new & FD_EV_READY_R))
392 new |= FD_EV_POLLED_R;
393 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
394
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100395 if ((old ^ new) & FD_EV_POLLED_R)
396 updt_fd_polling(fd);
397
Willy Tarreau4d841862018-01-17 22:57:54 +0100398 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100399 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100400 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200401}
402
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100403/* Prepare FD <fd> to try to send */
404static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200405{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100406 unsigned char old, new;
407
408 old = fdtab[fd].state;
409 do {
410 if (old & FD_EV_ACTIVE_W)
411 return;
412 new = old | FD_EV_ACTIVE_W;
413 if (!(new & FD_EV_READY_W))
414 new |= FD_EV_POLLED_W;
415 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
416
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100417 if ((old ^ new) & FD_EV_POLLED_W)
418 updt_fd_polling(fd);
419
Willy Tarreau4d841862018-01-17 22:57:54 +0100420 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100421 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100422 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200423}
Willy Tarreau2a429502006-10-15 14:52:29 +0200424
Christopher Faulet21e92672017-08-30 10:30:04 +0200425/* Update events seen for FD <fd> and its state if needed. This should be called
426 * by the poller to set FD_POLL_* flags. */
427static inline void fd_update_events(int fd, int evts)
428{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100429 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200430 fdtab[fd].ev &= FD_POLL_STICKY;
431 fdtab[fd].ev |= evts;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100432 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200433
434 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
435 fd_may_recv(fd);
436
437 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
438 fd_may_send(fd);
439}
440
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100441/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100442static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200443{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100444 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100445 fdtab[fd].owner = owner;
446 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100447 fdtab[fd].ev = 0;
Willy Tarreauebc78d72018-01-20 23:53:50 +0100448 fdtab[fd].update_mask &= ~tid_bit;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100449 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200450 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100451 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100452 /* note: do not reset polled_mask here as it indicates which poller
453 * still knows this FD from a possible previous round.
454 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100455 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200456}
457
Willy Tarreau322e6c72018-01-25 16:37:04 +0100458/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
459static inline void hap_fd_set(int fd, unsigned int *evts)
460{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100461 HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100462}
463
464static inline void hap_fd_clr(int fd, unsigned int *evts)
465{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100466 HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100467}
468
469static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
470{
471 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
472}
473
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474
475#endif /* _PROTO_FD_H */
476
477/*
478 * Local variables:
479 * c-indent-level: 8
480 * c-basic-offset: 8
481 * End:
482 */