1 /* $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
4 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
5 * Copyright 2007-2012 Niels Provos and Nick Mathewson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "event2/event-config.h"
30 #include "evconfig-private.h"
32 #ifdef EVENT__HAVE_POLL
34 #include <sys/types.h>
35 #ifdef EVENT__HAVE_SYS_TIME_H
38 #include <sys/queue.h>
48 #include "event-internal.h"
49 #include "evsignal-internal.h"
50 #include "log-internal.h"
51 #include "evmap-internal.h"
52 #include "event2/thread.h"
53 #include "evthread-internal.h"
54 #include "time-internal.h"
56 /* Since Linux 2.6.17, poll is able to report about peer half-closed connection
57 using special POLLRDHUP flag on a read event.
59 #if !defined(POLLRDHUP)
61 #define EARLY_CLOSE_IF_HAVE_RDHUP 0
63 #define EARLY_CLOSE_IF_HAVE_RDHUP EV_FEATURE_EARLY_CLOSE
72 int event_count; /* Highest number alloc */
73 int nfds; /* Highest number used */
74 int realloc_copy; /* True iff we must realloc
76 struct pollfd *event_set;
77 struct pollfd *event_set_copy;
80 static void *poll_init(struct event_base *);
81 static int poll_add(struct event_base *, int, short old, short events, void *idx);
82 static int poll_del(struct event_base *, int, short old, short events, void *idx);
83 static int poll_dispatch(struct event_base *, struct timeval *);
84 static void poll_dealloc(struct event_base *);
86 const struct eventop pollops = {
94 EV_FEATURE_FDS|EARLY_CLOSE_IF_HAVE_RDHUP,
95 sizeof(struct pollidx),
99 poll_init(struct event_base *base)
101 struct pollop *pollop;
103 if (!(pollop = mm_calloc(1, sizeof(struct pollop))))
108 evutil_weakrand_seed_(&base->weakrand_seed, 0);
113 #ifdef CHECK_INVARIANTS
115 poll_check_ok(struct pollop *pop)
120 for (i = 0; i < pop->fd_count; ++i) {
121 idx = pop->idxplus1_by_fd[i]-1;
124 EVUTIL_ASSERT(pop->event_set[idx].fd == i);
126 for (i = 0; i < pop->nfds; ++i) {
127 struct pollfd *pfd = &pop->event_set[i];
128 EVUTIL_ASSERT(pop->idxplus1_by_fd[pfd->fd] == i+1);
132 #define poll_check_ok(pop)
136 poll_dispatch(struct event_base *base, struct timeval *tv)
140 struct pollop *pop = base->evbase;
141 struct pollfd *event_set;
147 #ifndef EVENT__DISABLE_THREAD_SUPPORT
148 if (base->th_base_lock) {
149 /* If we're using this backend in a multithreaded setting,
150 * then we need to work on a copy of event_set, so that we can
151 * let other threads modify the main event_set while we're
152 * polling. If we're not multithreaded, then we'll skip the
153 * copy step here to save memory and time. */
154 if (pop->realloc_copy) {
155 struct pollfd *tmp = mm_realloc(pop->event_set_copy,
156 pop->event_count * sizeof(struct pollfd));
158 event_warn("realloc");
161 pop->event_set_copy = tmp;
162 pop->realloc_copy = 0;
164 memcpy(pop->event_set_copy, pop->event_set,
165 sizeof(struct pollfd)*nfds);
166 event_set = pop->event_set_copy;
168 event_set = pop->event_set;
171 event_set = pop->event_set;
175 msec = evutil_tv_to_msec_(tv);
176 if (msec < 0 || msec > INT_MAX)
180 EVBASE_RELEASE_LOCK(base, th_base_lock);
182 res = poll(event_set, nfds, msec);
184 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
187 if (errno != EINTR) {
195 event_debug(("%s: poll reports %d", __func__, res));
197 if (res == 0 || nfds == 0)
200 i = evutil_weakrand_range_(&base->weakrand_seed, nfds);
201 for (j = 0; j < nfds; j++) {
205 what = event_set[i].revents;
211 /* If the file gets closed notify */
212 if (what & (POLLHUP|POLLERR|POLLNVAL))
213 what |= POLLIN|POLLOUT;
218 if (what & POLLRDHUP)
223 evmap_io_active_(base, event_set[i].fd, res);
230 poll_add(struct event_base *base, int fd, short old, short events, void *idx_)
232 struct pollop *pop = base->evbase;
233 struct pollfd *pfd = NULL;
234 struct pollidx *idx = idx_;
237 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
238 if (!(events & (EV_READ|EV_WRITE|EV_CLOSED)))
242 if (pop->nfds + 1 >= pop->event_count) {
243 struct pollfd *tmp_event_set;
246 if (pop->event_count < 32)
247 tmp_event_count = 32;
249 tmp_event_count = pop->event_count * 2;
251 /* We need more file descriptors */
252 tmp_event_set = mm_realloc(pop->event_set,
253 tmp_event_count * sizeof(struct pollfd));
254 if (tmp_event_set == NULL) {
255 event_warn("realloc");
258 pop->event_set = tmp_event_set;
260 pop->event_count = tmp_event_count;
261 pop->realloc_copy = 1;
264 i = idx->idxplus1 - 1;
267 pfd = &pop->event_set[i];
270 pfd = &pop->event_set[i];
273 idx->idxplus1 = i + 1;
277 if (events & EV_WRITE)
278 pfd->events |= POLLOUT;
279 if (events & EV_READ)
280 pfd->events |= POLLIN;
281 if (events & EV_CLOSED)
282 pfd->events |= POLLRDHUP;
289 * Nothing to be done here.
293 poll_del(struct event_base *base, int fd, short old, short events, void *idx_)
295 struct pollop *pop = base->evbase;
296 struct pollfd *pfd = NULL;
297 struct pollidx *idx = idx_;
300 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
301 if (!(events & (EV_READ|EV_WRITE|EV_CLOSED)))
305 i = idx->idxplus1 - 1;
309 /* Do we still want to read or write? */
310 pfd = &pop->event_set[i];
311 if (events & EV_READ)
312 pfd->events &= ~POLLIN;
313 if (events & EV_WRITE)
314 pfd->events &= ~POLLOUT;
315 if (events & EV_CLOSED)
316 pfd->events &= ~POLLRDHUP;
319 /* Another event cares about that fd. */
322 /* Okay, so we aren't interested in that fd anymore. */
326 if (i != pop->nfds) {
328 * Shift the last pollfd down into the now-unoccupied
331 memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
332 sizeof(struct pollfd));
333 idx = evmap_io_get_fdinfo_(&base->io, pop->event_set[i].fd);
335 EVUTIL_ASSERT(idx->idxplus1 == pop->nfds + 1);
336 idx->idxplus1 = i + 1;
344 poll_dealloc(struct event_base *base)
346 struct pollop *pop = base->evbase;
348 evsig_dealloc_(base);
350 mm_free(pop->event_set);
351 if (pop->event_set_copy)
352 mm_free(pop->event_set_copy);
354 memset(pop, 0, sizeof(struct pollop));
358 #endif /* EVENT__HAVE_POLL */