event.h revision 1.1.1.4
1/*	$NetBSD: event.h,v 1.1.1.4 2021/04/07 02:43:14 christos Exp $	*/
2/*
3 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
4 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28#ifndef EVENT2_EVENT_H_INCLUDED_
29#define EVENT2_EVENT_H_INCLUDED_
30
31/**
32   @mainpage
33
34  @section intro Introduction
35
36  Libevent is an event notification library for developing scalable network
37  servers.  The Libevent API provides a mechanism to execute a callback
38  function when a specific event occurs on a file descriptor or after a
39  timeout has been reached. Furthermore, Libevent also support callbacks due
40  to signals or regular timeouts.
41
42  Libevent is meant to replace the event loop found in event driven network
43  servers. An application just needs to call event_base_dispatch() and then add or
44  remove events dynamically without having to change the event loop.
45
46
47  Currently, Libevent supports /dev/poll, kqueue(2), select(2), poll(2),
48  epoll(4), and evports. The internal event mechanism is completely
49  independent of the exposed event API, and a simple update of Libevent can
50  provide new functionality without having to redesign the applications. As a
51  result, Libevent allows for portable application development and provides
52  the most scalable event notification mechanism available on an operating
53  system.  Libevent can also be used for multithreaded programs.  Libevent
54  should compile on Linux, *BSD, Mac OS X, Solaris and, Windows.
55
56  @section usage Standard usage
57
58  Every program that uses Libevent must include the <event2/event.h>
59  header, and pass the -levent flag to the linker.  (You can instead link
60  -levent_core if you only want the main event and buffered IO-based code,
61  and don't want to link any protocol code.)
62
63  @section setup Library setup
64
65  Before you call any other Libevent functions, you need to set up the
66  library.  If you're going to use Libevent from multiple threads in a
67  multithreaded application, you need to initialize thread support --
68  typically by using evthread_use_pthreads() or
69  evthread_use_windows_threads().  See <event2/thread.h> for more
70  information.
71
72  This is also the point where you can replace Libevent's memory
73  management functions with event_set_mem_functions, and enable debug mode
74  with event_enable_debug_mode().
75
76  @section base Creating an event base
77
78  Next, you need to create an event_base structure, using event_base_new()
79  or event_base_new_with_config().  The event_base is responsible for
80  keeping track of which events are "pending" (that is to say, being
81  watched to see if they become active) and which events are "active".
82  Every event is associated with a single event_base.
83
84  @section event Event notification
85
86  For each file descriptor that you wish to monitor, you must create an
87  event structure with event_new().  (You may also declare an event
88  structure and call event_assign() to initialize the members of the
89  structure.)  To enable notification, you add the structure to the list
90  of monitored events by calling event_add().  The event structure must
91  remain allocated as long as it is active, so it should generally be
92  allocated on the heap.
93
94  @section loop Dispatching events.
95
96  Finally, you call event_base_dispatch() to loop and dispatch events.
97  You can also use event_base_loop() for more fine-grained control.
98
99  Currently, only one thread can be dispatching a given event_base at a
100  time.  If you want to run events in multiple threads at once, you can
101  either have a single event_base whose events add work to a work queue,
102  or you can create multiple event_base objects.
103
104  @section bufferevent I/O Buffers
105
106  Libevent provides a buffered I/O abstraction on top of the regular event
107  callbacks. This abstraction is called a bufferevent. A bufferevent
108  provides input and output buffers that get filled and drained
109  automatically. The user of a buffered event no longer deals directly
110  with the I/O, but instead is reading from input and writing to output
111  buffers.
112
113  Once initialized via bufferevent_socket_new(), the bufferevent structure
114  can be used repeatedly with bufferevent_enable() and
115  bufferevent_disable().  Instead of reading and writing directly to a
116  socket, you would call bufferevent_read() and bufferevent_write().
117
118  When read enabled the bufferevent will try to read from the file descriptor
119  and call the read callback. The write callback is executed whenever the
120  output buffer is drained below the write low watermark, which is 0 by
121  default.
122
123  See <event2/bufferevent*.h> for more information.
124
125  @section timers Timers
126
127  Libevent can also be used to create timers that invoke a callback after a
128  certain amount of time has expired. The evtimer_new() macro returns
129  an event struct to use as a timer. To activate the timer, call
130  evtimer_add(). Timers can be deactivated by calling evtimer_del().
131  (These macros are thin wrappers around event_new(), event_add(),
132  and event_del(); you can also use those instead.)
133
134  @section evdns Asynchronous DNS resolution
135
136  Libevent provides an asynchronous DNS resolver that should be used instead
137  of the standard DNS resolver functions.  See the <event2/dns.h>
138  functions for more detail.
139
140  @section evhttp Event-driven HTTP servers
141
142  Libevent provides a very simple event-driven HTTP server that can be
143  embedded in your program and used to service HTTP requests.
144
145  To use this capability, you need to include the <event2/http.h> header in your
146  program.  See that header for more information.
147
148  @section evrpc A framework for RPC servers and clients
149
150  Libevent provides a framework for creating RPC servers and clients.  It
151  takes care of marshaling and unmarshaling all data structures.
152
153  @section api API Reference
154
155  To browse the complete documentation of the libevent API, click on any of
156  the following links.
157
158  event2/event.h
159  The primary libevent header
160
161  event2/thread.h
162  Functions for use by multithreaded programs
163
164  event2/buffer.h and event2/bufferevent.h
165  Buffer management for network reading and writing
166
167  event2/util.h
168  Utility functions for portable nonblocking network code
169
170  event2/dns.h
171  Asynchronous DNS resolution
172
173  event2/http.h
174  An embedded libevent-based HTTP server
175
176  event2/rpc.h
177  A framework for creating RPC servers and clients
178
179 */
180
181/** @file event2/event.h
182
183  Core functions for waiting for and receiving events, and using event bases.
184*/
185
186#include <event2/visibility.h>
187
188#ifdef __cplusplus
189extern "C" {
190#endif
191
192#include <event2/event-config.h>
193#ifdef EVENT__HAVE_SYS_TYPES_H
194#include <sys/types.h>
195#endif
196#ifdef EVENT__HAVE_SYS_TIME_H
197#include <sys/time.h>
198#endif
199
200#include <stdio.h>
201
202/* For int types. */
203#include <event2/util.h>
204
205/**
206 * Structure to hold information and state for a Libevent dispatch loop.
207 *
208 * The event_base lies at the center of Libevent; every application will
209 * have one.  It keeps track of all pending and active events, and
210 * notifies your application of the active ones.
211 *
212 * This is an opaque structure; you can allocate one using
213 * event_base_new() or event_base_new_with_config().
214 *
215 * @see event_base_new(), event_base_free(), event_base_loop(),
216 *    event_base_new_with_config()
217 */
218struct event_base
219#ifdef EVENT_IN_DOXYGEN_
220{/*Empty body so that doxygen will generate documentation here.*/}
221#endif
222;
223
224/**
225 * @struct event
226 *
227 * Structure to represent a single event.
228 *
229 * An event can have some underlying condition it represents: a socket
230 * becoming readable or writeable (or both), or a signal becoming raised.
231 * (An event that represents no underlying condition is still useful: you
232 * can use one to implement a timer, or to communicate between threads.)
233 *
234 * Generally, you can create events with event_new(), then make them
235 * pending with event_add().  As your event_base runs, it will run the
236 * callbacks of an events whose conditions are triggered.  When you no
237 * longer want the event, free it with event_free().
238 *
239 * In more depth:
240 *
241 * An event may be "pending" (one whose condition we are watching),
242 * "active" (one whose condition has triggered and whose callback is about
243 * to run), neither, or both.  Events come into existence via
244 * event_assign() or event_new(), and are then neither active nor pending.
245 *
246 * To make an event pending, pass it to event_add().  When doing so, you
247 * can also set a timeout for the event.
248 *
249 * Events become active during an event_base_loop() call when either their
250 * condition has triggered, or when their timeout has elapsed.  You can
251 * also activate an event manually using event_active().  The even_base
252 * loop will run the callbacks of active events; after it has done so, it
253 * marks them as no longer active.
254 *
255 * You can make an event non-pending by passing it to event_del().  This
256 * also makes the event non-active.
257 *
258 * Events can be "persistent" or "non-persistent".  A non-persistent event
259 * becomes non-pending as soon as it is triggered: thus, it only runs at
260 * most once per call to event_add().  A persistent event remains pending
261 * even when it becomes active: you'll need to event_del() it manually in
262 * order to make it non-pending.  When a persistent event with a timeout
263 * becomes active, its timeout is reset: this means you can use persistent
264 * events to implement periodic timeouts.
265 *
266 * This should be treated as an opaque structure; you should never read or
267 * write any of its fields directly.  For backward compatibility with old
268 * code, it is defined in the event2/event_struct.h header; including this
269 * header may make your code incompatible with other versions of Libevent.
270 *
271 * @see event_new(), event_free(), event_assign(), event_get_assignment(),
272 *    event_add(), event_del(), event_active(), event_pending(),
273 *    event_get_fd(), event_get_base(), event_get_events(),
274 *    event_get_callback(), event_get_callback_arg(),
275 *    event_priority_set()
276 */
277struct event
278#ifdef EVENT_IN_DOXYGEN_
279{/*Empty body so that doxygen will generate documentation here.*/}
280#endif
281;
282
283/**
284 * Configuration for an event_base.
285 *
286 * There are many options that can be used to alter the behavior and
287 * implementation of an event_base.  To avoid having to pass them all in a
288 * complex many-argument constructor, we provide an abstract data type
289 * where you set up configuration information before passing it to
290 * event_base_new_with_config().
291 *
292 * @see event_config_new(), event_config_free(), event_base_new_with_config(),
293 *   event_config_avoid_method(), event_config_require_features(),
294 *   event_config_set_flag(), event_config_set_num_cpus_hint()
295 */
296struct event_config
297#ifdef EVENT_IN_DOXYGEN_
298{/*Empty body so that doxygen will generate documentation here.*/}
299#endif
300;
301
302/**
303 * Enable some relatively expensive debugging checks in Libevent that
304 * would normally be turned off.  Generally, these checks cause code that
305 * would otherwise crash mysteriously to fail earlier with an assertion
306 * failure.  Note that this method MUST be called before any events or
307 * event_bases have been created.
308 *
309 * Debug mode can currently catch the following errors:
310 *    An event is re-assigned while it is added
311 *    Any function is called on a non-assigned event
312 *
313 * Note that debugging mode uses memory to track every event that has been
314 * initialized (via event_assign, event_set, or event_new) but not yet
315 * released (via event_free or event_debug_unassign).  If you want to use
316 * debug mode, and you find yourself running out of memory, you will need
317 * to use event_debug_unassign to explicitly stop tracking events that
318 * are no longer considered set-up.
319 *
320 * @see event_debug_unassign()
321 */
322EVENT2_EXPORT_SYMBOL
323void event_enable_debug_mode(void);
324
325/**
326 * When debugging mode is enabled, informs Libevent that an event should no
327 * longer be considered as assigned. When debugging mode is not enabled, does
328 * nothing.
329 *
330 * This function must only be called on a non-added event.
331 *
332 * @see event_enable_debug_mode()
333 */
334EVENT2_EXPORT_SYMBOL
335void event_debug_unassign(struct event *);
336
337/**
338 * Create and return a new event_base to use with the rest of Libevent.
339 *
340 * @return a new event_base on success, or NULL on failure.
341 *
342 * @see event_base_free(), event_base_new_with_config()
343 */
344EVENT2_EXPORT_SYMBOL
345struct event_base *event_base_new(void);
346
347/**
348  Reinitialize the event base after a fork
349
350  Some event mechanisms do not survive across fork.   The event base needs
351  to be reinitialized with the event_reinit() function.
352
353  @param base the event base that needs to be re-initialized
354  @return 0 if successful, or -1 if some events could not be re-added.
355  @see event_base_new()
356*/
357EVENT2_EXPORT_SYMBOL
358int event_reinit(struct event_base *base);
359
360/**
361   Event dispatching loop
362
363  This loop will run the event base until either there are no more pending or
364  active, or until something calls event_base_loopbreak() or
365  event_base_loopexit().
366
367  @param base the event_base structure returned by event_base_new() or
368     event_base_new_with_config()
369  @return 0 if successful, -1 if an error occurred, or 1 if we exited because
370     no events were pending or active.
371  @see event_base_loop()
372 */
373EVENT2_EXPORT_SYMBOL
374int event_base_dispatch(struct event_base *);
375
376/**
377 Get the kernel event notification mechanism used by Libevent.
378
379 @param eb the event_base structure returned by event_base_new()
380 @return a string identifying the kernel event mechanism (kqueue, epoll, etc.)
381 */
382EVENT2_EXPORT_SYMBOL
383const char *event_base_get_method(const struct event_base *);
384
385/**
386   Gets all event notification mechanisms supported by Libevent.
387
388   This functions returns the event mechanism in order preferred by
389   Libevent.  Note that this list will include all backends that
390   Libevent has compiled-in support for, and will not necessarily check
391   your OS to see whether it has the required resources.
392
393   @return an array with pointers to the names of support methods.
394     The end of the array is indicated by a NULL pointer.  If an
395     error is encountered NULL is returned.
396*/
397EVENT2_EXPORT_SYMBOL
398const char **event_get_supported_methods(void);
399
400/** Query the current monotonic time from a the timer for a struct
401 * event_base.
402 */
403EVENT2_EXPORT_SYMBOL
404int event_gettime_monotonic(struct event_base *base, struct timeval *tp);
405
406/**
407   @name event type flag
408
409   Flags to pass to event_base_get_num_events() to specify the kinds of events
410   we want to aggregate counts for
411*/
412/**@{*/
413/** count the number of active events, which have been triggered.*/
414#define EVENT_BASE_COUNT_ACTIVE                1U
415/** count the number of virtual events, which is used to represent an internal
416 * condition, other than a pending event, that keeps the loop from exiting. */
417#define EVENT_BASE_COUNT_VIRTUAL       2U
418/** count the number of events which have been added to event base, including
419 * internal events. */
420#define EVENT_BASE_COUNT_ADDED         4U
421/**@}*/
422
423/**
424   Gets the number of events in event_base, as specified in the flags.
425
426   Since event base has some internal events added to make some of its
427   functionalities work, EVENT_BASE_COUNT_ADDED may return more than the
428   number of events you added using event_add().
429
430   If you pass EVENT_BASE_COUNT_ACTIVE and EVENT_BASE_COUNT_ADDED together, an
431   active event will be counted twice. However, this might not be the case in
432   future libevent versions.  The return value is an indication of the work
433   load, but the user shouldn't rely on the exact value as this may change in
434   the future.
435
436   @param eb the event_base structure returned by event_base_new()
437   @param flags a bitwise combination of the kinds of events to aggregate
438       counts for
439   @return the number of events specified in the flags
440*/
441EVENT2_EXPORT_SYMBOL
442int event_base_get_num_events(struct event_base *, unsigned int);
443
444/**
445  Get the maximum number of events in a given event_base as specified in the
446  flags.
447
448  @param eb the event_base structure returned by event_base_new()
449  @param flags a bitwise combination of the kinds of events to aggregate
450         counts for
451  @param clear option used to reset the maximum count.
452  @return the number of events specified in the flags
453 */
454EVENT2_EXPORT_SYMBOL
455int event_base_get_max_events(struct event_base *, unsigned int, int);
456
457/**
458   Allocates a new event configuration object.
459
460   The event configuration object can be used to change the behavior of
461   an event base.
462
463   @return an event_config object that can be used to store configuration, or
464     NULL if an error is encountered.
465   @see event_base_new_with_config(), event_config_free(), event_config
466*/
467EVENT2_EXPORT_SYMBOL
468struct event_config *event_config_new(void);
469
470/**
471   Deallocates all memory associated with an event configuration object
472
473   @param cfg the event configuration object to be freed.
474*/
475EVENT2_EXPORT_SYMBOL
476void event_config_free(struct event_config *cfg);
477
478/**
479   Enters an event method that should be avoided into the configuration.
480
481   This can be used to avoid event mechanisms that do not support certain
482   file descriptor types, or for debugging to avoid certain event
483   mechanisms.  An application can make use of multiple event bases to
484   accommodate incompatible file descriptor types.
485
486   @param cfg the event configuration object
487   @param method the name of the event method to avoid
488   @return 0 on success, -1 on failure.
489*/
490EVENT2_EXPORT_SYMBOL
491int event_config_avoid_method(struct event_config *cfg, const char *method);
492
493/**
494   A flag used to describe which features an event_base (must) provide.
495
496   Because of OS limitations, not every Libevent backend supports every
497   possible feature.  You can use this type with
498   event_config_require_features() to tell Libevent to only proceed if your
499   event_base implements a given feature, and you can receive this type from
500   event_base_get_features() to see which features are available.
501*/
502enum event_method_feature {
503    /** Require an event method that allows edge-triggered events with EV_ET. */
504    EV_FEATURE_ET = 0x01,
505    /** Require an event method where having one event triggered among
506     * many is [approximately] an O(1) operation. This excludes (for
507     * example) select and poll, which are approximately O(N) for N
508     * equal to the total number of possible events. */
509    EV_FEATURE_O1 = 0x02,
510    /** Require an event method that allows file descriptors as well as
511     * sockets. */
512    EV_FEATURE_FDS = 0x04,
513    /** Require an event method that allows you to use EV_CLOSED to detect
514     * connection close without the necessity of reading all the pending data.
515     *
516     * Methods that do support EV_CLOSED may not be able to provide support on
517     * all kernel versions.
518     **/
519    EV_FEATURE_EARLY_CLOSE = 0x08
520};
521
522/**
523   A flag passed to event_config_set_flag().
524
525    These flags change the behavior of an allocated event_base.
526
527    @see event_config_set_flag(), event_base_new_with_config(),
528       event_method_feature
529 */
530enum event_base_config_flag {
531	/** Do not allocate a lock for the event base, even if we have
532	    locking set up.
533
534	    Setting this option will make it unsafe and nonfunctional to call
535	    functions on the base concurrently from multiple threads.
536	*/
537	EVENT_BASE_FLAG_NOLOCK = 0x01,
538	/** Do not check the EVENT_* environment variables when configuring
539	    an event_base  */
540	EVENT_BASE_FLAG_IGNORE_ENV = 0x02,
541	/** Windows only: enable the IOCP dispatcher at startup
542
543	    If this flag is set then bufferevent_socket_new() and
544	    evconn_listener_new() will use IOCP-backed implementations
545	    instead of the usual select-based one on Windows.
546	 */
547	EVENT_BASE_FLAG_STARTUP_IOCP = 0x04,
548	/** Instead of checking the current time every time the event loop is
549	    ready to run timeout callbacks, check after each timeout callback.
550	 */
551	EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08,
552
553	/** If we are using the epoll backend, this flag says that it is
554	    safe to use Libevent's internal change-list code to batch up
555	    adds and deletes in order to try to do as few syscalls as
556	    possible.  Setting this flag can make your code run faster, but
557	    it may trigger a Linux bug: it is not safe to use this flag
558	    if you have any fds cloned by dup() or its variants.  Doing so
559	    will produce strange and hard-to-diagnose bugs.
560
561	    This flag can also be activated by setting the
562	    EVENT_EPOLL_USE_CHANGELIST environment variable.
563
564	    This flag has no effect if you wind up using a backend other than
565	    epoll.
566	 */
567	EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10,
568
569	/** Ordinarily, Libevent implements its time and timeout code using
570	    the fastest monotonic timer that we have.  If this flag is set,
571	    however, we use less efficient more precise timer, assuming one is
572	    present.
573	 */
574	EVENT_BASE_FLAG_PRECISE_TIMER = 0x20
575};
576
577/**
578   Return a bitmask of the features implemented by an event base.  This
579   will be a bitwise OR of one or more of the values of
580   event_method_feature
581
582   @see event_method_feature
583 */
584EVENT2_EXPORT_SYMBOL
585int event_base_get_features(const struct event_base *base);
586
587/**
588   Enters a required event method feature that the application demands.
589
590   Note that not every feature or combination of features is supported
591   on every platform.  Code that requests features should be prepared
592   to handle the case where event_base_new_with_config() returns NULL, as in:
593   <pre>
594     event_config_require_features(cfg, EV_FEATURE_ET);
595     base = event_base_new_with_config(cfg);
596     if (base == NULL) {
597       // We can't get edge-triggered behavior here.
598       event_config_require_features(cfg, 0);
599       base = event_base_new_with_config(cfg);
600     }
601   </pre>
602
603   @param cfg the event configuration object
604   @param feature a bitfield of one or more event_method_feature values.
605          Replaces values from previous calls to this function.
606   @return 0 on success, -1 on failure.
607   @see event_method_feature, event_base_new_with_config()
608*/
609EVENT2_EXPORT_SYMBOL
610int event_config_require_features(struct event_config *cfg, int feature);
611
612/**
613 * Sets one or more flags to configure what parts of the eventual event_base
614 * will be initialized, and how they'll work.
615 *
616 * @see event_base_config_flags, event_base_new_with_config()
617 **/
618EVENT2_EXPORT_SYMBOL
619int event_config_set_flag(struct event_config *cfg, int flag);
620
621/**
622 * Records a hint for the number of CPUs in the system. This is used for
623 * tuning thread pools, etc, for optimal performance.  In Libevent 2.0,
624 * it is only on Windows, and only when IOCP is in use.
625 *
626 * @param cfg the event configuration object
627 * @param cpus the number of cpus
628 * @return 0 on success, -1 on failure.
629 */
630EVENT2_EXPORT_SYMBOL
631int event_config_set_num_cpus_hint(struct event_config *cfg, int cpus);
632
633/**
634 * Record an interval and/or a number of callbacks after which the event base
635 * should check for new events.  By default, the event base will run as many
636 * events are as activated at the highest activated priority before checking
637 * for new events.  If you configure it by setting max_interval, it will check
638 * the time after each callback, and not allow more than max_interval to
639 * elapse before checking for new events.  If you configure it by setting
640 * max_callbacks to a value >= 0, it will run no more than max_callbacks
641 * callbacks before checking for new events.
642 *
643 * This option can decrease the latency of high-priority events, and
644 * avoid priority inversions where multiple low-priority events keep us from
645 * polling for high-priority events, but at the expense of slightly decreasing
646 * the throughput.  Use it with caution!
647 *
648 * @param cfg The event_base configuration object.
649 * @param max_interval An interval after which Libevent should stop running
650 *     callbacks and check for more events, or NULL if there should be
651 *     no such interval.
652 * @param max_callbacks A number of callbacks after which Libevent should
653 *     stop running callbacks and check for more events, or -1 if there
654 *     should be no such limit.
655 * @param min_priority A priority below which max_interval and max_callbacks
656 *     should not be enforced.  If this is set to 0, they are enforced
657 *     for events of every priority; if it's set to 1, they're enforced
658 *     for events of priority 1 and above, and so on.
659 * @return 0 on success, -1 on failure.
660 **/
661EVENT2_EXPORT_SYMBOL
662int event_config_set_max_dispatch_interval(struct event_config *cfg,
663    const struct timeval *max_interval, int max_callbacks,
664    int min_priority);
665
666/**
667  Initialize the event API.
668
669  Use event_base_new_with_config() to initialize a new event base, taking
670  the specified configuration under consideration.  The configuration object
671  can currently be used to avoid certain event notification mechanisms.
672
673  @param cfg the event configuration object
674  @return an initialized event_base that can be used to registering events,
675     or NULL if no event base can be created with the requested event_config.
676  @see event_base_new(), event_base_free(), event_init(), event_assign()
677*/
678EVENT2_EXPORT_SYMBOL
679struct event_base *event_base_new_with_config(const struct event_config *);
680
681/**
682  Deallocate all memory associated with an event_base, and free the base.
683
684  Note that this function will not close any fds or free any memory passed
685  to event_new as the argument to callback.
686
687  If there are any pending finalizer callbacks, this function will invoke
688  them.
689
690  @param eb an event_base to be freed
691 */
692EVENT2_EXPORT_SYMBOL
693void event_base_free(struct event_base *);
694
695/**
696   As event_base_free, but do not run finalizers.
697 */
698EVENT2_EXPORT_SYMBOL
699void event_base_free_nofinalize(struct event_base *);
700
701/** @name Log severities
702 */
703/**@{*/
704#define EVENT_LOG_DEBUG 0
705#define EVENT_LOG_MSG   1
706#define EVENT_LOG_WARN  2
707#define EVENT_LOG_ERR   3
708/**@}*/
709
710/* Obsolete names: these are deprecated, but older programs might use them.
711 * They violate the reserved-identifier namespace. */
712#define _EVENT_LOG_DEBUG EVENT_LOG_DEBUG
713#define _EVENT_LOG_MSG EVENT_LOG_MSG
714#define _EVENT_LOG_WARN EVENT_LOG_WARN
715#define _EVENT_LOG_ERR EVENT_LOG_ERR
716
717/**
718  A callback function used to intercept Libevent's log messages.
719
720  @see event_set_log_callback
721 */
722typedef void (*event_log_cb)(int severity, const char *msg);
723/**
724  Redirect Libevent's log messages.
725
726  @param cb a function taking two arguments: an integer severity between
727     EVENT_LOG_DEBUG and EVENT_LOG_ERR, and a string.  If cb is NULL,
728	 then the default log is used.
729
730  NOTE: The function you provide *must not* call any other libevent
731  functionality.  Doing so can produce undefined behavior.
732  */
733EVENT2_EXPORT_SYMBOL
734void event_set_log_callback(event_log_cb cb);
735
736/**
737   A function to be called if Libevent encounters a fatal internal error.
738
739   @see event_set_fatal_callback
740 */
741typedef void (*event_fatal_cb)(int err);
742
743/**
744 Override Libevent's behavior in the event of a fatal internal error.
745
746 By default, Libevent will call exit(1) if a programming error makes it
747 impossible to continue correct operation.  This function allows you to supply
748 another callback instead.  Note that if the function is ever invoked,
749 something is wrong with your program, or with Libevent: any subsequent calls
750 to Libevent may result in undefined behavior.
751
752 Libevent will (almost) always log an EVENT_LOG_ERR message before calling
753 this function; look at the last log message to see why Libevent has died.
754 */
755EVENT2_EXPORT_SYMBOL
756void event_set_fatal_callback(event_fatal_cb cb);
757
758#define EVENT_DBG_ALL 0xffffffffu
759#define EVENT_DBG_NONE 0
760
761/**
762 Turn on debugging logs and have them sent to the default log handler.
763
764 This is a global setting; if you are going to call it, you must call this
765 before any calls that create an event-base.  You must call it before any
766 multithreaded use of Libevent.
767
768 Debug logs are verbose.
769
770 @param which Controls which debug messages are turned on.  This option is
771   unused for now; for forward compatibility, you must pass in the constant
772   "EVENT_DBG_ALL" to turn debugging logs on, or "EVENT_DBG_NONE" to turn
773   debugging logs off.
774 */
775EVENT2_EXPORT_SYMBOL
776void event_enable_debug_logging(ev_uint32_t which);
777
778/**
779  Associate a different event base with an event.
780
781  The event to be associated must not be currently active or pending.
782
783  @param eb the event base
784  @param ev the event
785  @return 0 on success, -1 on failure.
786 */
787EVENT2_EXPORT_SYMBOL
788int event_base_set(struct event_base *, struct event *);
789
790/** @name Loop flags
791
792    These flags control the behavior of event_base_loop().
793 */
794/**@{*/
795/** Block until we have an active event, then exit once all active events
796 * have had their callbacks run. */
797#define EVLOOP_ONCE	0x01
798/** Do not block: see which events are ready now, run the callbacks
799 * of the highest-priority ones, then exit. */
800#define EVLOOP_NONBLOCK	0x02
801/** Do not exit the loop because we have no pending events.  Instead, keep
802 * running until event_base_loopexit() or event_base_loopbreak() makes us
803 * stop.
804 */
805#define EVLOOP_NO_EXIT_ON_EMPTY 0x04
806/**@}*/
807
808/**
809  Wait for events to become active, and run their callbacks.
810
811  This is a more flexible version of event_base_dispatch().
812
813  By default, this loop will run the event base until either there are no more
814  pending or active events, or until something calls event_base_loopbreak() or
815  event_base_loopexit().  You can override this behavior with the 'flags'
816  argument.
817
818  @param eb the event_base structure returned by event_base_new() or
819     event_base_new_with_config()
820  @param flags any combination of EVLOOP_ONCE | EVLOOP_NONBLOCK
821  @return 0 if successful, -1 if an error occurred, or 1 if we exited because
822     no events were pending or active.
823  @see event_base_loopexit(), event_base_dispatch(), EVLOOP_ONCE,
824     EVLOOP_NONBLOCK
825  */
826EVENT2_EXPORT_SYMBOL
827int event_base_loop(struct event_base *, int);
828
829/**
830  Exit the event loop after the specified time
831
832  The next event_base_loop() iteration after the given timer expires will
833  complete normally (handling all queued events) then exit without
834  blocking for events again.
835
836  Subsequent invocations of event_base_loop() will proceed normally.
837
838  @param eb the event_base structure returned by event_init()
839  @param tv the amount of time after which the loop should terminate,
840    or NULL to exit after running all currently active events.
841  @return 0 if successful, or -1 if an error occurred
842  @see event_base_loopbreak()
843 */
844EVENT2_EXPORT_SYMBOL
845int event_base_loopexit(struct event_base *, const struct timeval *);
846
847/**
848  Abort the active event_base_loop() immediately.
849
850  event_base_loop() will abort the loop after the next event is completed;
851  event_base_loopbreak() is typically invoked from this event's callback.
852  This behavior is analogous to the "break;" statement.
853
854  Subsequent invocations of event_base_loop() will proceed normally.
855
856  @param eb the event_base structure returned by event_init()
857  @return 0 if successful, or -1 if an error occurred
858  @see event_base_loopexit()
859 */
860EVENT2_EXPORT_SYMBOL
861int event_base_loopbreak(struct event_base *);
862
863/**
864  Tell the active event_base_loop() to scan for new events immediately.
865
866  Calling this function makes the currently active event_base_loop()
867  start the loop over again (scanning for new events) after the current
868  event callback finishes.  If the event loop is not running, this
869  function has no effect.
870
871  event_base_loopbreak() is typically invoked from this event's callback.
872  This behavior is analogous to the "continue;" statement.
873
874  Subsequent invocations of event loop will proceed normally.
875
876  @param eb the event_base structure returned by event_init()
877  @return 0 if successful, or -1 if an error occurred
878  @see event_base_loopbreak()
879 */
880EVENT2_EXPORT_SYMBOL
881int event_base_loopcontinue(struct event_base *);
882
883/**
884  Checks if the event loop was told to exit by event_base_loopexit().
885
886  This function will return true for an event_base at every point after
887  event_loopexit() is called, until the event loop is next entered.
888
889  @param eb the event_base structure returned by event_init()
890  @return true if event_base_loopexit() was called on this event base,
891    or 0 otherwise
892  @see event_base_loopexit()
893  @see event_base_got_break()
894 */
895EVENT2_EXPORT_SYMBOL
896int event_base_got_exit(struct event_base *);
897
898/**
899  Checks if the event loop was told to abort immediately by event_base_loopbreak().
900
901  This function will return true for an event_base at every point after
902  event_base_loopbreak() is called, until the event loop is next entered.
903
904  @param eb the event_base structure returned by event_init()
905  @return true if event_base_loopbreak() was called on this event base,
906    or 0 otherwise
907  @see event_base_loopbreak()
908  @see event_base_got_exit()
909 */
910EVENT2_EXPORT_SYMBOL
911int event_base_got_break(struct event_base *);
912
913/**
914 * @name event flags
915 *
916 * Flags to pass to event_new(), event_assign(), event_pending(), and
917 * anything else with an argument of the form "short events"
918 */
919/**@{*/
920/** Indicates that a timeout has occurred.  It's not necessary to pass
921 * this flag to event_for new()/event_assign() to get a timeout. */
922#define EV_TIMEOUT	0x01
923/** Wait for a socket or FD to become readable */
924#define EV_READ		0x02
925/** Wait for a socket or FD to become writeable */
926#define EV_WRITE	0x04
927/** Wait for a POSIX signal to be raised*/
928#define EV_SIGNAL	0x08
929/**
930 * Persistent event: won't get removed automatically when activated.
931 *
932 * When a persistent event with a timeout becomes activated, its timeout
933 * is reset to 0.
934 */
935#define EV_PERSIST	0x10
936/** Select edge-triggered behavior, if supported by the backend. */
937#define EV_ET		0x20
938/**
939 * If this option is provided, then event_del() will not block in one thread
940 * while waiting for the event callback to complete in another thread.
941 *
942 * To use this option safely, you may need to use event_finalize() or
943 * event_free_finalize() in order to safely tear down an event in a
944 * multithreaded application.  See those functions for more information.
945 **/
946#define EV_FINALIZE     0x40
947/**
948 * Detects connection close events.  You can use this to detect when a
949 * connection has been closed, without having to read all the pending data
950 * from a connection.
951 *
952 * Not all backends support EV_CLOSED.  To detect or require it, use the
953 * feature flag EV_FEATURE_EARLY_CLOSE.
954 **/
955#define EV_CLOSED	0x80
956/**@}*/
957
958/**
959   @name evtimer_* macros
960
961   Aliases for working with one-shot timer events
962   If you need EV_PERSIST timer use event_*() functions.
963 */
964/**@{*/
965#define evtimer_assign(ev, b, cb, arg) \
966	event_assign((ev), (b), -1, 0, (cb), (arg))
967#define evtimer_new(b, cb, arg)		event_new((b), -1, 0, (cb), (arg))
968#define evtimer_add(ev, tv)		event_add((ev), (tv))
969#define evtimer_del(ev)			event_del(ev)
970#define evtimer_pending(ev, tv)		event_pending((ev), EV_TIMEOUT, (tv))
971#define evtimer_initialized(ev)		event_initialized(ev)
972/**@}*/
973
974/**
975   @name evsignal_* macros
976
977   Aliases for working with signal events
978 */
979/**@{*/
980#define evsignal_add(ev, tv)		event_add((ev), (tv))
981#define evsignal_assign(ev, b, x, cb, arg)			\
982	event_assign((ev), (b), (x), EV_SIGNAL|EV_PERSIST, cb, (arg))
983#define evsignal_new(b, x, cb, arg)				\
984	event_new((b), (x), EV_SIGNAL|EV_PERSIST, (cb), (arg))
985#define evsignal_del(ev)		event_del(ev)
986#define evsignal_pending(ev, tv)	event_pending((ev), EV_SIGNAL, (tv))
987#define evsignal_initialized(ev)	event_initialized(ev)
988/**@}*/
989
990/**
991   @name evuser_* macros
992
993   Aliases for working with user-triggered events
994   If you need EV_PERSIST event use event_*() functions.
995 */
996/**@{*/
997#define evuser_new(b, cb, arg)		event_new((b), -1, 0, (cb), (arg))
998#define evuser_del(ev)			event_del(ev)
999#define evuser_pending(ev, tv)		event_pending((ev), 0, (tv))
1000#define evuser_initialized(ev)		event_initialized(ev)
1001#define evuser_trigger(ev)		event_active((ev), 0, 0)
1002/**@}*/
1003
1004/**
1005   A callback function for an event.
1006
1007   It receives three arguments:
1008
1009   @param fd An fd or signal
1010   @param events One or more EV_* flags
1011   @param arg A user-supplied argument.
1012
1013   @see event_new()
1014 */
1015typedef void (*event_callback_fn)(evutil_socket_t, short, void *);
1016
1017/**
1018  Return a value used to specify that the event itself must be used as the callback argument.
1019
1020  The function event_new() takes a callback argument which is passed
1021  to the event's callback function. To specify that the argument to be
1022  passed to the callback function is the event that event_new() returns,
1023  pass in the return value of event_self_cbarg() as the callback argument
1024  for event_new().
1025
1026  For example:
1027  <pre>
1028      struct event *ev = event_new(base, sock, events, callback, %event_self_cbarg());
1029  </pre>
1030
1031  For consistency with event_new(), it is possible to pass the return value
1032  of this function as the callback argument for event_assign() &ndash; this
1033  achieves the same result as passing the event in directly.
1034
1035  @return a value to be passed as the callback argument to event_new() or
1036  event_assign().
1037  @see event_new(), event_assign()
1038 */
1039EVENT2_EXPORT_SYMBOL
1040void *event_self_cbarg(void);
1041
1042/**
1043  Allocate and assign a new event structure, ready to be added.
1044
1045  The function event_new() returns a new event that can be used in
1046  future calls to event_add() and event_del().  The fd and events
1047  arguments determine which conditions will trigger the event; the
1048  callback and callback_arg arguments tell Libevent what to do when the
1049  event becomes active.
1050
1051  If events contains one of EV_READ, EV_WRITE, or EV_READ|EV_WRITE, then
1052  fd is a file descriptor or socket that should get monitored for
1053  readiness to read, readiness to write, or readiness for either operation
1054  (respectively).  If events contains EV_SIGNAL, then fd is a signal
1055  number to wait for.  If events contains none of those flags, then the
1056  event can be triggered only by a timeout or by manual activation with
1057  event_active(): In this case, fd must be -1.
1058
1059  The EV_PERSIST flag can also be passed in the events argument: it makes
1060  event_add() persistent until event_del() is called.
1061
1062  The EV_ET flag is compatible with EV_READ and EV_WRITE, and supported
1063  only by certain backends.  It tells Libevent to use edge-triggered
1064  events.
1065
1066  The EV_TIMEOUT flag has no effect here.
1067
1068  It is okay to have multiple events all listening on the same fds; but
1069  they must either all be edge-triggered, or all not be edge triggered.
1070
1071  When the event becomes active, the event loop will run the provided
1072  callback function, with three arguments.  The first will be the provided
1073  fd value.  The second will be a bitfield of the events that triggered:
1074  EV_READ, EV_WRITE, or EV_SIGNAL.  Here the EV_TIMEOUT flag indicates
1075  that a timeout occurred, and EV_ET indicates that an edge-triggered
1076  event occurred.  The third event will be the callback_arg pointer that
1077  you provide.
1078
1079  @param base the event base to which the event should be attached.
1080  @param fd the file descriptor or signal to be monitored, or -1.
1081  @param events desired events to monitor: bitfield of EV_READ, EV_WRITE,
1082      EV_SIGNAL, EV_PERSIST, EV_ET.
1083  @param callback callback function to be invoked when the event occurs
1084  @param callback_arg an argument to be passed to the callback function
1085
1086  @return a newly allocated struct event that must later be freed with
1087    event_free() or NULL if an error occurred.
1088  @see event_free(), event_add(), event_del(), event_assign()
1089 */
1090EVENT2_EXPORT_SYMBOL
1091struct event *event_new(struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
1092
1093
1094/**
1095  Prepare a new, already-allocated event structure to be added.
1096
1097  The function event_assign() prepares the event structure ev to be used
1098  in future calls to event_add() and event_del().  Unlike event_new(), it
1099  doesn't allocate memory itself: it requires that you have already
1100  allocated a struct event, probably on the heap.  Doing this will
1101  typically make your code depend on the size of the event structure, and
1102  thereby create incompatibility with future versions of Libevent.
1103
1104  The easiest way to avoid this problem is just to use event_new() and
1105  event_free() instead.
1106
1107  A slightly harder way to future-proof your code is to use
1108  event_get_struct_event_size() to determine the required size of an event
1109  at runtime.
1110
1111  Note that it is NOT safe to call this function on an event that is
1112  active or pending.  Doing so WILL corrupt internal data structures in
1113  Libevent, and lead to strange, hard-to-diagnose bugs.  You _can_ use
1114  event_assign to change an existing event, but only if it is not active
1115  or pending!
1116
1117  The arguments for this function, and the behavior of the events that it
1118  makes, are as for event_new().
1119
1120  @param ev an event struct to be modified
1121  @param base the event base to which ev should be attached.
1122  @param fd the file descriptor to be monitored
1123  @param events desired events to monitor; can be EV_READ and/or EV_WRITE
1124  @param callback callback function to be invoked when the event occurs
1125  @param callback_arg an argument to be passed to the callback function
1126
1127  @return 0 if success, or -1 on invalid arguments.
1128
1129  @see event_new(), event_add(), event_del(), event_base_once(),
1130    event_get_struct_event_size()
1131  */
1132EVENT2_EXPORT_SYMBOL
1133int event_assign(struct event *, struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
1134
1135/**
1136   Deallocate a struct event * returned by event_new().
1137
1138   If the event is pending or active, this function makes it non-pending
1139   and non-active first.
1140 */
1141EVENT2_EXPORT_SYMBOL
1142void event_free(struct event *);
1143
1144/**
1145 * Callback type for event_finalize and event_free_finalize().
1146 **/
1147typedef void (*event_finalize_callback_fn)(struct event *, void *);
1148/**
1149   @name Finalization functions
1150
1151   These functions are used to safely tear down an event in a multithreaded
1152   application.  If you construct your events with EV_FINALIZE to avoid
1153   deadlocks, you will need a way to remove an event in the certainty that
1154   it will definitely not be running its callback when you deallocate it
1155   and its callback argument.
1156
1157   To do this, call one of event_finalize() or event_free_finalize with
1158   0 for its first argument, the event to tear down as its second argument,
1159   and a callback function as its third argument.  The callback will be
1160   invoked as part of the event loop, with the event's priority.
1161
1162   After you call a finalizer function, event_add() and event_active() will
1163   no longer work on the event, and event_del() will produce a no-op. You
1164   must not try to change the event's fields with event_assign() or
1165   event_set() while the finalize callback is in progress.  Once the
1166   callback has been invoked, you should treat the event structure as
1167   containing uninitialized memory.
1168
1169   The event_free_finalize() function frees the event after it's finalized;
1170   event_finalize() does not.
1171
1172   A finalizer callback must not make events pending or active.  It must not
1173   add events, activate events, or attempt to "resuscitate" the event being
1174   finalized in any way.
1175
1176   @return 0 on success, -1 on failure.
1177 */
1178/**@{*/
1179EVENT2_EXPORT_SYMBOL
1180int event_finalize(unsigned, struct event *, event_finalize_callback_fn);
1181EVENT2_EXPORT_SYMBOL
1182int event_free_finalize(unsigned, struct event *, event_finalize_callback_fn);
1183/**@}*/
1184
1185/**
1186  Schedule a one-time event
1187
1188  The function event_base_once() is similar to event_new().  However, it
1189  schedules a callback to be called exactly once, and does not require the
1190  caller to prepare an event structure.
1191
1192  Note that in Libevent 2.0 and earlier, if the event is never triggered, the
1193  internal memory used to hold it will never be freed.  In Libevent 2.1,
1194  the internal memory will get freed by event_base_free() if the event
1195  is never triggered.  The 'arg' value, however, will not get freed in either
1196  case--you'll need to free that on your own if you want it to go away.
1197
1198  @param base an event_base
1199  @param fd a file descriptor to monitor, or -1 for no fd.
1200  @param events event(s) to monitor; can be any of EV_READ |
1201         EV_WRITE, or EV_TIMEOUT
1202  @param callback callback function to be invoked when the event occurs
1203  @param arg an argument to be passed to the callback function
1204  @param timeout the maximum amount of time to wait for the event. NULL
1205         makes an EV_READ/EV_WRITE event make forever; NULL makes an
1206        EV_TIMEOUT event success immediately.
1207  @return 0 if successful, or -1 if an error occurred
1208 */
1209EVENT2_EXPORT_SYMBOL
1210int event_base_once(struct event_base *, evutil_socket_t, short, event_callback_fn, void *, const struct timeval *);
1211
1212/**
1213  Add an event to the set of pending events.
1214
1215  The function event_add() schedules the execution of the event 'ev' when the
1216  condition specified by event_assign() or event_new() occurs, or when the time
1217  specified in timeout has elapsed.  If a timeout is NULL, no timeout
1218  occurs and the function will only be
1219  called if a matching event occurs.  The event in the
1220  ev argument must be already initialized by event_assign() or event_new()
1221  and may not be used
1222  in calls to event_assign() until it is no longer pending.
1223
1224  If the event in the ev argument already has a scheduled timeout, calling
1225  event_add() replaces the old timeout with the new one if tv is non-NULL.
1226
1227  @param ev an event struct initialized via event_assign() or event_new()
1228  @param timeout the maximum amount of time to wait for the event, or NULL
1229         to wait forever
1230  @return 0 if successful, or -1 if an error occurred
1231  @see event_del(), event_assign(), event_new()
1232  */
1233EVENT2_EXPORT_SYMBOL
1234int event_add(struct event *ev, const struct timeval *timeout);
1235
1236/**
1237   Remove a timer from a pending event without removing the event itself.
1238
1239   If the event has a scheduled timeout, this function unschedules it but
1240   leaves the event otherwise pending.
1241
1242   @param ev an event struct initialized via event_assign() or event_new()
1243   @return 0 on success, or -1 if an error occurred.
1244*/
1245EVENT2_EXPORT_SYMBOL
1246int event_remove_timer(struct event *ev);
1247
1248/**
1249  Remove an event from the set of monitored events.
1250
1251  The function event_del() will cancel the event in the argument ev.  If the
1252  event has already executed or has never been added the call will have no
1253  effect.
1254
1255  @param ev an event struct to be removed from the working set
1256  @return 0 if successful, or -1 if an error occurred
1257  @see event_add()
1258 */
1259EVENT2_EXPORT_SYMBOL
1260int event_del(struct event *);
1261
1262/**
1263   As event_del(), but never blocks while the event's callback is running
1264   in another thread, even if the event was constructed without the
1265   EV_FINALIZE flag.
1266 */
1267EVENT2_EXPORT_SYMBOL
1268int event_del_noblock(struct event *ev);
1269/**
1270   As event_del(), but always blocks while the event's callback is running
1271   in another thread, even if the event was constructed with the
1272   EV_FINALIZE flag.
1273 */
1274EVENT2_EXPORT_SYMBOL
1275int event_del_block(struct event *ev);
1276
1277/**
1278  Make an event active.
1279
1280  You can use this function on a pending or a non-pending event to make it
1281  active, so that its callback will be run by event_base_dispatch() or
1282  event_base_loop().
1283
1284  One common use in multithreaded programs is to wake the thread running
1285  event_base_loop() from another thread.
1286
1287  @param ev an event to make active.
1288  @param res a set of flags to pass to the event's callback.
1289  @param ncalls an obsolete argument: this is ignored.
1290 **/
1291EVENT2_EXPORT_SYMBOL
1292void event_active(struct event *ev, int res, short ncalls);
1293
1294/**
1295  Checks if a specific event is pending or scheduled.
1296
1297  @param ev an event struct previously passed to event_add()
1298  @param events the requested event type; any of EV_TIMEOUT|EV_READ|
1299         EV_WRITE|EV_SIGNAL
1300  @param tv if this field is not NULL, and the event has a timeout,
1301         this field is set to hold the time at which the timeout will
1302	 expire.
1303
1304  @return true if the event is pending on any of the events in 'what', (that
1305  is to say, it has been added), or 0 if the event is not added.
1306 */
1307EVENT2_EXPORT_SYMBOL
1308int event_pending(const struct event *ev, short events, struct timeval *tv);
1309
1310/**
1311   If called from within the callback for an event, returns that event.
1312
1313   The behavior of this function is not defined when called from outside the
1314   callback function for an event.
1315 */
1316EVENT2_EXPORT_SYMBOL
1317struct event *event_base_get_running_event(struct event_base *base);
1318
1319/**
1320  Test if an event structure might be initialized.
1321
1322  The event_initialized() function can be used to check if an event has been
1323  initialized.
1324
1325  Warning: This function is only useful for distinguishing a zeroed-out
1326    piece of memory from an initialized event, it can easily be confused by
1327    uninitialized memory.  Thus, it should ONLY be used to distinguish an
1328    initialized event from zero.
1329
1330  @param ev an event structure to be tested
1331  @return 1 if the structure might be initialized, or 0 if it has not been
1332          initialized
1333 */
1334EVENT2_EXPORT_SYMBOL
1335int event_initialized(const struct event *ev);
1336
1337/**
1338   Get the signal number assigned to a signal event
1339*/
1340#define event_get_signal(ev) ((int)event_get_fd(ev))
1341
1342/**
1343   Get the socket or signal assigned to an event, or -1 if the event has
1344   no socket.
1345*/
1346EVENT2_EXPORT_SYMBOL
1347evutil_socket_t event_get_fd(const struct event *ev);
1348
1349/**
1350   Get the event_base associated with an event.
1351*/
1352EVENT2_EXPORT_SYMBOL
1353struct event_base *event_get_base(const struct event *ev);
1354
1355/**
1356   Return the events (EV_READ, EV_WRITE, etc) assigned to an event.
1357*/
1358EVENT2_EXPORT_SYMBOL
1359short event_get_events(const struct event *ev);
1360
1361/**
1362   Return the callback assigned to an event.
1363*/
1364EVENT2_EXPORT_SYMBOL
1365event_callback_fn event_get_callback(const struct event *ev);
1366
1367/**
1368   Return the callback argument assigned to an event.
1369*/
1370EVENT2_EXPORT_SYMBOL
1371void *event_get_callback_arg(const struct event *ev);
1372
1373/**
1374   Return the priority of an event.
1375   @see event_priority_init(), event_get_priority()
1376*/
1377EVENT2_EXPORT_SYMBOL
1378int event_get_priority(const struct event *ev);
1379
1380/**
1381   Extract _all_ of arguments given to construct a given event.  The
1382   event_base is copied into *base_out, the fd is copied into *fd_out, and so
1383   on.
1384
1385   If any of the "_out" arguments is NULL, it will be ignored.
1386 */
1387EVENT2_EXPORT_SYMBOL
1388void event_get_assignment(const struct event *event,
1389    struct event_base **base_out, evutil_socket_t *fd_out, short *events_out,
1390    event_callback_fn *callback_out, void **arg_out);
1391
1392/**
1393   Return the size of struct event that the Libevent library was compiled
1394   with.
1395
1396   This will be NO GREATER than sizeof(struct event) if you're running with
1397   the same version of Libevent that your application was built with, but
1398   otherwise might not.
1399
1400   Note that it might be SMALLER than sizeof(struct event) if some future
1401   version of Libevent adds extra padding to the end of struct event.
1402   We might do this to help ensure ABI-compatibility between different
1403   versions of Libevent.
1404 */
1405EVENT2_EXPORT_SYMBOL
1406size_t event_get_struct_event_size(void);
1407
1408/**
1409   Get the Libevent version.
1410
1411   Note that this will give you the version of the library that you're
1412   currently linked against, not the version of the headers that you've
1413   compiled against.
1414
1415   @return a string containing the version number of Libevent
1416*/
1417EVENT2_EXPORT_SYMBOL
1418const char *event_get_version(void);
1419
1420/**
1421   Return a numeric representation of Libevent's version.
1422
1423   Note that this will give you the version of the library that you're
1424   currently linked against, not the version of the headers you've used to
1425   compile.
1426
1427   The format uses one byte each for the major, minor, and patchlevel parts of
1428   the version number.  The low-order byte is unused.  For example, version
1429   2.0.1-alpha has a numeric representation of 0x02000100
1430*/
1431EVENT2_EXPORT_SYMBOL
1432ev_uint32_t event_get_version_number(void);
1433
1434/** As event_get_version, but gives the version of Libevent's headers. */
1435#define LIBEVENT_VERSION EVENT__VERSION
1436/** As event_get_version_number, but gives the version number of Libevent's
1437 * headers. */
1438#define LIBEVENT_VERSION_NUMBER EVENT__NUMERIC_VERSION
1439
1440/** Largest number of priorities that Libevent can support. */
1441#define EVENT_MAX_PRIORITIES 256
1442/**
1443  Set the number of different event priorities
1444
1445  By default Libevent schedules all active events with the same priority.
1446  However, some time it is desirable to process some events with a higher
1447  priority than others.  For that reason, Libevent supports strict priority
1448  queues.  Active events with a lower priority are always processed before
1449  events with a higher priority.
1450
1451  The number of different priorities can be set initially with the
1452  event_base_priority_init() function.  This function should be called
1453  before the first call to event_base_dispatch().  The
1454  event_priority_set() function can be used to assign a priority to an
1455  event.  By default, Libevent assigns the middle priority to all events
1456  unless their priority is explicitly set.
1457
1458  Note that urgent-priority events can starve less-urgent events: after
1459  running all urgent-priority callbacks, Libevent checks for more urgent
1460  events again, before running less-urgent events.  Less-urgent events
1461  will not have their callbacks run until there are no events more urgent
1462  than them that want to be active.
1463
1464  @param eb the event_base structure returned by event_base_new()
1465  @param npriorities the maximum number of priorities
1466  @return 0 if successful, or -1 if an error occurred
1467  @see event_priority_set()
1468 */
1469EVENT2_EXPORT_SYMBOL
1470int	event_base_priority_init(struct event_base *, int);
1471
1472/**
1473  Get the number of different event priorities.
1474
1475  @param eb the event_base structure returned by event_base_new()
1476  @return Number of different event priorities
1477  @see event_base_priority_init()
1478*/
1479EVENT2_EXPORT_SYMBOL
1480int	event_base_get_npriorities(struct event_base *eb);
1481
1482/**
1483  Assign a priority to an event.
1484
1485  @param ev an event struct
1486  @param priority the new priority to be assigned
1487  @return 0 if successful, or -1 if an error occurred
1488  @see event_priority_init(), event_get_priority()
1489  */
1490EVENT2_EXPORT_SYMBOL
1491int	event_priority_set(struct event *, int);
1492
1493/**
1494   Prepare an event_base to use a large number of timeouts with the same
1495   duration.
1496
1497   Libevent's default scheduling algorithm is optimized for having a large
1498   number of timeouts with their durations more or less randomly
1499   distributed.  But if you have a large number of timeouts that all have
1500   the same duration (for example, if you have a large number of
1501   connections that all have a 10-second timeout), then you can improve
1502   Libevent's performance by telling Libevent about it.
1503
1504   To do this, call this function with the common duration.  It will return a
1505   pointer to a different, opaque timeout value.  (Don't depend on its actual
1506   contents!)  When you use this timeout value in event_add(), Libevent will
1507   schedule the event more efficiently.
1508
1509   (This optimization probably will not be worthwhile until you have thousands
1510   or tens of thousands of events with the same timeout.)
1511 */
1512EVENT2_EXPORT_SYMBOL
1513const struct timeval *event_base_init_common_timeout(struct event_base *base,
1514    const struct timeval *duration);
1515
1516#if !defined(EVENT__DISABLE_MM_REPLACEMENT) || defined(EVENT_IN_DOXYGEN_)
1517/**
1518 Override the functions that Libevent uses for memory management.
1519
1520 Usually, Libevent uses the standard libc functions malloc, realloc, and
1521 free to allocate memory.  Passing replacements for those functions to
1522 event_set_mem_functions() overrides this behavior.
1523
1524 Note that all memory returned from Libevent will be allocated by the
1525 replacement functions rather than by malloc() and realloc().  Thus, if you
1526 have replaced those functions, it will not be appropriate to free() memory
1527 that you get from Libevent.  Instead, you must use the free_fn replacement
1528 that you provided.
1529
1530 Note also that if you are going to call this function, you should do so
1531 before any call to any Libevent function that does allocation.
1532 Otherwise, those functions will allocate their memory using malloc(), but
1533 then later free it using your provided free_fn.
1534
1535 @param malloc_fn A replacement for malloc.
1536 @param realloc_fn A replacement for realloc
1537 @param free_fn A replacement for free.
1538 **/
1539EVENT2_EXPORT_SYMBOL
1540void event_set_mem_functions(
1541	void *(*malloc_fn)(size_t sz),
1542	void *(*realloc_fn)(void *ptr, size_t sz),
1543	void (*free_fn)(void *ptr));
1544/** This definition is present if Libevent was built with support for
1545    event_set_mem_functions() */
1546#define EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
1547#endif
1548
1549/**
1550   Writes a human-readable description of all inserted and/or active
1551   events to a provided stdio stream.
1552
1553   This is intended for debugging; its format is not guaranteed to be the same
1554   between libevent versions.
1555
1556   @param base An event_base on which to scan the events.
1557   @param output A stdio file to write on.
1558 */
1559EVENT2_EXPORT_SYMBOL
1560void event_base_dump_events(struct event_base *, FILE *);
1561
1562
1563/**
1564   Activates all pending events for the given fd and event mask.
1565
1566   This function activates pending events only.  Events which have not been
1567   added will not become active.
1568
1569   @param base the event_base on which to activate the events.
1570   @param fd An fd to active events on.
1571   @param events One or more of EV_{READ,WRITE,TIMEOUT}.
1572 */
1573EVENT2_EXPORT_SYMBOL
1574void event_base_active_by_fd(struct event_base *base, evutil_socket_t fd, short events);
1575
1576/**
1577   Activates all pending signals with a given signal number
1578
1579   This function activates pending events only.  Events which have not been
1580   added will not become active.
1581
1582   @param base the event_base on which to activate the events.
1583   @param fd The signal to active events on.
1584 */
1585EVENT2_EXPORT_SYMBOL
1586void event_base_active_by_signal(struct event_base *base, int sig);
1587
1588/**
1589 * Callback for iterating events in an event base via event_base_foreach_event
1590 */
1591typedef int (*event_base_foreach_event_cb)(const struct event_base *, const struct event *, void *);
1592
1593/**
1594   Iterate over all added or active events events in an event loop, and invoke
1595   a given callback on each one.
1596
1597   The callback must not call any function that modifies the event base, that
1598   modifies any event in the event base, or that adds or removes any event to
1599   the event base.  Doing so is unsupported and will lead to undefined
1600   behavior -- likely, to crashes.
1601
1602   event_base_foreach_event() holds a lock on the event_base() for the whole
1603   time it's running: slow callbacks are not advisable.
1604
1605   Note that Libevent adds some events of its own to make pieces of its
1606   functionality work.  You must not assume that the only events you'll
1607   encounter will be the ones you added yourself.
1608
1609   The callback function must return 0 to continue iteration, or some other
1610   integer to stop iterating.
1611
1612   @param base An event_base on which to scan the events.
1613   @param fn   A callback function to receive the events.
1614   @param arg  An argument passed to the callback function.
1615   @return 0 if we iterated over every event, or the value returned by the
1616      callback function if the loop exited early.
1617*/
1618EVENT2_EXPORT_SYMBOL
1619int event_base_foreach_event(struct event_base *base, event_base_foreach_event_cb fn, void *arg);
1620
1621
1622/** Sets 'tv' to the current time (as returned by gettimeofday()),
1623    looking at the cached value in 'base' if possible, and calling
1624    gettimeofday() or clock_gettime() as appropriate if there is no
1625    cached time.
1626
1627    Generally, this value will only be cached while actually
1628    processing event callbacks, and may be very inaccurate if your
1629    callbacks take a long time to execute.
1630
1631    Returns 0 on success, negative on failure.
1632 */
1633EVENT2_EXPORT_SYMBOL
1634int event_base_gettimeofday_cached(struct event_base *base,
1635    struct timeval *tv);
1636
1637/** Update cached_tv in the 'base' to the current time
1638 *
1639 * You can use this function is useful for selectively increasing
1640 * the accuracy of the cached time value in 'base' during callbacks
1641 * that take a long time to execute.
1642 *
1643 * This function has no effect if the base is currently not in its
1644 * event loop, or if timeval caching is disabled via
1645 * EVENT_BASE_FLAG_NO_CACHE_TIME.
1646 *
1647 * @return 0 on success, -1 on failure
1648 */
1649EVENT2_EXPORT_SYMBOL
1650int event_base_update_cache_time(struct event_base *base);
1651
1652/** Release up all globally-allocated resources allocated by Libevent.
1653
1654    This function does not free developer-controlled resources like
1655    event_bases, events, bufferevents, listeners, and so on.  It only releases
1656    resources like global locks that there is no other way to free.
1657
1658    It is not actually necessary to call this function before exit: every
1659    resource that it frees would be released anyway on exit.  It mainly exists
1660    so that resource-leak debugging tools don't see Libevent as holding
1661    resources at exit.
1662
1663    You should only call this function when no other Libevent functions will
1664    be invoked -- e.g., when cleanly exiting a program.
1665 */
1666EVENT2_EXPORT_SYMBOL
1667void libevent_global_shutdown(void);
1668
1669#ifdef __cplusplus
1670}
1671#endif
1672
1673#endif /* EVENT2_EVENT_H_INCLUDED_ */
1674