ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_epoll.c
(Generate patch)

Comparing libev/ev_epoll.c (file contents):
Revision 1.41 by root, Wed Oct 29 07:10:16 2008 UTC vs.
Revision 1.50 by root, Tue Mar 23 23:45:12 2010 UTC

1/* 1/*
2 * libev epoll fd activity backend 2 * libev epoll fd activity backend
3 * 3 *
4 * Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without modifica- 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 8 * tion, are permitted provided that the following conditions are met:
9 * 9 *
47 * b) the fact that ADD != MOD creates a lot of extra syscalls due to a) 47 * b) the fact that ADD != MOD creates a lot of extra syscalls due to a)
48 * and seems not to have any advantage. 48 * and seems not to have any advantage.
49 * c) the inability to handle fork or file descriptors (think dup) 49 * c) the inability to handle fork or file descriptors (think dup)
50 * limits the applicability over poll, so this is not a generic 50 * limits the applicability over poll, so this is not a generic
51 * poll replacement. 51 * poll replacement.
52 * d) epoll doesn't work the same as select with many file descriptors
53 * (such as files). while not critical, no other advanced interface
54 * seems to share this (rather non-unixy) limitation.
55 * e) epoll claims to be embeddable, but in practise you never get
56 * a ready event for the epoll fd.
52 * 57 *
53 * lots of "weird code" and complication handling in this file is due 58 * lots of "weird code" and complication handling in this file is due
54 * to these design problems with epoll, as we try very hard to avoid 59 * to these design problems with epoll, as we try very hard to avoid
55 * epoll_ctl syscalls for common usage patterns and handle the breakage 60 * epoll_ctl syscalls for common usage patterns and handle the breakage
56 * ensuing from receiving events for closed and otherwise long gone 61 * ensuing from receiving events for closed and otherwise long gone
68 /* 73 /*
69 * we handle EPOLL_CTL_DEL by ignoring it here 74 * we handle EPOLL_CTL_DEL by ignoring it here
70 * on the assumption that the fd is gone anyways 75 * on the assumption that the fd is gone anyways
71 * if that is wrong, we have to handle the spurious 76 * if that is wrong, we have to handle the spurious
72 * event in epoll_poll. 77 * event in epoll_poll.
73 * the fd is later added, we try to ADD it, and, if that 78 * if the fd is added again, we try to ADD it, and, if that
74 * fails, we assume it still has the same eventmask. 79 * fails, we assume it still has the same eventmask.
75 */ 80 */
76 if (!nev) 81 if (!nev)
77 return; 82 return;
78 83
79 oldmask = anfds [fd].emask; 84 oldmask = anfds [fd].emask;
80 anfds [fd].emask = nev; 85 anfds [fd].emask = nev;
81 86
82 /* store the generation counter in the upper 32 bits */ 87 /* store the generation counter in the upper 32 bits, the fd in the lower 32 bits */
83 ev.data.u64 = (uint64_t)(uint32_t)fd | ((uint64_t)(uint32_t)++anfds [fd].egen << 32); 88 ev.data.u64 = (uint64_t)(uint32_t)fd
89 | ((uint64_t)(uint32_t)++anfds [fd].egen << 32);
84 ev.events = (nev & EV_READ ? EPOLLIN : 0) 90 ev.events = (nev & EV_READ ? EPOLLIN : 0)
85 | (nev & EV_WRITE ? EPOLLOUT : 0); 91 | (nev & EV_WRITE ? EPOLLOUT : 0);
86 92
87 if (expect_true (!epoll_ctl (backend_fd, oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev))) 93 if (expect_true (!epoll_ctl (backend_fd, oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev)))
88 return; 94 return;
116 122
117static void 123static void
118epoll_poll (EV_P_ ev_tstamp timeout) 124epoll_poll (EV_P_ ev_tstamp timeout)
119{ 125{
120 int i; 126 int i;
127 int eventcnt;
128
129 /* epoll wait times cannot be larger than (LONG_MAX - 999UL) / HZ msecs, which is below */
130 /* the default libev max wait time, however. */
131 EV_RELEASE_CB;
121 int eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.)); 132 eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.));
133 EV_ACQUIRE_CB;
122 134
123 if (expect_false (eventcnt < 0)) 135 if (expect_false (eventcnt < 0))
124 { 136 {
125 if (errno != EINTR) 137 if (errno != EINTR)
126 ev_syserr ("(libev) epoll_wait"); 138 ev_syserr ("(libev) epoll_wait");
152 /* we received an event but are not interested in it, try mod or del */ 164 /* we received an event but are not interested in it, try mod or del */
153 /* I don't think we ever need MOD, but let's handle it anyways */ 165 /* I don't think we ever need MOD, but let's handle it anyways */
154 ev->events = (want & EV_READ ? EPOLLIN : 0) 166 ev->events = (want & EV_READ ? EPOLLIN : 0)
155 | (want & EV_WRITE ? EPOLLOUT : 0); 167 | (want & EV_WRITE ? EPOLLOUT : 0);
156 168
169 /* pre-2.6.9 kernels require a non-null pointer with EPOLL_CTL_DEL, */
170 /* which is fortunately easy to do for us. */
157 if (epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev)) 171 if (epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev))
158 { 172 {
159 postfork = 1; /* an error occured, recreate kernel state */ 173 postfork = 1; /* an error occured, recreate kernel state */
160 continue; 174 continue;
161 } 175 }
174} 188}
175 189
176int inline_size 190int inline_size
177epoll_init (EV_P_ int flags) 191epoll_init (EV_P_ int flags)
178{ 192{
193#ifdef EPOLL_CLOEXEC
194 backend_fd = epoll_create1 (EPOLL_CLOEXEC);
195
196 if (backend_fd <= 0)
197#endif
179 backend_fd = epoll_create (256); 198 backend_fd = epoll_create (256);
180 199
181 if (backend_fd < 0) 200 if (backend_fd < 0)
182 return 0; 201 return 0;
183 202
184 fcntl (backend_fd, F_SETFD, FD_CLOEXEC); 203 fcntl (backend_fd, F_SETFD, FD_CLOEXEC);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines