1290001Sglebius/*
2290001Sglebius * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
3290001Sglebius *
4290001Sglebius * Redistribution and use in source and binary forms, with or without
5290001Sglebius * modification, are permitted provided that the following conditions
6290001Sglebius * are met:
7290001Sglebius * 1. Redistributions of source code must retain the above copyright
8290001Sglebius *    notice, this list of conditions and the following disclaimer.
9290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright
10290001Sglebius *    notice, this list of conditions and the following disclaimer in the
11290001Sglebius *    documentation and/or other materials provided with the distribution.
12290001Sglebius * 3. The name of the author may not be used to endorse or promote products
13290001Sglebius *    derived from this software without specific prior written permission.
14290001Sglebius *
15290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16290001Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17290001Sglebius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18290001Sglebius * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19290001Sglebius * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20290001Sglebius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21290001Sglebius * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22290001Sglebius * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23290001Sglebius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24290001Sglebius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25290001Sglebius */
26290001Sglebius#ifndef BUFFEREVENT_INTERNAL_H_INCLUDED_
27290001Sglebius#define BUFFEREVENT_INTERNAL_H_INCLUDED_
28290001Sglebius
29290001Sglebius#ifdef __cplusplus
30290001Sglebiusextern "C" {
31290001Sglebius#endif
32290001Sglebius
33290001Sglebius#include "event2/event-config.h"
34290001Sglebius#include "event2/event_struct.h"
35290001Sglebius#include "evconfig-private.h"
36290001Sglebius#include "event2/util.h"
37290001Sglebius#include "defer-internal.h"
38290001Sglebius#include "evthread-internal.h"
39290001Sglebius#include "event2/thread.h"
40290001Sglebius#include "ratelim-internal.h"
41290001Sglebius#include "event2/bufferevent_struct.h"
42290001Sglebius
43290001Sglebius/* These flags are reasons that we might be declining to actually enable
44290001Sglebius   reading or writing on a bufferevent.
45290001Sglebius */
46290001Sglebius
47290001Sglebius/* On a all bufferevents, for reading: used when we have read up to the
48290001Sglebius   watermark value.
49290001Sglebius
50290001Sglebius   On a filtering bufferevent, for writing: used when the underlying
51290001Sglebius   bufferevent's write buffer has been filled up to its watermark
52290001Sglebius   value.
53290001Sglebius*/
54290001Sglebius#define BEV_SUSPEND_WM 0x01
55290001Sglebius/* On a base bufferevent: when we have emptied a bandwidth buckets */
56290001Sglebius#define BEV_SUSPEND_BW 0x02
57290001Sglebius/* On a base bufferevent: when we have emptied the group's bandwidth bucket. */
58290001Sglebius#define BEV_SUSPEND_BW_GROUP 0x04
59290001Sglebius/* On a socket bufferevent: can't do any operations while we're waiting for
60290001Sglebius * name lookup to finish. */
61290001Sglebius#define BEV_SUSPEND_LOOKUP 0x08
62290001Sglebius/* On a base bufferevent, for reading: used when a filter has choked this
63290001Sglebius * (underlying) bufferevent because it has stopped reading from it. */
64290001Sglebius#define BEV_SUSPEND_FILT_READ 0x10
65290001Sglebius
66290001Sglebiustypedef ev_uint16_t bufferevent_suspend_flags;
67290001Sglebius
68290001Sglebiusstruct bufferevent_rate_limit_group {
69290001Sglebius	/** List of all members in the group */
70290001Sglebius	LIST_HEAD(rlim_group_member_list, bufferevent_private) members;
71290001Sglebius	/** Current limits for the group. */
72290001Sglebius	struct ev_token_bucket rate_limit;
73290001Sglebius	struct ev_token_bucket_cfg rate_limit_cfg;
74290001Sglebius
75290001Sglebius	/** True iff we don't want to read from any member of the group.until
76290001Sglebius	 * the token bucket refills.  */
77290001Sglebius	unsigned read_suspended : 1;
78290001Sglebius	/** True iff we don't want to write from any member of the group.until
79290001Sglebius	 * the token bucket refills.  */
80290001Sglebius	unsigned write_suspended : 1;
81290001Sglebius	/** True iff we were unable to suspend one of the bufferevents in the
82290001Sglebius	 * group for reading the last time we tried, and we should try
83290001Sglebius	 * again. */
84290001Sglebius	unsigned pending_unsuspend_read : 1;
85290001Sglebius	/** True iff we were unable to suspend one of the bufferevents in the
86290001Sglebius	 * group for writing the last time we tried, and we should try
87290001Sglebius	 * again. */
88290001Sglebius	unsigned pending_unsuspend_write : 1;
89290001Sglebius
90290001Sglebius	/*@{*/
91290001Sglebius	/** Total number of bytes read or written in this group since last
92290001Sglebius	 * reset. */
93290001Sglebius	ev_uint64_t total_read;
94290001Sglebius	ev_uint64_t total_written;
95290001Sglebius	/*@}*/
96290001Sglebius
97290001Sglebius	/** The number of bufferevents in the group. */
98290001Sglebius	int n_members;
99290001Sglebius
100290001Sglebius	/** The smallest number of bytes that any member of the group should
101290001Sglebius	 * be limited to read or write at a time. */
102290001Sglebius	ev_ssize_t min_share;
103290001Sglebius	ev_ssize_t configured_min_share;
104290001Sglebius
105290001Sglebius	/** Timeout event that goes off once a tick, when the bucket is ready
106290001Sglebius	 * to refill. */
107290001Sglebius	struct event master_refill_event;
108290001Sglebius
109290001Sglebius	/** Seed for weak random number generator. Protected by 'lock' */
110290001Sglebius	struct evutil_weakrand_state weakrand_seed;
111290001Sglebius
112290001Sglebius	/** Lock to protect the members of this group.  This lock should nest
113290001Sglebius	 * within every bufferevent lock: if you are holding this lock, do
114290001Sglebius	 * not assume you can lock another bufferevent. */
115290001Sglebius	void *lock;
116290001Sglebius};
117290001Sglebius
118290001Sglebius/** Fields for rate-limiting a single bufferevent. */
119290001Sglebiusstruct bufferevent_rate_limit {
120290001Sglebius	/* Linked-list elements for storing this bufferevent_private in a
121290001Sglebius	 * group.
122290001Sglebius	 *
123290001Sglebius	 * Note that this field is supposed to be protected by the group
124290001Sglebius	 * lock */
125290001Sglebius	LIST_ENTRY(bufferevent_private) next_in_group;
126290001Sglebius	/** The rate-limiting group for this bufferevent, or NULL if it is
127290001Sglebius	 * only rate-limited on its own. */
128290001Sglebius	struct bufferevent_rate_limit_group *group;
129290001Sglebius
130290001Sglebius	/* This bufferevent's current limits. */
131290001Sglebius	struct ev_token_bucket limit;
132290001Sglebius	/* Pointer to the rate-limit configuration for this bufferevent.
133290001Sglebius	 * Can be shared.  XXX reference-count this? */
134290001Sglebius	struct ev_token_bucket_cfg *cfg;
135290001Sglebius
136290001Sglebius	/* Timeout event used when one this bufferevent's buckets are
137290001Sglebius	 * empty. */
138290001Sglebius	struct event refill_bucket_event;
139290001Sglebius};
140290001Sglebius
141290001Sglebius/** Parts of the bufferevent structure that are shared among all bufferevent
142290001Sglebius * types, but not exposed in bufferevent_struct.h. */
143290001Sglebiusstruct bufferevent_private {
144290001Sglebius	/** The underlying bufferevent structure. */
145290001Sglebius	struct bufferevent bev;
146290001Sglebius
147290001Sglebius	/** Evbuffer callback to enforce watermarks on input. */
148290001Sglebius	struct evbuffer_cb_entry *read_watermarks_cb;
149290001Sglebius
150290001Sglebius	/** If set, we should free the lock when we free the bufferevent. */
151290001Sglebius	unsigned own_lock : 1;
152290001Sglebius
153290001Sglebius	/** Flag: set if we have deferred callbacks and a read callback is
154290001Sglebius	 * pending. */
155290001Sglebius	unsigned readcb_pending : 1;
156290001Sglebius	/** Flag: set if we have deferred callbacks and a write callback is
157290001Sglebius	 * pending. */
158290001Sglebius	unsigned writecb_pending : 1;
159290001Sglebius	/** Flag: set if we are currently busy connecting. */
160290001Sglebius	unsigned connecting : 1;
161290001Sglebius	/** Flag: set if a connect failed prematurely; this is a hack for
162290001Sglebius	 * getting around the bufferevent abstraction. */
163290001Sglebius	unsigned connection_refused : 1;
164290001Sglebius	/** Set to the events pending if we have deferred callbacks and
165290001Sglebius	 * an events callback is pending. */
166290001Sglebius	short eventcb_pending;
167290001Sglebius
168290001Sglebius	/** If set, read is suspended until one or more conditions are over.
169290001Sglebius	 * The actual value here is a bitfield of those conditions; see the
170290001Sglebius	 * BEV_SUSPEND_* flags above. */
171290001Sglebius	bufferevent_suspend_flags read_suspended;
172290001Sglebius
173290001Sglebius	/** If set, writing is suspended until one or more conditions are over.
174290001Sglebius	 * The actual value here is a bitfield of those conditions; see the
175290001Sglebius	 * BEV_SUSPEND_* flags above. */
176290001Sglebius	bufferevent_suspend_flags write_suspended;
177290001Sglebius
178290001Sglebius	/** Set to the current socket errno if we have deferred callbacks and
179290001Sglebius	 * an events callback is pending. */
180290001Sglebius	int errno_pending;
181290001Sglebius
182290001Sglebius	/** The DNS error code for bufferevent_socket_connect_hostname */
183290001Sglebius	int dns_error;
184290001Sglebius
185290001Sglebius	/** Used to implement deferred callbacks */
186290001Sglebius	struct event_callback deferred;
187290001Sglebius
188290001Sglebius	/** The options this bufferevent was constructed with */
189290001Sglebius	enum bufferevent_options options;
190290001Sglebius
191290001Sglebius	/** Current reference count for this bufferevent. */
192290001Sglebius	int refcnt;
193290001Sglebius
194290001Sglebius	/** Lock for this bufferevent.  Shared by the inbuf and the outbuf.
195290001Sglebius	 * If NULL, locking is disabled. */
196290001Sglebius	void *lock;
197290001Sglebius
198290001Sglebius	/** No matter how big our bucket gets, don't try to read more than this
199290001Sglebius	 * much in a single read operation. */
200290001Sglebius	ev_ssize_t max_single_read;
201290001Sglebius
202290001Sglebius	/** No matter how big our bucket gets, don't try to write more than this
203290001Sglebius	 * much in a single write operation. */
204290001Sglebius	ev_ssize_t max_single_write;
205290001Sglebius
206290001Sglebius	/** Rate-limiting information for this bufferevent */
207290001Sglebius	struct bufferevent_rate_limit *rate_limiting;
208290001Sglebius};
209290001Sglebius
210290001Sglebius/** Possible operations for a control callback. */
211290001Sglebiusenum bufferevent_ctrl_op {
212290001Sglebius	BEV_CTRL_SET_FD,
213290001Sglebius	BEV_CTRL_GET_FD,
214290001Sglebius	BEV_CTRL_GET_UNDERLYING,
215290001Sglebius	BEV_CTRL_CANCEL_ALL
216290001Sglebius};
217290001Sglebius
218290001Sglebius/** Possible data types for a control callback */
219290001Sglebiusunion bufferevent_ctrl_data {
220290001Sglebius	void *ptr;
221290001Sglebius	evutil_socket_t fd;
222290001Sglebius};
223290001Sglebius
224290001Sglebius/**
225290001Sglebius   Implementation table for a bufferevent: holds function pointers and other
226290001Sglebius   information to make the various bufferevent types work.
227290001Sglebius*/
228290001Sglebiusstruct bufferevent_ops {
229290001Sglebius	/** The name of the bufferevent's type. */
230290001Sglebius	const char *type;
231290001Sglebius	/** At what offset into the implementation type will we find a
232290001Sglebius	    bufferevent structure?
233290001Sglebius
234290001Sglebius	    Example: if the type is implemented as
235290001Sglebius	    struct bufferevent_x {
236290001Sglebius	       int extra_data;
237290001Sglebius	       struct bufferevent bev;
238290001Sglebius	    }
239290001Sglebius	    then mem_offset should be offsetof(struct bufferevent_x, bev)
240290001Sglebius	*/
241290001Sglebius	off_t mem_offset;
242290001Sglebius
243290001Sglebius	/** Enables one or more of EV_READ|EV_WRITE on a bufferevent.  Does
244290001Sglebius	    not need to adjust the 'enabled' field.  Returns 0 on success, -1
245290001Sglebius	    on failure.
246290001Sglebius	 */
247290001Sglebius	int (*enable)(struct bufferevent *, short);
248290001Sglebius
249290001Sglebius	/** Disables one or more of EV_READ|EV_WRITE on a bufferevent.  Does
250290001Sglebius	    not need to adjust the 'enabled' field.  Returns 0 on success, -1
251290001Sglebius	    on failure.
252290001Sglebius	 */
253290001Sglebius	int (*disable)(struct bufferevent *, short);
254290001Sglebius
255290001Sglebius	/** Detatches the bufferevent from related data structures. Called as
256290001Sglebius	 * soon as its reference count reaches 0. */
257290001Sglebius	void (*unlink)(struct bufferevent *);
258290001Sglebius
259290001Sglebius	/** Free any storage and deallocate any extra data or structures used
260290001Sglebius	    in this implementation. Called when the bufferevent is
261290001Sglebius	    finalized.
262290001Sglebius	 */
263290001Sglebius	void (*destruct)(struct bufferevent *);
264290001Sglebius
265290001Sglebius	/** Called when the timeouts on the bufferevent have changed.*/
266290001Sglebius	int (*adj_timeouts)(struct bufferevent *);
267290001Sglebius
268290001Sglebius	/** Called to flush data. */
269290001Sglebius	int (*flush)(struct bufferevent *, short, enum bufferevent_flush_mode);
270290001Sglebius
271290001Sglebius	/** Called to access miscellaneous fields. */
272290001Sglebius	int (*ctrl)(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
273290001Sglebius
274290001Sglebius};
275290001Sglebius
276290001Sglebiusextern const struct bufferevent_ops bufferevent_ops_socket;
277290001Sglebiusextern const struct bufferevent_ops bufferevent_ops_filter;
278290001Sglebiusextern const struct bufferevent_ops bufferevent_ops_pair;
279290001Sglebius
280290001Sglebius#define BEV_IS_SOCKET(bevp) ((bevp)->be_ops == &bufferevent_ops_socket)
281290001Sglebius#define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter)
282290001Sglebius#define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair)
283290001Sglebius
284290001Sglebius#ifdef _WIN32
285290001Sglebiusextern const struct bufferevent_ops bufferevent_ops_async;
286290001Sglebius#define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async)
287290001Sglebius#else
288290001Sglebius#define BEV_IS_ASYNC(bevp) 0
289290001Sglebius#endif
290290001Sglebius
291290001Sglebius/** Initialize the shared parts of a bufferevent. */
292290001Sglebiusint bufferevent_init_common_(struct bufferevent_private *, struct event_base *, const struct bufferevent_ops *, enum bufferevent_options options);
293290001Sglebius
294290001Sglebius/** For internal use: temporarily stop all reads on bufev, until the conditions
295290001Sglebius * in 'what' are over. */
296290001Sglebiusvoid bufferevent_suspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
297290001Sglebius/** For internal use: clear the conditions 'what' on bufev, and re-enable
298290001Sglebius * reading if there are no conditions left. */
299290001Sglebiusvoid bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
300290001Sglebius
301290001Sglebius/** For internal use: temporarily stop all writes on bufev, until the conditions
302290001Sglebius * in 'what' are over. */
303290001Sglebiusvoid bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
304290001Sglebius/** For internal use: clear the conditions 'what' on bufev, and re-enable
305290001Sglebius * writing if there are no conditions left. */
306290001Sglebiusvoid bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
307290001Sglebius
308290001Sglebius#define bufferevent_wm_suspend_read(b) \
309290001Sglebius	bufferevent_suspend_read_((b), BEV_SUSPEND_WM)
310290001Sglebius#define bufferevent_wm_unsuspend_read(b) \
311290001Sglebius	bufferevent_unsuspend_read_((b), BEV_SUSPEND_WM)
312290001Sglebius
313290001Sglebius/*
314290001Sglebius  Disable a bufferevent.  Equivalent to bufferevent_disable(), but
315290001Sglebius  first resets 'connecting' flag to force EV_WRITE down for sure.
316290001Sglebius
317290001Sglebius  XXXX this method will go away in the future; try not to add new users.
318290001Sglebius    See comment in evhttp_connection_reset_() for discussion.
319290001Sglebius
320290001Sglebius  @param bufev the bufferevent to be disabled
321290001Sglebius  @param event any combination of EV_READ | EV_WRITE.
322290001Sglebius  @return 0 if successful, or -1 if an error occurred
323290001Sglebius  @see bufferevent_disable()
324290001Sglebius */
325290001Sglebiusint bufferevent_disable_hard_(struct bufferevent *bufev, short event);
326290001Sglebius
327290001Sglebius/** Internal: Set up locking on a bufferevent.  If lock is set, use it.
328290001Sglebius * Otherwise, use a new lock. */
329290001Sglebiusint bufferevent_enable_locking_(struct bufferevent *bufev, void *lock);
330290001Sglebius/** Internal: Increment the reference count on bufev. */
331290001Sglebiusvoid bufferevent_incref_(struct bufferevent *bufev);
332290001Sglebius/** Internal: Lock bufev and increase its reference count.
333290001Sglebius * unlocking it otherwise. */
334290001Sglebiusvoid bufferevent_incref_and_lock_(struct bufferevent *bufev);
335290001Sglebius/** Internal: Decrement the reference count on bufev.  Returns 1 if it freed
336290001Sglebius * the bufferevent.*/
337290001Sglebiusint bufferevent_decref_(struct bufferevent *bufev);
338290001Sglebius/** Internal: Drop the reference count on bufev, freeing as necessary, and
339290001Sglebius * unlocking it otherwise.  Returns 1 if it freed the bufferevent. */
340290001Sglebiusint bufferevent_decref_and_unlock_(struct bufferevent *bufev);
341290001Sglebius
342290001Sglebius/** Internal: If callbacks are deferred and we have a read callback, schedule
343290001Sglebius * a readcb.  Otherwise just run the readcb. Ignores watermarks. */
344290001Sglebiusvoid bufferevent_run_readcb_(struct bufferevent *bufev, int options);
345290001Sglebius/** Internal: If callbacks are deferred and we have a write callback, schedule
346290001Sglebius * a writecb.  Otherwise just run the writecb. Ignores watermarks. */
347290001Sglebiusvoid bufferevent_run_writecb_(struct bufferevent *bufev, int options);
348290001Sglebius/** Internal: If callbacks are deferred and we have an eventcb, schedule
349290001Sglebius * it to run with events "what".  Otherwise just run the eventcb.
350290001Sglebius * See bufferevent_trigger_event for meaning of "options". */
351290001Sglebiusvoid bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options);
352290001Sglebius
353290001Sglebius/** Internal: Run or schedule (if deferred or options contain
354290001Sglebius * BEV_TRIG_DEFER_CALLBACKS) I/O callbacks specified in iotype.
355290001Sglebius * Must already hold the bufev lock. Honors watermarks unless
356290001Sglebius * BEV_TRIG_IGNORE_WATERMARKS is in options. */
357290001Sglebiusstatic inline void bufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options);
358290001Sglebius
359290001Sglebius/* Making this inline since all of the common-case calls to this function in
360290001Sglebius * libevent use constant arguments. */
361290001Sglebiusstatic inline void
362290001Sglebiusbufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options)
363290001Sglebius{
364290001Sglebius	if ((iotype & EV_READ) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
365290001Sglebius	    evbuffer_get_length(bufev->input) >= bufev->wm_read.low))
366290001Sglebius		bufferevent_run_readcb_(bufev, options);
367290001Sglebius	if ((iotype & EV_WRITE) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
368290001Sglebius	    evbuffer_get_length(bufev->output) <= bufev->wm_write.low))
369290001Sglebius		bufferevent_run_writecb_(bufev, options);
370290001Sglebius}
371290001Sglebius
372290001Sglebius/** Internal: Add the event 'ev' with timeout tv, unless tv is set to 0, in
373290001Sglebius * which case add ev with no timeout. */
374290001Sglebiusint bufferevent_add_event_(struct event *ev, const struct timeval *tv);
375290001Sglebius
376290001Sglebius/* =========
377290001Sglebius * These next functions implement timeouts for bufferevents that aren't doing
378290001Sglebius * anything else with ev_read and ev_write, to handle timeouts.
379290001Sglebius * ========= */
380290001Sglebius/** Internal use: Set up the ev_read and ev_write callbacks so that
381290001Sglebius * the other "generic_timeout" functions will work on it.  Call this from
382290001Sglebius * the constructor function. */
383290001Sglebiusvoid bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev);
384290001Sglebius/** Internal use: Add or delete the generic timeout events as appropriate.
385290001Sglebius * (If an event is enabled and a timeout is set, we add the event.  Otherwise
386290001Sglebius * we delete it.)  Call this from anything that changes the timeout values,
387290001Sglebius * that enabled EV_READ or EV_WRITE, or that disables EV_READ or EV_WRITE. */
388290001Sglebiusint bufferevent_generic_adj_timeouts_(struct bufferevent *bev);
389290001Sglebius
390290001Sglebiusenum bufferevent_options bufferevent_get_options_(struct bufferevent *bev);
391290001Sglebius
392290001Sglebius/** Internal use: We have just successfully read data into an inbuf, so
393290001Sglebius * reset the read timeout (if any). */
394290001Sglebius#define BEV_RESET_GENERIC_READ_TIMEOUT(bev)				\
395290001Sglebius	do {								\
396290001Sglebius		if (evutil_timerisset(&(bev)->timeout_read))		\
397290001Sglebius			event_add(&(bev)->ev_read, &(bev)->timeout_read); \
398290001Sglebius	} while (0)
399290001Sglebius/** Internal use: We have just successfully written data from an inbuf, so
400290001Sglebius * reset the read timeout (if any). */
401290001Sglebius#define BEV_RESET_GENERIC_WRITE_TIMEOUT(bev)				\
402290001Sglebius	do {								\
403290001Sglebius		if (evutil_timerisset(&(bev)->timeout_write))		\
404290001Sglebius			event_add(&(bev)->ev_write, &(bev)->timeout_write); \
405290001Sglebius	} while (0)
406290001Sglebius#define BEV_DEL_GENERIC_READ_TIMEOUT(bev)	\
407290001Sglebius		event_del(&(bev)->ev_read)
408290001Sglebius#define BEV_DEL_GENERIC_WRITE_TIMEOUT(bev)	\
409290001Sglebius		event_del(&(bev)->ev_write)
410290001Sglebius
411290001Sglebius
412290001Sglebius/** Internal: Given a bufferevent, return its corresponding
413290001Sglebius * bufferevent_private. */
414290001Sglebius#define BEV_UPCAST(b) EVUTIL_UPCAST((b), struct bufferevent_private, bev)
415290001Sglebius
416290001Sglebius#ifdef EVENT__DISABLE_THREAD_SUPPORT
417290001Sglebius#define BEV_LOCK(b) EVUTIL_NIL_STMT_
418290001Sglebius#define BEV_UNLOCK(b) EVUTIL_NIL_STMT_
419290001Sglebius#else
420290001Sglebius/** Internal: Grab the lock (if any) on a bufferevent */
421290001Sglebius#define BEV_LOCK(b) do {						\
422290001Sglebius		struct bufferevent_private *locking =  BEV_UPCAST(b);	\
423290001Sglebius		EVLOCK_LOCK(locking->lock, 0);				\
424290001Sglebius	} while (0)
425290001Sglebius
426290001Sglebius/** Internal: Release the lock (if any) on a bufferevent */
427290001Sglebius#define BEV_UNLOCK(b) do {						\
428290001Sglebius		struct bufferevent_private *locking =  BEV_UPCAST(b);	\
429290001Sglebius		EVLOCK_UNLOCK(locking->lock, 0);			\
430290001Sglebius	} while (0)
431290001Sglebius#endif
432290001Sglebius
433290001Sglebius
434290001Sglebius/* ==== For rate-limiting. */
435290001Sglebius
436290001Sglebiusint bufferevent_decrement_write_buckets_(struct bufferevent_private *bev,
437290001Sglebius    ev_ssize_t bytes);
438290001Sglebiusint bufferevent_decrement_read_buckets_(struct bufferevent_private *bev,
439290001Sglebius    ev_ssize_t bytes);
440290001Sglebiusev_ssize_t bufferevent_get_read_max_(struct bufferevent_private *bev);
441290001Sglebiusev_ssize_t bufferevent_get_write_max_(struct bufferevent_private *bev);
442290001Sglebius
443290001Sglebiusint bufferevent_ratelim_init_(struct bufferevent_private *bev);
444290001Sglebius
445290001Sglebius#ifdef __cplusplus
446290001Sglebius}
447290001Sglebius#endif
448290001Sglebius
449290001Sglebius
450290001Sglebius#endif /* BUFFEREVENT_INTERNAL_H_INCLUDED_ */
451