xref: /freebsd/contrib/ntp/sntp/libevent/bufferevent.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
32b15cb3dSCy Schubert  * Copyright (c) 2007-2012 Niels Provos, Nick Mathewson
42b15cb3dSCy Schubert  *
52b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
62b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
72b15cb3dSCy Schubert  * are met:
82b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
92b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
102b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
112b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
122b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
132b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
142b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
152b15cb3dSCy Schubert  *
162b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262b15cb3dSCy Schubert  */
272b15cb3dSCy Schubert 
282b15cb3dSCy Schubert #include "event2/event-config.h"
292b15cb3dSCy Schubert #include "evconfig-private.h"
302b15cb3dSCy Schubert 
312b15cb3dSCy Schubert #include <sys/types.h>
322b15cb3dSCy Schubert 
332b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
342b15cb3dSCy Schubert #include <sys/time.h>
352b15cb3dSCy Schubert #endif
362b15cb3dSCy Schubert 
372b15cb3dSCy Schubert #include <errno.h>
382b15cb3dSCy Schubert #include <stdio.h>
392b15cb3dSCy Schubert #include <stdlib.h>
402b15cb3dSCy Schubert #include <string.h>
412b15cb3dSCy Schubert #ifdef EVENT__HAVE_STDARG_H
422b15cb3dSCy Schubert #include <stdarg.h>
432b15cb3dSCy Schubert #endif
442b15cb3dSCy Schubert 
452b15cb3dSCy Schubert #ifdef _WIN32
462b15cb3dSCy Schubert #include <winsock2.h>
472b15cb3dSCy Schubert #endif
482b15cb3dSCy Schubert 
492b15cb3dSCy Schubert #include "event2/util.h"
502b15cb3dSCy Schubert #include "event2/buffer.h"
512b15cb3dSCy Schubert #include "event2/buffer_compat.h"
522b15cb3dSCy Schubert #include "event2/bufferevent.h"
532b15cb3dSCy Schubert #include "event2/bufferevent_struct.h"
542b15cb3dSCy Schubert #include "event2/bufferevent_compat.h"
552b15cb3dSCy Schubert #include "event2/event.h"
562b15cb3dSCy Schubert #include "event-internal.h"
572b15cb3dSCy Schubert #include "log-internal.h"
582b15cb3dSCy Schubert #include "mm-internal.h"
592b15cb3dSCy Schubert #include "bufferevent-internal.h"
602b15cb3dSCy Schubert #include "evbuffer-internal.h"
612b15cb3dSCy Schubert #include "util-internal.h"
622b15cb3dSCy Schubert 
632b15cb3dSCy Schubert static void bufferevent_cancel_all_(struct bufferevent *bev);
642b15cb3dSCy Schubert static void bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_);
652b15cb3dSCy Schubert 
662b15cb3dSCy Schubert void
bufferevent_suspend_read_(struct bufferevent * bufev,bufferevent_suspend_flags what)672b15cb3dSCy Schubert bufferevent_suspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what)
682b15cb3dSCy Schubert {
69*a466cc55SCy Schubert 	struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
702b15cb3dSCy Schubert 	BEV_LOCK(bufev);
712b15cb3dSCy Schubert 	if (!bufev_private->read_suspended)
722b15cb3dSCy Schubert 		bufev->be_ops->disable(bufev, EV_READ);
732b15cb3dSCy Schubert 	bufev_private->read_suspended |= what;
742b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
752b15cb3dSCy Schubert }
762b15cb3dSCy Schubert 
772b15cb3dSCy Schubert void
bufferevent_unsuspend_read_(struct bufferevent * bufev,bufferevent_suspend_flags what)782b15cb3dSCy Schubert bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what)
792b15cb3dSCy Schubert {
80*a466cc55SCy Schubert 	struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
812b15cb3dSCy Schubert 	BEV_LOCK(bufev);
822b15cb3dSCy Schubert 	bufev_private->read_suspended &= ~what;
832b15cb3dSCy Schubert 	if (!bufev_private->read_suspended && (bufev->enabled & EV_READ))
842b15cb3dSCy Schubert 		bufev->be_ops->enable(bufev, EV_READ);
852b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
862b15cb3dSCy Schubert }
872b15cb3dSCy Schubert 
882b15cb3dSCy Schubert void
bufferevent_suspend_write_(struct bufferevent * bufev,bufferevent_suspend_flags what)892b15cb3dSCy Schubert bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what)
902b15cb3dSCy Schubert {
91*a466cc55SCy Schubert 	struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
922b15cb3dSCy Schubert 	BEV_LOCK(bufev);
932b15cb3dSCy Schubert 	if (!bufev_private->write_suspended)
942b15cb3dSCy Schubert 		bufev->be_ops->disable(bufev, EV_WRITE);
952b15cb3dSCy Schubert 	bufev_private->write_suspended |= what;
962b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
972b15cb3dSCy Schubert }
982b15cb3dSCy Schubert 
992b15cb3dSCy Schubert void
bufferevent_unsuspend_write_(struct bufferevent * bufev,bufferevent_suspend_flags what)1002b15cb3dSCy Schubert bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what)
1012b15cb3dSCy Schubert {
102*a466cc55SCy Schubert 	struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
1032b15cb3dSCy Schubert 	BEV_LOCK(bufev);
1042b15cb3dSCy Schubert 	bufev_private->write_suspended &= ~what;
1052b15cb3dSCy Schubert 	if (!bufev_private->write_suspended && (bufev->enabled & EV_WRITE))
1062b15cb3dSCy Schubert 		bufev->be_ops->enable(bufev, EV_WRITE);
1072b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
1082b15cb3dSCy Schubert }
1092b15cb3dSCy Schubert 
110*a466cc55SCy Schubert /**
111*a466cc55SCy Schubert  * Sometimes bufferevent's implementation can overrun high watermarks
112*a466cc55SCy Schubert  * (one of examples is openssl) and in this case if the read callback
113*a466cc55SCy Schubert  * will not handle enough data do over condition above the read
114*a466cc55SCy Schubert  * callback will never be called again (due to suspend above).
115*a466cc55SCy Schubert  *
116*a466cc55SCy Schubert  * To avoid this we are scheduling read callback again here, but only
117*a466cc55SCy Schubert  * from the user callback to avoid multiple scheduling:
118*a466cc55SCy Schubert  * - when the data had been added to it
119*a466cc55SCy Schubert  * - when the data had been drained from it (user specified read callback)
120*a466cc55SCy Schubert  */
bufferevent_inbuf_wm_check(struct bufferevent * bev)121*a466cc55SCy Schubert static void bufferevent_inbuf_wm_check(struct bufferevent *bev)
122*a466cc55SCy Schubert {
123*a466cc55SCy Schubert 	if (!bev->wm_read.high)
124*a466cc55SCy Schubert 		return;
125*a466cc55SCy Schubert 	if (!(bev->enabled & EV_READ))
126*a466cc55SCy Schubert 		return;
127*a466cc55SCy Schubert 	if (evbuffer_get_length(bev->input) < bev->wm_read.high)
128*a466cc55SCy Schubert 		return;
129*a466cc55SCy Schubert 
130*a466cc55SCy Schubert 	bufferevent_trigger(bev, EV_READ, BEV_OPT_DEFER_CALLBACKS);
131*a466cc55SCy Schubert }
1322b15cb3dSCy Schubert 
1332b15cb3dSCy Schubert /* Callback to implement watermarks on the input buffer.  Only enabled
1342b15cb3dSCy Schubert  * if the watermark is set. */
1352b15cb3dSCy Schubert static void
bufferevent_inbuf_wm_cb(struct evbuffer * buf,const struct evbuffer_cb_info * cbinfo,void * arg)1362b15cb3dSCy Schubert bufferevent_inbuf_wm_cb(struct evbuffer *buf,
1372b15cb3dSCy Schubert     const struct evbuffer_cb_info *cbinfo,
1382b15cb3dSCy Schubert     void *arg)
1392b15cb3dSCy Schubert {
1402b15cb3dSCy Schubert 	struct bufferevent *bufev = arg;
1412b15cb3dSCy Schubert 	size_t size;
1422b15cb3dSCy Schubert 
1432b15cb3dSCy Schubert 	size = evbuffer_get_length(buf);
1442b15cb3dSCy Schubert 
1452b15cb3dSCy Schubert 	if (size >= bufev->wm_read.high)
1462b15cb3dSCy Schubert 		bufferevent_wm_suspend_read(bufev);
1472b15cb3dSCy Schubert 	else
1482b15cb3dSCy Schubert 		bufferevent_wm_unsuspend_read(bufev);
1492b15cb3dSCy Schubert }
1502b15cb3dSCy Schubert 
1512b15cb3dSCy Schubert static void
bufferevent_run_deferred_callbacks_locked(struct event_callback * cb,void * arg)1522b15cb3dSCy Schubert bufferevent_run_deferred_callbacks_locked(struct event_callback *cb, void *arg)
1532b15cb3dSCy Schubert {
1542b15cb3dSCy Schubert 	struct bufferevent_private *bufev_private = arg;
1552b15cb3dSCy Schubert 	struct bufferevent *bufev = &bufev_private->bev;
1562b15cb3dSCy Schubert 
1572b15cb3dSCy Schubert 	BEV_LOCK(bufev);
1582b15cb3dSCy Schubert 	if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) &&
1592b15cb3dSCy Schubert 	    bufev->errorcb) {
1602b15cb3dSCy Schubert 		/* The "connected" happened before any reads or writes, so
1612b15cb3dSCy Schubert 		   send it first. */
1622b15cb3dSCy Schubert 		bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED;
1632b15cb3dSCy Schubert 		bufev->errorcb(bufev, BEV_EVENT_CONNECTED, bufev->cbarg);
1642b15cb3dSCy Schubert 	}
1652b15cb3dSCy Schubert 	if (bufev_private->readcb_pending && bufev->readcb) {
1662b15cb3dSCy Schubert 		bufev_private->readcb_pending = 0;
1672b15cb3dSCy Schubert 		bufev->readcb(bufev, bufev->cbarg);
168*a466cc55SCy Schubert 		bufferevent_inbuf_wm_check(bufev);
1692b15cb3dSCy Schubert 	}
1702b15cb3dSCy Schubert 	if (bufev_private->writecb_pending && bufev->writecb) {
1712b15cb3dSCy Schubert 		bufev_private->writecb_pending = 0;
1722b15cb3dSCy Schubert 		bufev->writecb(bufev, bufev->cbarg);
1732b15cb3dSCy Schubert 	}
1742b15cb3dSCy Schubert 	if (bufev_private->eventcb_pending && bufev->errorcb) {
1752b15cb3dSCy Schubert 		short what = bufev_private->eventcb_pending;
1762b15cb3dSCy Schubert 		int err = bufev_private->errno_pending;
1772b15cb3dSCy Schubert 		bufev_private->eventcb_pending = 0;
1782b15cb3dSCy Schubert 		bufev_private->errno_pending = 0;
1792b15cb3dSCy Schubert 		EVUTIL_SET_SOCKET_ERROR(err);
1802b15cb3dSCy Schubert 		bufev->errorcb(bufev, what, bufev->cbarg);
1812b15cb3dSCy Schubert 	}
1822b15cb3dSCy Schubert 	bufferevent_decref_and_unlock_(bufev);
1832b15cb3dSCy Schubert }
1842b15cb3dSCy Schubert 
1852b15cb3dSCy Schubert static void
bufferevent_run_deferred_callbacks_unlocked(struct event_callback * cb,void * arg)1862b15cb3dSCy Schubert bufferevent_run_deferred_callbacks_unlocked(struct event_callback *cb, void *arg)
1872b15cb3dSCy Schubert {
1882b15cb3dSCy Schubert 	struct bufferevent_private *bufev_private = arg;
1892b15cb3dSCy Schubert 	struct bufferevent *bufev = &bufev_private->bev;
1902b15cb3dSCy Schubert 
1912b15cb3dSCy Schubert 	BEV_LOCK(bufev);
1922b15cb3dSCy Schubert #define UNLOCKED(stmt) \
1932b15cb3dSCy Schubert 	do { BEV_UNLOCK(bufev); stmt; BEV_LOCK(bufev); } while(0)
1942b15cb3dSCy Schubert 
1952b15cb3dSCy Schubert 	if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) &&
1962b15cb3dSCy Schubert 	    bufev->errorcb) {
1972b15cb3dSCy Schubert 		/* The "connected" happened before any reads or writes, so
1982b15cb3dSCy Schubert 		   send it first. */
1992b15cb3dSCy Schubert 		bufferevent_event_cb errorcb = bufev->errorcb;
2002b15cb3dSCy Schubert 		void *cbarg = bufev->cbarg;
2012b15cb3dSCy Schubert 		bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED;
2022b15cb3dSCy Schubert 		UNLOCKED(errorcb(bufev, BEV_EVENT_CONNECTED, cbarg));
2032b15cb3dSCy Schubert 	}
2042b15cb3dSCy Schubert 	if (bufev_private->readcb_pending && bufev->readcb) {
2052b15cb3dSCy Schubert 		bufferevent_data_cb readcb = bufev->readcb;
2062b15cb3dSCy Schubert 		void *cbarg = bufev->cbarg;
2072b15cb3dSCy Schubert 		bufev_private->readcb_pending = 0;
2082b15cb3dSCy Schubert 		UNLOCKED(readcb(bufev, cbarg));
209*a466cc55SCy Schubert 		bufferevent_inbuf_wm_check(bufev);
2102b15cb3dSCy Schubert 	}
2112b15cb3dSCy Schubert 	if (bufev_private->writecb_pending && bufev->writecb) {
2122b15cb3dSCy Schubert 		bufferevent_data_cb writecb = bufev->writecb;
2132b15cb3dSCy Schubert 		void *cbarg = bufev->cbarg;
2142b15cb3dSCy Schubert 		bufev_private->writecb_pending = 0;
2152b15cb3dSCy Schubert 		UNLOCKED(writecb(bufev, cbarg));
2162b15cb3dSCy Schubert 	}
2172b15cb3dSCy Schubert 	if (bufev_private->eventcb_pending && bufev->errorcb) {
2182b15cb3dSCy Schubert 		bufferevent_event_cb errorcb = bufev->errorcb;
2192b15cb3dSCy Schubert 		void *cbarg = bufev->cbarg;
2202b15cb3dSCy Schubert 		short what = bufev_private->eventcb_pending;
2212b15cb3dSCy Schubert 		int err = bufev_private->errno_pending;
2222b15cb3dSCy Schubert 		bufev_private->eventcb_pending = 0;
2232b15cb3dSCy Schubert 		bufev_private->errno_pending = 0;
2242b15cb3dSCy Schubert 		EVUTIL_SET_SOCKET_ERROR(err);
2252b15cb3dSCy Schubert 		UNLOCKED(errorcb(bufev,what,cbarg));
2262b15cb3dSCy Schubert 	}
2272b15cb3dSCy Schubert 	bufferevent_decref_and_unlock_(bufev);
2282b15cb3dSCy Schubert #undef UNLOCKED
2292b15cb3dSCy Schubert }
2302b15cb3dSCy Schubert 
2312b15cb3dSCy Schubert #define SCHEDULE_DEFERRED(bevp)						\
2322b15cb3dSCy Schubert 	do {								\
2332b15cb3dSCy Schubert 		if (event_deferred_cb_schedule_(			\
2342b15cb3dSCy Schubert 			    (bevp)->bev.ev_base,			\
2352b15cb3dSCy Schubert 			&(bevp)->deferred))				\
2362b15cb3dSCy Schubert 			bufferevent_incref_(&(bevp)->bev);		\
2372b15cb3dSCy Schubert 	} while (0)
2382b15cb3dSCy Schubert 
2392b15cb3dSCy Schubert 
2402b15cb3dSCy Schubert void
bufferevent_run_readcb_(struct bufferevent * bufev,int options)2412b15cb3dSCy Schubert bufferevent_run_readcb_(struct bufferevent *bufev, int options)
2422b15cb3dSCy Schubert {
2432b15cb3dSCy Schubert 	/* Requires that we hold the lock and a reference */
244*a466cc55SCy Schubert 	struct bufferevent_private *p = BEV_UPCAST(bufev);
2452b15cb3dSCy Schubert 	if (bufev->readcb == NULL)
2462b15cb3dSCy Schubert 		return;
2472b15cb3dSCy Schubert 	if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
2482b15cb3dSCy Schubert 		p->readcb_pending = 1;
2492b15cb3dSCy Schubert 		SCHEDULE_DEFERRED(p);
2502b15cb3dSCy Schubert 	} else {
2512b15cb3dSCy Schubert 		bufev->readcb(bufev, bufev->cbarg);
252*a466cc55SCy Schubert 		bufferevent_inbuf_wm_check(bufev);
2532b15cb3dSCy Schubert 	}
2542b15cb3dSCy Schubert }
2552b15cb3dSCy Schubert 
2562b15cb3dSCy Schubert void
bufferevent_run_writecb_(struct bufferevent * bufev,int options)2572b15cb3dSCy Schubert bufferevent_run_writecb_(struct bufferevent *bufev, int options)
2582b15cb3dSCy Schubert {
2592b15cb3dSCy Schubert 	/* Requires that we hold the lock and a reference */
260*a466cc55SCy Schubert 	struct bufferevent_private *p = BEV_UPCAST(bufev);
2612b15cb3dSCy Schubert 	if (bufev->writecb == NULL)
2622b15cb3dSCy Schubert 		return;
2632b15cb3dSCy Schubert 	if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
2642b15cb3dSCy Schubert 		p->writecb_pending = 1;
2652b15cb3dSCy Schubert 		SCHEDULE_DEFERRED(p);
2662b15cb3dSCy Schubert 	} else {
2672b15cb3dSCy Schubert 		bufev->writecb(bufev, bufev->cbarg);
2682b15cb3dSCy Schubert 	}
2692b15cb3dSCy Schubert }
2702b15cb3dSCy Schubert 
2712b15cb3dSCy Schubert #define BEV_TRIG_ALL_OPTS (			\
2722b15cb3dSCy Schubert 		BEV_TRIG_IGNORE_WATERMARKS|	\
2732b15cb3dSCy Schubert 		BEV_TRIG_DEFER_CALLBACKS	\
2742b15cb3dSCy Schubert 	)
2752b15cb3dSCy Schubert 
2762b15cb3dSCy Schubert void
bufferevent_trigger(struct bufferevent * bufev,short iotype,int options)2772b15cb3dSCy Schubert bufferevent_trigger(struct bufferevent *bufev, short iotype, int options)
2782b15cb3dSCy Schubert {
2792b15cb3dSCy Schubert 	bufferevent_incref_and_lock_(bufev);
2802b15cb3dSCy Schubert 	bufferevent_trigger_nolock_(bufev, iotype, options&BEV_TRIG_ALL_OPTS);
2812b15cb3dSCy Schubert 	bufferevent_decref_and_unlock_(bufev);
2822b15cb3dSCy Schubert }
2832b15cb3dSCy Schubert 
2842b15cb3dSCy Schubert void
bufferevent_run_eventcb_(struct bufferevent * bufev,short what,int options)2852b15cb3dSCy Schubert bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options)
2862b15cb3dSCy Schubert {
2872b15cb3dSCy Schubert 	/* Requires that we hold the lock and a reference */
288*a466cc55SCy Schubert 	struct bufferevent_private *p = BEV_UPCAST(bufev);
2892b15cb3dSCy Schubert 	if (bufev->errorcb == NULL)
2902b15cb3dSCy Schubert 		return;
2912b15cb3dSCy Schubert 	if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
2922b15cb3dSCy Schubert 		p->eventcb_pending |= what;
2932b15cb3dSCy Schubert 		p->errno_pending = EVUTIL_SOCKET_ERROR();
2942b15cb3dSCy Schubert 		SCHEDULE_DEFERRED(p);
2952b15cb3dSCy Schubert 	} else {
2962b15cb3dSCy Schubert 		bufev->errorcb(bufev, what, bufev->cbarg);
2972b15cb3dSCy Schubert 	}
2982b15cb3dSCy Schubert }
2992b15cb3dSCy Schubert 
3002b15cb3dSCy Schubert void
bufferevent_trigger_event(struct bufferevent * bufev,short what,int options)3012b15cb3dSCy Schubert bufferevent_trigger_event(struct bufferevent *bufev, short what, int options)
3022b15cb3dSCy Schubert {
3032b15cb3dSCy Schubert 	bufferevent_incref_and_lock_(bufev);
3042b15cb3dSCy Schubert 	bufferevent_run_eventcb_(bufev, what, options&BEV_TRIG_ALL_OPTS);
3052b15cb3dSCy Schubert 	bufferevent_decref_and_unlock_(bufev);
3062b15cb3dSCy Schubert }
3072b15cb3dSCy Schubert 
3082b15cb3dSCy Schubert int
bufferevent_init_common_(struct bufferevent_private * bufev_private,struct event_base * base,const struct bufferevent_ops * ops,enum bufferevent_options options)3092b15cb3dSCy Schubert bufferevent_init_common_(struct bufferevent_private *bufev_private,
3102b15cb3dSCy Schubert     struct event_base *base,
3112b15cb3dSCy Schubert     const struct bufferevent_ops *ops,
3122b15cb3dSCy Schubert     enum bufferevent_options options)
3132b15cb3dSCy Schubert {
3142b15cb3dSCy Schubert 	struct bufferevent *bufev = &bufev_private->bev;
3152b15cb3dSCy Schubert 
3162b15cb3dSCy Schubert 	if (!bufev->input) {
3172b15cb3dSCy Schubert 		if ((bufev->input = evbuffer_new()) == NULL)
318*a466cc55SCy Schubert 			goto err;
3192b15cb3dSCy Schubert 	}
3202b15cb3dSCy Schubert 
3212b15cb3dSCy Schubert 	if (!bufev->output) {
322*a466cc55SCy Schubert 		if ((bufev->output = evbuffer_new()) == NULL)
323*a466cc55SCy Schubert 			goto err;
3242b15cb3dSCy Schubert 	}
3252b15cb3dSCy Schubert 
3262b15cb3dSCy Schubert 	bufev_private->refcnt = 1;
3272b15cb3dSCy Schubert 	bufev->ev_base = base;
3282b15cb3dSCy Schubert 
3292b15cb3dSCy Schubert 	/* Disable timeouts. */
3302b15cb3dSCy Schubert 	evutil_timerclear(&bufev->timeout_read);
3312b15cb3dSCy Schubert 	evutil_timerclear(&bufev->timeout_write);
3322b15cb3dSCy Schubert 
3332b15cb3dSCy Schubert 	bufev->be_ops = ops;
3342b15cb3dSCy Schubert 
335*a466cc55SCy Schubert 	if (bufferevent_ratelim_init_(bufev_private))
336*a466cc55SCy Schubert 		goto err;
3372b15cb3dSCy Schubert 
3382b15cb3dSCy Schubert 	/*
3392b15cb3dSCy Schubert 	 * Set to EV_WRITE so that using bufferevent_write is going to
3402b15cb3dSCy Schubert 	 * trigger a callback.  Reading needs to be explicitly enabled
3412b15cb3dSCy Schubert 	 * because otherwise no data will be available.
3422b15cb3dSCy Schubert 	 */
3432b15cb3dSCy Schubert 	bufev->enabled = EV_WRITE;
3442b15cb3dSCy Schubert 
3452b15cb3dSCy Schubert #ifndef EVENT__DISABLE_THREAD_SUPPORT
3462b15cb3dSCy Schubert 	if (options & BEV_OPT_THREADSAFE) {
347*a466cc55SCy Schubert 		if (bufferevent_enable_locking_(bufev, NULL) < 0)
348*a466cc55SCy Schubert 			goto err;
3492b15cb3dSCy Schubert 	}
3502b15cb3dSCy Schubert #endif
3512b15cb3dSCy Schubert 	if ((options & (BEV_OPT_DEFER_CALLBACKS|BEV_OPT_UNLOCK_CALLBACKS))
3522b15cb3dSCy Schubert 	    == BEV_OPT_UNLOCK_CALLBACKS) {
3532b15cb3dSCy Schubert 		event_warnx("UNLOCK_CALLBACKS requires DEFER_CALLBACKS");
354*a466cc55SCy Schubert 		goto err;
3552b15cb3dSCy Schubert 	}
3562b15cb3dSCy Schubert 	if (options & BEV_OPT_UNLOCK_CALLBACKS)
3572b15cb3dSCy Schubert 		event_deferred_cb_init_(
3582b15cb3dSCy Schubert 		    &bufev_private->deferred,
3592b15cb3dSCy Schubert 		    event_base_get_npriorities(base) / 2,
3602b15cb3dSCy Schubert 		    bufferevent_run_deferred_callbacks_unlocked,
3612b15cb3dSCy Schubert 		    bufev_private);
3622b15cb3dSCy Schubert 	else
3632b15cb3dSCy Schubert 		event_deferred_cb_init_(
3642b15cb3dSCy Schubert 		    &bufev_private->deferred,
3652b15cb3dSCy Schubert 		    event_base_get_npriorities(base) / 2,
3662b15cb3dSCy Schubert 		    bufferevent_run_deferred_callbacks_locked,
3672b15cb3dSCy Schubert 		    bufev_private);
3682b15cb3dSCy Schubert 
3692b15cb3dSCy Schubert 	bufev_private->options = options;
3702b15cb3dSCy Schubert 
3712b15cb3dSCy Schubert 	evbuffer_set_parent_(bufev->input, bufev);
3722b15cb3dSCy Schubert 	evbuffer_set_parent_(bufev->output, bufev);
3732b15cb3dSCy Schubert 
3742b15cb3dSCy Schubert 	return 0;
375*a466cc55SCy Schubert 
376*a466cc55SCy Schubert err:
377*a466cc55SCy Schubert 	if (bufev->input) {
378*a466cc55SCy Schubert 		evbuffer_free(bufev->input);
379*a466cc55SCy Schubert 		bufev->input = NULL;
380*a466cc55SCy Schubert 	}
381*a466cc55SCy Schubert 	if (bufev->output) {
382*a466cc55SCy Schubert 		evbuffer_free(bufev->output);
383*a466cc55SCy Schubert 		bufev->output = NULL;
384*a466cc55SCy Schubert 	}
385*a466cc55SCy Schubert 	return -1;
3862b15cb3dSCy Schubert }
3872b15cb3dSCy Schubert 
3882b15cb3dSCy Schubert void
bufferevent_setcb(struct bufferevent * bufev,bufferevent_data_cb readcb,bufferevent_data_cb writecb,bufferevent_event_cb eventcb,void * cbarg)3892b15cb3dSCy Schubert bufferevent_setcb(struct bufferevent *bufev,
3902b15cb3dSCy Schubert     bufferevent_data_cb readcb, bufferevent_data_cb writecb,
3912b15cb3dSCy Schubert     bufferevent_event_cb eventcb, void *cbarg)
3922b15cb3dSCy Schubert {
3932b15cb3dSCy Schubert 	BEV_LOCK(bufev);
3942b15cb3dSCy Schubert 
3952b15cb3dSCy Schubert 	bufev->readcb = readcb;
3962b15cb3dSCy Schubert 	bufev->writecb = writecb;
3972b15cb3dSCy Schubert 	bufev->errorcb = eventcb;
3982b15cb3dSCy Schubert 
3992b15cb3dSCy Schubert 	bufev->cbarg = cbarg;
4002b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
4012b15cb3dSCy Schubert }
4022b15cb3dSCy Schubert 
4032b15cb3dSCy Schubert void
bufferevent_getcb(struct bufferevent * bufev,bufferevent_data_cb * readcb_ptr,bufferevent_data_cb * writecb_ptr,bufferevent_event_cb * eventcb_ptr,void ** cbarg_ptr)4042b15cb3dSCy Schubert bufferevent_getcb(struct bufferevent *bufev,
4052b15cb3dSCy Schubert     bufferevent_data_cb *readcb_ptr,
4062b15cb3dSCy Schubert     bufferevent_data_cb *writecb_ptr,
4072b15cb3dSCy Schubert     bufferevent_event_cb *eventcb_ptr,
4082b15cb3dSCy Schubert     void **cbarg_ptr)
4092b15cb3dSCy Schubert {
4102b15cb3dSCy Schubert 	BEV_LOCK(bufev);
4112b15cb3dSCy Schubert 	if (readcb_ptr)
4122b15cb3dSCy Schubert 		*readcb_ptr = bufev->readcb;
4132b15cb3dSCy Schubert 	if (writecb_ptr)
4142b15cb3dSCy Schubert 		*writecb_ptr = bufev->writecb;
4152b15cb3dSCy Schubert 	if (eventcb_ptr)
4162b15cb3dSCy Schubert 		*eventcb_ptr = bufev->errorcb;
4172b15cb3dSCy Schubert 	if (cbarg_ptr)
4182b15cb3dSCy Schubert 		*cbarg_ptr = bufev->cbarg;
4192b15cb3dSCy Schubert 
4202b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
4212b15cb3dSCy Schubert }
4222b15cb3dSCy Schubert 
4232b15cb3dSCy Schubert struct evbuffer *
bufferevent_get_input(struct bufferevent * bufev)4242b15cb3dSCy Schubert bufferevent_get_input(struct bufferevent *bufev)
4252b15cb3dSCy Schubert {
4262b15cb3dSCy Schubert 	return bufev->input;
4272b15cb3dSCy Schubert }
4282b15cb3dSCy Schubert 
4292b15cb3dSCy Schubert struct evbuffer *
bufferevent_get_output(struct bufferevent * bufev)4302b15cb3dSCy Schubert bufferevent_get_output(struct bufferevent *bufev)
4312b15cb3dSCy Schubert {
4322b15cb3dSCy Schubert 	return bufev->output;
4332b15cb3dSCy Schubert }
4342b15cb3dSCy Schubert 
4352b15cb3dSCy Schubert struct event_base *
bufferevent_get_base(struct bufferevent * bufev)4362b15cb3dSCy Schubert bufferevent_get_base(struct bufferevent *bufev)
4372b15cb3dSCy Schubert {
4382b15cb3dSCy Schubert 	return bufev->ev_base;
4392b15cb3dSCy Schubert }
4402b15cb3dSCy Schubert 
4412b15cb3dSCy Schubert int
bufferevent_get_priority(const struct bufferevent * bufev)4422b15cb3dSCy Schubert bufferevent_get_priority(const struct bufferevent *bufev)
4432b15cb3dSCy Schubert {
4442b15cb3dSCy Schubert 	if (event_initialized(&bufev->ev_read)) {
4452b15cb3dSCy Schubert 		return event_get_priority(&bufev->ev_read);
4462b15cb3dSCy Schubert 	} else {
4472b15cb3dSCy Schubert 		return event_base_get_npriorities(bufev->ev_base) / 2;
4482b15cb3dSCy Schubert 	}
4492b15cb3dSCy Schubert }
4502b15cb3dSCy Schubert 
4512b15cb3dSCy Schubert int
bufferevent_write(struct bufferevent * bufev,const void * data,size_t size)4522b15cb3dSCy Schubert bufferevent_write(struct bufferevent *bufev, const void *data, size_t size)
4532b15cb3dSCy Schubert {
4542b15cb3dSCy Schubert 	if (evbuffer_add(bufev->output, data, size) == -1)
4552b15cb3dSCy Schubert 		return (-1);
4562b15cb3dSCy Schubert 
4572b15cb3dSCy Schubert 	return 0;
4582b15cb3dSCy Schubert }
4592b15cb3dSCy Schubert 
4602b15cb3dSCy Schubert int
bufferevent_write_buffer(struct bufferevent * bufev,struct evbuffer * buf)4612b15cb3dSCy Schubert bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf)
4622b15cb3dSCy Schubert {
4632b15cb3dSCy Schubert 	if (evbuffer_add_buffer(bufev->output, buf) == -1)
4642b15cb3dSCy Schubert 		return (-1);
4652b15cb3dSCy Schubert 
4662b15cb3dSCy Schubert 	return 0;
4672b15cb3dSCy Schubert }
4682b15cb3dSCy Schubert 
4692b15cb3dSCy Schubert size_t
bufferevent_read(struct bufferevent * bufev,void * data,size_t size)4702b15cb3dSCy Schubert bufferevent_read(struct bufferevent *bufev, void *data, size_t size)
4712b15cb3dSCy Schubert {
4722b15cb3dSCy Schubert 	return (evbuffer_remove(bufev->input, data, size));
4732b15cb3dSCy Schubert }
4742b15cb3dSCy Schubert 
4752b15cb3dSCy Schubert int
bufferevent_read_buffer(struct bufferevent * bufev,struct evbuffer * buf)4762b15cb3dSCy Schubert bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf)
4772b15cb3dSCy Schubert {
4782b15cb3dSCy Schubert 	return (evbuffer_add_buffer(buf, bufev->input));
4792b15cb3dSCy Schubert }
4802b15cb3dSCy Schubert 
4812b15cb3dSCy Schubert int
bufferevent_enable(struct bufferevent * bufev,short event)4822b15cb3dSCy Schubert bufferevent_enable(struct bufferevent *bufev, short event)
4832b15cb3dSCy Schubert {
484*a466cc55SCy Schubert 	struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
4852b15cb3dSCy Schubert 	short impl_events = event;
4862b15cb3dSCy Schubert 	int r = 0;
4872b15cb3dSCy Schubert 
4882b15cb3dSCy Schubert 	bufferevent_incref_and_lock_(bufev);
4892b15cb3dSCy Schubert 	if (bufev_private->read_suspended)
4902b15cb3dSCy Schubert 		impl_events &= ~EV_READ;
4912b15cb3dSCy Schubert 	if (bufev_private->write_suspended)
4922b15cb3dSCy Schubert 		impl_events &= ~EV_WRITE;
4932b15cb3dSCy Schubert 
4942b15cb3dSCy Schubert 	bufev->enabled |= event;
4952b15cb3dSCy Schubert 
4962b15cb3dSCy Schubert 	if (impl_events && bufev->be_ops->enable(bufev, impl_events) < 0)
4972b15cb3dSCy Schubert 		r = -1;
498*a466cc55SCy Schubert 	if (r)
499*a466cc55SCy Schubert 		event_debug(("%s: cannot enable 0x%hx on %p", __func__, event, bufev));
5002b15cb3dSCy Schubert 
5012b15cb3dSCy Schubert 	bufferevent_decref_and_unlock_(bufev);
5022b15cb3dSCy Schubert 	return r;
5032b15cb3dSCy Schubert }
5042b15cb3dSCy Schubert 
5052b15cb3dSCy Schubert int
bufferevent_set_timeouts(struct bufferevent * bufev,const struct timeval * tv_read,const struct timeval * tv_write)5062b15cb3dSCy Schubert bufferevent_set_timeouts(struct bufferevent *bufev,
5072b15cb3dSCy Schubert 			 const struct timeval *tv_read,
5082b15cb3dSCy Schubert 			 const struct timeval *tv_write)
5092b15cb3dSCy Schubert {
5102b15cb3dSCy Schubert 	int r = 0;
5112b15cb3dSCy Schubert 	BEV_LOCK(bufev);
5122b15cb3dSCy Schubert 	if (tv_read) {
5132b15cb3dSCy Schubert 		bufev->timeout_read = *tv_read;
5142b15cb3dSCy Schubert 	} else {
5152b15cb3dSCy Schubert 		evutil_timerclear(&bufev->timeout_read);
5162b15cb3dSCy Schubert 	}
5172b15cb3dSCy Schubert 	if (tv_write) {
5182b15cb3dSCy Schubert 		bufev->timeout_write = *tv_write;
5192b15cb3dSCy Schubert 	} else {
5202b15cb3dSCy Schubert 		evutil_timerclear(&bufev->timeout_write);
5212b15cb3dSCy Schubert 	}
5222b15cb3dSCy Schubert 
5232b15cb3dSCy Schubert 	if (bufev->be_ops->adj_timeouts)
5242b15cb3dSCy Schubert 		r = bufev->be_ops->adj_timeouts(bufev);
5252b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
5262b15cb3dSCy Schubert 
5272b15cb3dSCy Schubert 	return r;
5282b15cb3dSCy Schubert }
5292b15cb3dSCy Schubert 
5302b15cb3dSCy Schubert 
5312b15cb3dSCy Schubert /* Obsolete; use bufferevent_set_timeouts */
5322b15cb3dSCy Schubert void
bufferevent_settimeout(struct bufferevent * bufev,int timeout_read,int timeout_write)5332b15cb3dSCy Schubert bufferevent_settimeout(struct bufferevent *bufev,
5342b15cb3dSCy Schubert 		       int timeout_read, int timeout_write)
5352b15cb3dSCy Schubert {
5362b15cb3dSCy Schubert 	struct timeval tv_read, tv_write;
5372b15cb3dSCy Schubert 	struct timeval *ptv_read = NULL, *ptv_write = NULL;
5382b15cb3dSCy Schubert 
5392b15cb3dSCy Schubert 	memset(&tv_read, 0, sizeof(tv_read));
5402b15cb3dSCy Schubert 	memset(&tv_write, 0, sizeof(tv_write));
5412b15cb3dSCy Schubert 
5422b15cb3dSCy Schubert 	if (timeout_read) {
5432b15cb3dSCy Schubert 		tv_read.tv_sec = timeout_read;
5442b15cb3dSCy Schubert 		ptv_read = &tv_read;
5452b15cb3dSCy Schubert 	}
5462b15cb3dSCy Schubert 	if (timeout_write) {
5472b15cb3dSCy Schubert 		tv_write.tv_sec = timeout_write;
5482b15cb3dSCy Schubert 		ptv_write = &tv_write;
5492b15cb3dSCy Schubert 	}
5502b15cb3dSCy Schubert 
5512b15cb3dSCy Schubert 	bufferevent_set_timeouts(bufev, ptv_read, ptv_write);
5522b15cb3dSCy Schubert }
5532b15cb3dSCy Schubert 
5542b15cb3dSCy Schubert 
5552b15cb3dSCy Schubert int
bufferevent_disable_hard_(struct bufferevent * bufev,short event)5562b15cb3dSCy Schubert bufferevent_disable_hard_(struct bufferevent *bufev, short event)
5572b15cb3dSCy Schubert {
5582b15cb3dSCy Schubert 	int r = 0;
559*a466cc55SCy Schubert 	struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
5602b15cb3dSCy Schubert 
5612b15cb3dSCy Schubert 	BEV_LOCK(bufev);
5622b15cb3dSCy Schubert 	bufev->enabled &= ~event;
5632b15cb3dSCy Schubert 
5642b15cb3dSCy Schubert 	bufev_private->connecting = 0;
5652b15cb3dSCy Schubert 	if (bufev->be_ops->disable(bufev, event) < 0)
5662b15cb3dSCy Schubert 		r = -1;
5672b15cb3dSCy Schubert 
5682b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
5692b15cb3dSCy Schubert 	return r;
5702b15cb3dSCy Schubert }
5712b15cb3dSCy Schubert 
5722b15cb3dSCy Schubert int
bufferevent_disable(struct bufferevent * bufev,short event)5732b15cb3dSCy Schubert bufferevent_disable(struct bufferevent *bufev, short event)
5742b15cb3dSCy Schubert {
5752b15cb3dSCy Schubert 	int r = 0;
5762b15cb3dSCy Schubert 
5772b15cb3dSCy Schubert 	BEV_LOCK(bufev);
5782b15cb3dSCy Schubert 	bufev->enabled &= ~event;
5792b15cb3dSCy Schubert 
5802b15cb3dSCy Schubert 	if (bufev->be_ops->disable(bufev, event) < 0)
5812b15cb3dSCy Schubert 		r = -1;
582*a466cc55SCy Schubert 	if (r)
583*a466cc55SCy Schubert 		event_debug(("%s: cannot disable 0x%hx on %p", __func__, event, bufev));
5842b15cb3dSCy Schubert 
5852b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
5862b15cb3dSCy Schubert 	return r;
5872b15cb3dSCy Schubert }
5882b15cb3dSCy Schubert 
5892b15cb3dSCy Schubert /*
5902b15cb3dSCy Schubert  * Sets the water marks
5912b15cb3dSCy Schubert  */
5922b15cb3dSCy Schubert 
5932b15cb3dSCy Schubert void
bufferevent_setwatermark(struct bufferevent * bufev,short events,size_t lowmark,size_t highmark)5942b15cb3dSCy Schubert bufferevent_setwatermark(struct bufferevent *bufev, short events,
5952b15cb3dSCy Schubert     size_t lowmark, size_t highmark)
5962b15cb3dSCy Schubert {
597*a466cc55SCy Schubert 	struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
5982b15cb3dSCy Schubert 
5992b15cb3dSCy Schubert 	BEV_LOCK(bufev);
6002b15cb3dSCy Schubert 	if (events & EV_WRITE) {
6012b15cb3dSCy Schubert 		bufev->wm_write.low = lowmark;
6022b15cb3dSCy Schubert 		bufev->wm_write.high = highmark;
6032b15cb3dSCy Schubert 	}
6042b15cb3dSCy Schubert 
6052b15cb3dSCy Schubert 	if (events & EV_READ) {
6062b15cb3dSCy Schubert 		bufev->wm_read.low = lowmark;
6072b15cb3dSCy Schubert 		bufev->wm_read.high = highmark;
6082b15cb3dSCy Schubert 
6092b15cb3dSCy Schubert 		if (highmark) {
6102b15cb3dSCy Schubert 			/* There is now a new high-water mark for read.
6112b15cb3dSCy Schubert 			   enable the callback if needed, and see if we should
6122b15cb3dSCy Schubert 			   suspend/bufferevent_wm_unsuspend. */
6132b15cb3dSCy Schubert 
6142b15cb3dSCy Schubert 			if (bufev_private->read_watermarks_cb == NULL) {
6152b15cb3dSCy Schubert 				bufev_private->read_watermarks_cb =
6162b15cb3dSCy Schubert 				    evbuffer_add_cb(bufev->input,
6172b15cb3dSCy Schubert 						    bufferevent_inbuf_wm_cb,
6182b15cb3dSCy Schubert 						    bufev);
6192b15cb3dSCy Schubert 			}
6202b15cb3dSCy Schubert 			evbuffer_cb_set_flags(bufev->input,
6212b15cb3dSCy Schubert 				      bufev_private->read_watermarks_cb,
6222b15cb3dSCy Schubert 				      EVBUFFER_CB_ENABLED|EVBUFFER_CB_NODEFER);
6232b15cb3dSCy Schubert 
624a25439b6SCy Schubert 			if (evbuffer_get_length(bufev->input) >= highmark)
6252b15cb3dSCy Schubert 				bufferevent_wm_suspend_read(bufev);
6262b15cb3dSCy Schubert 			else if (evbuffer_get_length(bufev->input) < highmark)
6272b15cb3dSCy Schubert 				bufferevent_wm_unsuspend_read(bufev);
6282b15cb3dSCy Schubert 		} else {
6292b15cb3dSCy Schubert 			/* There is now no high-water mark for read. */
6302b15cb3dSCy Schubert 			if (bufev_private->read_watermarks_cb)
6312b15cb3dSCy Schubert 				evbuffer_cb_clear_flags(bufev->input,
6322b15cb3dSCy Schubert 				    bufev_private->read_watermarks_cb,
6332b15cb3dSCy Schubert 				    EVBUFFER_CB_ENABLED);
6342b15cb3dSCy Schubert 			bufferevent_wm_unsuspend_read(bufev);
6352b15cb3dSCy Schubert 		}
6362b15cb3dSCy Schubert 	}
6372b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
6382b15cb3dSCy Schubert }
6392b15cb3dSCy Schubert 
640a25439b6SCy Schubert int
bufferevent_getwatermark(struct bufferevent * bufev,short events,size_t * lowmark,size_t * highmark)6412b15cb3dSCy Schubert bufferevent_getwatermark(struct bufferevent *bufev, short events,
6422b15cb3dSCy Schubert     size_t *lowmark, size_t *highmark)
6432b15cb3dSCy Schubert {
6442b15cb3dSCy Schubert 	if (events == EV_WRITE) {
645a25439b6SCy Schubert 		BEV_LOCK(bufev);
6462b15cb3dSCy Schubert 		if (lowmark)
6472b15cb3dSCy Schubert 			*lowmark = bufev->wm_write.low;
6482b15cb3dSCy Schubert 		if (highmark)
6492b15cb3dSCy Schubert 			*highmark = bufev->wm_write.high;
650a25439b6SCy Schubert 		BEV_UNLOCK(bufev);
651a25439b6SCy Schubert 		return 0;
6522b15cb3dSCy Schubert 	}
6532b15cb3dSCy Schubert 
6542b15cb3dSCy Schubert 	if (events == EV_READ) {
655a25439b6SCy Schubert 		BEV_LOCK(bufev);
6562b15cb3dSCy Schubert 		if (lowmark)
6572b15cb3dSCy Schubert 			*lowmark = bufev->wm_read.low;
6582b15cb3dSCy Schubert 		if (highmark)
6592b15cb3dSCy Schubert 			*highmark = bufev->wm_read.high;
6602b15cb3dSCy Schubert 		BEV_UNLOCK(bufev);
661a25439b6SCy Schubert 		return 0;
662a25439b6SCy Schubert 	}
663a25439b6SCy Schubert 	return -1;
6642b15cb3dSCy Schubert }
6652b15cb3dSCy Schubert 
6662b15cb3dSCy Schubert int
bufferevent_flush(struct bufferevent * bufev,short iotype,enum bufferevent_flush_mode mode)6672b15cb3dSCy Schubert bufferevent_flush(struct bufferevent *bufev,
6682b15cb3dSCy Schubert     short iotype,
6692b15cb3dSCy Schubert     enum bufferevent_flush_mode mode)
6702b15cb3dSCy Schubert {
6712b15cb3dSCy Schubert 	int r = -1;
6722b15cb3dSCy Schubert 	BEV_LOCK(bufev);
6732b15cb3dSCy Schubert 	if (bufev->be_ops->flush)
6742b15cb3dSCy Schubert 		r = bufev->be_ops->flush(bufev, iotype, mode);
6752b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
6762b15cb3dSCy Schubert 	return r;
6772b15cb3dSCy Schubert }
6782b15cb3dSCy Schubert 
6792b15cb3dSCy Schubert void
bufferevent_incref_and_lock_(struct bufferevent * bufev)6802b15cb3dSCy Schubert bufferevent_incref_and_lock_(struct bufferevent *bufev)
6812b15cb3dSCy Schubert {
682*a466cc55SCy Schubert 	struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
6832b15cb3dSCy Schubert 	BEV_LOCK(bufev);
6842b15cb3dSCy Schubert 	++bufev_private->refcnt;
6852b15cb3dSCy Schubert }
6862b15cb3dSCy Schubert 
6872b15cb3dSCy Schubert #if 0
6882b15cb3dSCy Schubert static void
6892b15cb3dSCy Schubert bufferevent_transfer_lock_ownership_(struct bufferevent *donor,
6902b15cb3dSCy Schubert     struct bufferevent *recipient)
6912b15cb3dSCy Schubert {
6922b15cb3dSCy Schubert 	struct bufferevent_private *d = BEV_UPCAST(donor);
6932b15cb3dSCy Schubert 	struct bufferevent_private *r = BEV_UPCAST(recipient);
6942b15cb3dSCy Schubert 	if (d->lock != r->lock)
6952b15cb3dSCy Schubert 		return;
6962b15cb3dSCy Schubert 	if (r->own_lock)
6972b15cb3dSCy Schubert 		return;
6982b15cb3dSCy Schubert 	if (d->own_lock) {
6992b15cb3dSCy Schubert 		d->own_lock = 0;
7002b15cb3dSCy Schubert 		r->own_lock = 1;
7012b15cb3dSCy Schubert 	}
7022b15cb3dSCy Schubert }
7032b15cb3dSCy Schubert #endif
7042b15cb3dSCy Schubert 
7052b15cb3dSCy Schubert int
bufferevent_decref_and_unlock_(struct bufferevent * bufev)7062b15cb3dSCy Schubert bufferevent_decref_and_unlock_(struct bufferevent *bufev)
7072b15cb3dSCy Schubert {
708*a466cc55SCy Schubert 	struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
7092b15cb3dSCy Schubert 	int n_cbs = 0;
7102b15cb3dSCy Schubert #define MAX_CBS 16
7112b15cb3dSCy Schubert 	struct event_callback *cbs[MAX_CBS];
7122b15cb3dSCy Schubert 
7132b15cb3dSCy Schubert 	EVUTIL_ASSERT(bufev_private->refcnt > 0);
7142b15cb3dSCy Schubert 
7152b15cb3dSCy Schubert 	if (--bufev_private->refcnt) {
7162b15cb3dSCy Schubert 		BEV_UNLOCK(bufev);
7172b15cb3dSCy Schubert 		return 0;
7182b15cb3dSCy Schubert 	}
7192b15cb3dSCy Schubert 
7202b15cb3dSCy Schubert 	if (bufev->be_ops->unlink)
7212b15cb3dSCy Schubert 		bufev->be_ops->unlink(bufev);
7222b15cb3dSCy Schubert 
7232b15cb3dSCy Schubert 	/* Okay, we're out of references. Let's finalize this once all the
7242b15cb3dSCy Schubert 	 * callbacks are done running. */
7252b15cb3dSCy Schubert 	cbs[0] = &bufev->ev_read.ev_evcallback;
7262b15cb3dSCy Schubert 	cbs[1] = &bufev->ev_write.ev_evcallback;
7272b15cb3dSCy Schubert 	cbs[2] = &bufev_private->deferred;
7282b15cb3dSCy Schubert 	n_cbs = 3;
7292b15cb3dSCy Schubert 	if (bufev_private->rate_limiting) {
7302b15cb3dSCy Schubert 		struct event *e = &bufev_private->rate_limiting->refill_bucket_event;
7312b15cb3dSCy Schubert 		if (event_initialized(e))
7322b15cb3dSCy Schubert 			cbs[n_cbs++] = &e->ev_evcallback;
7332b15cb3dSCy Schubert 	}
7342b15cb3dSCy Schubert 	n_cbs += evbuffer_get_callbacks_(bufev->input, cbs+n_cbs, MAX_CBS-n_cbs);
7352b15cb3dSCy Schubert 	n_cbs += evbuffer_get_callbacks_(bufev->output, cbs+n_cbs, MAX_CBS-n_cbs);
7362b15cb3dSCy Schubert 
7372b15cb3dSCy Schubert 	event_callback_finalize_many_(bufev->ev_base, n_cbs, cbs,
7382b15cb3dSCy Schubert 	    bufferevent_finalize_cb_);
7392b15cb3dSCy Schubert 
7402b15cb3dSCy Schubert #undef MAX_CBS
7412b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
7422b15cb3dSCy Schubert 
7432b15cb3dSCy Schubert 	return 1;
7442b15cb3dSCy Schubert }
7452b15cb3dSCy Schubert 
7462b15cb3dSCy Schubert static void
bufferevent_finalize_cb_(struct event_callback * evcb,void * arg_)7472b15cb3dSCy Schubert bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_)
7482b15cb3dSCy Schubert {
7492b15cb3dSCy Schubert 	struct bufferevent *bufev = arg_;
7502b15cb3dSCy Schubert 	struct bufferevent *underlying;
751*a466cc55SCy Schubert 	struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
7522b15cb3dSCy Schubert 
7532b15cb3dSCy Schubert 	BEV_LOCK(bufev);
7542b15cb3dSCy Schubert 	underlying = bufferevent_get_underlying(bufev);
7552b15cb3dSCy Schubert 
7562b15cb3dSCy Schubert 	/* Clean up the shared info */
7572b15cb3dSCy Schubert 	if (bufev->be_ops->destruct)
7582b15cb3dSCy Schubert 		bufev->be_ops->destruct(bufev);
7592b15cb3dSCy Schubert 
7602b15cb3dSCy Schubert 	/* XXX what happens if refcnt for these buffers is > 1?
7612b15cb3dSCy Schubert 	 * The buffers can share a lock with this bufferevent object,
7622b15cb3dSCy Schubert 	 * but the lock might be destroyed below. */
7632b15cb3dSCy Schubert 	/* evbuffer will free the callbacks */
7642b15cb3dSCy Schubert 	evbuffer_free(bufev->input);
7652b15cb3dSCy Schubert 	evbuffer_free(bufev->output);
7662b15cb3dSCy Schubert 
7672b15cb3dSCy Schubert 	if (bufev_private->rate_limiting) {
7682b15cb3dSCy Schubert 		if (bufev_private->rate_limiting->group)
7692b15cb3dSCy Schubert 			bufferevent_remove_from_rate_limit_group_internal_(bufev,0);
7702b15cb3dSCy Schubert 		mm_free(bufev_private->rate_limiting);
7712b15cb3dSCy Schubert 		bufev_private->rate_limiting = NULL;
7722b15cb3dSCy Schubert 	}
7732b15cb3dSCy Schubert 
7742b15cb3dSCy Schubert 
7752b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
7762b15cb3dSCy Schubert 
7772b15cb3dSCy Schubert 	if (bufev_private->own_lock)
7782b15cb3dSCy Schubert 		EVTHREAD_FREE_LOCK(bufev_private->lock,
7792b15cb3dSCy Schubert 		    EVTHREAD_LOCKTYPE_RECURSIVE);
7802b15cb3dSCy Schubert 
7812b15cb3dSCy Schubert 	/* Free the actual allocated memory. */
7822b15cb3dSCy Schubert 	mm_free(((char*)bufev) - bufev->be_ops->mem_offset);
7832b15cb3dSCy Schubert 
7842b15cb3dSCy Schubert 	/* Release the reference to underlying now that we no longer need the
7852b15cb3dSCy Schubert 	 * reference to it.  We wait this long mainly in case our lock is
7862b15cb3dSCy Schubert 	 * shared with underlying.
7872b15cb3dSCy Schubert 	 *
7882b15cb3dSCy Schubert 	 * The 'destruct' function will also drop a reference to underlying
7892b15cb3dSCy Schubert 	 * if BEV_OPT_CLOSE_ON_FREE is set.
7902b15cb3dSCy Schubert 	 *
7912b15cb3dSCy Schubert 	 * XXX Should we/can we just refcount evbuffer/bufferevent locks?
7922b15cb3dSCy Schubert 	 * It would probably save us some headaches.
7932b15cb3dSCy Schubert 	 */
7942b15cb3dSCy Schubert 	if (underlying)
7952b15cb3dSCy Schubert 		bufferevent_decref_(underlying);
7962b15cb3dSCy Schubert }
7972b15cb3dSCy Schubert 
7982b15cb3dSCy Schubert int
bufferevent_decref(struct bufferevent * bufev)799*a466cc55SCy Schubert bufferevent_decref(struct bufferevent *bufev)
8002b15cb3dSCy Schubert {
8012b15cb3dSCy Schubert 	BEV_LOCK(bufev);
8022b15cb3dSCy Schubert 	return bufferevent_decref_and_unlock_(bufev);
8032b15cb3dSCy Schubert }
8042b15cb3dSCy Schubert 
8052b15cb3dSCy Schubert void
bufferevent_free(struct bufferevent * bufev)8062b15cb3dSCy Schubert bufferevent_free(struct bufferevent *bufev)
8072b15cb3dSCy Schubert {
8082b15cb3dSCy Schubert 	BEV_LOCK(bufev);
8092b15cb3dSCy Schubert 	bufferevent_setcb(bufev, NULL, NULL, NULL, NULL);
8102b15cb3dSCy Schubert 	bufferevent_cancel_all_(bufev);
8112b15cb3dSCy Schubert 	bufferevent_decref_and_unlock_(bufev);
8122b15cb3dSCy Schubert }
8132b15cb3dSCy Schubert 
8142b15cb3dSCy Schubert void
bufferevent_incref(struct bufferevent * bufev)815*a466cc55SCy Schubert bufferevent_incref(struct bufferevent *bufev)
8162b15cb3dSCy Schubert {
817*a466cc55SCy Schubert 	struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
8182b15cb3dSCy Schubert 
819*a466cc55SCy Schubert 	/* XXX: now that this function is public, we might want to
820*a466cc55SCy Schubert 	 * - return the count from this function
821*a466cc55SCy Schubert 	 * - create a new function to atomically grab the current refcount
822*a466cc55SCy Schubert 	 */
8232b15cb3dSCy Schubert 	BEV_LOCK(bufev);
8242b15cb3dSCy Schubert 	++bufev_private->refcnt;
8252b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
8262b15cb3dSCy Schubert }
8272b15cb3dSCy Schubert 
8282b15cb3dSCy Schubert int
bufferevent_enable_locking_(struct bufferevent * bufev,void * lock)8292b15cb3dSCy Schubert bufferevent_enable_locking_(struct bufferevent *bufev, void *lock)
8302b15cb3dSCy Schubert {
8312b15cb3dSCy Schubert #ifdef EVENT__DISABLE_THREAD_SUPPORT
8322b15cb3dSCy Schubert 	return -1;
8332b15cb3dSCy Schubert #else
8342b15cb3dSCy Schubert 	struct bufferevent *underlying;
8352b15cb3dSCy Schubert 
8362b15cb3dSCy Schubert 	if (BEV_UPCAST(bufev)->lock)
8372b15cb3dSCy Schubert 		return -1;
8382b15cb3dSCy Schubert 	underlying = bufferevent_get_underlying(bufev);
8392b15cb3dSCy Schubert 
8402b15cb3dSCy Schubert 	if (!lock && underlying && BEV_UPCAST(underlying)->lock) {
8412b15cb3dSCy Schubert 		lock = BEV_UPCAST(underlying)->lock;
8422b15cb3dSCy Schubert 		BEV_UPCAST(bufev)->lock = lock;
8432b15cb3dSCy Schubert 		BEV_UPCAST(bufev)->own_lock = 0;
8442b15cb3dSCy Schubert 	} else if (!lock) {
8452b15cb3dSCy Schubert 		EVTHREAD_ALLOC_LOCK(lock, EVTHREAD_LOCKTYPE_RECURSIVE);
8462b15cb3dSCy Schubert 		if (!lock)
8472b15cb3dSCy Schubert 			return -1;
8482b15cb3dSCy Schubert 		BEV_UPCAST(bufev)->lock = lock;
8492b15cb3dSCy Schubert 		BEV_UPCAST(bufev)->own_lock = 1;
8502b15cb3dSCy Schubert 	} else {
8512b15cb3dSCy Schubert 		BEV_UPCAST(bufev)->lock = lock;
8522b15cb3dSCy Schubert 		BEV_UPCAST(bufev)->own_lock = 0;
8532b15cb3dSCy Schubert 	}
8542b15cb3dSCy Schubert 	evbuffer_enable_locking(bufev->input, lock);
8552b15cb3dSCy Schubert 	evbuffer_enable_locking(bufev->output, lock);
8562b15cb3dSCy Schubert 
8572b15cb3dSCy Schubert 	if (underlying && !BEV_UPCAST(underlying)->lock)
8582b15cb3dSCy Schubert 		bufferevent_enable_locking_(underlying, lock);
8592b15cb3dSCy Schubert 
8602b15cb3dSCy Schubert 	return 0;
8612b15cb3dSCy Schubert #endif
8622b15cb3dSCy Schubert }
8632b15cb3dSCy Schubert 
8642b15cb3dSCy Schubert int
bufferevent_setfd(struct bufferevent * bev,evutil_socket_t fd)8652b15cb3dSCy Schubert bufferevent_setfd(struct bufferevent *bev, evutil_socket_t fd)
8662b15cb3dSCy Schubert {
8672b15cb3dSCy Schubert 	union bufferevent_ctrl_data d;
8682b15cb3dSCy Schubert 	int res = -1;
8692b15cb3dSCy Schubert 	d.fd = fd;
8702b15cb3dSCy Schubert 	BEV_LOCK(bev);
8712b15cb3dSCy Schubert 	if (bev->be_ops->ctrl)
8722b15cb3dSCy Schubert 		res = bev->be_ops->ctrl(bev, BEV_CTRL_SET_FD, &d);
873*a466cc55SCy Schubert 	if (res)
874*a466cc55SCy Schubert 		event_debug(("%s: cannot set fd for %p to "EV_SOCK_FMT, __func__, bev, fd));
8752b15cb3dSCy Schubert 	BEV_UNLOCK(bev);
8762b15cb3dSCy Schubert 	return res;
8772b15cb3dSCy Schubert }
8782b15cb3dSCy Schubert 
8792b15cb3dSCy Schubert evutil_socket_t
bufferevent_getfd(struct bufferevent * bev)8802b15cb3dSCy Schubert bufferevent_getfd(struct bufferevent *bev)
8812b15cb3dSCy Schubert {
8822b15cb3dSCy Schubert 	union bufferevent_ctrl_data d;
8832b15cb3dSCy Schubert 	int res = -1;
8842b15cb3dSCy Schubert 	d.fd = -1;
8852b15cb3dSCy Schubert 	BEV_LOCK(bev);
8862b15cb3dSCy Schubert 	if (bev->be_ops->ctrl)
8872b15cb3dSCy Schubert 		res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_FD, &d);
888*a466cc55SCy Schubert 	if (res)
889*a466cc55SCy Schubert 		event_debug(("%s: cannot get fd for %p", __func__, bev));
8902b15cb3dSCy Schubert 	BEV_UNLOCK(bev);
8912b15cb3dSCy Schubert 	return (res<0) ? -1 : d.fd;
8922b15cb3dSCy Schubert }
8932b15cb3dSCy Schubert 
8942b15cb3dSCy Schubert enum bufferevent_options
bufferevent_get_options_(struct bufferevent * bev)8952b15cb3dSCy Schubert bufferevent_get_options_(struct bufferevent *bev)
8962b15cb3dSCy Schubert {
897*a466cc55SCy Schubert 	struct bufferevent_private *bev_p = BEV_UPCAST(bev);
8982b15cb3dSCy Schubert 	enum bufferevent_options options;
8992b15cb3dSCy Schubert 
9002b15cb3dSCy Schubert 	BEV_LOCK(bev);
9012b15cb3dSCy Schubert 	options = bev_p->options;
9022b15cb3dSCy Schubert 	BEV_UNLOCK(bev);
9032b15cb3dSCy Schubert 	return options;
9042b15cb3dSCy Schubert }
9052b15cb3dSCy Schubert 
9062b15cb3dSCy Schubert 
9072b15cb3dSCy Schubert static void
bufferevent_cancel_all_(struct bufferevent * bev)9082b15cb3dSCy Schubert bufferevent_cancel_all_(struct bufferevent *bev)
9092b15cb3dSCy Schubert {
9102b15cb3dSCy Schubert 	union bufferevent_ctrl_data d;
9112b15cb3dSCy Schubert 	memset(&d, 0, sizeof(d));
9122b15cb3dSCy Schubert 	BEV_LOCK(bev);
9132b15cb3dSCy Schubert 	if (bev->be_ops->ctrl)
9142b15cb3dSCy Schubert 		bev->be_ops->ctrl(bev, BEV_CTRL_CANCEL_ALL, &d);
9152b15cb3dSCy Schubert 	BEV_UNLOCK(bev);
9162b15cb3dSCy Schubert }
9172b15cb3dSCy Schubert 
9182b15cb3dSCy Schubert short
bufferevent_get_enabled(struct bufferevent * bufev)9192b15cb3dSCy Schubert bufferevent_get_enabled(struct bufferevent *bufev)
9202b15cb3dSCy Schubert {
9212b15cb3dSCy Schubert 	short r;
9222b15cb3dSCy Schubert 	BEV_LOCK(bufev);
9232b15cb3dSCy Schubert 	r = bufev->enabled;
9242b15cb3dSCy Schubert 	BEV_UNLOCK(bufev);
9252b15cb3dSCy Schubert 	return r;
9262b15cb3dSCy Schubert }
9272b15cb3dSCy Schubert 
9282b15cb3dSCy Schubert struct bufferevent *
bufferevent_get_underlying(struct bufferevent * bev)9292b15cb3dSCy Schubert bufferevent_get_underlying(struct bufferevent *bev)
9302b15cb3dSCy Schubert {
9312b15cb3dSCy Schubert 	union bufferevent_ctrl_data d;
9322b15cb3dSCy Schubert 	int res = -1;
9332b15cb3dSCy Schubert 	d.ptr = NULL;
9342b15cb3dSCy Schubert 	BEV_LOCK(bev);
9352b15cb3dSCy Schubert 	if (bev->be_ops->ctrl)
9362b15cb3dSCy Schubert 		res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_UNDERLYING, &d);
9372b15cb3dSCy Schubert 	BEV_UNLOCK(bev);
9382b15cb3dSCy Schubert 	return (res<0) ? NULL : d.ptr;
9392b15cb3dSCy Schubert }
9402b15cb3dSCy Schubert 
9412b15cb3dSCy Schubert static void
bufferevent_generic_read_timeout_cb(evutil_socket_t fd,short event,void * ctx)9422b15cb3dSCy Schubert bufferevent_generic_read_timeout_cb(evutil_socket_t fd, short event, void *ctx)
9432b15cb3dSCy Schubert {
9442b15cb3dSCy Schubert 	struct bufferevent *bev = ctx;
9452b15cb3dSCy Schubert 	bufferevent_incref_and_lock_(bev);
9462b15cb3dSCy Schubert 	bufferevent_disable(bev, EV_READ);
9472b15cb3dSCy Schubert 	bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_READING, 0);
9482b15cb3dSCy Schubert 	bufferevent_decref_and_unlock_(bev);
9492b15cb3dSCy Schubert }
9502b15cb3dSCy Schubert static void
bufferevent_generic_write_timeout_cb(evutil_socket_t fd,short event,void * ctx)9512b15cb3dSCy Schubert bufferevent_generic_write_timeout_cb(evutil_socket_t fd, short event, void *ctx)
9522b15cb3dSCy Schubert {
9532b15cb3dSCy Schubert 	struct bufferevent *bev = ctx;
9542b15cb3dSCy Schubert 	bufferevent_incref_and_lock_(bev);
9552b15cb3dSCy Schubert 	bufferevent_disable(bev, EV_WRITE);
9562b15cb3dSCy Schubert 	bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING, 0);
9572b15cb3dSCy Schubert 	bufferevent_decref_and_unlock_(bev);
9582b15cb3dSCy Schubert }
9592b15cb3dSCy Schubert 
9602b15cb3dSCy Schubert void
bufferevent_init_generic_timeout_cbs_(struct bufferevent * bev)9612b15cb3dSCy Schubert bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev)
9622b15cb3dSCy Schubert {
9632b15cb3dSCy Schubert 	event_assign(&bev->ev_read, bev->ev_base, -1, EV_FINALIZE,
9642b15cb3dSCy Schubert 	    bufferevent_generic_read_timeout_cb, bev);
9652b15cb3dSCy Schubert 	event_assign(&bev->ev_write, bev->ev_base, -1, EV_FINALIZE,
9662b15cb3dSCy Schubert 	    bufferevent_generic_write_timeout_cb, bev);
9672b15cb3dSCy Schubert }
9682b15cb3dSCy Schubert 
9692b15cb3dSCy Schubert int
bufferevent_generic_adj_timeouts_(struct bufferevent * bev)9702b15cb3dSCy Schubert bufferevent_generic_adj_timeouts_(struct bufferevent *bev)
9712b15cb3dSCy Schubert {
9722b15cb3dSCy Schubert 	const short enabled = bev->enabled;
973*a466cc55SCy Schubert 	struct bufferevent_private *bev_p = BEV_UPCAST(bev);
9742b15cb3dSCy Schubert 	int r1=0, r2=0;
9752b15cb3dSCy Schubert 	if ((enabled & EV_READ) && !bev_p->read_suspended &&
9762b15cb3dSCy Schubert 	    evutil_timerisset(&bev->timeout_read))
9772b15cb3dSCy Schubert 		r1 = event_add(&bev->ev_read, &bev->timeout_read);
9782b15cb3dSCy Schubert 	else
9792b15cb3dSCy Schubert 		r1 = event_del(&bev->ev_read);
9802b15cb3dSCy Schubert 
9812b15cb3dSCy Schubert 	if ((enabled & EV_WRITE) && !bev_p->write_suspended &&
9822b15cb3dSCy Schubert 	    evutil_timerisset(&bev->timeout_write) &&
9832b15cb3dSCy Schubert 	    evbuffer_get_length(bev->output))
9842b15cb3dSCy Schubert 		r2 = event_add(&bev->ev_write, &bev->timeout_write);
9852b15cb3dSCy Schubert 	else
9862b15cb3dSCy Schubert 		r2 = event_del(&bev->ev_write);
9872b15cb3dSCy Schubert 	if (r1 < 0 || r2 < 0)
9882b15cb3dSCy Schubert 		return -1;
9892b15cb3dSCy Schubert 	return 0;
9902b15cb3dSCy Schubert }
9912b15cb3dSCy Schubert 
9922b15cb3dSCy Schubert int
bufferevent_generic_adj_existing_timeouts_(struct bufferevent * bev)993*a466cc55SCy Schubert bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev)
994*a466cc55SCy Schubert {
995*a466cc55SCy Schubert 	int r = 0;
996*a466cc55SCy Schubert 	if (event_pending(&bev->ev_read, EV_READ, NULL)) {
997*a466cc55SCy Schubert 		if (evutil_timerisset(&bev->timeout_read)) {
998*a466cc55SCy Schubert 			    if (bufferevent_add_event_(&bev->ev_read, &bev->timeout_read) < 0)
999*a466cc55SCy Schubert 				    r = -1;
1000*a466cc55SCy Schubert 		} else {
1001*a466cc55SCy Schubert 			event_remove_timer(&bev->ev_read);
1002*a466cc55SCy Schubert 		}
1003*a466cc55SCy Schubert 	}
1004*a466cc55SCy Schubert 	if (event_pending(&bev->ev_write, EV_WRITE, NULL)) {
1005*a466cc55SCy Schubert 		if (evutil_timerisset(&bev->timeout_write)) {
1006*a466cc55SCy Schubert 			if (bufferevent_add_event_(&bev->ev_write, &bev->timeout_write) < 0)
1007*a466cc55SCy Schubert 				r = -1;
1008*a466cc55SCy Schubert 		} else {
1009*a466cc55SCy Schubert 			event_remove_timer(&bev->ev_write);
1010*a466cc55SCy Schubert 		}
1011*a466cc55SCy Schubert 	}
1012*a466cc55SCy Schubert 	return r;
1013*a466cc55SCy Schubert }
1014*a466cc55SCy Schubert 
1015*a466cc55SCy Schubert int
bufferevent_add_event_(struct event * ev,const struct timeval * tv)10162b15cb3dSCy Schubert bufferevent_add_event_(struct event *ev, const struct timeval *tv)
10172b15cb3dSCy Schubert {
1018*a466cc55SCy Schubert 	if (!evutil_timerisset(tv))
10192b15cb3dSCy Schubert 		return event_add(ev, NULL);
10202b15cb3dSCy Schubert 	else
10212b15cb3dSCy Schubert 		return event_add(ev, tv);
10222b15cb3dSCy Schubert }
10232b15cb3dSCy Schubert 
10242b15cb3dSCy Schubert /* For use by user programs only; internally, we should be calling
10252b15cb3dSCy Schubert    either bufferevent_incref_and_lock_(), or BEV_LOCK. */
10262b15cb3dSCy Schubert void
bufferevent_lock(struct bufferevent * bev)10272b15cb3dSCy Schubert bufferevent_lock(struct bufferevent *bev)
10282b15cb3dSCy Schubert {
10292b15cb3dSCy Schubert 	bufferevent_incref_and_lock_(bev);
10302b15cb3dSCy Schubert }
10312b15cb3dSCy Schubert 
10322b15cb3dSCy Schubert void
bufferevent_unlock(struct bufferevent * bev)10332b15cb3dSCy Schubert bufferevent_unlock(struct bufferevent *bev)
10342b15cb3dSCy Schubert {
10352b15cb3dSCy Schubert 	bufferevent_decref_and_unlock_(bev);
10362b15cb3dSCy Schubert }
1037