]> arthur.barton.de Git - netatalk.git/blob - libevent/bufferevent-internal.h
Update libevent to 2.0.12
[netatalk.git] / libevent / bufferevent-internal.h
1 /*
2  * Copyright (c) 2008-2010 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef _BUFFEREVENT_INTERNAL_H_
27 #define _BUFFEREVENT_INTERNAL_H_
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #include "event2/event-config.h"
34 #include "event2/util.h"
35 #include "defer-internal.h"
36 #include "evthread-internal.h"
37 #include "event2/thread.h"
38 #include "ratelim-internal.h"
39 #include "event2/bufferevent_struct.h"
40
41 /* These flags are reasons that we might be declining to actually enable
42    reading or writing on a bufferevent.
43  */
44
45 /* On a all bufferevents, for reading: used when we have read up to the
46    watermark value.
47
48    On a filtering bufferevent, for writing: used when the underlying
49    bufferevent's write buffer has been filled up to its watermark
50    value.
51 */
52 #define BEV_SUSPEND_WM 0x01
53 /* On a base bufferevent: when we have emptied a bandwidth buckets */
54 #define BEV_SUSPEND_BW 0x02
55 /* On a base bufferevent: when we have emptied the group's bandwidth bucket. */
56 #define BEV_SUSPEND_BW_GROUP 0x04
57 /* On a socket bufferevent: can't do any operations while we're waiting for
58  * name lookup to finish. */
59 #define BEV_SUSPEND_LOOKUP 0x08
60 /* On a base bufferevent, for reading: used when a filter has choked this
61  * (underlying) bufferevent because it has stopped reading from it. */
62 #define BEV_SUSPEND_FILT_READ 0x10
63
64 typedef ev_uint16_t bufferevent_suspend_flags;
65
66 struct bufferevent_rate_limit_group {
67         /** List of all members in the group */
68         TAILQ_HEAD(rlim_group_member_list, bufferevent_private) members;
69         /** Current limits for the group. */
70         struct ev_token_bucket rate_limit;
71         struct ev_token_bucket_cfg rate_limit_cfg;
72
73         /** True iff we don't want to read from any member of the group.until
74          * the token bucket refills.  */
75         unsigned read_suspended : 1;
76         /** True iff we don't want to write from any member of the group.until
77          * the token bucket refills.  */
78         unsigned write_suspended : 1;
79         /** True iff we were unable to suspend one of the bufferevents in the
80          * group for reading the last time we tried, and we should try
81          * again. */
82         unsigned pending_unsuspend_read : 1;
83         /** True iff we were unable to suspend one of the bufferevents in the
84          * group for writing the last time we tried, and we should try
85          * again. */
86         unsigned pending_unsuspend_write : 1;
87
88         /*@{*/
89         /** Total number of bytes read or written in this group since last
90          * reset. */
91         ev_uint64_t total_read;
92         ev_uint64_t total_written;
93         /*@}*/
94
95         /** The number of bufferevents in the group. */
96         int n_members;
97
98         /** The smallest number of bytes that any member of the group should
99          * be limited to read or write at a time. */
100         ev_ssize_t min_share;
101         /** Timeout event that goes off once a tick, when the bucket is ready
102          * to refill. */
103         struct event master_refill_event;
104         /** Lock to protect the members of this group.  This lock should nest
105          * within every bufferevent lock: if you are holding this lock, do
106          * not assume you can lock another bufferevent. */
107         void *lock;
108 };
109
110 /** Fields for rate-limiting a single bufferevent. */
111 struct bufferevent_rate_limit {
112         /* Linked-list elements for storing this bufferevent_private in a
113          * group.
114          *
115          * Note that this field is supposed to be protected by the group
116          * lock */
117         TAILQ_ENTRY(bufferevent_private) next_in_group;
118         /** The rate-limiting group for this bufferevent, or NULL if it is
119          * only rate-limited on its own. */
120         struct bufferevent_rate_limit_group *group;
121
122         /* This bufferevent's current limits. */
123         struct ev_token_bucket limit;
124         /* Pointer to the rate-limit configuration for this bufferevent.
125          * Can be shared.  XXX reference-count this? */
126         struct ev_token_bucket_cfg *cfg;
127
128         /* Timeout event used when one this bufferevent's buckets are
129          * empty. */
130         struct event refill_bucket_event;
131 };
132
133 /** Parts of the bufferevent structure that are shared among all bufferevent
134  * types, but not exposed in bufferevent_struct.h. */
135 struct bufferevent_private {
136         /** The underlying bufferevent structure. */
137         struct bufferevent bev;
138
139         /** Evbuffer callback to enforce watermarks on input. */
140         struct evbuffer_cb_entry *read_watermarks_cb;
141
142         /** If set, we should free the lock when we free the bufferevent. */
143         unsigned own_lock : 1;
144
145         /** Flag: set if we have deferred callbacks and a read callback is
146          * pending. */
147         unsigned readcb_pending : 1;
148         /** Flag: set if we have deferred callbacks and a write callback is
149          * pending. */
150         unsigned writecb_pending : 1;
151         /** Flag: set if we are currently busy connecting. */
152         unsigned connecting : 1;
153         /** Flag: set if a connect failed prematurely; this is a hack for
154          * getting around the bufferevent abstraction. */
155         unsigned connection_refused : 1;
156         /** Set to the events pending if we have deferred callbacks and
157          * an events callback is pending. */
158         short eventcb_pending;
159
160         /** If set, read is suspended until one or more conditions are over.
161          * The actual value here is a bitfield of those conditions; see the
162          * BEV_SUSPEND_* flags above. */
163         bufferevent_suspend_flags read_suspended;
164
165         /** If set, writing is suspended until one or more conditions are over.
166          * The actual value here is a bitfield of those conditions; see the
167          * BEV_SUSPEND_* flags above. */
168         bufferevent_suspend_flags write_suspended;
169
170         /** Set to the current socket errno if we have deferred callbacks and
171          * an events callback is pending. */
172         int errno_pending;
173
174         /** The DNS error code for bufferevent_socket_connect_hostname */
175         int dns_error;
176
177         /** Used to implement deferred callbacks */
178         struct deferred_cb deferred;
179
180         /** The options this bufferevent was constructed with */
181         enum bufferevent_options options;
182
183         /** Current reference count for this bufferevent. */
184         int refcnt;
185
186         /** Lock for this bufferevent.  Shared by the inbuf and the outbuf.
187          * If NULL, locking is disabled. */
188         void *lock;
189
190         /** Rate-limiting information for this bufferevent */
191         struct bufferevent_rate_limit *rate_limiting;
192 };
193
194 /** Possible operations for a control callback. */
195 enum bufferevent_ctrl_op {
196         BEV_CTRL_SET_FD,
197         BEV_CTRL_GET_FD,
198         BEV_CTRL_GET_UNDERLYING
199 };
200
201 /** Possible data types for a control callback */
202 union bufferevent_ctrl_data {
203         void *ptr;
204         evutil_socket_t fd;
205 };
206
207 /**
208    Implementation table for a bufferevent: holds function pointers and other
209    information to make the various bufferevent types work.
210 */
211 struct bufferevent_ops {
212         /** The name of the bufferevent's type. */
213         const char *type;
214         /** At what offset into the implementation type will we find a
215             bufferevent structure?
216
217             Example: if the type is implemented as
218             struct bufferevent_x {
219                int extra_data;
220                struct bufferevent bev;
221             }
222             then mem_offset should be offsetof(struct bufferevent_x, bev)
223         */
224         off_t mem_offset;
225
226         /** Enables one or more of EV_READ|EV_WRITE on a bufferevent.  Does
227             not need to adjust the 'enabled' field.  Returns 0 on success, -1
228             on failure.
229          */
230         int (*enable)(struct bufferevent *, short);
231
232         /** Disables one or more of EV_READ|EV_WRITE on a bufferevent.  Does
233             not need to adjust the 'enabled' field.  Returns 0 on success, -1
234             on failure.
235          */
236         int (*disable)(struct bufferevent *, short);
237
238         /** Free any storage and deallocate any extra data or structures used
239             in this implementation.
240          */
241         void (*destruct)(struct bufferevent *);
242
243         /** Called when the timeouts on the bufferevent have changed.*/
244         int (*adj_timeouts)(struct bufferevent *);
245
246         /** Called to flush data. */
247         int (*flush)(struct bufferevent *, short, enum bufferevent_flush_mode);
248
249         /** Called to access miscellaneous fields. */
250         int (*ctrl)(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
251
252 };
253
254 extern const struct bufferevent_ops bufferevent_ops_socket;
255 extern const struct bufferevent_ops bufferevent_ops_filter;
256 extern const struct bufferevent_ops bufferevent_ops_pair;
257
258 #define BEV_IS_SOCKET(bevp) ((bevp)->be_ops == &bufferevent_ops_socket)
259 #define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter)
260 #define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair)
261
262 #ifdef WIN32
263 extern const struct bufferevent_ops bufferevent_ops_async;
264 #define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async)
265 #else
266 #define BEV_IS_ASYNC(bevp) 0
267 #endif
268
269 /** Initialize the shared parts of a bufferevent. */
270 int bufferevent_init_common(struct bufferevent_private *, struct event_base *, const struct bufferevent_ops *, enum bufferevent_options options);
271
272 /** For internal use: temporarily stop all reads on bufev, until the conditions
273  * in 'what' are over. */
274 void bufferevent_suspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what);
275 /** For internal use: clear the conditions 'what' on bufev, and re-enable
276  * reading if there are no conditions left. */
277 void bufferevent_unsuspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what);
278
279 /** For internal use: temporarily stop all writes on bufev, until the conditions
280  * in 'what' are over. */
281 void bufferevent_suspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what);
282 /** For internal use: clear the conditions 'what' on bufev, and re-enable
283  * writing if there are no conditions left. */
284 void bufferevent_unsuspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what);
285
286 #define bufferevent_wm_suspend_read(b) \
287         bufferevent_suspend_read((b), BEV_SUSPEND_WM)
288 #define bufferevent_wm_unsuspend_read(b) \
289         bufferevent_unsuspend_read((b), BEV_SUSPEND_WM)
290
291 /*
292   Disable a bufferevent.  Equivalent to bufferevent_disable(), but
293   first resets 'connecting' flag to force EV_WRITE down for sure.
294
295   XXXX this method will go away in the future; try not to add new users.
296     See comment in evhttp_connection_reset() for discussion.
297
298   @param bufev the bufferevent to be disabled
299   @param event any combination of EV_READ | EV_WRITE.
300   @return 0 if successful, or -1 if an error occurred
301   @see bufferevent_disable()
302  */
303 int bufferevent_disable_hard(struct bufferevent *bufev, short event);
304
305 /** Internal: Set up locking on a bufferevent.  If lock is set, use it.
306  * Otherwise, use a new lock. */
307 int bufferevent_enable_locking(struct bufferevent *bufev, void *lock);
308 /** Internal: Increment the reference count on bufev. */
309 void bufferevent_incref(struct bufferevent *bufev);
310 /** Internal: Lock bufev and increase its reference count.
311  * unlocking it otherwise. */
312 void _bufferevent_incref_and_lock(struct bufferevent *bufev);
313 /** Internal: Decrement the reference count on bufev.  Returns 1 if it freed
314  * the bufferevent.*/
315 int bufferevent_decref(struct bufferevent *bufev);
316 /** Internal: Drop the reference count on bufev, freeing as necessary, and
317  * unlocking it otherwise.  Returns 1 if it freed the bufferevent. */
318 int _bufferevent_decref_and_unlock(struct bufferevent *bufev);
319
320 /** Internal: If callbacks are deferred and we have a read callback, schedule
321  * a readcb.  Otherwise just run the readcb. */
322 void _bufferevent_run_readcb(struct bufferevent *bufev);
323 /** Internal: If callbacks are deferred and we have a write callback, schedule
324  * a writecb.  Otherwise just run the writecb. */
325 void _bufferevent_run_writecb(struct bufferevent *bufev);
326 /** Internal: If callbacks are deferred and we have an eventcb, schedule
327  * it to run with events "what".  Otherwise just run the eventcb. */
328 void _bufferevent_run_eventcb(struct bufferevent *bufev, short what);
329
330 /** Internal: Add the event 'ev' with timeout tv, unless tv is set to 0, in
331  * which case add ev with no timeout. */
332 int _bufferevent_add_event(struct event *ev, const struct timeval *tv);
333
334 /* =========
335  * These next functions implement timeouts for bufferevents that aren't doing
336  * anything else with ev_read and ev_write, to handle timeouts.
337  * ========= */
338 /** Internal use: Set up the ev_read and ev_write callbacks so that
339  * the other "generic_timeout" functions will work on it.  Call this from
340  * the constructor function. */
341 void _bufferevent_init_generic_timeout_cbs(struct bufferevent *bev);
342 /** Internal use: Delete the ev_read and ev_write callbacks if they're pending.
343  * Call this from the destructor function. */
344 int _bufferevent_del_generic_timeout_cbs(struct bufferevent *bev);
345 /** Internal use: Add or delete the generic timeout events as appropriate.
346  * (If an event is enabled and a timeout is set, we add the event.  Otherwise
347  * we delete it.)  Call this from anything that changes the timeout values,
348  * that enabled EV_READ or EV_WRITE, or that disables EV_READ or EV_WRITE. */
349 int _bufferevent_generic_adj_timeouts(struct bufferevent *bev);
350
351 /** Internal use: We have just successfully read data into an inbuf, so
352  * reset the read timeout (if any). */
353 #define BEV_RESET_GENERIC_READ_TIMEOUT(bev)                             \
354         do {                                                            \
355                 if (evutil_timerisset(&(bev)->timeout_read))            \
356                         event_add(&(bev)->ev_read, &(bev)->timeout_read); \
357         } while (0)
358 /** Internal use: We have just successfully written data from an inbuf, so
359  * reset the read timeout (if any). */
360 #define BEV_RESET_GENERIC_WRITE_TIMEOUT(bev)                            \
361         do {                                                            \
362                 if (evutil_timerisset(&(bev)->timeout_write))           \
363                         event_add(&(bev)->ev_write, &(bev)->timeout_write); \
364         } while (0)
365 #define BEV_DEL_GENERIC_READ_TIMEOUT(bev)       \
366                 event_del(&(bev)->ev_read)
367 #define BEV_DEL_GENERIC_WRITE_TIMEOUT(bev)      \
368                 event_del(&(bev)->ev_write)
369
370
371 /** Internal: Given a bufferevent, return its corresponding
372  * bufferevent_private. */
373 #define BEV_UPCAST(b) EVUTIL_UPCAST((b), struct bufferevent_private, bev)
374
375 #ifdef _EVENT_DISABLE_THREAD_SUPPORT
376 #define BEV_LOCK(b) _EVUTIL_NIL_STMT
377 #define BEV_UNLOCK(b) _EVUTIL_NIL_STMT
378 #else
379 /** Internal: Grab the lock (if any) on a bufferevent */
380 #define BEV_LOCK(b) do {                                                \
381                 struct bufferevent_private *locking =  BEV_UPCAST(b);   \
382                 EVLOCK_LOCK(locking->lock, 0);                          \
383         } while (0)
384
385 /** Internal: Release the lock (if any) on a bufferevent */
386 #define BEV_UNLOCK(b) do {                                              \
387                 struct bufferevent_private *locking =  BEV_UPCAST(b);   \
388                 EVLOCK_UNLOCK(locking->lock, 0);                        \
389         } while (0)
390 #endif
391
392
393 /* ==== For rate-limiting. */
394
395 int _bufferevent_decrement_write_buckets(struct bufferevent_private *bev,
396     ev_ssize_t bytes);
397 int _bufferevent_decrement_read_buckets(struct bufferevent_private *bev,
398     ev_ssize_t bytes);
399 ev_ssize_t _bufferevent_get_read_max(struct bufferevent_private *bev);
400 ev_ssize_t _bufferevent_get_write_max(struct bufferevent_private *bev);
401
402 #ifdef __cplusplus
403 }
404 #endif
405
406
407 #endif /* _BUFFEREVENT_INTERNAL_H_ */