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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/third_party/libevent/devpoll.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,306 @@
     1.4 +/*
     1.5 + * Copyright 2000-2009 Niels Provos <provos@citi.umich.edu>
     1.6 + * Copyright 2009-2012 Niels Provos and Nick Mathewson
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + * 1. Redistributions of source code must retain the above copyright
    1.12 + *    notice, this list of conditions and the following disclaimer.
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + * 3. The name of the author may not be used to endorse or promote products
    1.17 + *    derived from this software without specific prior written permission.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.20 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    1.21 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.22 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.23 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.24 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.25 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.26 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.27 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    1.28 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.29 + */
    1.30 +#include "event2/event-config.h"
    1.31 +
    1.32 +#include <sys/types.h>
    1.33 +#include <sys/resource.h>
    1.34 +#ifdef _EVENT_HAVE_SYS_TIME_H
    1.35 +#include <sys/time.h>
    1.36 +#endif
    1.37 +#include <sys/queue.h>
    1.38 +#include <sys/devpoll.h>
    1.39 +#include <signal.h>
    1.40 +#include <stdio.h>
    1.41 +#include <stdlib.h>
    1.42 +#include <string.h>
    1.43 +#include <unistd.h>
    1.44 +#include <fcntl.h>
    1.45 +#include <errno.h>
    1.46 +
    1.47 +#include "event2/event.h"
    1.48 +#include "event2/event_struct.h"
    1.49 +#include "event2/thread.h"
    1.50 +#include "event-internal.h"
    1.51 +#include "evsignal-internal.h"
    1.52 +#include "log-internal.h"
    1.53 +#include "evmap-internal.h"
    1.54 +#include "evthread-internal.h"
    1.55 +
    1.56 +struct devpollop {
    1.57 +	struct pollfd *events;
    1.58 +	int nevents;
    1.59 +	int dpfd;
    1.60 +	struct pollfd *changes;
    1.61 +	int nchanges;
    1.62 +};
    1.63 +
    1.64 +static void *devpoll_init(struct event_base *);
    1.65 +static int devpoll_add(struct event_base *, int fd, short old, short events, void *);
    1.66 +static int devpoll_del(struct event_base *, int fd, short old, short events, void *);
    1.67 +static int devpoll_dispatch(struct event_base *, struct timeval *);
    1.68 +static void devpoll_dealloc(struct event_base *);
    1.69 +
    1.70 +const struct eventop devpollops = {
    1.71 +	"devpoll",
    1.72 +	devpoll_init,
    1.73 +	devpoll_add,
    1.74 +	devpoll_del,
    1.75 +	devpoll_dispatch,
    1.76 +	devpoll_dealloc,
    1.77 +	1, /* need reinit */
    1.78 +	EV_FEATURE_FDS|EV_FEATURE_O1,
    1.79 +	0
    1.80 +};
    1.81 +
    1.82 +#define NEVENT	32000
    1.83 +
    1.84 +static int
    1.85 +devpoll_commit(struct devpollop *devpollop)
    1.86 +{
    1.87 +	/*
    1.88 +	 * Due to a bug in Solaris, we have to use pwrite with an offset of 0.
    1.89 +	 * Write is limited to 2GB of data, until it will fail.
    1.90 +	 */
    1.91 +	if (pwrite(devpollop->dpfd, devpollop->changes,
    1.92 +		sizeof(struct pollfd) * devpollop->nchanges, 0) == -1)
    1.93 +		return (-1);
    1.94 +
    1.95 +	devpollop->nchanges = 0;
    1.96 +	return (0);
    1.97 +}
    1.98 +
    1.99 +static int
   1.100 +devpoll_queue(struct devpollop *devpollop, int fd, int events) {
   1.101 +	struct pollfd *pfd;
   1.102 +
   1.103 +	if (devpollop->nchanges >= devpollop->nevents) {
   1.104 +		/*
   1.105 +		 * Change buffer is full, must commit it to /dev/poll before
   1.106 +		 * adding more
   1.107 +		 */
   1.108 +		if (devpoll_commit(devpollop) != 0)
   1.109 +			return (-1);
   1.110 +	}
   1.111 +
   1.112 +	pfd = &devpollop->changes[devpollop->nchanges++];
   1.113 +	pfd->fd = fd;
   1.114 +	pfd->events = events;
   1.115 +	pfd->revents = 0;
   1.116 +
   1.117 +	return (0);
   1.118 +}
   1.119 +
   1.120 +static void *
   1.121 +devpoll_init(struct event_base *base)
   1.122 +{
   1.123 +	int dpfd, nfiles = NEVENT;
   1.124 +	struct rlimit rl;
   1.125 +	struct devpollop *devpollop;
   1.126 +
   1.127 +	if (!(devpollop = mm_calloc(1, sizeof(struct devpollop))))
   1.128 +		return (NULL);
   1.129 +
   1.130 +	if (getrlimit(RLIMIT_NOFILE, &rl) == 0 &&
   1.131 +	    rl.rlim_cur != RLIM_INFINITY)
   1.132 +		nfiles = rl.rlim_cur;
   1.133 +
   1.134 +	/* Initialize the kernel queue */
   1.135 +	if ((dpfd = evutil_open_closeonexec("/dev/poll", O_RDWR, 0)) == -1) {
   1.136 +		event_warn("open: /dev/poll");
   1.137 +		mm_free(devpollop);
   1.138 +		return (NULL);
   1.139 +	}
   1.140 +
   1.141 +	devpollop->dpfd = dpfd;
   1.142 +
   1.143 +	/* Initialize fields */
   1.144 +	/* FIXME: allocating 'nfiles' worth of space here can be
   1.145 +	 * expensive and unnecessary.  See how epoll.c does it instead. */
   1.146 +	devpollop->events = mm_calloc(nfiles, sizeof(struct pollfd));
   1.147 +	if (devpollop->events == NULL) {
   1.148 +		mm_free(devpollop);
   1.149 +		close(dpfd);
   1.150 +		return (NULL);
   1.151 +	}
   1.152 +	devpollop->nevents = nfiles;
   1.153 +
   1.154 +	devpollop->changes = mm_calloc(nfiles, sizeof(struct pollfd));
   1.155 +	if (devpollop->changes == NULL) {
   1.156 +		mm_free(devpollop->events);
   1.157 +		mm_free(devpollop);
   1.158 +		close(dpfd);
   1.159 +		return (NULL);
   1.160 +	}
   1.161 +
   1.162 +	evsig_init(base);
   1.163 +
   1.164 +	return (devpollop);
   1.165 +}
   1.166 +
   1.167 +static int
   1.168 +devpoll_dispatch(struct event_base *base, struct timeval *tv)
   1.169 +{
   1.170 +	struct devpollop *devpollop = base->evbase;
   1.171 +	struct pollfd *events = devpollop->events;
   1.172 +	struct dvpoll dvp;
   1.173 +	int i, res, timeout = -1;
   1.174 +
   1.175 +	if (devpollop->nchanges)
   1.176 +		devpoll_commit(devpollop);
   1.177 +
   1.178 +	if (tv != NULL)
   1.179 +		timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
   1.180 +
   1.181 +	dvp.dp_fds = devpollop->events;
   1.182 +	dvp.dp_nfds = devpollop->nevents;
   1.183 +	dvp.dp_timeout = timeout;
   1.184 +
   1.185 +	EVBASE_RELEASE_LOCK(base, th_base_lock);
   1.186 +
   1.187 +	res = ioctl(devpollop->dpfd, DP_POLL, &dvp);
   1.188 +
   1.189 +	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
   1.190 +
   1.191 +	if (res == -1) {
   1.192 +		if (errno != EINTR) {
   1.193 +			event_warn("ioctl: DP_POLL");
   1.194 +			return (-1);
   1.195 +		}
   1.196 +
   1.197 +		return (0);
   1.198 +	}
   1.199 +
   1.200 +	event_debug(("%s: devpoll_wait reports %d", __func__, res));
   1.201 +
   1.202 +	for (i = 0; i < res; i++) {
   1.203 +		int which = 0;
   1.204 +		int what = events[i].revents;
   1.205 +
   1.206 +		if (what & POLLHUP)
   1.207 +			what |= POLLIN | POLLOUT;
   1.208 +		else if (what & POLLERR)
   1.209 +			what |= POLLIN | POLLOUT;
   1.210 +
   1.211 +		if (what & POLLIN)
   1.212 +			which |= EV_READ;
   1.213 +		if (what & POLLOUT)
   1.214 +			which |= EV_WRITE;
   1.215 +
   1.216 +		if (!which)
   1.217 +			continue;
   1.218 +
   1.219 +		/* XXX(niels): not sure if this works for devpoll */
   1.220 +		evmap_io_active(base, events[i].fd, which);
   1.221 +	}
   1.222 +
   1.223 +	return (0);
   1.224 +}
   1.225 +
   1.226 +
   1.227 +static int
   1.228 +devpoll_add(struct event_base *base, int fd, short old, short events, void *p)
   1.229 +{
   1.230 +	struct devpollop *devpollop = base->evbase;
   1.231 +	int res;
   1.232 +	(void)p;
   1.233 +
   1.234 +	/*
   1.235 +	 * It's not necessary to OR the existing read/write events that we
   1.236 +	 * are currently interested in with the new event we are adding.
   1.237 +	 * The /dev/poll driver ORs any new events with the existing events
   1.238 +	 * that it has cached for the fd.
   1.239 +	 */
   1.240 +
   1.241 +	res = 0;
   1.242 +	if (events & EV_READ)
   1.243 +		res |= POLLIN;
   1.244 +	if (events & EV_WRITE)
   1.245 +		res |= POLLOUT;
   1.246 +
   1.247 +	if (devpoll_queue(devpollop, fd, res) != 0)
   1.248 +		return (-1);
   1.249 +
   1.250 +	return (0);
   1.251 +}
   1.252 +
   1.253 +static int
   1.254 +devpoll_del(struct event_base *base, int fd, short old, short events, void *p)
   1.255 +{
   1.256 +	struct devpollop *devpollop = base->evbase;
   1.257 +	int res;
   1.258 +	(void)p;
   1.259 +
   1.260 +	res = 0;
   1.261 +	if (events & EV_READ)
   1.262 +		res |= POLLIN;
   1.263 +	if (events & EV_WRITE)
   1.264 +		res |= POLLOUT;
   1.265 +
   1.266 +	/*
   1.267 +	 * The only way to remove an fd from the /dev/poll monitored set is
   1.268 +	 * to use POLLREMOVE by itself.  This removes ALL events for the fd
   1.269 +	 * provided so if we care about two events and are only removing one
   1.270 +	 * we must re-add the other event after POLLREMOVE.
   1.271 +	 */
   1.272 +
   1.273 +	if (devpoll_queue(devpollop, fd, POLLREMOVE) != 0)
   1.274 +		return (-1);
   1.275 +
   1.276 +	if ((res & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) {
   1.277 +		/*
   1.278 +		 * We're not deleting all events, so we must resubmit the
   1.279 +		 * event that we are still interested in if one exists.
   1.280 +		 */
   1.281 +
   1.282 +		if ((res & POLLIN) && (old & EV_WRITE)) {
   1.283 +			/* Deleting read, still care about write */
   1.284 +			devpoll_queue(devpollop, fd, POLLOUT);
   1.285 +		} else if ((res & POLLOUT) && (old & EV_READ)) {
   1.286 +			/* Deleting write, still care about read */
   1.287 +			devpoll_queue(devpollop, fd, POLLIN);
   1.288 +		}
   1.289 +	}
   1.290 +
   1.291 +	return (0);
   1.292 +}
   1.293 +
   1.294 +static void
   1.295 +devpoll_dealloc(struct event_base *base)
   1.296 +{
   1.297 +	struct devpollop *devpollop = base->evbase;
   1.298 +
   1.299 +	evsig_dealloc(base);
   1.300 +	if (devpollop->events)
   1.301 +		mm_free(devpollop->events);
   1.302 +	if (devpollop->changes)
   1.303 +		mm_free(devpollop->changes);
   1.304 +	if (devpollop->dpfd >= 0)
   1.305 +		close(devpollop->dpfd);
   1.306 +
   1.307 +	memset(devpollop, 0, sizeof(struct devpollop));
   1.308 +	mm_free(devpollop);
   1.309 +}

mercurial