ipc/chromium/src/third_party/libevent/bufferevent.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/bufferevent.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,875 @@
     1.4 +/*
     1.5 + * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
     1.6 + * Copyright (c) 2007-2012 Niels Provos, 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 +
    1.31 +#include <sys/types.h>
    1.32 +
    1.33 +#include "event2/event-config.h"
    1.34 +
    1.35 +#ifdef _EVENT_HAVE_SYS_TIME_H
    1.36 +#include <sys/time.h>
    1.37 +#endif
    1.38 +
    1.39 +#include <errno.h>
    1.40 +#include <stdio.h>
    1.41 +#include <stdlib.h>
    1.42 +#include <string.h>
    1.43 +#ifdef _EVENT_HAVE_STDARG_H
    1.44 +#include <stdarg.h>
    1.45 +#endif
    1.46 +
    1.47 +#ifdef WIN32
    1.48 +#include <winsock2.h>
    1.49 +#endif
    1.50 +#include <errno.h>
    1.51 +
    1.52 +#include "event2/util.h"
    1.53 +#include "event2/buffer.h"
    1.54 +#include "event2/buffer_compat.h"
    1.55 +#include "event2/bufferevent.h"
    1.56 +#include "event2/bufferevent_struct.h"
    1.57 +#include "event2/bufferevent_compat.h"
    1.58 +#include "event2/event.h"
    1.59 +#include "log-internal.h"
    1.60 +#include "mm-internal.h"
    1.61 +#include "bufferevent-internal.h"
    1.62 +#include "evbuffer-internal.h"
    1.63 +#include "util-internal.h"
    1.64 +
    1.65 +static void _bufferevent_cancel_all(struct bufferevent *bev);
    1.66 +
    1.67 +
    1.68 +void
    1.69 +bufferevent_suspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what)
    1.70 +{
    1.71 +	struct bufferevent_private *bufev_private =
    1.72 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
    1.73 +	BEV_LOCK(bufev);
    1.74 +	if (!bufev_private->read_suspended)
    1.75 +		bufev->be_ops->disable(bufev, EV_READ);
    1.76 +	bufev_private->read_suspended |= what;
    1.77 +	BEV_UNLOCK(bufev);
    1.78 +}
    1.79 +
    1.80 +void
    1.81 +bufferevent_unsuspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what)
    1.82 +{
    1.83 +	struct bufferevent_private *bufev_private =
    1.84 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
    1.85 +	BEV_LOCK(bufev);
    1.86 +	bufev_private->read_suspended &= ~what;
    1.87 +	if (!bufev_private->read_suspended && (bufev->enabled & EV_READ))
    1.88 +		bufev->be_ops->enable(bufev, EV_READ);
    1.89 +	BEV_UNLOCK(bufev);
    1.90 +}
    1.91 +
    1.92 +void
    1.93 +bufferevent_suspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what)
    1.94 +{
    1.95 +	struct bufferevent_private *bufev_private =
    1.96 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
    1.97 +	BEV_LOCK(bufev);
    1.98 +	if (!bufev_private->write_suspended)
    1.99 +		bufev->be_ops->disable(bufev, EV_WRITE);
   1.100 +	bufev_private->write_suspended |= what;
   1.101 +	BEV_UNLOCK(bufev);
   1.102 +}
   1.103 +
   1.104 +void
   1.105 +bufferevent_unsuspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what)
   1.106 +{
   1.107 +	struct bufferevent_private *bufev_private =
   1.108 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
   1.109 +	BEV_LOCK(bufev);
   1.110 +	bufev_private->write_suspended &= ~what;
   1.111 +	if (!bufev_private->write_suspended && (bufev->enabled & EV_WRITE))
   1.112 +		bufev->be_ops->enable(bufev, EV_WRITE);
   1.113 +	BEV_UNLOCK(bufev);
   1.114 +}
   1.115 +
   1.116 +
   1.117 +/* Callback to implement watermarks on the input buffer.  Only enabled
   1.118 + * if the watermark is set. */
   1.119 +static void
   1.120 +bufferevent_inbuf_wm_cb(struct evbuffer *buf,
   1.121 +    const struct evbuffer_cb_info *cbinfo,
   1.122 +    void *arg)
   1.123 +{
   1.124 +	struct bufferevent *bufev = arg;
   1.125 +	size_t size;
   1.126 +
   1.127 +	size = evbuffer_get_length(buf);
   1.128 +
   1.129 +	if (size >= bufev->wm_read.high)
   1.130 +		bufferevent_wm_suspend_read(bufev);
   1.131 +	else
   1.132 +		bufferevent_wm_unsuspend_read(bufev);
   1.133 +}
   1.134 +
   1.135 +static void
   1.136 +bufferevent_run_deferred_callbacks_locked(struct deferred_cb *_, void *arg)
   1.137 +{
   1.138 +	struct bufferevent_private *bufev_private = arg;
   1.139 +	struct bufferevent *bufev = &bufev_private->bev;
   1.140 +
   1.141 +	BEV_LOCK(bufev);
   1.142 +	if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) &&
   1.143 +	    bufev->errorcb) {
   1.144 +		/* The "connected" happened before any reads or writes, so
   1.145 +		   send it first. */
   1.146 +		bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED;
   1.147 +		bufev->errorcb(bufev, BEV_EVENT_CONNECTED, bufev->cbarg);
   1.148 +	}
   1.149 +	if (bufev_private->readcb_pending && bufev->readcb) {
   1.150 +		bufev_private->readcb_pending = 0;
   1.151 +		bufev->readcb(bufev, bufev->cbarg);
   1.152 +	}
   1.153 +	if (bufev_private->writecb_pending && bufev->writecb) {
   1.154 +		bufev_private->writecb_pending = 0;
   1.155 +		bufev->writecb(bufev, bufev->cbarg);
   1.156 +	}
   1.157 +	if (bufev_private->eventcb_pending && bufev->errorcb) {
   1.158 +		short what = bufev_private->eventcb_pending;
   1.159 +		int err = bufev_private->errno_pending;
   1.160 +		bufev_private->eventcb_pending = 0;
   1.161 +		bufev_private->errno_pending = 0;
   1.162 +		EVUTIL_SET_SOCKET_ERROR(err);
   1.163 +		bufev->errorcb(bufev, what, bufev->cbarg);
   1.164 +	}
   1.165 +	_bufferevent_decref_and_unlock(bufev);
   1.166 +}
   1.167 +
   1.168 +static void
   1.169 +bufferevent_run_deferred_callbacks_unlocked(struct deferred_cb *_, void *arg)
   1.170 +{
   1.171 +	struct bufferevent_private *bufev_private = arg;
   1.172 +	struct bufferevent *bufev = &bufev_private->bev;
   1.173 +
   1.174 +	BEV_LOCK(bufev);
   1.175 +#define UNLOCKED(stmt) \
   1.176 +	do { BEV_UNLOCK(bufev); stmt; BEV_LOCK(bufev); } while(0)
   1.177 +
   1.178 +	if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) &&
   1.179 +	    bufev->errorcb) {
   1.180 +		/* The "connected" happened before any reads or writes, so
   1.181 +		   send it first. */
   1.182 +		bufferevent_event_cb errorcb = bufev->errorcb;
   1.183 +		void *cbarg = bufev->cbarg;
   1.184 +		bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED;
   1.185 +		UNLOCKED(errorcb(bufev, BEV_EVENT_CONNECTED, cbarg));
   1.186 +	}
   1.187 +	if (bufev_private->readcb_pending && bufev->readcb) {
   1.188 +		bufferevent_data_cb readcb = bufev->readcb;
   1.189 +		void *cbarg = bufev->cbarg;
   1.190 +		bufev_private->readcb_pending = 0;
   1.191 +		UNLOCKED(readcb(bufev, cbarg));
   1.192 +	}
   1.193 +	if (bufev_private->writecb_pending && bufev->writecb) {
   1.194 +		bufferevent_data_cb writecb = bufev->writecb;
   1.195 +		void *cbarg = bufev->cbarg;
   1.196 +		bufev_private->writecb_pending = 0;
   1.197 +		UNLOCKED(writecb(bufev, cbarg));
   1.198 +	}
   1.199 +	if (bufev_private->eventcb_pending && bufev->errorcb) {
   1.200 +		bufferevent_event_cb errorcb = bufev->errorcb;
   1.201 +		void *cbarg = bufev->cbarg;
   1.202 +		short what = bufev_private->eventcb_pending;
   1.203 +		int err = bufev_private->errno_pending;
   1.204 +		bufev_private->eventcb_pending = 0;
   1.205 +		bufev_private->errno_pending = 0;
   1.206 +		EVUTIL_SET_SOCKET_ERROR(err);
   1.207 +		UNLOCKED(errorcb(bufev,what,cbarg));
   1.208 +	}
   1.209 +	_bufferevent_decref_and_unlock(bufev);
   1.210 +#undef UNLOCKED
   1.211 +}
   1.212 +
   1.213 +#define SCHEDULE_DEFERRED(bevp)						\
   1.214 +	do {								\
   1.215 +		bufferevent_incref(&(bevp)->bev);			\
   1.216 +		event_deferred_cb_schedule(				\
   1.217 +			event_base_get_deferred_cb_queue((bevp)->bev.ev_base), \
   1.218 +			&(bevp)->deferred);				\
   1.219 +	} while (0)
   1.220 +
   1.221 +
   1.222 +void
   1.223 +_bufferevent_run_readcb(struct bufferevent *bufev)
   1.224 +{
   1.225 +	/* Requires that we hold the lock and a reference */
   1.226 +	struct bufferevent_private *p =
   1.227 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
   1.228 +	if (bufev->readcb == NULL)
   1.229 +		return;
   1.230 +	if (p->options & BEV_OPT_DEFER_CALLBACKS) {
   1.231 +		p->readcb_pending = 1;
   1.232 +		if (!p->deferred.queued)
   1.233 +			SCHEDULE_DEFERRED(p);
   1.234 +	} else {
   1.235 +		bufev->readcb(bufev, bufev->cbarg);
   1.236 +	}
   1.237 +}
   1.238 +
   1.239 +void
   1.240 +_bufferevent_run_writecb(struct bufferevent *bufev)
   1.241 +{
   1.242 +	/* Requires that we hold the lock and a reference */
   1.243 +	struct bufferevent_private *p =
   1.244 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
   1.245 +	if (bufev->writecb == NULL)
   1.246 +		return;
   1.247 +	if (p->options & BEV_OPT_DEFER_CALLBACKS) {
   1.248 +		p->writecb_pending = 1;
   1.249 +		if (!p->deferred.queued)
   1.250 +			SCHEDULE_DEFERRED(p);
   1.251 +	} else {
   1.252 +		bufev->writecb(bufev, bufev->cbarg);
   1.253 +	}
   1.254 +}
   1.255 +
   1.256 +void
   1.257 +_bufferevent_run_eventcb(struct bufferevent *bufev, short what)
   1.258 +{
   1.259 +	/* Requires that we hold the lock and a reference */
   1.260 +	struct bufferevent_private *p =
   1.261 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
   1.262 +	if (bufev->errorcb == NULL)
   1.263 +		return;
   1.264 +	if (p->options & BEV_OPT_DEFER_CALLBACKS) {
   1.265 +		p->eventcb_pending |= what;
   1.266 +		p->errno_pending = EVUTIL_SOCKET_ERROR();
   1.267 +		if (!p->deferred.queued)
   1.268 +			SCHEDULE_DEFERRED(p);
   1.269 +	} else {
   1.270 +		bufev->errorcb(bufev, what, bufev->cbarg);
   1.271 +	}
   1.272 +}
   1.273 +
   1.274 +int
   1.275 +bufferevent_init_common(struct bufferevent_private *bufev_private,
   1.276 +    struct event_base *base,
   1.277 +    const struct bufferevent_ops *ops,
   1.278 +    enum bufferevent_options options)
   1.279 +{
   1.280 +	struct bufferevent *bufev = &bufev_private->bev;
   1.281 +
   1.282 +	if (!bufev->input) {
   1.283 +		if ((bufev->input = evbuffer_new()) == NULL)
   1.284 +			return -1;
   1.285 +	}
   1.286 +
   1.287 +	if (!bufev->output) {
   1.288 +		if ((bufev->output = evbuffer_new()) == NULL) {
   1.289 +			evbuffer_free(bufev->input);
   1.290 +			return -1;
   1.291 +		}
   1.292 +	}
   1.293 +
   1.294 +	bufev_private->refcnt = 1;
   1.295 +	bufev->ev_base = base;
   1.296 +
   1.297 +	/* Disable timeouts. */
   1.298 +	evutil_timerclear(&bufev->timeout_read);
   1.299 +	evutil_timerclear(&bufev->timeout_write);
   1.300 +
   1.301 +	bufev->be_ops = ops;
   1.302 +
   1.303 +	/*
   1.304 +	 * Set to EV_WRITE so that using bufferevent_write is going to
   1.305 +	 * trigger a callback.  Reading needs to be explicitly enabled
   1.306 +	 * because otherwise no data will be available.
   1.307 +	 */
   1.308 +	bufev->enabled = EV_WRITE;
   1.309 +
   1.310 +#ifndef _EVENT_DISABLE_THREAD_SUPPORT
   1.311 +	if (options & BEV_OPT_THREADSAFE) {
   1.312 +		if (bufferevent_enable_locking(bufev, NULL) < 0) {
   1.313 +			/* cleanup */
   1.314 +			evbuffer_free(bufev->input);
   1.315 +			evbuffer_free(bufev->output);
   1.316 +			bufev->input = NULL;
   1.317 +			bufev->output = NULL;
   1.318 +			return -1;
   1.319 +		}
   1.320 +	}
   1.321 +#endif
   1.322 +	if ((options & (BEV_OPT_DEFER_CALLBACKS|BEV_OPT_UNLOCK_CALLBACKS))
   1.323 +	    == BEV_OPT_UNLOCK_CALLBACKS) {
   1.324 +		event_warnx("UNLOCK_CALLBACKS requires DEFER_CALLBACKS");
   1.325 +		return -1;
   1.326 +	}
   1.327 +	if (options & BEV_OPT_DEFER_CALLBACKS) {
   1.328 +		if (options & BEV_OPT_UNLOCK_CALLBACKS)
   1.329 +			event_deferred_cb_init(&bufev_private->deferred,
   1.330 +			    bufferevent_run_deferred_callbacks_unlocked,
   1.331 +			    bufev_private);
   1.332 +		else
   1.333 +			event_deferred_cb_init(&bufev_private->deferred,
   1.334 +			    bufferevent_run_deferred_callbacks_locked,
   1.335 +			    bufev_private);
   1.336 +	}
   1.337 +
   1.338 +	bufev_private->options = options;
   1.339 +
   1.340 +	evbuffer_set_parent(bufev->input, bufev);
   1.341 +	evbuffer_set_parent(bufev->output, bufev);
   1.342 +
   1.343 +	return 0;
   1.344 +}
   1.345 +
   1.346 +void
   1.347 +bufferevent_setcb(struct bufferevent *bufev,
   1.348 +    bufferevent_data_cb readcb, bufferevent_data_cb writecb,
   1.349 +    bufferevent_event_cb eventcb, void *cbarg)
   1.350 +{
   1.351 +	BEV_LOCK(bufev);
   1.352 +
   1.353 +	bufev->readcb = readcb;
   1.354 +	bufev->writecb = writecb;
   1.355 +	bufev->errorcb = eventcb;
   1.356 +
   1.357 +	bufev->cbarg = cbarg;
   1.358 +	BEV_UNLOCK(bufev);
   1.359 +}
   1.360 +
   1.361 +struct evbuffer *
   1.362 +bufferevent_get_input(struct bufferevent *bufev)
   1.363 +{
   1.364 +	return bufev->input;
   1.365 +}
   1.366 +
   1.367 +struct evbuffer *
   1.368 +bufferevent_get_output(struct bufferevent *bufev)
   1.369 +{
   1.370 +	return bufev->output;
   1.371 +}
   1.372 +
   1.373 +struct event_base *
   1.374 +bufferevent_get_base(struct bufferevent *bufev)
   1.375 +{
   1.376 +	return bufev->ev_base;
   1.377 +}
   1.378 +
   1.379 +int
   1.380 +bufferevent_write(struct bufferevent *bufev, const void *data, size_t size)
   1.381 +{
   1.382 +	if (evbuffer_add(bufev->output, data, size) == -1)
   1.383 +		return (-1);
   1.384 +
   1.385 +	return 0;
   1.386 +}
   1.387 +
   1.388 +int
   1.389 +bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf)
   1.390 +{
   1.391 +	if (evbuffer_add_buffer(bufev->output, buf) == -1)
   1.392 +		return (-1);
   1.393 +
   1.394 +	return 0;
   1.395 +}
   1.396 +
   1.397 +size_t
   1.398 +bufferevent_read(struct bufferevent *bufev, void *data, size_t size)
   1.399 +{
   1.400 +	return (evbuffer_remove(bufev->input, data, size));
   1.401 +}
   1.402 +
   1.403 +int
   1.404 +bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf)
   1.405 +{
   1.406 +	return (evbuffer_add_buffer(buf, bufev->input));
   1.407 +}
   1.408 +
   1.409 +int
   1.410 +bufferevent_enable(struct bufferevent *bufev, short event)
   1.411 +{
   1.412 +	struct bufferevent_private *bufev_private =
   1.413 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
   1.414 +	short impl_events = event;
   1.415 +	int r = 0;
   1.416 +
   1.417 +	_bufferevent_incref_and_lock(bufev);
   1.418 +	if (bufev_private->read_suspended)
   1.419 +		impl_events &= ~EV_READ;
   1.420 +	if (bufev_private->write_suspended)
   1.421 +		impl_events &= ~EV_WRITE;
   1.422 +
   1.423 +	bufev->enabled |= event;
   1.424 +
   1.425 +	if (impl_events && bufev->be_ops->enable(bufev, impl_events) < 0)
   1.426 +		r = -1;
   1.427 +
   1.428 +	_bufferevent_decref_and_unlock(bufev);
   1.429 +	return r;
   1.430 +}
   1.431 +
   1.432 +int
   1.433 +bufferevent_set_timeouts(struct bufferevent *bufev,
   1.434 +			 const struct timeval *tv_read,
   1.435 +			 const struct timeval *tv_write)
   1.436 +{
   1.437 +	int r = 0;
   1.438 +	BEV_LOCK(bufev);
   1.439 +	if (tv_read) {
   1.440 +		bufev->timeout_read = *tv_read;
   1.441 +	} else {
   1.442 +		evutil_timerclear(&bufev->timeout_read);
   1.443 +	}
   1.444 +	if (tv_write) {
   1.445 +		bufev->timeout_write = *tv_write;
   1.446 +	} else {
   1.447 +		evutil_timerclear(&bufev->timeout_write);
   1.448 +	}
   1.449 +
   1.450 +	if (bufev->be_ops->adj_timeouts)
   1.451 +		r = bufev->be_ops->adj_timeouts(bufev);
   1.452 +	BEV_UNLOCK(bufev);
   1.453 +
   1.454 +	return r;
   1.455 +}
   1.456 +
   1.457 +
   1.458 +/* Obsolete; use bufferevent_set_timeouts */
   1.459 +void
   1.460 +bufferevent_settimeout(struct bufferevent *bufev,
   1.461 +		       int timeout_read, int timeout_write)
   1.462 +{
   1.463 +	struct timeval tv_read, tv_write;
   1.464 +	struct timeval *ptv_read = NULL, *ptv_write = NULL;
   1.465 +
   1.466 +	memset(&tv_read, 0, sizeof(tv_read));
   1.467 +	memset(&tv_write, 0, sizeof(tv_write));
   1.468 +
   1.469 +	if (timeout_read) {
   1.470 +		tv_read.tv_sec = timeout_read;
   1.471 +		ptv_read = &tv_read;
   1.472 +	}
   1.473 +	if (timeout_write) {
   1.474 +		tv_write.tv_sec = timeout_write;
   1.475 +		ptv_write = &tv_write;
   1.476 +	}
   1.477 +
   1.478 +	bufferevent_set_timeouts(bufev, ptv_read, ptv_write);
   1.479 +}
   1.480 +
   1.481 +
   1.482 +int
   1.483 +bufferevent_disable_hard(struct bufferevent *bufev, short event)
   1.484 +{
   1.485 +	int r = 0;
   1.486 +	struct bufferevent_private *bufev_private =
   1.487 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
   1.488 +
   1.489 +	BEV_LOCK(bufev);
   1.490 +	bufev->enabled &= ~event;
   1.491 +
   1.492 +	bufev_private->connecting = 0;
   1.493 +	if (bufev->be_ops->disable(bufev, event) < 0)
   1.494 +		r = -1;
   1.495 +
   1.496 +	BEV_UNLOCK(bufev);
   1.497 +	return r;
   1.498 +}
   1.499 +
   1.500 +int
   1.501 +bufferevent_disable(struct bufferevent *bufev, short event)
   1.502 +{
   1.503 +	int r = 0;
   1.504 +
   1.505 +	BEV_LOCK(bufev);
   1.506 +	bufev->enabled &= ~event;
   1.507 +
   1.508 +	if (bufev->be_ops->disable(bufev, event) < 0)
   1.509 +		r = -1;
   1.510 +
   1.511 +	BEV_UNLOCK(bufev);
   1.512 +	return r;
   1.513 +}
   1.514 +
   1.515 +/*
   1.516 + * Sets the water marks
   1.517 + */
   1.518 +
   1.519 +void
   1.520 +bufferevent_setwatermark(struct bufferevent *bufev, short events,
   1.521 +    size_t lowmark, size_t highmark)
   1.522 +{
   1.523 +	struct bufferevent_private *bufev_private =
   1.524 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
   1.525 +
   1.526 +	BEV_LOCK(bufev);
   1.527 +	if (events & EV_WRITE) {
   1.528 +		bufev->wm_write.low = lowmark;
   1.529 +		bufev->wm_write.high = highmark;
   1.530 +	}
   1.531 +
   1.532 +	if (events & EV_READ) {
   1.533 +		bufev->wm_read.low = lowmark;
   1.534 +		bufev->wm_read.high = highmark;
   1.535 +
   1.536 +		if (highmark) {
   1.537 +			/* There is now a new high-water mark for read.
   1.538 +			   enable the callback if needed, and see if we should
   1.539 +			   suspend/bufferevent_wm_unsuspend. */
   1.540 +
   1.541 +			if (bufev_private->read_watermarks_cb == NULL) {
   1.542 +				bufev_private->read_watermarks_cb =
   1.543 +				    evbuffer_add_cb(bufev->input,
   1.544 +						    bufferevent_inbuf_wm_cb,
   1.545 +						    bufev);
   1.546 +			}
   1.547 +			evbuffer_cb_set_flags(bufev->input,
   1.548 +				      bufev_private->read_watermarks_cb,
   1.549 +				      EVBUFFER_CB_ENABLED|EVBUFFER_CB_NODEFER);
   1.550 +
   1.551 +			if (evbuffer_get_length(bufev->input) > highmark)
   1.552 +				bufferevent_wm_suspend_read(bufev);
   1.553 +			else if (evbuffer_get_length(bufev->input) < highmark)
   1.554 +				bufferevent_wm_unsuspend_read(bufev);
   1.555 +		} else {
   1.556 +			/* There is now no high-water mark for read. */
   1.557 +			if (bufev_private->read_watermarks_cb)
   1.558 +				evbuffer_cb_clear_flags(bufev->input,
   1.559 +				    bufev_private->read_watermarks_cb,
   1.560 +				    EVBUFFER_CB_ENABLED);
   1.561 +			bufferevent_wm_unsuspend_read(bufev);
   1.562 +		}
   1.563 +	}
   1.564 +	BEV_UNLOCK(bufev);
   1.565 +}
   1.566 +
   1.567 +int
   1.568 +bufferevent_flush(struct bufferevent *bufev,
   1.569 +    short iotype,
   1.570 +    enum bufferevent_flush_mode mode)
   1.571 +{
   1.572 +	int r = -1;
   1.573 +	BEV_LOCK(bufev);
   1.574 +	if (bufev->be_ops->flush)
   1.575 +		r = bufev->be_ops->flush(bufev, iotype, mode);
   1.576 +	BEV_UNLOCK(bufev);
   1.577 +	return r;
   1.578 +}
   1.579 +
   1.580 +void
   1.581 +_bufferevent_incref_and_lock(struct bufferevent *bufev)
   1.582 +{
   1.583 +	struct bufferevent_private *bufev_private =
   1.584 +	    BEV_UPCAST(bufev);
   1.585 +	BEV_LOCK(bufev);
   1.586 +	++bufev_private->refcnt;
   1.587 +}
   1.588 +
   1.589 +#if 0
   1.590 +static void
   1.591 +_bufferevent_transfer_lock_ownership(struct bufferevent *donor,
   1.592 +    struct bufferevent *recipient)
   1.593 +{
   1.594 +	struct bufferevent_private *d = BEV_UPCAST(donor);
   1.595 +	struct bufferevent_private *r = BEV_UPCAST(recipient);
   1.596 +	if (d->lock != r->lock)
   1.597 +		return;
   1.598 +	if (r->own_lock)
   1.599 +		return;
   1.600 +	if (d->own_lock) {
   1.601 +		d->own_lock = 0;
   1.602 +		r->own_lock = 1;
   1.603 +	}
   1.604 +}
   1.605 +#endif
   1.606 +
   1.607 +int
   1.608 +_bufferevent_decref_and_unlock(struct bufferevent *bufev)
   1.609 +{
   1.610 +	struct bufferevent_private *bufev_private =
   1.611 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
   1.612 +	struct bufferevent *underlying;
   1.613 +
   1.614 +	EVUTIL_ASSERT(bufev_private->refcnt > 0);
   1.615 +
   1.616 +	if (--bufev_private->refcnt) {
   1.617 +		BEV_UNLOCK(bufev);
   1.618 +		return 0;
   1.619 +	}
   1.620 +
   1.621 +	underlying = bufferevent_get_underlying(bufev);
   1.622 +
   1.623 +	/* Clean up the shared info */
   1.624 +	if (bufev->be_ops->destruct)
   1.625 +		bufev->be_ops->destruct(bufev);
   1.626 +
   1.627 +	/* XXX what happens if refcnt for these buffers is > 1?
   1.628 +	 * The buffers can share a lock with this bufferevent object,
   1.629 +	 * but the lock might be destroyed below. */
   1.630 +	/* evbuffer will free the callbacks */
   1.631 +	evbuffer_free(bufev->input);
   1.632 +	evbuffer_free(bufev->output);
   1.633 +
   1.634 +	if (bufev_private->rate_limiting) {
   1.635 +		if (bufev_private->rate_limiting->group)
   1.636 +			bufferevent_remove_from_rate_limit_group_internal(bufev,0);
   1.637 +		if (event_initialized(&bufev_private->rate_limiting->refill_bucket_event))
   1.638 +			event_del(&bufev_private->rate_limiting->refill_bucket_event);
   1.639 +		event_debug_unassign(&bufev_private->rate_limiting->refill_bucket_event);
   1.640 +		mm_free(bufev_private->rate_limiting);
   1.641 +		bufev_private->rate_limiting = NULL;
   1.642 +	}
   1.643 +
   1.644 +	event_debug_unassign(&bufev->ev_read);
   1.645 +	event_debug_unassign(&bufev->ev_write);
   1.646 +
   1.647 +	BEV_UNLOCK(bufev);
   1.648 +	if (bufev_private->own_lock)
   1.649 +		EVTHREAD_FREE_LOCK(bufev_private->lock,
   1.650 +		    EVTHREAD_LOCKTYPE_RECURSIVE);
   1.651 +
   1.652 +	/* Free the actual allocated memory. */
   1.653 +	mm_free(((char*)bufev) - bufev->be_ops->mem_offset);
   1.654 +
   1.655 +	/* Release the reference to underlying now that we no longer need the
   1.656 +	 * reference to it.  We wait this long mainly in case our lock is
   1.657 +	 * shared with underlying.
   1.658 +	 *
   1.659 +	 * The 'destruct' function will also drop a reference to underlying
   1.660 +	 * if BEV_OPT_CLOSE_ON_FREE is set.
   1.661 +	 *
   1.662 +	 * XXX Should we/can we just refcount evbuffer/bufferevent locks?
   1.663 +	 * It would probably save us some headaches.
   1.664 +	 */
   1.665 +	if (underlying)
   1.666 +		bufferevent_decref(underlying);
   1.667 +
   1.668 +	return 1;
   1.669 +}
   1.670 +
   1.671 +int
   1.672 +bufferevent_decref(struct bufferevent *bufev)
   1.673 +{
   1.674 +	BEV_LOCK(bufev);
   1.675 +	return _bufferevent_decref_and_unlock(bufev);
   1.676 +}
   1.677 +
   1.678 +void
   1.679 +bufferevent_free(struct bufferevent *bufev)
   1.680 +{
   1.681 +	BEV_LOCK(bufev);
   1.682 +	bufferevent_setcb(bufev, NULL, NULL, NULL, NULL);
   1.683 +	_bufferevent_cancel_all(bufev);
   1.684 +	_bufferevent_decref_and_unlock(bufev);
   1.685 +}
   1.686 +
   1.687 +void
   1.688 +bufferevent_incref(struct bufferevent *bufev)
   1.689 +{
   1.690 +	struct bufferevent_private *bufev_private =
   1.691 +	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
   1.692 +
   1.693 +	BEV_LOCK(bufev);
   1.694 +	++bufev_private->refcnt;
   1.695 +	BEV_UNLOCK(bufev);
   1.696 +}
   1.697 +
   1.698 +int
   1.699 +bufferevent_enable_locking(struct bufferevent *bufev, void *lock)
   1.700 +{
   1.701 +#ifdef _EVENT_DISABLE_THREAD_SUPPORT
   1.702 +	return -1;
   1.703 +#else
   1.704 +	struct bufferevent *underlying;
   1.705 +
   1.706 +	if (BEV_UPCAST(bufev)->lock)
   1.707 +		return -1;
   1.708 +	underlying = bufferevent_get_underlying(bufev);
   1.709 +
   1.710 +	if (!lock && underlying && BEV_UPCAST(underlying)->lock) {
   1.711 +		lock = BEV_UPCAST(underlying)->lock;
   1.712 +		BEV_UPCAST(bufev)->lock = lock;
   1.713 +		BEV_UPCAST(bufev)->own_lock = 0;
   1.714 +	} else if (!lock) {
   1.715 +		EVTHREAD_ALLOC_LOCK(lock, EVTHREAD_LOCKTYPE_RECURSIVE);
   1.716 +		if (!lock)
   1.717 +			return -1;
   1.718 +		BEV_UPCAST(bufev)->lock = lock;
   1.719 +		BEV_UPCAST(bufev)->own_lock = 1;
   1.720 +	} else {
   1.721 +		BEV_UPCAST(bufev)->lock = lock;
   1.722 +		BEV_UPCAST(bufev)->own_lock = 0;
   1.723 +	}
   1.724 +	evbuffer_enable_locking(bufev->input, lock);
   1.725 +	evbuffer_enable_locking(bufev->output, lock);
   1.726 +
   1.727 +	if (underlying && !BEV_UPCAST(underlying)->lock)
   1.728 +		bufferevent_enable_locking(underlying, lock);
   1.729 +
   1.730 +	return 0;
   1.731 +#endif
   1.732 +}
   1.733 +
   1.734 +int
   1.735 +bufferevent_setfd(struct bufferevent *bev, evutil_socket_t fd)
   1.736 +{
   1.737 +	union bufferevent_ctrl_data d;
   1.738 +	int res = -1;
   1.739 +	d.fd = fd;
   1.740 +	BEV_LOCK(bev);
   1.741 +	if (bev->be_ops->ctrl)
   1.742 +		res = bev->be_ops->ctrl(bev, BEV_CTRL_SET_FD, &d);
   1.743 +	BEV_UNLOCK(bev);
   1.744 +	return res;
   1.745 +}
   1.746 +
   1.747 +evutil_socket_t
   1.748 +bufferevent_getfd(struct bufferevent *bev)
   1.749 +{
   1.750 +	union bufferevent_ctrl_data d;
   1.751 +	int res = -1;
   1.752 +	d.fd = -1;
   1.753 +	BEV_LOCK(bev);
   1.754 +	if (bev->be_ops->ctrl)
   1.755 +		res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_FD, &d);
   1.756 +	BEV_UNLOCK(bev);
   1.757 +	return (res<0) ? -1 : d.fd;
   1.758 +}
   1.759 +
   1.760 +static void
   1.761 +_bufferevent_cancel_all(struct bufferevent *bev)
   1.762 +{
   1.763 +	union bufferevent_ctrl_data d;
   1.764 +	memset(&d, 0, sizeof(d));
   1.765 +	BEV_LOCK(bev);
   1.766 +	if (bev->be_ops->ctrl)
   1.767 +		bev->be_ops->ctrl(bev, BEV_CTRL_CANCEL_ALL, &d);
   1.768 +	BEV_UNLOCK(bev);
   1.769 +}
   1.770 +
   1.771 +short
   1.772 +bufferevent_get_enabled(struct bufferevent *bufev)
   1.773 +{
   1.774 +	short r;
   1.775 +	BEV_LOCK(bufev);
   1.776 +	r = bufev->enabled;
   1.777 +	BEV_UNLOCK(bufev);
   1.778 +	return r;
   1.779 +}
   1.780 +
   1.781 +struct bufferevent *
   1.782 +bufferevent_get_underlying(struct bufferevent *bev)
   1.783 +{
   1.784 +	union bufferevent_ctrl_data d;
   1.785 +	int res = -1;
   1.786 +	d.ptr = NULL;
   1.787 +	BEV_LOCK(bev);
   1.788 +	if (bev->be_ops->ctrl)
   1.789 +		res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_UNDERLYING, &d);
   1.790 +	BEV_UNLOCK(bev);
   1.791 +	return (res<0) ? NULL : d.ptr;
   1.792 +}
   1.793 +
   1.794 +static void
   1.795 +bufferevent_generic_read_timeout_cb(evutil_socket_t fd, short event, void *ctx)
   1.796 +{
   1.797 +	struct bufferevent *bev = ctx;
   1.798 +	_bufferevent_incref_and_lock(bev);
   1.799 +	bufferevent_disable(bev, EV_READ);
   1.800 +	_bufferevent_run_eventcb(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_READING);
   1.801 +	_bufferevent_decref_and_unlock(bev);
   1.802 +}
   1.803 +static void
   1.804 +bufferevent_generic_write_timeout_cb(evutil_socket_t fd, short event, void *ctx)
   1.805 +{
   1.806 +	struct bufferevent *bev = ctx;
   1.807 +	_bufferevent_incref_and_lock(bev);
   1.808 +	bufferevent_disable(bev, EV_WRITE);
   1.809 +	_bufferevent_run_eventcb(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING);
   1.810 +	_bufferevent_decref_and_unlock(bev);
   1.811 +}
   1.812 +
   1.813 +void
   1.814 +_bufferevent_init_generic_timeout_cbs(struct bufferevent *bev)
   1.815 +{
   1.816 +	evtimer_assign(&bev->ev_read, bev->ev_base,
   1.817 +	    bufferevent_generic_read_timeout_cb, bev);
   1.818 +	evtimer_assign(&bev->ev_write, bev->ev_base,
   1.819 +	    bufferevent_generic_write_timeout_cb, bev);
   1.820 +}
   1.821 +
   1.822 +int
   1.823 +_bufferevent_del_generic_timeout_cbs(struct bufferevent *bev)
   1.824 +{
   1.825 +	int r1,r2;
   1.826 +	r1 = event_del(&bev->ev_read);
   1.827 +	r2 = event_del(&bev->ev_write);
   1.828 +	if (r1<0 || r2<0)
   1.829 +		return -1;
   1.830 +	return 0;
   1.831 +}
   1.832 +
   1.833 +int
   1.834 +_bufferevent_generic_adj_timeouts(struct bufferevent *bev)
   1.835 +{
   1.836 +	const short enabled = bev->enabled;
   1.837 +	struct bufferevent_private *bev_p =
   1.838 +	    EVUTIL_UPCAST(bev, struct bufferevent_private, bev);
   1.839 +	int r1=0, r2=0;
   1.840 +	if ((enabled & EV_READ) && !bev_p->read_suspended &&
   1.841 +	    evutil_timerisset(&bev->timeout_read))
   1.842 +		r1 = event_add(&bev->ev_read, &bev->timeout_read);
   1.843 +	else
   1.844 +		r1 = event_del(&bev->ev_read);
   1.845 +
   1.846 +	if ((enabled & EV_WRITE) && !bev_p->write_suspended &&
   1.847 +	    evutil_timerisset(&bev->timeout_write) &&
   1.848 +	    evbuffer_get_length(bev->output))
   1.849 +		r2 = event_add(&bev->ev_write, &bev->timeout_write);
   1.850 +	else
   1.851 +		r2 = event_del(&bev->ev_write);
   1.852 +	if (r1 < 0 || r2 < 0)
   1.853 +		return -1;
   1.854 +	return 0;
   1.855 +}
   1.856 +
   1.857 +int
   1.858 +_bufferevent_add_event(struct event *ev, const struct timeval *tv)
   1.859 +{
   1.860 +	if (tv->tv_sec == 0 && tv->tv_usec == 0)
   1.861 +		return event_add(ev, NULL);
   1.862 +	else
   1.863 +		return event_add(ev, tv);
   1.864 +}
   1.865 +
   1.866 +/* For use by user programs only; internally, we should be calling
   1.867 +   either _bufferevent_incref_and_lock(), or BEV_LOCK. */
   1.868 +void
   1.869 +bufferevent_lock(struct bufferevent *bev)
   1.870 +{
   1.871 +	_bufferevent_incref_and_lock(bev);
   1.872 +}
   1.873 +
   1.874 +void
   1.875 +bufferevent_unlock(struct bufferevent *bev)
   1.876 +{
   1.877 +	_bufferevent_decref_and_unlock(bev);
   1.878 +}

mercurial