ipc/chromium/src/third_party/libevent/poll.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
michael@0 2
michael@0 3 /*
michael@0 4 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
michael@0 5 * Copyright 2007-2012 Niels Provos and Nick Mathewson
michael@0 6 *
michael@0 7 * Redistribution and use in source and binary forms, with or without
michael@0 8 * modification, are permitted provided that the following conditions
michael@0 9 * are met:
michael@0 10 * 1. Redistributions of source code must retain the above copyright
michael@0 11 * notice, this list of conditions and the following disclaimer.
michael@0 12 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 13 * notice, this list of conditions and the following disclaimer in the
michael@0 14 * documentation and/or other materials provided with the distribution.
michael@0 15 * 3. The name of the author may not be used to endorse or promote products
michael@0 16 * derived from this software without specific prior written permission.
michael@0 17 *
michael@0 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
michael@0 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
michael@0 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
michael@0 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
michael@0 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
michael@0 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
michael@0 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 28 */
michael@0 29 #include "event2/event-config.h"
michael@0 30
michael@0 31 #include <sys/types.h>
michael@0 32 #ifdef _EVENT_HAVE_SYS_TIME_H
michael@0 33 #include <sys/time.h>
michael@0 34 #endif
michael@0 35 #include <sys/queue.h>
michael@0 36 #include <poll.h>
michael@0 37 #include <signal.h>
michael@0 38 #include <limits.h>
michael@0 39 #include <stdio.h>
michael@0 40 #include <stdlib.h>
michael@0 41 #include <string.h>
michael@0 42 #include <unistd.h>
michael@0 43 #include <errno.h>
michael@0 44
michael@0 45 #include "event-internal.h"
michael@0 46 #include "evsignal-internal.h"
michael@0 47 #include "log-internal.h"
michael@0 48 #include "evmap-internal.h"
michael@0 49 #include "event2/thread.h"
michael@0 50 #include "evthread-internal.h"
michael@0 51
michael@0 52 struct pollidx {
michael@0 53 int idxplus1;
michael@0 54 };
michael@0 55
michael@0 56 struct pollop {
michael@0 57 int event_count; /* Highest number alloc */
michael@0 58 int nfds; /* Highest number used */
michael@0 59 int realloc_copy; /* True iff we must realloc
michael@0 60 * event_set_copy */
michael@0 61 struct pollfd *event_set;
michael@0 62 struct pollfd *event_set_copy;
michael@0 63 };
michael@0 64
michael@0 65 static void *poll_init(struct event_base *);
michael@0 66 static int poll_add(struct event_base *, int, short old, short events, void *_idx);
michael@0 67 static int poll_del(struct event_base *, int, short old, short events, void *_idx);
michael@0 68 static int poll_dispatch(struct event_base *, struct timeval *);
michael@0 69 static void poll_dealloc(struct event_base *);
michael@0 70
michael@0 71 const struct eventop pollops = {
michael@0 72 "poll",
michael@0 73 poll_init,
michael@0 74 poll_add,
michael@0 75 poll_del,
michael@0 76 poll_dispatch,
michael@0 77 poll_dealloc,
michael@0 78 0, /* doesn't need_reinit */
michael@0 79 EV_FEATURE_FDS,
michael@0 80 sizeof(struct pollidx),
michael@0 81 };
michael@0 82
michael@0 83 static void *
michael@0 84 poll_init(struct event_base *base)
michael@0 85 {
michael@0 86 struct pollop *pollop;
michael@0 87
michael@0 88 if (!(pollop = mm_calloc(1, sizeof(struct pollop))))
michael@0 89 return (NULL);
michael@0 90
michael@0 91 evsig_init(base);
michael@0 92
michael@0 93 return (pollop);
michael@0 94 }
michael@0 95
michael@0 96 #ifdef CHECK_INVARIANTS
michael@0 97 static void
michael@0 98 poll_check_ok(struct pollop *pop)
michael@0 99 {
michael@0 100 int i, idx;
michael@0 101 struct event *ev;
michael@0 102
michael@0 103 for (i = 0; i < pop->fd_count; ++i) {
michael@0 104 idx = pop->idxplus1_by_fd[i]-1;
michael@0 105 if (idx < 0)
michael@0 106 continue;
michael@0 107 EVUTIL_ASSERT(pop->event_set[idx].fd == i);
michael@0 108 }
michael@0 109 for (i = 0; i < pop->nfds; ++i) {
michael@0 110 struct pollfd *pfd = &pop->event_set[i];
michael@0 111 EVUTIL_ASSERT(pop->idxplus1_by_fd[pfd->fd] == i+1);
michael@0 112 }
michael@0 113 }
michael@0 114 #else
michael@0 115 #define poll_check_ok(pop)
michael@0 116 #endif
michael@0 117
michael@0 118 static int
michael@0 119 poll_dispatch(struct event_base *base, struct timeval *tv)
michael@0 120 {
michael@0 121 int res, i, j, nfds;
michael@0 122 long msec = -1;
michael@0 123 struct pollop *pop = base->evbase;
michael@0 124 struct pollfd *event_set;
michael@0 125
michael@0 126 poll_check_ok(pop);
michael@0 127
michael@0 128 nfds = pop->nfds;
michael@0 129
michael@0 130 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
michael@0 131 if (base->th_base_lock) {
michael@0 132 /* If we're using this backend in a multithreaded setting,
michael@0 133 * then we need to work on a copy of event_set, so that we can
michael@0 134 * let other threads modify the main event_set while we're
michael@0 135 * polling. If we're not multithreaded, then we'll skip the
michael@0 136 * copy step here to save memory and time. */
michael@0 137 if (pop->realloc_copy) {
michael@0 138 struct pollfd *tmp = mm_realloc(pop->event_set_copy,
michael@0 139 pop->event_count * sizeof(struct pollfd));
michael@0 140 if (tmp == NULL) {
michael@0 141 event_warn("realloc");
michael@0 142 return -1;
michael@0 143 }
michael@0 144 pop->event_set_copy = tmp;
michael@0 145 pop->realloc_copy = 0;
michael@0 146 }
michael@0 147 memcpy(pop->event_set_copy, pop->event_set,
michael@0 148 sizeof(struct pollfd)*nfds);
michael@0 149 event_set = pop->event_set_copy;
michael@0 150 } else {
michael@0 151 event_set = pop->event_set;
michael@0 152 }
michael@0 153 #else
michael@0 154 event_set = pop->event_set;
michael@0 155 #endif
michael@0 156
michael@0 157 if (tv != NULL) {
michael@0 158 msec = evutil_tv_to_msec(tv);
michael@0 159 if (msec < 0 || msec > INT_MAX)
michael@0 160 msec = INT_MAX;
michael@0 161 }
michael@0 162
michael@0 163 EVBASE_RELEASE_LOCK(base, th_base_lock);
michael@0 164
michael@0 165 res = poll(event_set, nfds, msec);
michael@0 166
michael@0 167 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
michael@0 168
michael@0 169 if (res == -1) {
michael@0 170 if (errno != EINTR) {
michael@0 171 event_warn("poll");
michael@0 172 return (-1);
michael@0 173 }
michael@0 174
michael@0 175 return (0);
michael@0 176 }
michael@0 177
michael@0 178 event_debug(("%s: poll reports %d", __func__, res));
michael@0 179
michael@0 180 if (res == 0 || nfds == 0)
michael@0 181 return (0);
michael@0 182
michael@0 183 i = random() % nfds;
michael@0 184 for (j = 0; j < nfds; j++) {
michael@0 185 int what;
michael@0 186 if (++i == nfds)
michael@0 187 i = 0;
michael@0 188 what = event_set[i].revents;
michael@0 189 if (!what)
michael@0 190 continue;
michael@0 191
michael@0 192 res = 0;
michael@0 193
michael@0 194 /* If the file gets closed notify */
michael@0 195 if (what & (POLLHUP|POLLERR))
michael@0 196 what |= POLLIN|POLLOUT;
michael@0 197 if (what & POLLIN)
michael@0 198 res |= EV_READ;
michael@0 199 if (what & POLLOUT)
michael@0 200 res |= EV_WRITE;
michael@0 201 if (res == 0)
michael@0 202 continue;
michael@0 203
michael@0 204 evmap_io_active(base, event_set[i].fd, res);
michael@0 205 }
michael@0 206
michael@0 207 return (0);
michael@0 208 }
michael@0 209
michael@0 210 static int
michael@0 211 poll_add(struct event_base *base, int fd, short old, short events, void *_idx)
michael@0 212 {
michael@0 213 struct pollop *pop = base->evbase;
michael@0 214 struct pollfd *pfd = NULL;
michael@0 215 struct pollidx *idx = _idx;
michael@0 216 int i;
michael@0 217
michael@0 218 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
michael@0 219 if (!(events & (EV_READ|EV_WRITE)))
michael@0 220 return (0);
michael@0 221
michael@0 222 poll_check_ok(pop);
michael@0 223 if (pop->nfds + 1 >= pop->event_count) {
michael@0 224 struct pollfd *tmp_event_set;
michael@0 225 int tmp_event_count;
michael@0 226
michael@0 227 if (pop->event_count < 32)
michael@0 228 tmp_event_count = 32;
michael@0 229 else
michael@0 230 tmp_event_count = pop->event_count * 2;
michael@0 231
michael@0 232 /* We need more file descriptors */
michael@0 233 tmp_event_set = mm_realloc(pop->event_set,
michael@0 234 tmp_event_count * sizeof(struct pollfd));
michael@0 235 if (tmp_event_set == NULL) {
michael@0 236 event_warn("realloc");
michael@0 237 return (-1);
michael@0 238 }
michael@0 239 pop->event_set = tmp_event_set;
michael@0 240
michael@0 241 pop->event_count = tmp_event_count;
michael@0 242 pop->realloc_copy = 1;
michael@0 243 }
michael@0 244
michael@0 245 i = idx->idxplus1 - 1;
michael@0 246
michael@0 247 if (i >= 0) {
michael@0 248 pfd = &pop->event_set[i];
michael@0 249 } else {
michael@0 250 i = pop->nfds++;
michael@0 251 pfd = &pop->event_set[i];
michael@0 252 pfd->events = 0;
michael@0 253 pfd->fd = fd;
michael@0 254 idx->idxplus1 = i + 1;
michael@0 255 }
michael@0 256
michael@0 257 pfd->revents = 0;
michael@0 258 if (events & EV_WRITE)
michael@0 259 pfd->events |= POLLOUT;
michael@0 260 if (events & EV_READ)
michael@0 261 pfd->events |= POLLIN;
michael@0 262 poll_check_ok(pop);
michael@0 263
michael@0 264 return (0);
michael@0 265 }
michael@0 266
michael@0 267 /*
michael@0 268 * Nothing to be done here.
michael@0 269 */
michael@0 270
michael@0 271 static int
michael@0 272 poll_del(struct event_base *base, int fd, short old, short events, void *_idx)
michael@0 273 {
michael@0 274 struct pollop *pop = base->evbase;
michael@0 275 struct pollfd *pfd = NULL;
michael@0 276 struct pollidx *idx = _idx;
michael@0 277 int i;
michael@0 278
michael@0 279 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
michael@0 280 if (!(events & (EV_READ|EV_WRITE)))
michael@0 281 return (0);
michael@0 282
michael@0 283 poll_check_ok(pop);
michael@0 284 i = idx->idxplus1 - 1;
michael@0 285 if (i < 0)
michael@0 286 return (-1);
michael@0 287
michael@0 288 /* Do we still want to read or write? */
michael@0 289 pfd = &pop->event_set[i];
michael@0 290 if (events & EV_READ)
michael@0 291 pfd->events &= ~POLLIN;
michael@0 292 if (events & EV_WRITE)
michael@0 293 pfd->events &= ~POLLOUT;
michael@0 294 poll_check_ok(pop);
michael@0 295 if (pfd->events)
michael@0 296 /* Another event cares about that fd. */
michael@0 297 return (0);
michael@0 298
michael@0 299 /* Okay, so we aren't interested in that fd anymore. */
michael@0 300 idx->idxplus1 = 0;
michael@0 301
michael@0 302 --pop->nfds;
michael@0 303 if (i != pop->nfds) {
michael@0 304 /*
michael@0 305 * Shift the last pollfd down into the now-unoccupied
michael@0 306 * position.
michael@0 307 */
michael@0 308 memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
michael@0 309 sizeof(struct pollfd));
michael@0 310 idx = evmap_io_get_fdinfo(&base->io, pop->event_set[i].fd);
michael@0 311 EVUTIL_ASSERT(idx);
michael@0 312 EVUTIL_ASSERT(idx->idxplus1 == pop->nfds + 1);
michael@0 313 idx->idxplus1 = i + 1;
michael@0 314 }
michael@0 315
michael@0 316 poll_check_ok(pop);
michael@0 317 return (0);
michael@0 318 }
michael@0 319
michael@0 320 static void
michael@0 321 poll_dealloc(struct event_base *base)
michael@0 322 {
michael@0 323 struct pollop *pop = base->evbase;
michael@0 324
michael@0 325 evsig_dealloc(base);
michael@0 326 if (pop->event_set)
michael@0 327 mm_free(pop->event_set);
michael@0 328 if (pop->event_set_copy)
michael@0 329 mm_free(pop->event_set_copy);
michael@0 330
michael@0 331 memset(pop, 0, sizeof(struct pollop));
michael@0 332 mm_free(pop);
michael@0 333 }

mercurial