libevent
event.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 #ifndef _EVENT2_EVENT_H_
28 #define _EVENT2_EVENT_H_
29 
183 #ifdef __cplusplus
184 extern "C" {
185 #endif
186 
187 #include <event2/event-config.h>
188 #ifdef _EVENT_HAVE_SYS_TYPES_H
189 #include <sys/types.h>
190 #endif
191 #ifdef _EVENT_HAVE_SYS_TIME_H
192 #include <sys/time.h>
193 #endif
194 
195 #include <stdio.h>
196 
197 /* For int types. */
198 #include <event2/util.h>
199 
214 #ifdef _EVENT_IN_DOXYGEN
215 {/*Empty body so that doxygen will generate documentation here.*/}
216 #endif
217 ;
218 
272 struct event
273 #ifdef _EVENT_IN_DOXYGEN
274 {/*Empty body so that doxygen will generate documentation here.*/}
275 #endif
276 ;
277 
292 #ifdef _EVENT_IN_DOXYGEN
293 {/*Empty body so that doxygen will generate documentation here.*/}
294 #endif
295 ;
296 
317 void event_enable_debug_mode(void);
318 
328 void event_debug_unassign(struct event *);
329 
337 struct event_base *event_base_new(void);
338 
349 int event_reinit(struct event_base *base);
350 
364 int event_base_dispatch(struct event_base *);
365 
372 const char *event_base_get_method(const struct event_base *);
373 
386 const char **event_get_supported_methods(void);
387 
398 struct event_config *event_config_new(void);
399 
405 void event_config_free(struct event_config *cfg);
406 
419 int event_config_avoid_method(struct event_config *cfg, const char *method);
420 
441 };
442 
469 
485 };
486 
494 int event_base_get_features(const struct event_base *base);
495 
518 int event_config_require_features(struct event_config *cfg, int feature);
519 
526 int event_config_set_flag(struct event_config *cfg, int flag);
527 
537 int event_config_set_num_cpus_hint(struct event_config *cfg, int cpus);
538 
551 struct event_base *event_base_new_with_config(const struct event_config *);
552 
561 void event_base_free(struct event_base *);
562 
566 #define EVENT_LOG_DEBUG 0
567 #define EVENT_LOG_MSG 1
568 #define EVENT_LOG_WARN 2
569 #define EVENT_LOG_ERR 3
570 
572 /* Obsolete names: these are deprecated, but older programs might use them.
573  * They violate the reserved-identifier namespace. */
574 #define _EVENT_LOG_DEBUG EVENT_LOG_DEBUG
575 #define _EVENT_LOG_MSG EVENT_LOG_MSG
576 #define _EVENT_LOG_WARN EVENT_LOG_WARN
577 #define _EVENT_LOG_ERR EVENT_LOG_ERR
578 
584 typedef void (*event_log_cb)(int severity, const char *msg);
596 
602 typedef void (*event_fatal_cb)(int err);
603 
617 
627 int event_base_set(struct event_base *, struct event *);
628 
636 #define EVLOOP_ONCE 0x01
637 
639 #define EVLOOP_NONBLOCK 0x02
640 
660 int event_base_loop(struct event_base *, int);
661 
677 int event_base_loopexit(struct event_base *, const struct timeval *);
678 
692 int event_base_loopbreak(struct event_base *);
693 
706 int event_base_got_exit(struct event_base *);
707 
720 int event_base_got_break(struct event_base *);
721 
731 #define EV_TIMEOUT 0x01
732 
733 #define EV_READ 0x02
734 
735 #define EV_WRITE 0x04
736 
737 #define EV_SIGNAL 0x08
738 
744 #define EV_PERSIST 0x10
745 
746 #define EV_ET 0x20
747 
754 #define evtimer_assign(ev, b, cb, arg) \
755  event_assign((ev), (b), -1, 0, (cb), (arg))
756 #define evtimer_new(b, cb, arg) event_new((b), -1, 0, (cb), (arg))
757 #define evtimer_add(ev, tv) event_add((ev), (tv))
758 #define evtimer_del(ev) event_del(ev)
759 #define evtimer_pending(ev, tv) event_pending((ev), EV_TIMEOUT, (tv))
760 #define evtimer_initialized(ev) event_initialized(ev)
761 
769 #define evsignal_add(ev, tv) event_add((ev), (tv))
770 #define evsignal_assign(ev, b, x, cb, arg) \
771  event_assign((ev), (b), (x), EV_SIGNAL|EV_PERSIST, cb, (arg))
772 #define evsignal_new(b, x, cb, arg) \
773  event_new((b), (x), EV_SIGNAL|EV_PERSIST, (cb), (arg))
774 #define evsignal_del(ev) event_del(ev)
775 #define evsignal_pending(ev, tv) event_pending((ev), EV_SIGNAL, (tv))
776 #define evsignal_initialized(ev) event_initialized(ev)
777 
790 typedef void (*event_callback_fn)(evutil_socket_t, short, void *);
791 
840 struct event *event_new(struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
841 
842 
881 int event_assign(struct event *, struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
882 
889 void event_free(struct event *);
890 
913 int event_base_once(struct event_base *, evutil_socket_t, short, event_callback_fn, void *, const struct timeval *);
914 
937 int event_add(struct event *ev, const struct timeval *timeout);
938 
950 int event_del(struct event *);
951 
952 
967 void event_active(struct event *ev, int res, short ncalls);
968 
982 int event_pending(const struct event *ev, short events, struct timeval *tv);
983 
984 
1000 int event_initialized(const struct event *ev);
1001 
1005 #define event_get_signal(ev) ((int)event_get_fd(ev))
1006 
1011 evutil_socket_t event_get_fd(const struct event *ev);
1012 
1016 struct event_base *event_get_base(const struct event *ev);
1017 
1021 short event_get_events(const struct event *ev);
1022 
1026 event_callback_fn event_get_callback(const struct event *ev);
1027 
1031 void *event_get_callback_arg(const struct event *ev);
1032 
1040 void event_get_assignment(const struct event *event,
1041  struct event_base **base_out, evutil_socket_t *fd_out, short *events_out,
1042  event_callback_fn *callback_out, void **arg_out);
1043 
1057 size_t event_get_struct_event_size(void);
1058 
1068 const char *event_get_version(void);
1069 
1081 ev_uint32_t event_get_version_number(void);
1082 
1084 #define LIBEVENT_VERSION _EVENT_VERSION
1085 
1087 #define LIBEVENT_VERSION_NUMBER _EVENT_NUMERIC_VERSION
1088 
1090 #define EVENT_MAX_PRIORITIES 256
1091 
1118 int event_base_priority_init(struct event_base *, int);
1119 
1128 int event_priority_set(struct event *, int);
1129 
1149 const struct timeval *event_base_init_common_timeout(struct event_base *base,
1150  const struct timeval *duration);
1151 
1152 #if !defined(_EVENT_DISABLE_MM_REPLACEMENT) || defined(_EVENT_IN_DOXYGEN)
1153 
1176  void *(*malloc_fn)(size_t sz),
1177  void *(*realloc_fn)(void *ptr, size_t sz),
1178  void (*free_fn)(void *ptr));
1181 #define EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
1182 #endif
1183 
1184 void event_base_dump_events(struct event_base *, FILE *);
1185 
1198  struct timeval *tv);
1199 
1200 #ifdef __cplusplus
1201 }
1202 #endif
1203 
1204 #endif /* _EVENT2_EVENT_H_ */
int event_reinit(struct event_base *base)
Reinitialize the event base after a fork.
int event_base_loopbreak(struct event_base *)
Abort the active event_base_loop() immediately.
const struct timeval * event_base_init_common_timeout(struct event_base *base, const struct timeval *duration)
Prepare an event_base to use a large number of timeouts with the same duration.
short event_get_events(const struct event *ev)
Return the events (EV_READ, EV_WRITE, etc) assigned to an event.
Do not check the EVENT_* environment variables when configuring an event_base.
Definition: event.h:457
struct event * event_new(struct event_base *, evutil_socket_t, short, event_callback_fn, void *)
Allocate and asssign a new event structure, ready to be added.
Configuration for an event_base.
Definition: event.h:291
struct event_base * event_base_new(void)
Create and return a new event_base to use with the rest of Libevent.
int event_base_set(struct event_base *, struct event *)
Associate a different event base with an event.
int event_base_priority_init(struct event_base *, int)
Set the number of different event priorities.
int event_base_dispatch(struct event_base *)
Event dispatching loop.
ev_uint32_t event_get_version_number(void)
Return a numeric representation of Libevent&#39;s version.
void(* event_log_cb)(int severity, const char *msg)
A callback function used to intercept Libevent&#39;s log messages.
Definition: event.h:584
Require an event method where having one event triggered among many is [approximately] an O(1) operat...
Definition: event.h:437
int event_assign(struct event *, struct event_base *, evutil_socket_t, short, event_callback_fn, void *)
Prepare a new, already-allocated event structure to be added.
struct event_base * event_base_new_with_config(const struct event_config *)
Initialize the event API.
Structure to represent a single event.
Definition: event.h:272
const char * event_base_get_method(const struct event_base *)
Get the kernel event notification mechanism used by Libevent.
Windows only: enable the IOCP dispatcher at startup.
Definition: event.h:464
const char ** event_get_supported_methods(void)
Gets all event notification mechanisms supported by Libevent.
void(* event_fatal_cb)(int err)
A function to be called if Libevent encounters a fatal internal error.
Definition: event.h:602
int event_config_set_num_cpus_hint(struct event_config *cfg, int cpus)
Records a hint for the number of CPUs in the system.
event_base_config_flag
A flag passed to event_config_set_flag().
Definition: event.h:451
int event_add(struct event *ev, const struct timeval *timeout)
Add an event to the set of pending events.
void event_debug_unassign(struct event *)
When debugging mode is enabled, informs Libevent that an event should no longer be considered as assi...
void event_free(struct event *)
Deallocate a struct event * returned by event_new().
#define evutil_socket_t
A type wide enough to hold the output of "socket()" or "accept()".
Definition: util.h:274
struct event_base * event_get_base(const struct event *ev)
Get the event_base associated with an event.
int event_base_loop(struct event_base *, int)
Wait for events to become active, and run their callbacks.
int event_config_require_features(struct event_config *cfg, int feature)
Enters a required event method feature that the application demands.
int event_base_gettimeofday_cached(struct event_base *base, struct timeval *tv)
Sets &#39;tv&#39; to the current time (as returned by gettimeofday()), looking at the cached value in &#39;base&#39; ...
event_method_feature
A flag used to describe which features an event_base (must) provide.
Definition: event.h:430
void(* event_callback_fn)(evutil_socket_t, short, void *)
A callback function for an event.
Definition: event.h:790
int event_del(struct event *)
Remove an event from the set of monitored events.
Common convenience functions for cross-platform portability and related socket manipulations.
void event_config_free(struct event_config *cfg)
Deallocates all memory associated with an event configuration object.
void event_set_fatal_callback(event_fatal_cb cb)
Override Libevent&#39;s behavior in the event of a fatal internal error.
struct event_config * event_config_new(void)
Allocates a new event configuration object.
void event_set_log_callback(event_log_cb cb)
Redirect Libevent&#39;s log messages.
Do not allocate a lock for the event base, even if we have locking set up.
Definition: event.h:454
int event_base_loopexit(struct event_base *, const struct timeval *)
Exit the event loop after the specified time.
void event_enable_debug_mode(void)
Enable some relatively expensive debugging checks in Libevent that would normally be turned off...
void event_get_assignment(const struct event *event, struct event_base **base_out, evutil_socket_t *fd_out, short *events_out, event_callback_fn *callback_out, void **arg_out)
Extract all of arguments given to construct a given event.
event_callback_fn event_get_callback(const struct event *ev)
Return the callback assigned to an event.
int event_config_avoid_method(struct event_config *cfg, const char *method)
Enters an event method that should be avoided into the configuration.
int event_initialized(const struct event *ev)
Test if an event structure might be initialized.
const char * event_get_version(void)
Get the Libevent version.
int event_config_set_flag(struct event_config *cfg, int flag)
Sets one or more flags to configure what parts of the eventual event_base will be initialized...
int event_base_got_break(struct event_base *)
Checks if the event loop was told to abort immediately by event_loopbreak().
void event_base_free(struct event_base *)
Deallocate all memory associated with an event_base, and free the base.
void event_active(struct event *ev, int res, short ncalls)
Make an event active.
int event_base_get_features(const struct event_base *base)
Return a bitmask of the features implemented by an event base.
Instead of checking the current time every time the event loop is ready to run timeout callbacks...
Definition: event.h:468
int event_base_once(struct event_base *, evutil_socket_t, short, event_callback_fn, void *, const struct timeval *)
Schedule a one-time event.
If we are using the epoll backend, this flag says that it is safe to use Libevent&#39;s internal change-l...
Definition: event.h:484
int event_base_got_exit(struct event_base *)
Checks if the event loop was told to exit by event_loopexit().
void event_set_mem_functions(void *(*malloc_fn)(size_t sz), void *(*realloc_fn)(void *ptr, size_t sz), void(*free_fn)(void *ptr))
Override the functions that Libevent uses for memory management.
Structure to hold information and state for a Libevent dispatch loop.
Definition: event.h:213
void * event_get_callback_arg(const struct event *ev)
Return the callback argument assigned to an event.
int event_pending(const struct event *ev, short events, struct timeval *tv)
Checks if a specific event is pending or scheduled.
size_t event_get_struct_event_size(void)
Return the size of struct event that the Libevent library was compiled with.
int event_priority_set(struct event *, int)
Assign a priority to an event.
Require an event method that allows edge-triggered events with EV_ET.
Definition: event.h:432
evutil_socket_t event_get_fd(const struct event *ev)
Get the socket or signal assigned to an event, or -1 if the event has no socket.
Require an event method that allows file descriptors as well as sockets.
Definition: event.h:440