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