blob: c58bf5394969f1509739ec719a341a2bfdfe8be5 [file] [log] [blame]
Willy Tarreaude99e992007-04-16 00:53:59 +02001/*
2 * FD polling functions for Speculative I/O combined with Linux epoll()
3 *
4 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <unistd.h>
14#include <sys/time.h>
15#include <sys/types.h>
16
17#include <common/compat.h>
18#include <common/config.h>
19#include <common/standard.h>
20#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020021#include <common/tools.h>
Willy Tarreaude99e992007-04-16 00:53:59 +020022
23#include <types/fd.h>
24#include <types/global.h>
25
26#include <proto/fd.h>
27#include <proto/task.h>
28
29#if defined(USE_MY_EPOLL)
30#include <common/epoll.h>
31#include <errno.h>
32#include <sys/syscall.h>
33static _syscall1 (int, epoll_create, int, size);
34static _syscall4 (int, epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event *, event);
35static _syscall4 (int, epoll_wait, int, epfd, struct epoll_event *, events, int, maxevents, int, timeout);
36#else
37#include <sys/epoll.h>
38#endif
39
40/*
41 * We define 4 states for each direction of a file descriptor, which we store
42 * as 2 bits :
43 *
44 * 00 = IDLE : we're not interested in this event
45 * 01 = SPEC : perform speculative I/O on this FD
46 * 10 = WAIT : really wait for an availability event on this FD (poll)
47 * 11 = STOP : was marked WAIT, but disabled. It can switch back to WAIT if
48 * the application changes its mind, otherwise disable FD polling
49 * and switch back to IDLE.
50 *
51 * Since we do not want to scan all the FD list to find speculative I/O events,
52 * we store them in a list consisting in a linear array holding only the FD
53 * indexes right now.
54 *
55 * The STOP state requires the event to be present in the spec list so that
56 * it can be detected and flushed upon next scan without having to scan the
57 * whole FD list.
58 *
59 * This translates like this :
60 *
61 * EVENT_IN_SPEC_LIST = 01
62 * EVENT_IN_POLL_LIST = 10
63 *
64 * IDLE = 0
65 * SPEC = (EVENT_IN_SPEC_LIST)
66 * WAIT = (EVENT_IN_POLL_LIST)
67 * STOP = (EVENT_IN_SPEC_LIST|EVENT_IN_POLL_LIST)
68 *
69 * fd_is_set() just consists in checking that the status is 01 or 10.
70 *
71 * For efficiency reasons, we will store the Read and Write bits interlaced to
72 * form a 4-bit field, so that we can simply shift the value right by 0/1 and
73 * get what we want :
74 * 3 2 1 0
75 * Wp Rp Ws Rs
76 *
77 * The FD array has to hold a back reference to the speculative list. This
78 * reference is only valid if at least one of the directions is marked SPEC.
79 *
80 */
81
82#define FD_EV_IN_SL 1
83#define FD_EV_IN_PL 4
84
85#define FD_EV_IDLE 0
86#define FD_EV_SPEC (FD_EV_IN_SL)
87#define FD_EV_WAIT (FD_EV_IN_PL)
88#define FD_EV_STOP (FD_EV_IN_SL|FD_EV_IN_PL)
89
90/* Those match any of R or W for Spec list or Poll list */
91#define FD_EV_RW_SL (FD_EV_IN_SL | (FD_EV_IN_SL << 1))
92#define FD_EV_RW_PL (FD_EV_IN_PL | (FD_EV_IN_PL << 1))
93#define FD_EV_MASK_DIR (FD_EV_IN_SL|FD_EV_IN_PL)
94
95#define FD_EV_IDLE_R 0
96#define FD_EV_SPEC_R (FD_EV_IN_SL)
97#define FD_EV_WAIT_R (FD_EV_IN_PL)
98#define FD_EV_STOP_R (FD_EV_IN_SL|FD_EV_IN_PL)
99#define FD_EV_MASK_R (FD_EV_IN_SL|FD_EV_IN_PL)
100
101#define FD_EV_IDLE_W (FD_EV_IDLE_R << 1)
102#define FD_EV_SPEC_W (FD_EV_SPEC_R << 1)
103#define FD_EV_WAIT_W (FD_EV_WAIT_R << 1)
104#define FD_EV_STOP_W (FD_EV_STOP_R << 1)
105#define FD_EV_MASK_W (FD_EV_MASK_R << 1)
106
107#define FD_EV_MASK (FD_EV_MASK_W | FD_EV_MASK_R)
108
Willy Tarreau6653d172007-05-13 01:52:05 +0200109/* This is the minimum number of events successfully processed in speculative
110 * mode above which we agree to return without checking epoll() (1/2 times).
111 */
112#define MIN_RETURN_EVENTS 25
Willy Tarreaude99e992007-04-16 00:53:59 +0200113
114/* descriptor of one FD.
115 * FIXME: should be a bit field */
116struct fd_status {
117 unsigned int e:4; // read and write events status.
Willy Tarreau4eac2092007-08-31 17:01:18 +0200118 unsigned int s1:28; // Position in spec list+1. 0=not in list. Should be last.
Willy Tarreaude99e992007-04-16 00:53:59 +0200119};
120
121static int nbspec = 0; // current size of the spec list
122
123static struct fd_status *fd_list = NULL; // list of FDs
124static unsigned int *spec_list = NULL; // speculative I/O list
125
126/* private data */
127static struct epoll_event *epoll_events;
128static int epoll_fd;
129
130/* This structure may be used for any purpose. Warning! do not use it in
131 * recursive functions !
132 */
133static struct epoll_event ev;
134
135
136REGPRM1 static void alloc_spec_entry(const int fd)
137{
Willy Tarreau4eac2092007-08-31 17:01:18 +0200138 if (fd_list[fd].s1)
Willy Tarreaude99e992007-04-16 00:53:59 +0200139 return;
Willy Tarreau4eac2092007-08-31 17:01:18 +0200140 fd_list[fd].s1 = nbspec + 1;
141 spec_list[nbspec] = fd;
142 nbspec++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200143}
144
Willy Tarreau4eac2092007-08-31 17:01:18 +0200145/* Removes entry used by fd <fd> from the spec list and replaces it with the
146 * last one. The fd_list is adjusted to match the back reference if needed.
147 * If the fd has no entry assigned, return immediately.
Willy Tarreaude99e992007-04-16 00:53:59 +0200148 */
Willy Tarreau4eac2092007-08-31 17:01:18 +0200149REGPRM1 static void release_spec_entry(int fd)
Willy Tarreaude99e992007-04-16 00:53:59 +0200150{
Willy Tarreau4eac2092007-08-31 17:01:18 +0200151 unsigned int pos;
152
153 pos = fd_list[fd].s1;
154 if (!pos)
155 return;
156
157 fd_list[fd].s1 = 0;
158 pos--;
159 /* we have spec_list[pos]==fd */
Willy Tarreaude99e992007-04-16 00:53:59 +0200160
161 nbspec--;
162 if (pos == nbspec)
163 return;
164
Willy Tarreau4eac2092007-08-31 17:01:18 +0200165 /* we replace current FD by the highest one, which may sometimes be the same */
Willy Tarreaude99e992007-04-16 00:53:59 +0200166 fd = spec_list[nbspec];
Willy Tarreau4eac2092007-08-31 17:01:18 +0200167 fd_list[fd].s1 = pos + 1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200168 spec_list[pos] = fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200169}
170
171/*
172 * Returns non-zero if <fd> is already monitored for events in direction <dir>.
173 */
174REGPRM2 static int __fd_is_set(const int fd, int dir)
175{
176 int ret;
177
178 ret = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
179 return (ret == FD_EV_SPEC || ret == FD_EV_WAIT);
180}
181
182/*
183 * Don't worry about the strange constructs in __fd_set/__fd_clr, they are
184 * designed like this in order to reduce the number of jumps (verified).
185 */
186REGPRM2 static int __fd_set(const int fd, int dir)
187{
188 __label__ switch_state;
189 unsigned int i;
190
191 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
192
193 if (i == FD_EV_IDLE) {
194 // switch to SPEC state and allocate a SPEC entry.
195 alloc_spec_entry(fd);
196 switch_state:
197 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
198 return 1;
199 }
200 else if (i == FD_EV_STOP) {
201 // switch to WAIT state
202 goto switch_state;
203 }
204 else
205 return 0;
206}
207
208REGPRM2 static int __fd_clr(const int fd, int dir)
209{
210 __label__ switch_state;
211 unsigned int i;
212
213 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
214
215 if (i == FD_EV_SPEC) {
216 // switch to IDLE state
217 goto switch_state;
218 }
219 else if (likely(i == FD_EV_WAIT)) {
220 // switch to STOP state
221 /* We will create a queue entry for this one because we want to
222 * process it later in order to merge it with other events on
223 * the same FD.
224 */
225 alloc_spec_entry(fd);
226 switch_state:
227 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
228 return 1;
229 }
230 return 0;
231}
232
Willy Tarreau6653d172007-05-13 01:52:05 +0200233/* normally unused */
Willy Tarreaude99e992007-04-16 00:53:59 +0200234REGPRM1 static void __fd_rem(int fd)
235{
236 __fd_clr(fd, DIR_RD);
237 __fd_clr(fd, DIR_WR);
238}
239
240/*
241 * On valid epoll() implementations, a call to close() automatically removes
242 * the fds. This means that the FD will appear as previously unset.
243 */
244REGPRM1 static void __fd_clo(int fd)
245{
246 if (fd_list[fd].e & FD_EV_RW_SL)
Willy Tarreau4eac2092007-08-31 17:01:18 +0200247 release_spec_entry(fd);
Willy Tarreaude99e992007-04-16 00:53:59 +0200248 fd_list[fd].e &= ~(FD_EV_MASK);
249}
250
Willy Tarreaudc246a72007-05-09 21:57:51 +0200251/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200252 * speculative epoll() poller
253 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200254REGPRM2 static void _do_poll(struct poller *p, struct timeval *exp)
Willy Tarreaude99e992007-04-16 00:53:59 +0200255{
256 static unsigned int last_skipped;
Willy Tarreau6653d172007-05-13 01:52:05 +0200257 int status, eo;
Willy Tarreaude99e992007-04-16 00:53:59 +0200258 int fd, opcode;
259 int count;
260 int spec_idx;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200261 int wait_time;
Willy Tarreaude99e992007-04-16 00:53:59 +0200262
263
264 /* Here we have two options :
Willy Tarreau6653d172007-05-13 01:52:05 +0200265 * - either walk the list forwards and hope to match more events
Willy Tarreaude99e992007-04-16 00:53:59 +0200266 * - or walk it backwards to minimize the number of changes and
267 * to make better use of the cache.
268 * Tests have shown that walking backwards improves perf by 0.2%.
269 */
270
Willy Tarreau6653d172007-05-13 01:52:05 +0200271 status = 0;
Willy Tarreaude99e992007-04-16 00:53:59 +0200272 spec_idx = nbspec;
273 while (likely(spec_idx > 0)) {
274 spec_idx--;
275 fd = spec_list[spec_idx];
Willy Tarreau6653d172007-05-13 01:52:05 +0200276 eo = fd_list[fd].e; /* save old events */
Willy Tarreaude99e992007-04-16 00:53:59 +0200277
Willy Tarreau6653d172007-05-13 01:52:05 +0200278 /*
279 * Process the speculative events.
280 *
281 * Principle: events which are marked FD_EV_SPEC are processed
282 * with their assigned function. If the function returns 0, it
283 * means there is nothing doable without polling first. We will
284 * then convert the event to a pollable one by assigning them
285 * the WAIT status.
Willy Tarreaude99e992007-04-16 00:53:59 +0200286 */
287
Willy Tarreaude99e992007-04-16 00:53:59 +0200288 fdtab[fd].ev = 0;
Willy Tarreau6653d172007-05-13 01:52:05 +0200289 if ((eo & FD_EV_MASK_R) == FD_EV_SPEC_R) {
290 /* The owner is interested in reading from this FD */
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200291 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200292 /* Pretend there is something to read */
Willy Tarreaude99e992007-04-16 00:53:59 +0200293 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau6653d172007-05-13 01:52:05 +0200294 if (!fdtab[fd].cb[DIR_RD].f(fd))
295 fd_list[fd].e ^= (FD_EV_WAIT_R ^ FD_EV_SPEC_R);
296 else
297 status++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200298 }
299 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200300 else if ((eo & FD_EV_MASK_R) == FD_EV_STOP_R) {
301 /* This FD was being polled and is now being removed. */
302 fd_list[fd].e &= ~FD_EV_MASK_R;
303 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200304
Willy Tarreau6653d172007-05-13 01:52:05 +0200305 if ((eo & FD_EV_MASK_W) == FD_EV_SPEC_W) {
306 /* The owner is interested in writing to this FD */
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200307 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200308 /* Pretend there is something to write */
Willy Tarreaude99e992007-04-16 00:53:59 +0200309 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau6653d172007-05-13 01:52:05 +0200310 if (!fdtab[fd].cb[DIR_WR].f(fd))
311 fd_list[fd].e ^= (FD_EV_WAIT_W ^ FD_EV_SPEC_W);
312 else
313 status++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200314 }
315 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200316 else if ((eo & FD_EV_MASK_W) == FD_EV_STOP_W) {
317 /* This FD was being polled and is now being removed. */
318 fd_list[fd].e &= ~FD_EV_MASK_W;
319 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200320
Willy Tarreau6653d172007-05-13 01:52:05 +0200321 /* Now, we will adjust the event in the poll list. Indeed, it
322 * is possible that an event which was previously in the poll
323 * list now goes out, and the opposite is possible too. We can
324 * have opposite changes for READ and WRITE too.
325 */
326
327 if ((eo ^ fd_list[fd].e) & FD_EV_RW_PL) {
328 /* poll status changed*/
329 if ((fd_list[fd].e & FD_EV_RW_PL) == 0) {
330 /* fd removed from poll list */
331 opcode = EPOLL_CTL_DEL;
332 }
333 else if ((eo & FD_EV_RW_PL) == 0) {
334 /* new fd in the poll list */
Willy Tarreaude99e992007-04-16 00:53:59 +0200335 opcode = EPOLL_CTL_ADD;
336 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200337 else {
338 /* fd status changed */
339 opcode = EPOLL_CTL_MOD;
340 }
341
342 /* construct the epoll events based on new state */
343 ev.events = 0;
344 if (fd_list[fd].e & FD_EV_WAIT_R)
345 ev.events |= EPOLLIN;
346
347 if (fd_list[fd].e & FD_EV_WAIT_W)
348 ev.events |= EPOLLOUT;
349
350 ev.data.fd = fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200351 epoll_ctl(epoll_fd, opcode, fd, &ev);
Willy Tarreau6653d172007-05-13 01:52:05 +0200352 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200353
Willy Tarreaude99e992007-04-16 00:53:59 +0200354
Willy Tarreau6653d172007-05-13 01:52:05 +0200355 if (!(fd_list[fd].e & FD_EV_RW_SL)) {
356 /* This fd switched to combinations of either WAIT or
357 * IDLE. It must be removed from the spec list.
358 */
Willy Tarreau4eac2092007-08-31 17:01:18 +0200359 release_spec_entry(fd);
Willy Tarreau6653d172007-05-13 01:52:05 +0200360 continue;
Willy Tarreaude99e992007-04-16 00:53:59 +0200361 }
362 }
363
Willy Tarreau6653d172007-05-13 01:52:05 +0200364 /* It may make sense to immediately return here if there are enough
365 * processed events, without passing through epoll_wait() because we
366 * have exactly done a poll.
367 * Measures have shown a great performance increase if we call the
368 * epoll_wait() only the second time after speculative accesses have
369 * succeeded. This reduces the number of unsucessful calls to
370 * epoll_wait() by a factor of about 3, and the total number of calls
371 * by about 2.
Willy Tarreaude99e992007-04-16 00:53:59 +0200372 */
Willy Tarreau6653d172007-05-13 01:52:05 +0200373
374 if (status >= MIN_RETURN_EVENTS) {
375 /* We have processed at least MIN_RETURN_EVENTS, it's worth
376 * returning now without checking epoll_wait().
377 */
378 if (++last_skipped <= 1) {
Willy Tarreaude99e992007-04-16 00:53:59 +0200379 tv_now(&now);
380 return;
381 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200382 }
383 last_skipped = 0;
384
Willy Tarreau6653d172007-05-13 01:52:05 +0200385 if (nbspec || status) {
386 /* Maybe we have processed some events that we must report, or
387 * maybe we still have events in the spec list, so we must not
388 * wait in epoll() otherwise we will delay their delivery by
389 * the next timeout.
390 */
Willy Tarreaude99e992007-04-16 00:53:59 +0200391 wait_time = 0;
392 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200393 else {
Willy Tarreaubdefc512007-05-14 02:02:04 +0200394 if (tv_iseternity(exp))
Willy Tarreaud825eef2007-05-12 22:35:00 +0200395 wait_time = -1;
Willy Tarreaubdefc512007-05-14 02:02:04 +0200396 else if (tv_isge(&now, exp))
397 wait_time = 0;
398 else
399 wait_time = __tv_ms_elapsed(&now, exp) + 1;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200400 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200401
Willy Tarreau6653d172007-05-13 01:52:05 +0200402 /* now let's wait for real events */
Willy Tarreau1db37712007-06-03 17:16:49 +0200403 fd = MIN(maxfd, global.tune.maxpollevents);
404 status = epoll_wait(epoll_fd, epoll_events, fd, wait_time);
Willy Tarreau6653d172007-05-13 01:52:05 +0200405
Willy Tarreaude99e992007-04-16 00:53:59 +0200406 tv_now(&now);
407
408 for (count = 0; count < status; count++) {
409 int e = epoll_events[count].events;
410 fd = epoll_events[count].data.fd;
411
412 /* it looks complicated but gcc can optimize it away when constants
413 * have same values.
414 */
415 fdtab[fd].ev =
416 ((e & EPOLLIN ) ? FD_POLL_IN : 0) |
417 ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |
418 ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |
419 ((e & EPOLLERR) ? FD_POLL_ERR : 0) |
420 ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
421
422 if ((fd_list[fd].e & FD_EV_MASK_R) == FD_EV_WAIT_R) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200423 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200424 continue;
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200425 if (fdtab[fd].ev & (FD_POLL_RD|FD_POLL_HUP|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200426 fdtab[fd].cb[DIR_RD].f(fd);
427 }
428
429 if ((fd_list[fd].e & FD_EV_MASK_W) == FD_EV_WAIT_W) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200430 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200431 continue;
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200432 if (fdtab[fd].ev & (FD_POLL_WR|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200433 fdtab[fd].cb[DIR_WR].f(fd);
434 }
435 }
436}
437
438/*
439 * Initialization of the speculative epoll() poller.
440 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
441 * disables the poller by setting its pref to 0.
442 */
443REGPRM1 static int _do_init(struct poller *p)
444{
445 __label__ fail_fd_list, fail_spec, fail_ee, fail_fd;
446
447 p->private = NULL;
448
449 epoll_fd = epoll_create(global.maxsock + 1);
450 if (epoll_fd < 0)
451 goto fail_fd;
452
453 epoll_events = (struct epoll_event*)
Willy Tarreau1db37712007-06-03 17:16:49 +0200454 calloc(1, sizeof(struct epoll_event) * global.tune.maxpollevents);
Willy Tarreaude99e992007-04-16 00:53:59 +0200455
456 if (epoll_events == NULL)
457 goto fail_ee;
458
459 if ((spec_list = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL)
460 goto fail_spec;
461
462 fd_list = (struct fd_status *)calloc(1, sizeof(struct fd_status) * global.maxsock);
463 if (fd_list == NULL)
464 goto fail_fd_list;
465
466 return 1;
467
468 fail_fd_list:
469 free(spec_list);
470 fail_spec:
471 free(epoll_events);
472 fail_ee:
473 close(epoll_fd);
474 epoll_fd = 0;
475 fail_fd:
476 p->pref = 0;
477 return 0;
478}
479
480/*
481 * Termination of the speculative epoll() poller.
482 * Memory is released and the poller is marked as unselectable.
483 */
484REGPRM1 static void _do_term(struct poller *p)
485{
486 if (fd_list)
487 free(fd_list);
488 if (spec_list)
489 free(spec_list);
490 if (epoll_events)
491 free(epoll_events);
492
493 close(epoll_fd);
494 epoll_fd = 0;
495
496 fd_list = NULL;
497 spec_list = NULL;
498 epoll_events = NULL;
499
500 p->private = NULL;
501 p->pref = 0;
502}
503
504/*
505 * Check that the poller works.
506 * Returns 1 if OK, otherwise 0.
507 */
508REGPRM1 static int _do_test(struct poller *p)
509{
510 int fd;
511
512 fd = epoll_create(global.maxsock + 1);
513 if (fd < 0)
514 return 0;
515 close(fd);
516 return 1;
517}
518
519/*
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200520 * Recreate the epoll file descriptor after a fork(). Returns 1 if OK,
521 * otherwise 0. It will ensure that all processes will not share their
522 * epoll_fd. Some side effects were encountered because of this, such
523 * as epoll_wait() returning an FD which was previously deleted.
524 */
525REGPRM1 static int _do_fork(struct poller *p)
526{
527 close(epoll_fd);
528 epoll_fd = epoll_create(global.maxsock + 1);
529 if (epoll_fd < 0)
530 return 0;
531 return 1;
532}
533
534/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200535 * It is a constructor, which means that it will automatically be called before
536 * main(). This is GCC-specific but it works at least since 2.95.
537 * Special care must be taken so that it does not need any uninitialized data.
538 */
539__attribute__((constructor))
540static void _do_register(void)
541{
542 struct poller *p;
543
544 if (nbpollers >= MAX_POLLERS)
545 return;
546 p = &pollers[nbpollers++];
547
548 p->name = "sepoll";
549 p->pref = 400;
550 p->private = NULL;
551
552 p->test = _do_test;
553 p->init = _do_init;
554 p->term = _do_term;
555 p->poll = _do_poll;
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200556 p->fork = _do_fork;
Willy Tarreaude99e992007-04-16 00:53:59 +0200557
558 p->is_set = __fd_is_set;
559 p->cond_s = p->set = __fd_set;
560 p->cond_c = p->clr = __fd_clr;
561 p->rem = __fd_rem;
562 p->clo = __fd_clo;
563}
564
565
566/*
567 * Local variables:
568 * c-indent-level: 8
569 * c-basic-offset: 8
570 * End:
571 */