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"
61 int event_count; /* Highest number alloc */
62 int nfds; /* Highest number used */
63 int realloc_copy; /* True iff we must realloc
65 struct pollfd *event_set;
66 struct pollfd *event_set_copy;
69 static void *poll_init(struct event_base *);
70 static int poll_add(struct event_base *, int, short old, short events, void *idx);
71 static int poll_del(struct event_base *, int, short old, short events, void *idx);
72 static int poll_dispatch(struct event_base *, struct timeval *);
73 static void poll_dealloc(struct event_base *);
75 const struct eventop pollops = {
82 0, /* doesn't need_reinit */
84 sizeof(struct pollidx),
88 poll_init(struct event_base *base)
90 struct pollop *pollop;
92 if (!(pollop = mm_calloc(1, sizeof(struct pollop))))
97 evutil_weakrand_seed_(&base->weakrand_seed, 0);
102 #ifdef CHECK_INVARIANTS
104 poll_check_ok(struct pollop *pop)
109 for (i = 0; i < pop->fd_count; ++i) {
110 idx = pop->idxplus1_by_fd[i]-1;
113 EVUTIL_ASSERT(pop->event_set[idx].fd == i);
115 for (i = 0; i < pop->nfds; ++i) {
116 struct pollfd *pfd = &pop->event_set[i];
117 EVUTIL_ASSERT(pop->idxplus1_by_fd[pfd->fd] == i+1);
121 #define poll_check_ok(pop)
125 poll_dispatch(struct event_base *base, struct timeval *tv)
129 struct pollop *pop = base->evbase;
130 struct pollfd *event_set;
136 #ifndef EVENT__DISABLE_THREAD_SUPPORT
137 if (base->th_base_lock) {
138 /* If we're using this backend in a multithreaded setting,
139 * then we need to work on a copy of event_set, so that we can
140 * let other threads modify the main event_set while we're
141 * polling. If we're not multithreaded, then we'll skip the
142 * copy step here to save memory and time. */
143 if (pop->realloc_copy) {
144 struct pollfd *tmp = mm_realloc(pop->event_set_copy,
145 pop->event_count * sizeof(struct pollfd));
147 event_warn("realloc");
150 pop->event_set_copy = tmp;
151 pop->realloc_copy = 0;
153 memcpy(pop->event_set_copy, pop->event_set,
154 sizeof(struct pollfd)*nfds);
155 event_set = pop->event_set_copy;
157 event_set = pop->event_set;
160 event_set = pop->event_set;
164 msec = evutil_tv_to_msec_(tv);
165 if (msec < 0 || msec > INT_MAX)
169 EVBASE_RELEASE_LOCK(base, th_base_lock);
171 res = poll(event_set, nfds, msec);
173 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
176 if (errno != EINTR) {
184 event_debug(("%s: poll reports %d", __func__, res));
186 if (res == 0 || nfds == 0)
189 i = evutil_weakrand_range_(&base->weakrand_seed, nfds);
190 for (j = 0; j < nfds; j++) {
194 what = event_set[i].revents;
200 /* If the file gets closed notify */
201 if (what & (POLLHUP|POLLERR|POLLNVAL))
202 what |= POLLIN|POLLOUT;
210 evmap_io_active_(base, event_set[i].fd, res);
217 poll_add(struct event_base *base, int fd, short old, short events, void *idx_)
219 struct pollop *pop = base->evbase;
220 struct pollfd *pfd = NULL;
221 struct pollidx *idx = idx_;
224 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
225 if (!(events & (EV_READ|EV_WRITE)))
229 if (pop->nfds + 1 >= pop->event_count) {
230 struct pollfd *tmp_event_set;
233 if (pop->event_count < 32)
234 tmp_event_count = 32;
236 tmp_event_count = pop->event_count * 2;
238 /* We need more file descriptors */
239 tmp_event_set = mm_realloc(pop->event_set,
240 tmp_event_count * sizeof(struct pollfd));
241 if (tmp_event_set == NULL) {
242 event_warn("realloc");
245 pop->event_set = tmp_event_set;
247 pop->event_count = tmp_event_count;
248 pop->realloc_copy = 1;
251 i = idx->idxplus1 - 1;
254 pfd = &pop->event_set[i];
257 pfd = &pop->event_set[i];
260 idx->idxplus1 = i + 1;
264 if (events & EV_WRITE)
265 pfd->events |= POLLOUT;
266 if (events & EV_READ)
267 pfd->events |= POLLIN;
274 * Nothing to be done here.
278 poll_del(struct event_base *base, int fd, short old, short events, void *idx_)
280 struct pollop *pop = base->evbase;
281 struct pollfd *pfd = NULL;
282 struct pollidx *idx = idx_;
285 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
286 if (!(events & (EV_READ|EV_WRITE)))
290 i = idx->idxplus1 - 1;
294 /* Do we still want to read or write? */
295 pfd = &pop->event_set[i];
296 if (events & EV_READ)
297 pfd->events &= ~POLLIN;
298 if (events & EV_WRITE)
299 pfd->events &= ~POLLOUT;
302 /* Another event cares about that fd. */
305 /* Okay, so we aren't interested in that fd anymore. */
309 if (i != pop->nfds) {
311 * Shift the last pollfd down into the now-unoccupied
314 memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
315 sizeof(struct pollfd));
316 idx = evmap_io_get_fdinfo_(&base->io, pop->event_set[i].fd);
318 EVUTIL_ASSERT(idx->idxplus1 == pop->nfds + 1);
319 idx->idxplus1 = i + 1;
327 poll_dealloc(struct event_base *base)
329 struct pollop *pop = base->evbase;
331 evsig_dealloc_(base);
333 mm_free(pop->event_set);
334 if (pop->event_set_copy)
335 mm_free(pop->event_set_copy);
337 memset(pop, 0, sizeof(struct pollop));
341 #endif /* EVENT__HAVE_POLL */