blob: 7fe616e381b3787216aac45e19147130b9838a42 [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 *
5 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
6 *
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 Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/fd.h>
32
Willy Tarreau7be79a42012-11-11 15:02:54 +010033/* public variables */
34extern int fd_nbspec; // number of speculative events in the list
35extern int fd_nbupdt; // number of updates in the list
36extern unsigned int *fd_spec; // speculative I/O list
37extern unsigned int *fd_updt; // FD updates list
38
Willy Tarreaubaaee002006-06-26 02:48:02 +020039/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
40 * The file descriptor is also closed.
41 */
42void fd_delete(int fd);
43
Willy Tarreau4f60f162007-04-08 16:39:58 +020044/* disable the specified poller */
45void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau2a429502006-10-15 14:52:29 +020047/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020048 * Initialize the pollers till the best one is found.
49 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020050 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020051 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020052int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020053
Willy Tarreau4f60f162007-04-08 16:39:58 +020054/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020055 * Deinitialize the pollers.
56 */
57void deinit_pollers();
58
59/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020060 * Some pollers may lose their connection after a fork(). It may be necessary
61 * to create initialize part of them again. Returns 0 in case of failure,
62 * otherwise 1. The fork() function may be NULL if unused. In case of error,
63 * the the current poller is destroyed and the caller is responsible for trying
64 * another one by calling init_pollers() again.
65 */
66int fork_poller();
67
68/*
69 * Lists the known pollers on <out>.
70 * Should be performed only before initialization.
71 */
72int list_pollers(FILE *out);
73
74/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020075 * Runs the polling loop
76 */
77void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +020078
Willy Tarreau09f24562012-11-11 16:43:45 +010079/* Scan and process the speculative events. This should be called right after
80 * the poller.
81 */
82void fd_process_spec_events();
83
Willy Tarreau7be79a42012-11-11 15:02:54 +010084/* Mark fd <fd> as updated and allocate an entry in the update list for this if
85 * it was not already there. This can be done at any time.
86 */
87static inline void updt_fd(const int fd)
88{
89 if (fdtab[fd].updated)
90 /* already scheduled for update */
91 return;
Willy Tarreau7be79a42012-11-11 15:02:54 +010092 fdtab[fd].updated = 1;
Willy Tarreau4a291442012-12-13 23:34:18 +010093 fd_updt[fd_nbupdt++] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +010094}
95
96
97/* allocate an entry for a speculative event. This can be done at any time. */
98static inline void alloc_spec_entry(const int fd)
99{
100 if (fdtab[fd].spec_p)
101 /* FD already in speculative I/O list */
102 return;
Willy Tarreau4a291442012-12-13 23:34:18 +0100103 fd_nbspec++;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100104 fdtab[fd].spec_p = fd_nbspec;
Willy Tarreau4a291442012-12-13 23:34:18 +0100105 fd_spec[fd_nbspec-1] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100106}
107
108/* Removes entry used by fd <fd> from the spec list and replaces it with the
109 * last one. The fdtab.spec is adjusted to match the back reference if needed.
110 * If the fd has no entry assigned, return immediately.
111 */
112static inline void release_spec_entry(int fd)
113{
114 unsigned int pos;
115
116 pos = fdtab[fd].spec_p;
117 if (!pos)
118 return;
119 fdtab[fd].spec_p = 0;
120 fd_nbspec--;
Willy Tarreau4a291442012-12-13 23:34:18 +0100121 if (likely(pos <= fd_nbspec)) {
Willy Tarreau7be79a42012-11-11 15:02:54 +0100122 /* was not the last entry */
123 fd = fd_spec[fd_nbspec];
124 fd_spec[pos - 1] = fd;
125 fdtab[fd].spec_p = pos;
126 }
127}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200128
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100129/*
130 * Returns non-zero if <fd> is already monitored for events in direction <dir>.
131 */
132static inline int fd_ev_is_set(const int fd, int dir)
133{
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100134 return ((unsigned)fdtab[fd].spec_e >> dir) & FD_EV_STATUS;
135}
136
137/* Disable processing of events on fd <fd> for direction <dir>. Note: this
138 * function was optimized to be used with a constant for <dir>.
139 */
140static inline void fd_ev_clr(const int fd, int dir)
141{
142 unsigned int i = ((unsigned int)fdtab[fd].spec_e) & (FD_EV_STATUS << dir);
143 if (i == 0)
144 return; /* already disabled */
145 fdtab[fd].spec_e ^= i;
146 updt_fd(fd); /* need an update entry to change the state */
147}
148
149/* Enable polling for events on fd <fd> for direction <dir>. Note: this
150 * function was optimized to be used with a constant for <dir>.
151 */
152static inline void fd_ev_wai(const int fd, int dir)
153{
154 unsigned int i = ((unsigned int)fdtab[fd].spec_e) & (FD_EV_STATUS << dir);
155 if (i == (FD_EV_POLLED << dir))
156 return; /* already in desired state */
157 fdtab[fd].spec_e ^= i ^ (FD_EV_POLLED << dir);
158 updt_fd(fd); /* need an update entry to change the state */
159}
160
161/* Enable processing of events on fd <fd> for direction <dir>. Note: this
162 * function was optimized to be used with a constant for <dir>.
163 */
164static inline void fd_ev_set(int fd, int dir)
165{
166 unsigned int i = ((unsigned int)fdtab[fd].spec_e) & (FD_EV_STATUS << dir);
167
168 /* note that we don't care about disabling the polled state when
169 * enabling the active state, since it brings no benefit but costs
170 * some syscalls.
171 */
172 if (i & (FD_EV_ACTIVE << dir))
173 return; /* already in desired state */
174 fdtab[fd].spec_e |= (FD_EV_ACTIVE << dir);
175 updt_fd(fd); /* need an update entry to change the state */
176}
177
178/* Disable processing of events on fd <fd> for both directions. */
179static inline void fd_ev_rem(const int fd)
180{
181 unsigned int i = ((unsigned int)fdtab[fd].spec_e) & FD_EV_CURR_MASK;
182 if (i == 0)
183 return; /* already disabled */
184 fdtab[fd].spec_e ^= i;
185 updt_fd(fd); /* need an update entry to change the state */
186}
187
Willy Tarreau49b046d2012-08-09 12:11:58 +0200188/* event manipulation primitives for use by I/O callbacks */
189static inline void fd_want_recv(int fd)
190{
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100191 return fd_ev_set(fd, DIR_RD);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200192}
193
194static inline void fd_stop_recv(int fd)
195{
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100196 return fd_ev_clr(fd, DIR_RD);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200197}
198
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200199static inline void fd_poll_recv(int fd)
200{
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100201 return fd_ev_wai(fd, DIR_RD);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200202}
203
Willy Tarreau49b046d2012-08-09 12:11:58 +0200204static inline void fd_want_send(int fd)
205{
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100206 return fd_ev_set(fd, DIR_WR);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200207}
208
209static inline void fd_stop_send(int fd)
210{
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100211 return fd_ev_clr(fd, DIR_WR);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200212}
Willy Tarreau2a429502006-10-15 14:52:29 +0200213
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200214static inline void fd_poll_send(int fd)
215{
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100216 return fd_ev_wai(fd, DIR_WR);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200217}
218
Willy Tarreau49b046d2012-08-09 12:11:58 +0200219static inline void fd_stop_both(int fd)
220{
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100221 return fd_ev_rem(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200222}
Willy Tarreau2a429502006-10-15 14:52:29 +0200223
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100224/* Prepares <fd> for being polled */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200225static inline void fd_insert(int fd)
226{
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100227 fdtab[fd].ev = 0;
Willy Tarreau037d2c12012-11-06 02:34:46 +0100228 fdtab[fd].new = 1;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100229 fdtab[fd].linger_risk = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200230 if (fd + 1 > maxfd)
231 maxfd = fd + 1;
232}
233
234
235#endif /* _PROTO_FD_H */
236
237/*
238 * Local variables:
239 * c-indent-level: 8
240 * c-basic-offset: 8
241 * End:
242 */