1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/third_party/libevent/bufferevent-internal.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,410 @@ 1.4 +/* 1.5 + * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1. Redistributions of source code must retain the above copyright 1.11 + * notice, this list of conditions and the following disclaimer. 1.12 + * 2. Redistributions in binary form must reproduce the above copyright 1.13 + * notice, this list of conditions and the following disclaimer in the 1.14 + * documentation and/or other materials provided with the distribution. 1.15 + * 3. The name of the author may not be used to endorse or promote products 1.16 + * derived from this software without specific prior written permission. 1.17 + * 1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1.20 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1.21 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1.22 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1.23 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.24 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.25 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.27 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.28 + */ 1.29 +#ifndef _BUFFEREVENT_INTERNAL_H_ 1.30 +#define _BUFFEREVENT_INTERNAL_H_ 1.31 + 1.32 +#ifdef __cplusplus 1.33 +extern "C" { 1.34 +#endif 1.35 + 1.36 +#include "event2/event-config.h" 1.37 +#include "event2/util.h" 1.38 +#include "defer-internal.h" 1.39 +#include "evthread-internal.h" 1.40 +#include "event2/thread.h" 1.41 +#include "ratelim-internal.h" 1.42 +#include "event2/bufferevent_struct.h" 1.43 + 1.44 +/* These flags are reasons that we might be declining to actually enable 1.45 + reading or writing on a bufferevent. 1.46 + */ 1.47 + 1.48 +/* On a all bufferevents, for reading: used when we have read up to the 1.49 + watermark value. 1.50 + 1.51 + On a filtering bufferevent, for writing: used when the underlying 1.52 + bufferevent's write buffer has been filled up to its watermark 1.53 + value. 1.54 +*/ 1.55 +#define BEV_SUSPEND_WM 0x01 1.56 +/* On a base bufferevent: when we have emptied a bandwidth buckets */ 1.57 +#define BEV_SUSPEND_BW 0x02 1.58 +/* On a base bufferevent: when we have emptied the group's bandwidth bucket. */ 1.59 +#define BEV_SUSPEND_BW_GROUP 0x04 1.60 +/* On a socket bufferevent: can't do any operations while we're waiting for 1.61 + * name lookup to finish. */ 1.62 +#define BEV_SUSPEND_LOOKUP 0x08 1.63 +/* On a base bufferevent, for reading: used when a filter has choked this 1.64 + * (underlying) bufferevent because it has stopped reading from it. */ 1.65 +#define BEV_SUSPEND_FILT_READ 0x10 1.66 + 1.67 +typedef ev_uint16_t bufferevent_suspend_flags; 1.68 + 1.69 +struct bufferevent_rate_limit_group { 1.70 + /** List of all members in the group */ 1.71 + TAILQ_HEAD(rlim_group_member_list, bufferevent_private) members; 1.72 + /** Current limits for the group. */ 1.73 + struct ev_token_bucket rate_limit; 1.74 + struct ev_token_bucket_cfg rate_limit_cfg; 1.75 + 1.76 + /** True iff we don't want to read from any member of the group.until 1.77 + * the token bucket refills. */ 1.78 + unsigned read_suspended : 1; 1.79 + /** True iff we don't want to write from any member of the group.until 1.80 + * the token bucket refills. */ 1.81 + unsigned write_suspended : 1; 1.82 + /** True iff we were unable to suspend one of the bufferevents in the 1.83 + * group for reading the last time we tried, and we should try 1.84 + * again. */ 1.85 + unsigned pending_unsuspend_read : 1; 1.86 + /** True iff we were unable to suspend one of the bufferevents in the 1.87 + * group for writing the last time we tried, and we should try 1.88 + * again. */ 1.89 + unsigned pending_unsuspend_write : 1; 1.90 + 1.91 + /*@{*/ 1.92 + /** Total number of bytes read or written in this group since last 1.93 + * reset. */ 1.94 + ev_uint64_t total_read; 1.95 + ev_uint64_t total_written; 1.96 + /*@}*/ 1.97 + 1.98 + /** The number of bufferevents in the group. */ 1.99 + int n_members; 1.100 + 1.101 + /** The smallest number of bytes that any member of the group should 1.102 + * be limited to read or write at a time. */ 1.103 + ev_ssize_t min_share; 1.104 + ev_ssize_t configured_min_share; 1.105 + 1.106 + /** Timeout event that goes off once a tick, when the bucket is ready 1.107 + * to refill. */ 1.108 + struct event master_refill_event; 1.109 + /** Lock to protect the members of this group. This lock should nest 1.110 + * within every bufferevent lock: if you are holding this lock, do 1.111 + * not assume you can lock another bufferevent. */ 1.112 + void *lock; 1.113 +}; 1.114 + 1.115 +/** Fields for rate-limiting a single bufferevent. */ 1.116 +struct bufferevent_rate_limit { 1.117 + /* Linked-list elements for storing this bufferevent_private in a 1.118 + * group. 1.119 + * 1.120 + * Note that this field is supposed to be protected by the group 1.121 + * lock */ 1.122 + TAILQ_ENTRY(bufferevent_private) next_in_group; 1.123 + /** The rate-limiting group for this bufferevent, or NULL if it is 1.124 + * only rate-limited on its own. */ 1.125 + struct bufferevent_rate_limit_group *group; 1.126 + 1.127 + /* This bufferevent's current limits. */ 1.128 + struct ev_token_bucket limit; 1.129 + /* Pointer to the rate-limit configuration for this bufferevent. 1.130 + * Can be shared. XXX reference-count this? */ 1.131 + struct ev_token_bucket_cfg *cfg; 1.132 + 1.133 + /* Timeout event used when one this bufferevent's buckets are 1.134 + * empty. */ 1.135 + struct event refill_bucket_event; 1.136 +}; 1.137 + 1.138 +/** Parts of the bufferevent structure that are shared among all bufferevent 1.139 + * types, but not exposed in bufferevent_struct.h. */ 1.140 +struct bufferevent_private { 1.141 + /** The underlying bufferevent structure. */ 1.142 + struct bufferevent bev; 1.143 + 1.144 + /** Evbuffer callback to enforce watermarks on input. */ 1.145 + struct evbuffer_cb_entry *read_watermarks_cb; 1.146 + 1.147 + /** If set, we should free the lock when we free the bufferevent. */ 1.148 + unsigned own_lock : 1; 1.149 + 1.150 + /** Flag: set if we have deferred callbacks and a read callback is 1.151 + * pending. */ 1.152 + unsigned readcb_pending : 1; 1.153 + /** Flag: set if we have deferred callbacks and a write callback is 1.154 + * pending. */ 1.155 + unsigned writecb_pending : 1; 1.156 + /** Flag: set if we are currently busy connecting. */ 1.157 + unsigned connecting : 1; 1.158 + /** Flag: set if a connect failed prematurely; this is a hack for 1.159 + * getting around the bufferevent abstraction. */ 1.160 + unsigned connection_refused : 1; 1.161 + /** Set to the events pending if we have deferred callbacks and 1.162 + * an events callback is pending. */ 1.163 + short eventcb_pending; 1.164 + 1.165 + /** If set, read is suspended until one or more conditions are over. 1.166 + * The actual value here is a bitfield of those conditions; see the 1.167 + * BEV_SUSPEND_* flags above. */ 1.168 + bufferevent_suspend_flags read_suspended; 1.169 + 1.170 + /** If set, writing is suspended until one or more conditions are over. 1.171 + * The actual value here is a bitfield of those conditions; see the 1.172 + * BEV_SUSPEND_* flags above. */ 1.173 + bufferevent_suspend_flags write_suspended; 1.174 + 1.175 + /** Set to the current socket errno if we have deferred callbacks and 1.176 + * an events callback is pending. */ 1.177 + int errno_pending; 1.178 + 1.179 + /** The DNS error code for bufferevent_socket_connect_hostname */ 1.180 + int dns_error; 1.181 + 1.182 + /** Used to implement deferred callbacks */ 1.183 + struct deferred_cb deferred; 1.184 + 1.185 + /** The options this bufferevent was constructed with */ 1.186 + enum bufferevent_options options; 1.187 + 1.188 + /** Current reference count for this bufferevent. */ 1.189 + int refcnt; 1.190 + 1.191 + /** Lock for this bufferevent. Shared by the inbuf and the outbuf. 1.192 + * If NULL, locking is disabled. */ 1.193 + void *lock; 1.194 + 1.195 + /** Rate-limiting information for this bufferevent */ 1.196 + struct bufferevent_rate_limit *rate_limiting; 1.197 +}; 1.198 + 1.199 +/** Possible operations for a control callback. */ 1.200 +enum bufferevent_ctrl_op { 1.201 + BEV_CTRL_SET_FD, 1.202 + BEV_CTRL_GET_FD, 1.203 + BEV_CTRL_GET_UNDERLYING, 1.204 + BEV_CTRL_CANCEL_ALL 1.205 +}; 1.206 + 1.207 +/** Possible data types for a control callback */ 1.208 +union bufferevent_ctrl_data { 1.209 + void *ptr; 1.210 + evutil_socket_t fd; 1.211 +}; 1.212 + 1.213 +/** 1.214 + Implementation table for a bufferevent: holds function pointers and other 1.215 + information to make the various bufferevent types work. 1.216 +*/ 1.217 +struct bufferevent_ops { 1.218 + /** The name of the bufferevent's type. */ 1.219 + const char *type; 1.220 + /** At what offset into the implementation type will we find a 1.221 + bufferevent structure? 1.222 + 1.223 + Example: if the type is implemented as 1.224 + struct bufferevent_x { 1.225 + int extra_data; 1.226 + struct bufferevent bev; 1.227 + } 1.228 + then mem_offset should be offsetof(struct bufferevent_x, bev) 1.229 + */ 1.230 + off_t mem_offset; 1.231 + 1.232 + /** Enables one or more of EV_READ|EV_WRITE on a bufferevent. Does 1.233 + not need to adjust the 'enabled' field. Returns 0 on success, -1 1.234 + on failure. 1.235 + */ 1.236 + int (*enable)(struct bufferevent *, short); 1.237 + 1.238 + /** Disables one or more of EV_READ|EV_WRITE on a bufferevent. Does 1.239 + not need to adjust the 'enabled' field. Returns 0 on success, -1 1.240 + on failure. 1.241 + */ 1.242 + int (*disable)(struct bufferevent *, short); 1.243 + 1.244 + /** Free any storage and deallocate any extra data or structures used 1.245 + in this implementation. 1.246 + */ 1.247 + void (*destruct)(struct bufferevent *); 1.248 + 1.249 + /** Called when the timeouts on the bufferevent have changed.*/ 1.250 + int (*adj_timeouts)(struct bufferevent *); 1.251 + 1.252 + /** Called to flush data. */ 1.253 + int (*flush)(struct bufferevent *, short, enum bufferevent_flush_mode); 1.254 + 1.255 + /** Called to access miscellaneous fields. */ 1.256 + int (*ctrl)(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *); 1.257 + 1.258 +}; 1.259 + 1.260 +extern const struct bufferevent_ops bufferevent_ops_socket; 1.261 +extern const struct bufferevent_ops bufferevent_ops_filter; 1.262 +extern const struct bufferevent_ops bufferevent_ops_pair; 1.263 + 1.264 +#define BEV_IS_SOCKET(bevp) ((bevp)->be_ops == &bufferevent_ops_socket) 1.265 +#define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter) 1.266 +#define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair) 1.267 + 1.268 +#ifdef WIN32 1.269 +extern const struct bufferevent_ops bufferevent_ops_async; 1.270 +#define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async) 1.271 +#else 1.272 +#define BEV_IS_ASYNC(bevp) 0 1.273 +#endif 1.274 + 1.275 +/** Initialize the shared parts of a bufferevent. */ 1.276 +int bufferevent_init_common(struct bufferevent_private *, struct event_base *, const struct bufferevent_ops *, enum bufferevent_options options); 1.277 + 1.278 +/** For internal use: temporarily stop all reads on bufev, until the conditions 1.279 + * in 'what' are over. */ 1.280 +void bufferevent_suspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what); 1.281 +/** For internal use: clear the conditions 'what' on bufev, and re-enable 1.282 + * reading if there are no conditions left. */ 1.283 +void bufferevent_unsuspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what); 1.284 + 1.285 +/** For internal use: temporarily stop all writes on bufev, until the conditions 1.286 + * in 'what' are over. */ 1.287 +void bufferevent_suspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what); 1.288 +/** For internal use: clear the conditions 'what' on bufev, and re-enable 1.289 + * writing if there are no conditions left. */ 1.290 +void bufferevent_unsuspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what); 1.291 + 1.292 +#define bufferevent_wm_suspend_read(b) \ 1.293 + bufferevent_suspend_read((b), BEV_SUSPEND_WM) 1.294 +#define bufferevent_wm_unsuspend_read(b) \ 1.295 + bufferevent_unsuspend_read((b), BEV_SUSPEND_WM) 1.296 + 1.297 +/* 1.298 + Disable a bufferevent. Equivalent to bufferevent_disable(), but 1.299 + first resets 'connecting' flag to force EV_WRITE down for sure. 1.300 + 1.301 + XXXX this method will go away in the future; try not to add new users. 1.302 + See comment in evhttp_connection_reset() for discussion. 1.303 + 1.304 + @param bufev the bufferevent to be disabled 1.305 + @param event any combination of EV_READ | EV_WRITE. 1.306 + @return 0 if successful, or -1 if an error occurred 1.307 + @see bufferevent_disable() 1.308 + */ 1.309 +int bufferevent_disable_hard(struct bufferevent *bufev, short event); 1.310 + 1.311 +/** Internal: Set up locking on a bufferevent. If lock is set, use it. 1.312 + * Otherwise, use a new lock. */ 1.313 +int bufferevent_enable_locking(struct bufferevent *bufev, void *lock); 1.314 +/** Internal: Increment the reference count on bufev. */ 1.315 +void bufferevent_incref(struct bufferevent *bufev); 1.316 +/** Internal: Lock bufev and increase its reference count. 1.317 + * unlocking it otherwise. */ 1.318 +void _bufferevent_incref_and_lock(struct bufferevent *bufev); 1.319 +/** Internal: Decrement the reference count on bufev. Returns 1 if it freed 1.320 + * the bufferevent.*/ 1.321 +int bufferevent_decref(struct bufferevent *bufev); 1.322 +/** Internal: Drop the reference count on bufev, freeing as necessary, and 1.323 + * unlocking it otherwise. Returns 1 if it freed the bufferevent. */ 1.324 +int _bufferevent_decref_and_unlock(struct bufferevent *bufev); 1.325 + 1.326 +/** Internal: If callbacks are deferred and we have a read callback, schedule 1.327 + * a readcb. Otherwise just run the readcb. */ 1.328 +void _bufferevent_run_readcb(struct bufferevent *bufev); 1.329 +/** Internal: If callbacks are deferred and we have a write callback, schedule 1.330 + * a writecb. Otherwise just run the writecb. */ 1.331 +void _bufferevent_run_writecb(struct bufferevent *bufev); 1.332 +/** Internal: If callbacks are deferred and we have an eventcb, schedule 1.333 + * it to run with events "what". Otherwise just run the eventcb. */ 1.334 +void _bufferevent_run_eventcb(struct bufferevent *bufev, short what); 1.335 + 1.336 +/** Internal: Add the event 'ev' with timeout tv, unless tv is set to 0, in 1.337 + * which case add ev with no timeout. */ 1.338 +int _bufferevent_add_event(struct event *ev, const struct timeval *tv); 1.339 + 1.340 +/* ========= 1.341 + * These next functions implement timeouts for bufferevents that aren't doing 1.342 + * anything else with ev_read and ev_write, to handle timeouts. 1.343 + * ========= */ 1.344 +/** Internal use: Set up the ev_read and ev_write callbacks so that 1.345 + * the other "generic_timeout" functions will work on it. Call this from 1.346 + * the constructor function. */ 1.347 +void _bufferevent_init_generic_timeout_cbs(struct bufferevent *bev); 1.348 +/** Internal use: Delete the ev_read and ev_write callbacks if they're pending. 1.349 + * Call this from the destructor function. */ 1.350 +int _bufferevent_del_generic_timeout_cbs(struct bufferevent *bev); 1.351 +/** Internal use: Add or delete the generic timeout events as appropriate. 1.352 + * (If an event is enabled and a timeout is set, we add the event. Otherwise 1.353 + * we delete it.) Call this from anything that changes the timeout values, 1.354 + * that enabled EV_READ or EV_WRITE, or that disables EV_READ or EV_WRITE. */ 1.355 +int _bufferevent_generic_adj_timeouts(struct bufferevent *bev); 1.356 + 1.357 +/** Internal use: We have just successfully read data into an inbuf, so 1.358 + * reset the read timeout (if any). */ 1.359 +#define BEV_RESET_GENERIC_READ_TIMEOUT(bev) \ 1.360 + do { \ 1.361 + if (evutil_timerisset(&(bev)->timeout_read)) \ 1.362 + event_add(&(bev)->ev_read, &(bev)->timeout_read); \ 1.363 + } while (0) 1.364 +/** Internal use: We have just successfully written data from an inbuf, so 1.365 + * reset the read timeout (if any). */ 1.366 +#define BEV_RESET_GENERIC_WRITE_TIMEOUT(bev) \ 1.367 + do { \ 1.368 + if (evutil_timerisset(&(bev)->timeout_write)) \ 1.369 + event_add(&(bev)->ev_write, &(bev)->timeout_write); \ 1.370 + } while (0) 1.371 +#define BEV_DEL_GENERIC_READ_TIMEOUT(bev) \ 1.372 + event_del(&(bev)->ev_read) 1.373 +#define BEV_DEL_GENERIC_WRITE_TIMEOUT(bev) \ 1.374 + event_del(&(bev)->ev_write) 1.375 + 1.376 + 1.377 +/** Internal: Given a bufferevent, return its corresponding 1.378 + * bufferevent_private. */ 1.379 +#define BEV_UPCAST(b) EVUTIL_UPCAST((b), struct bufferevent_private, bev) 1.380 + 1.381 +#ifdef _EVENT_DISABLE_THREAD_SUPPORT 1.382 +#define BEV_LOCK(b) _EVUTIL_NIL_STMT 1.383 +#define BEV_UNLOCK(b) _EVUTIL_NIL_STMT 1.384 +#else 1.385 +/** Internal: Grab the lock (if any) on a bufferevent */ 1.386 +#define BEV_LOCK(b) do { \ 1.387 + struct bufferevent_private *locking = BEV_UPCAST(b); \ 1.388 + EVLOCK_LOCK(locking->lock, 0); \ 1.389 + } while (0) 1.390 + 1.391 +/** Internal: Release the lock (if any) on a bufferevent */ 1.392 +#define BEV_UNLOCK(b) do { \ 1.393 + struct bufferevent_private *locking = BEV_UPCAST(b); \ 1.394 + EVLOCK_UNLOCK(locking->lock, 0); \ 1.395 + } while (0) 1.396 +#endif 1.397 + 1.398 + 1.399 +/* ==== For rate-limiting. */ 1.400 + 1.401 +int _bufferevent_decrement_write_buckets(struct bufferevent_private *bev, 1.402 + ev_ssize_t bytes); 1.403 +int _bufferevent_decrement_read_buckets(struct bufferevent_private *bev, 1.404 + ev_ssize_t bytes); 1.405 +ev_ssize_t _bufferevent_get_read_max(struct bufferevent_private *bev); 1.406 +ev_ssize_t _bufferevent_get_write_max(struct bufferevent_private *bev); 1.407 + 1.408 +#ifdef __cplusplus 1.409 +} 1.410 +#endif 1.411 + 1.412 + 1.413 +#endif /* _BUFFEREVENT_INTERNAL_H_ */