blob: 81aea87bc8f0db2a0cb6ec5ccc05f80461bdcde1 [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>
Willy Tarreauf37ba942018-10-17 11:25:54 +020031#include <common/ticks.h>
32#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <types/fd.h>
Willy Tarreau609aad92018-11-22 08:31:09 +010034#include <proto/activity.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreau7be79a42012-11-11 15:02:54 +010036/* public variables */
Christopher Fauletd4604ad2017-05-29 10:40:41 +020037
Olivier Houchard4815c8c2018-01-24 18:17:56 +010038extern volatile struct fdlist fd_cache;
39extern volatile struct fdlist fd_cache_local[MAX_THREADS];
40
Olivier Houchard6b96f722018-04-25 16:58:25 +020041extern volatile struct fdlist update_list;
42
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020043extern unsigned long *polled_mask;
44
Christopher Faulet69553fe2018-01-15 11:57:03 +010045extern unsigned long fd_cache_mask; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +020046
47extern THREAD_LOCAL int *fd_updt; // FD updates list
48extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
49
Olivier Houchard79321b92018-07-26 17:55:11 +020050extern int poller_wr_pipe[MAX_THREADS];
51
Willy Tarreau8b949692017-11-26 11:07:34 +010052__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 +010053
Willy Tarreau173d9952018-01-26 21:48:23 +010054/* Deletes an FD from the fdsets.
Willy Tarreaubaaee002006-06-26 02:48:02 +020055 * The file descriptor is also closed.
56 */
57void fd_delete(int fd);
58
Willy Tarreau173d9952018-01-26 21:48:23 +010059/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +020060 * The file descriptor is kept open.
61 */
62void fd_remove(int fd);
63
Willy Tarreau4f60f162007-04-08 16:39:58 +020064/* disable the specified poller */
65void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020066
Olivier Houchard79321b92018-07-26 17:55:11 +020067void poller_pipe_io_handler(int fd);
68
Willy Tarreau2a429502006-10-15 14:52:29 +020069/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020070 * Initialize the pollers till the best one is found.
71 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020072 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020073 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020074int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020075
Willy Tarreau4f60f162007-04-08 16:39:58 +020076/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020077 * Deinitialize the pollers.
78 */
79void deinit_pollers();
80
81/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020082 * Some pollers may lose their connection after a fork(). It may be necessary
83 * to create initialize part of them again. Returns 0 in case of failure,
84 * otherwise 1. The fork() function may be NULL if unused. In case of error,
85 * the the current poller is destroyed and the caller is responsible for trying
86 * another one by calling init_pollers() again.
87 */
88int fork_poller();
89
90/*
91 * Lists the known pollers on <out>.
92 * Should be performed only before initialization.
93 */
94int list_pollers(FILE *out);
95
96/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020097 * Runs the polling loop
98 */
99void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +0200100
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100101/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +0100102 * the poller.
103 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100104void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +0100105
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200106void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off);
107void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100108
Willy Tarreau5be2f352014-11-19 19:43:05 +0100109/* Mark fd <fd> as updated for polling and allocate an entry in the update list
110 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +0100111 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100112static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100113{
Olivier Houchard6aab7372018-08-17 13:37:59 +0200114 if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) {
Olivier Houchard6b96f722018-04-25 16:58:25 +0200115 unsigned int oldupdt;
116
117 /* note: we don't have a test-and-set yet in hathreads */
Willy Tarreau4d841862018-01-17 22:57:54 +0100118
Olivier Houchard6b96f722018-04-25 16:58:25 +0200119 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
120 return;
121
122 oldupdt = HA_ATOMIC_ADD(&fd_nbupdt, 1) - 1;
123 fd_updt[oldupdt] = fd;
124 } else {
125 unsigned long update_mask = fdtab[fd].update_mask;
126 do {
127 if (update_mask == fdtab[fd].thread_mask)
128 return;
129 } while (!HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask,
130 fdtab[fd].thread_mask));
131 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
132 }
133
134}
Willy Tarreau4d841862018-01-17 22:57:54 +0100135
Olivier Houchard6b96f722018-04-25 16:58:25 +0200136/* Called from the poller to acknoledge we read an entry from the global
137 * update list, to remove our bit from the update_mask, and remove it from
138 * the list if we were the last one.
139 */
140static inline void done_update_polling(int fd)
141{
142 unsigned long update_mask;
143
144 update_mask = HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
145 while ((update_mask & all_threads_mask)== 0) {
146 /* If we were the last one that had to update that entry, remove it from the list */
147 fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update));
148 if (update_list.first == fd)
149 abort();
150 update_mask = (volatile unsigned long)fdtab[fd].update_mask;
151 if ((update_mask & all_threads_mask) != 0) {
152 /* Maybe it's been re-updated in the meanwhile, and we
153 * wrongly removed it from the list, if so, re-add it
154 */
155 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
156 update_mask = (volatile unsigned long)(fdtab[fd].update_mask);
157 /* And then check again, just in case after all it
158 * should be removed, even if it's very unlikely, given
159 * the current thread wouldn't have been able to take
160 * care of it yet */
161 } else
162 break;
Willy Tarreau4d841862018-01-17 22:57:54 +0100163
Olivier Houchard6b96f722018-04-25 16:58:25 +0200164 }
Willy Tarreau7be79a42012-11-11 15:02:54 +0100165}
166
Willy Tarreau899d9572014-01-25 19:20:35 +0100167/* Allocates a cache entry for a file descriptor if it does not yet have one.
168 * This can be done at any time.
169 */
170static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100171{
Willy Tarreau26fb5d82018-03-20 19:06:52 +0100172 HA_ATOMIC_OR(&fd_cache_mask, fdtab[fd].thread_mask);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100173 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200174 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 +0100175 else
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200176 fd_add_to_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache));
Willy Tarreau7be79a42012-11-11 15:02:54 +0100177}
178
Willy Tarreau899d9572014-01-25 19:20:35 +0100179/* Removes entry used by fd <fd> from the FD cache and replaces it with the
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100180 * last one.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100181 * If the fd has no entry assigned, return immediately.
182 */
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100183static inline void fd_release_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100184{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100185 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200186 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 +0100187 else
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200188 fd_rm_from_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache));
Willy Tarreau7be79a42012-11-11 15:02:54 +0100189}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200190
Willy Tarreau5be2f352014-11-19 19:43:05 +0100191/* This function automatically enables/disables caching for an entry depending
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100192 * on its state. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100193 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100194static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100195{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100196 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
197 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
198 ((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 +0100199 fd_alloc_cache_entry(fd);
200 }
201 else {
202 fd_release_cache_entry(fd);
203 }
204}
205
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100206/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100207 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100208 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100209static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100210{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100211 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100212}
213
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100214/*
215 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100216 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100217static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100218{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100219 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100220}
221
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100222/*
223 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100224 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100225static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100226{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100227 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
228}
229
230/*
231 * returns true if the FD is polled for recv
232 */
233static inline int fd_recv_polled(const int fd)
234{
235 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
236}
237
238/*
239 * returns the FD's send state (FD_EV_*)
240 */
241static inline int fd_send_state(const int fd)
242{
243 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
244}
245
246/*
247 * returns true if the FD is active for send
248 */
249static inline int fd_send_active(const int fd)
250{
251 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100252}
253
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100254/*
255 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100256 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100257static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100258{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100259 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
260}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100261
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100262/*
263 * returns true if the FD is polled for send
264 */
265static inline int fd_send_polled(const int fd)
266{
267 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
268}
269
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200270/*
271 * returns true if the FD is active for recv or send
272 */
273static inline int fd_active(const int fd)
274{
275 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
276}
277
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100278/* Disable processing recv events on fd <fd> */
279static inline void fd_stop_recv(int fd)
280{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100281 unsigned char old, new;
282
283 old = fdtab[fd].state;
284 do {
285 if (!(old & FD_EV_ACTIVE_R))
286 return;
287 new = old & ~FD_EV_ACTIVE_R;
288 new &= ~FD_EV_POLLED_R;
289 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
290
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100291 if ((old ^ new) & FD_EV_POLLED_R)
292 updt_fd_polling(fd);
293
Willy Tarreau87d54a92018-10-15 09:44:46 +0200294 if (atleast2(fdtab[fd].thread_mask))
295 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100296 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200297 if (atleast2(fdtab[fd].thread_mask))
298 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100299}
300
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100301/* Disable processing send events on fd <fd> */
302static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100303{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100304 unsigned char old, new;
305
306 old = fdtab[fd].state;
307 do {
308 if (!(old & FD_EV_ACTIVE_W))
309 return;
310 new = old & ~FD_EV_ACTIVE_W;
311 new &= ~FD_EV_POLLED_W;
312 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
313
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100314 if ((old ^ new) & FD_EV_POLLED_W)
315 updt_fd_polling(fd);
316
Willy Tarreau87d54a92018-10-15 09:44:46 +0200317 if (atleast2(fdtab[fd].thread_mask))
318 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100319 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200320 if (atleast2(fdtab[fd].thread_mask))
321 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100322}
323
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100324/* Disable processing of events on fd <fd> for both directions. */
325static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200326{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100327 unsigned char old, new;
328
329 old = fdtab[fd].state;
330 do {
331 if (!(old & FD_EV_ACTIVE_RW))
332 return;
333 new = old & ~FD_EV_ACTIVE_RW;
334 new &= ~FD_EV_POLLED_RW;
335 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
336
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100337 if ((old ^ new) & FD_EV_POLLED_RW)
338 updt_fd_polling(fd);
339
Willy Tarreau87d54a92018-10-15 09:44:46 +0200340 if (atleast2(fdtab[fd].thread_mask))
341 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100342 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200343 if (atleast2(fdtab[fd].thread_mask))
344 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200345}
346
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100347/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
348static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200349{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100350 unsigned char old, new;
351
352 old = fdtab[fd].state;
353 do {
354 if (!(old & FD_EV_READY_R))
355 return;
356 new = old & ~FD_EV_READY_R;
357 if (new & FD_EV_ACTIVE_R)
358 new |= FD_EV_POLLED_R;
359 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
360
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100361 if ((old ^ new) & FD_EV_POLLED_R)
362 updt_fd_polling(fd);
363
Willy Tarreau87d54a92018-10-15 09:44:46 +0200364 if (atleast2(fdtab[fd].thread_mask))
365 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 */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200367 if (atleast2(fdtab[fd].thread_mask))
368 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200369}
370
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100371/* Report that FD <fd> can receive anymore without polling. */
372static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200373{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100374 /* marking ready never changes polled status */
375 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_R);
376
Willy Tarreau87d54a92018-10-15 09:44:46 +0200377 if (atleast2(fdtab[fd].thread_mask))
378 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100379 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200380 if (atleast2(fdtab[fd].thread_mask))
381 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200382}
383
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100384/* Disable readiness when polled. This is useful to interrupt reading when it
385 * is suspected that the end of data might have been reached (eg: short read).
386 * This can only be done using level-triggered pollers, so if any edge-triggered
387 * is ever implemented, a test will have to be added here.
388 */
389static inline void fd_done_recv(const int fd)
390{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100391 unsigned char old, new;
392
393 old = fdtab[fd].state;
394 do {
395 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
396 return;
397 new = old & ~FD_EV_READY_R;
398 if (new & FD_EV_ACTIVE_R)
399 new |= FD_EV_POLLED_R;
400 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
401
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100402 if ((old ^ new) & FD_EV_POLLED_R)
403 updt_fd_polling(fd);
404
Willy Tarreau87d54a92018-10-15 09:44:46 +0200405 if (atleast2(fdtab[fd].thread_mask))
406 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100407 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200408 if (atleast2(fdtab[fd].thread_mask))
409 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100410}
411
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100412/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
413static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200414{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100415 unsigned char old, new;
416
417 old = fdtab[fd].state;
418 do {
419 if (!(old & FD_EV_READY_W))
420 return;
421 new = old & ~FD_EV_READY_W;
422 if (new & FD_EV_ACTIVE_W)
423 new |= FD_EV_POLLED_W;
424 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
425
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100426 if ((old ^ new) & FD_EV_POLLED_W)
427 updt_fd_polling(fd);
428
Willy Tarreau87d54a92018-10-15 09:44:46 +0200429 if (atleast2(fdtab[fd].thread_mask))
430 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100431 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200432 if (atleast2(fdtab[fd].thread_mask))
433 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200434}
435
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100436/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
437static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200438{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100439 /* marking ready never changes polled status */
440 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W);
441
Willy Tarreau87d54a92018-10-15 09:44:46 +0200442 if (atleast2(fdtab[fd].thread_mask))
443 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100444 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200445 if (atleast2(fdtab[fd].thread_mask))
446 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200447}
Willy Tarreau2a429502006-10-15 14:52:29 +0200448
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100449/* Prepare FD <fd> to try to receive */
450static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +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_R)
457 return;
458 new = old | FD_EV_ACTIVE_R;
459 if (!(new & FD_EV_READY_R))
460 new |= FD_EV_POLLED_R;
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_R)
464 updt_fd_polling(fd);
465
Willy Tarreau87d54a92018-10-15 09:44:46 +0200466 if (atleast2(fdtab[fd].thread_mask))
467 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100468 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200469 if (atleast2(fdtab[fd].thread_mask))
470 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200471}
472
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100473/* Prepare FD <fd> to try to send */
474static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200475{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100476 unsigned char old, new;
477
478 old = fdtab[fd].state;
479 do {
480 if (old & FD_EV_ACTIVE_W)
481 return;
482 new = old | FD_EV_ACTIVE_W;
483 if (!(new & FD_EV_READY_W))
484 new |= FD_EV_POLLED_W;
485 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
486
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100487 if ((old ^ new) & FD_EV_POLLED_W)
488 updt_fd_polling(fd);
489
Willy Tarreau87d54a92018-10-15 09:44:46 +0200490 if (atleast2(fdtab[fd].thread_mask))
491 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100492 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200493 if (atleast2(fdtab[fd].thread_mask))
494 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200495}
Willy Tarreau2a429502006-10-15 14:52:29 +0200496
Christopher Faulet21e92672017-08-30 10:30:04 +0200497/* Update events seen for FD <fd> and its state if needed. This should be called
498 * by the poller to set FD_POLL_* flags. */
499static inline void fd_update_events(int fd, int evts)
500{
Willy Tarreau87d54a92018-10-15 09:44:46 +0200501 if (atleast2(fdtab[fd].thread_mask))
502 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200503 fdtab[fd].ev &= FD_POLL_STICKY;
504 fdtab[fd].ev |= evts;
Willy Tarreau87d54a92018-10-15 09:44:46 +0200505 if (atleast2(fdtab[fd].thread_mask))
506 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200507
508 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
509 fd_may_recv(fd);
510
511 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
512 fd_may_send(fd);
513}
514
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100515/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100516static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200517{
Willy Tarreau87d54a92018-10-15 09:44:46 +0200518 if (atleast2(thread_mask))
519 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100520 fdtab[fd].owner = owner;
521 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100522 fdtab[fd].ev = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100523 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200524 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100525 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100526 /* note: do not reset polled_mask here as it indicates which poller
527 * still knows this FD from a possible previous round.
528 */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200529 if (atleast2(thread_mask))
530 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531}
532
Willy Tarreauf37ba942018-10-17 11:25:54 +0200533/* Computes the bounded poll() timeout based on the next expiration timer <next>
534 * by bounding it to MAX_DELAY_MS. <next> may equal TICK_ETERNITY. The pollers
535 * just needs to call this function right before polling to get their timeout
536 * value. Timeouts that are already expired (possibly due to a pending event)
537 * are accounted for in activity.poll_exp.
538 */
539static inline int compute_poll_timeout(int next)
540{
541 int wait_time;
542
543 if (!tick_isset(next))
544 wait_time = MAX_DELAY_MS;
545 else if (tick_is_expired(next, now_ms)) {
546 activity[tid].poll_exp++;
547 wait_time = 0;
548 }
549 else {
550 wait_time = TICKS_TO_MS(tick_remain(now_ms, next)) + 1;
551 if (wait_time > MAX_DELAY_MS)
552 wait_time = MAX_DELAY_MS;
553 }
554 return wait_time;
555}
556
Willy Tarreau322e6c72018-01-25 16:37:04 +0100557/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
558static inline void hap_fd_set(int fd, unsigned int *evts)
559{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100560 HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100561}
562
563static inline void hap_fd_clr(int fd, unsigned int *evts)
564{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100565 HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100566}
567
568static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
569{
570 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
571}
572
Olivier Houchard79321b92018-07-26 17:55:11 +0200573static inline void wake_thread(int tid)
574{
575 char c = 'c';
576
577 shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1));
578}
579
Willy Tarreaubaaee002006-06-26 02:48:02 +0200580
581#endif /* _PROTO_FD_H */
582
583/*
584 * Local variables:
585 * c-indent-level: 8
586 * c-basic-offset: 8
587 * End:
588 */