#include <event2/visibility.h>
#include <event2/event-config.h>
#include <stdio.h>
#include <event2/util.h>
struct event
Structure to represent a single event. struct event_base
Structure to hold information and state for a Libevent dispatch loop.
struct event_config
Configuration for an event_base.
#define _EVENT_LOG_DEBUG EVENT_LOG_DEBUG
#define _EVENT_LOG_ERR EVENT_LOG_ERR
#define _EVENT_LOG_MSG EVENT_LOG_MSG
#define _EVENT_LOG_WARN EVENT_LOG_WARN
#define EVENT_DBG_ALL 0xffffffffu
#define EVENT_DBG_NONE 0
#define event_get_signal(ev) ((int)event_get_fd(ev))
Get the signal number assigned to a signal event. #define
EVENT_MAX_PRIORITIES 256
Largest number of priorities that Libevent can support. #define
EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
This definition is present if Libevent was built with support for
event_set_mem_functions() #define
LIBEVENT_VERSION EVENT__VERSION
As event_get_version, but gives the version of Libevent's headers.
#define LIBEVENT_VERSION_NUMBER EVENT__NUMERIC_VERSION
As event_get_version_number, but gives the version number of Libevent's
headers.
event type flag
Flags to pass to event_base_get_num_events() to specify the kinds of
events we want to aggregate counts for
#define EVENT_BASE_COUNT_ACTIVE 1U
count the number of active events, which have been triggered. #define
EVENT_BASE_COUNT_VIRTUAL 2U
count the number of virtual events, which is used to represent an internal
condition, other than a pending event, that keeps the loop from exiting.
#define EVENT_BASE_COUNT_ADDED 4U
count the number of events which have been added to event base, including
internal events.
Log severities
#define EVENT_LOG_DEBUG 0
#define EVENT_LOG_MSG 1
#define EVENT_LOG_WARN 2
#define EVENT_LOG_ERR 3
Loop flags
These flags control the behavior of event_base_loop().
#define EVLOOP_ONCE 0x01
Block until we have an active event, then exit once all active events have
had their callbacks run. #define EVLOOP_NONBLOCK 0x02
Do not block: see which events are ready now, run the callbacks of the
highest-priority ones, then exit. #define
EVLOOP_NO_EXIT_ON_EMPTY 0x04
Do not exit the loop because we have no pending events.
event flags
Flags to pass to event_new(), event_assign(),
event_pending(), and anything else with an argument of the form
'short events'
#define EV_TIMEOUT 0x01
Indicates that a timeout has occurred. #define EV_READ 0x02
Wait for a socket or FD to become readable. #define EV_WRITE
0x04
Wait for a socket or FD to become writeable. #define EV_SIGNAL
0x08
Wait for a POSIX signal to be raised. #define EV_PERSIST 0x10
Persistent event: won't get removed automatically when activated.
#define EV_ET 0x20
Select edge-triggered behavior, if supported by the backend. #define
EV_FINALIZE 0x40
If this option is provided, then event_del() will not block
in one thread while waiting for the event callback to complete in another
thread. #define EV_CLOSED 0x80
Detects connection close events.
evtimer_* macros
Aliases for working with one-shot timer events
#define evtimer_assign(ev, b, cb, arg) event_assign((ev), (b),
-1, 0, (cb), (arg))
#define evtimer_new(b, cb, arg) event_new((b), -1, 0, (cb),
(arg))
#define evtimer_add(ev, tv) event_add((ev), (tv))
#define evtimer_del(ev) event_del(ev)
#define evtimer_pending(ev, tv) event_pending((ev),
EV_TIMEOUT, (tv))
#define evtimer_initialized(ev) event_initialized(ev)
evsignal_* macros
Aliases for working with signal events
#define evsignal_add(ev, tv) event_add((ev), (tv))
#define evsignal_assign(ev, b, x, cb, arg) event_assign((ev),
(b), (x), EV_SIGNAL|EV_PERSIST, cb, (arg))
#define evsignal_new(b, x, cb, arg) event_new((b), (x),
EV_SIGNAL|EV_PERSIST, (cb), (arg))
#define evsignal_del(ev) event_del(ev)
#define evsignal_pending(ev, tv) event_pending((ev),
EV_SIGNAL, (tv))
#define evsignal_initialized(ev) event_initialized(ev)
typedef int(* event_base_foreach_event_cb) (const struct
event_base *, const struct event *, void *)
Callback for iterating events in an event base via event_base_foreach_event.
typedef void(* event_callback_fn) (evutil_socket_t, short,
void *)
A callback function for an event. typedef void(* event_fatal_cb)
(int err)
A function to be called if Libevent encounters a fatal internal error.
typedef void(* event_finalize_callback_fn) (struct event *, void
*)
Callback type for event_finalize and event_free_finalize(). typedef
void(* event_log_cb) (int severity, const char *msg)
A callback function used to intercept Libevent's log messages.
enum event_base_config_flag { EVENT_BASE_FLAG_NOLOCK = 0x01,
EVENT_BASE_FLAG_IGNORE_ENV = 0x02, EVENT_BASE_FLAG_STARTUP_IOCP
= 0x04, EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08,
EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10,
EVENT_BASE_FLAG_PRECISE_TIMER = 0x20 } A flag passed to
event_config_set_flag(). "
enum event_method_feature { EV_FEATURE_ET = 0x01,
EV_FEATURE_O1 = 0x02, EV_FEATURE_FDS = 0x04,
EV_FEATURE_EARLY_CLOSE = 0x08 } A flag used to describe which
features an event_base (must) provide. "
EVENT2_EXPORT_SYMBOL void event_active (struct event *ev, int res,
short ncalls)
Make an event active. EVENT2_EXPORT_SYMBOL int event_add (struct
event *ev, const struct timeval *timeout)
Add an event to the set of pending events. EVENT2_EXPORT_SYMBOL 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.
EVENT2_EXPORT_SYMBOL void event_base_active_by_fd (struct
event_base *base, evutil_socket_t fd, short events)
Activates all pending events for the given fd and event mask.
EVENT2_EXPORT_SYMBOL void event_base_active_by_signal (struct
event_base *base, int sig)
Activates all pending signals with a given signal number.
EVENT2_EXPORT_SYMBOL int event_base_dispatch (struct event_base
*)
Event dispatching loop. EVENT2_EXPORT_SYMBOL void
event_base_dump_events (struct event_base *, FILE *)
Writes a human-readable description of all inserted and/or active events to a
provided stdio stream. EVENT2_EXPORT_SYMBOL int
event_base_foreach_event (struct event_base *base,
event_base_foreach_event_cb fn, void *arg)
Iterate over all added or active events events in an event loop, and invoke a
given callback on each one. EVENT2_EXPORT_SYMBOL void
event_base_free (struct event_base *)
Deallocate all memory associated with an event_base, and free
the base. EVENT2_EXPORT_SYMBOL void
event_base_free_nofinalize (struct event_base *)
As event_free, but do not run finalizers. EVENT2_EXPORT_SYMBOL int
event_base_get_features (const struct event_base *base)
Return a bitmask of the features implemented by an event base.
EVENT2_EXPORT_SYMBOL int event_base_get_max_events (struct
event_base *, unsigned int, int)
Get the maximum number of events in a given event_base as
specified in the flags. EVENT2_EXPORT_SYMBOL const char *
event_base_get_method (const struct event_base *)
Get the kernel event notification mechanism used by Libevent.
EVENT2_EXPORT_SYMBOL int event_base_get_npriorities (struct
event_base *eb)
Get the number of different event priorities. EVENT2_EXPORT_SYMBOL int
event_base_get_num_events (struct event_base *, unsigned int)
Gets the number of events in event_base, as specified in the
flags. EVENT2_EXPORT_SYMBOL struct event *
event_base_get_running_event (struct event_base *base)
If called from within the callback for an event, returns that event.
EVENT2_EXPORT_SYMBOL int event_base_gettimeofday_cached (struct
event_base *base, struct timeval *tv)
Sets 'tv' to the current time (as returned by gettimeofday()), looking at the
cached value in 'base' if possible, and calling gettimeofday() or
clock_gettime() as appropriate if there is no cached time.
EVENT2_EXPORT_SYMBOL int event_base_got_break (struct event_base
*)
Checks if the event loop was told to abort immediately by
event_base_loopbreak(). EVENT2_EXPORT_SYMBOL int
event_base_got_exit (struct event_base *)
Checks if the event loop was told to exit by
event_base_loopexit(). EVENT2_EXPORT_SYMBOL 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. EVENT2_EXPORT_SYMBOL int event_base_loop
(struct event_base *, int)
Wait for events to become active, and run their callbacks.
EVENT2_EXPORT_SYMBOL int event_base_loopbreak (struct event_base
*)
Abort the active event_base_loop() immediately.
EVENT2_EXPORT_SYMBOL int event_base_loopcontinue (struct
event_base *)
Tell the active event_base_loop() to scan for new events
immediately. EVENT2_EXPORT_SYMBOL int event_base_loopexit
(struct event_base *, const struct timeval *)
Exit the event loop after the specified time. EVENT2_EXPORT_SYMBOL
struct event_base * event_base_new (void)
Create and return a new event_base to use with the rest of
Libevent. EVENT2_EXPORT_SYMBOL struct event_base *
event_base_new_with_config (const struct event_config *)
Initialize the event API. EVENT2_EXPORT_SYMBOL int
event_base_once (struct event_base *, evutil_socket_t,
short, event_callback_fn, void *, const struct timeval *)
Schedule a one-time event. EVENT2_EXPORT_SYMBOL int
event_base_priority_init (struct event_base *, int)
Set the number of different event priorities. EVENT2_EXPORT_SYMBOL int
event_base_set (struct event_base *, struct event *)
Associate a different event base with an event. EVENT2_EXPORT_SYMBOL int
event_base_update_cache_time (struct event_base *base)
Update cached_tv in the 'base' to the current time. EVENT2_EXPORT_SYMBOL
int event_config_avoid_method (struct event_config *cfg, const
char *method)
Enters an event method that should be avoided into the configuration.
EVENT2_EXPORT_SYMBOL void event_config_free (struct event_config
*cfg)
Deallocates all memory associated with an event configuration object.
EVENT2_EXPORT_SYMBOL struct event_config * event_config_new
(void)
Allocates a new event configuration object. EVENT2_EXPORT_SYMBOL int
event_config_require_features (struct event_config *cfg, int
feature)
Enters a required event method feature that the application demands.
EVENT2_EXPORT_SYMBOL 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, and how they'll work.
EVENT2_EXPORT_SYMBOL int
event_config_set_max_dispatch_interval (struct event_config
*cfg, const struct timeval *max_interval, int max_callbacks, int min_priority)
Record an interval and/or a number of callbacks after which the event base
should check for new events. EVENT2_EXPORT_SYMBOL int
event_config_set_num_cpus_hint (struct event_config *cfg, int
cpus)
Records a hint for the number of CPUs in the system.
EVENT2_EXPORT_SYMBOL void event_debug_unassign (struct event *)
When debugging mode is enabled, informs Libevent that an event should no
longer be considered as assigned. EVENT2_EXPORT_SYMBOL int
event_del (struct event *)
Remove an event from the set of monitored events. EVENT2_EXPORT_SYMBOL
int event_del_block (struct event *ev)
As event_del(), but always blocks while the event's callback is
running in another thread, even if the event was constructed with the
EV_FINALIZE flag. EVENT2_EXPORT_SYMBOL int
event_del_noblock (struct event *ev)
As event_del(), but never blocks while the event's callback is
running in another thread, even if the event was constructed without the
EV_FINALIZE flag. EVENT2_EXPORT_SYMBOL void
event_enable_debug_logging (ev_uint32_t which)
Turn on debugging logs and have them sent to the default log handler.
EVENT2_EXPORT_SYMBOL void event_enable_debug_mode (void)
Enable some relatively expensive debugging checks in Libevent that would
normally be turned off. EVENT2_EXPORT_SYMBOL void event_free
(struct event *)
Deallocate a struct event * returned by event_new().
EVENT2_EXPORT_SYMBOL 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.
EVENT2_EXPORT_SYMBOL struct event_base *
event_get_base (const struct event *ev)
Get the event_base associated with an event.
EVENT2_EXPORT_SYMBOL event_callback_fn event_get_callback (const
struct event *ev)
Return the callback assigned to an event. EVENT2_EXPORT_SYMBOL void *
event_get_callback_arg (const struct event *ev)
Return the callback argument assigned to an event. EVENT2_EXPORT_SYMBOL
short event_get_events (const struct event *ev)
Return the events (EV_READ, EV_WRITE, etc) assigned to an event.
EVENT2_EXPORT_SYMBOL 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. EVENT2_EXPORT_SYMBOL int event_get_priority (const struct
event *ev)
Return the priority of an event. EVENT2_EXPORT_SYMBOL size_t
event_get_struct_event_size (void)
Return the size of struct event that the Libevent library was compiled with.
EVENT2_EXPORT_SYMBOL const char ** event_get_supported_methods
(void)
Gets all event notification mechanisms supported by Libevent.
EVENT2_EXPORT_SYMBOL const char * event_get_version (void)
Get the Libevent version. EVENT2_EXPORT_SYMBOL ev_uint32_t
event_get_version_number (void)
Return a numeric representation of Libevent's version.
EVENT2_EXPORT_SYMBOL int event_gettime_monotonic (struct
event_base *base, struct timeval *tp)
Query the current monotonic time from a the timer for a struct
event_base. EVENT2_EXPORT_SYMBOL int
event_initialized (const struct event *ev)
Test if an event structure might be initialized. EVENT2_EXPORT_SYMBOL
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.
EVENT2_EXPORT_SYMBOL int event_pending (const struct event *ev,
short events, struct timeval *tv)
Checks if a specific event is pending or scheduled. EVENT2_EXPORT_SYMBOL
int event_priority_set (struct event *, int)
Assign a priority to an event. EVENT2_EXPORT_SYMBOL int
event_reinit (struct event_base *base)
Reinitialize the event base after a fork. EVENT2_EXPORT_SYMBOL int
event_remove_timer (struct event *ev)
Remove a timer from a pending event without removing the event itself.
EVENT2_EXPORT_SYMBOL void * event_self_cbarg (void)
Return a value used to specify that the event itself must be used as the
callback argument. EVENT2_EXPORT_SYMBOL void
event_set_fatal_callback (event_fatal_cb cb)
Override Libevent's behavior in the event of a fatal internal error.
EVENT2_EXPORT_SYMBOL void event_set_log_callback (event_log_cb
cb)
Redirect Libevent's log messages. EVENT2_EXPORT_SYMBOL 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.
EVENT2_EXPORT_SYMBOL void libevent_global_shutdown (void)
Release up all globally-allocated resources allocated by Libevent.
Finalization functions
These functions are used to safely tear down an event in a multithreaded
application.
If you construct your events with EV_FINALIZE to avoid deadlocks,
you will need a way to remove an event in the certainty that it will
definitely not be running its callback when you deallocate it and its
callback argument.
To do this, call one of event_finalize() or event_free_finalize
with 0 for its first argument, the event to tear down as its second
argument, and a callback function as its third argument. The callback will
be invoked as part of the event loop, with the event's priority.
After you call a finalizer function, event_add() and
event_active() will no longer work on the event, and
event_del() will produce a no-op. You must not try to change the
event's fields with event_assign() or event_set() while the
finalize callback is in progress. Once the callback has been invoked, you
should treat the event structure as containing uninitialized memory.
The event_free_finalize() function frees the event after it's
finalized; event_finalize() does not.
A finalizer callback must not make events pending or active. It
must not add events, activate events, or attempt to 'resucitate' the event
being finalized in any way.
THIS IS AN EXPERIMENTAL API. IT MIGHT CHANGE BEFORE THE LIBEVENT
2.1 SERIES BECOMES STABLE.
Returns:
0 on succes, -1 on failure.
EVENT2_EXPORT_SYMBOL int event_finalize (unsigned, struct event
*, event_finalize_callback_fn)
EVENT2_EXPORT_SYMBOL int event_free_finalize (unsigned, struct
event *, event_finalize_callback_fn)
Make an event active. You can use this function on a pending or a non-pending
event to make it active, so that its callback will be run by
event_base_dispatch() or event_base_loop().
One common use in multithreaded programs is to wake the thread
running event_base_loop() from another thread.
Parameters:
ev an event to make active.
res a set of flags to pass to the event's callback.
ncalls an obsolete argument: this is ignored.
Add an event to the set of pending events. The function event_add()
schedules the execution of the event 'ev' when the condition specified by
event_assign() or event_new() occurs, or when the time specified
in timeout has elapesed. If atimeout is NULL, no timeout occurs and the
function will only be called if a matching event occurs. The event in the ev
argument must be already initialized by event_assign() or
event_new() and may not be used in calls to event_assign() until
it is no longer pending.
If the event in the ev argument already has a scheduled timeout,
calling event_add() replaces the old timeout with the new one if tv
is non-NULL.
Parameters:
ev an event struct initialized via
event_assign() or event_new()
timeout the maximum amount of time to wait for the event, or NULL to wait
forever
Returns:
0 if successful, or -1 if an error occurred
See also:
event_del(), event_assign(),
event_new()
Prepare a new, already-allocated event structure to be added. The function
event_assign() prepares the event structure ev to be used in future
calls to event_add() and event_del(). Unlike event_new(),
it doesn't allocate memory itself: it requires that you have already allocated
a struct event, probably on the heap. Doing this will typically make your code
depend on the size of the event structure, and thereby create incompatibility
with future versions of Libevent.
The easiest way to avoid this problem is just to use
event_new() and event_free() instead.
A slightly harder way to future-proof your code is to use
event_get_struct_event_size() to determine the required size of an
event at runtime.
Note that it is NOT safe to call this function on an event that is
active or pending. Doing so WILL corrupt internal data structures in
Libevent, and lead to strange, hard-to-diagnose bugs. You can use
event_assign to change an existing event, but only if it is not active or
pending!
The arguments for this function, and the behavior of the events
that it makes, are as for event_new().
Parameters:
ev an event struct to be modified
base the event base to which ev should be attached.
fd the file descriptor to be monitored
events desired events to monitor; can be EV_READ and/or EV_WRITE
callback callback function to be invoked when the event occurs
callback_arg an argument to be passed to the callback function
Returns:
0 if success, or -1 on invalid arguments.
See also:
event_new(), event_add(),
event_del(), event_base_once(),
event_get_struct_event_size()
Activates all pending events for the given fd and event mask. This function
activates pending events only. Events which have not been added will not
become active.
Parameters:
base the event_base on which to activate
the events.
fd An fd to active events on.
events One or more of EV_{READ,WRITE}.
Activates all pending signals with a given signal number. This function
activates pending events only. Events which have not been added will not
become active.
Parameters:
base the event_base on which to activate
the events.
fd The signal to active events on.
Event dispatching loop. This loop will run the event base until either there are
no more pending or active, or until something calls
event_base_loopbreak() or event_base_loopexit().
Parameters:
base the event_base structure returned by
event_base_new() or event_base_new_with_config()
Returns:
0 if successful, -1 if an error occurred, or 1 if we
exited because no events were pending or active.
See also:
event_base_loop()
Writes a human-readable description of all inserted and/or active events to a
provided stdio stream. This is intended for debugging; its format is not
guaranteed to be the same between libevent versions.
Parameters:
base An event_base on which to scan the
events.
output A stdio file to write on.
Iterate over all added or active events events in an event loop, and invoke a
given callback on each one. The callback must not call any function that
modifies the event base, that modifies any event in the event base, or that
adds or removes any event to the event base. Doing so is unsupported and will
lead to undefined behavior -- likely, to crashes.
event_base_foreach_event() holds a lock on the
event_base() for the whole time it's running: slow callbacks are not
advisable.
Note that Libevent adds some events of its own to make pieces of
its functionality work. You must not assume that the only events you'll
encounter will be the ones you added yourself.
The callback function must return 0 to continue iteration, or some
other integer to stop iterating.
Parameters:
base An event_base on which to scan the
events.
fn A callback function to receive the events.
arg An argument passed to the callback function.
Returns:
0 if we iterated over every event, or the value returned
by the callback function if the loop exited early.
Deallocate all memory associated with an event_base, and free the base.
Note that this function will not close any fds or free any memory passed to
event_new as the argument to callback.
If there are any pending finalizer callbacks, this function will
invoke them.
Parameters:
eb an event_base to be freed
As event_free, but do not run finalizers. THIS IS AN EXPERIMENTAL API. IT MIGHT
CHANGE BEFORE THE LIBEVENT 2.1 SERIES BECOMES STABLE.
Return a bitmask of the features implemented by an event base. This will be a
bitwise OR of one or more of the values of event_method_feature
See also:
event_method_feature
Get the maximum number of events in a given event_base as specified in
the flags.
Parameters:
eb the event_base structure returned by
event_base_new()
flags a bitwise combination of the kinds of events to aggregate counts
for
clear option used to reset the maximum count.
Returns:
the number of events specified in the flags
Get the kernel event notification mechanism used by Libevent.
Parameters:
eb the event_base structure returned by
event_base_new()
Returns:
a string identifying the kernel event mechanism (kqueue,
epoll, etc.)
Get the number of different event priorities.
Parameters:
eb the event_base structure returned by
event_base_new()
Returns:
Number of different event priorities
See also:
event_base_priority_init()
Gets the number of events in event_base, as specified in the flags. Since
event base has some internal events added to make some of its functionalities
work, EVENT_BASE_COUNT_ADDED may return more than the number of events you
added using event_add().
If you pass EVENT_BASE_COUNT_ACTIVE and EVENT_BASE_COUNT_ADDED
together, an active event will be counted twice. However, this might not be
the case in future libevent versions. The return value is an indication of
the work load, but the user shouldn't rely on the exact value as this may
change in the future.
Parameters:
eb the event_base structure returned by
event_base_new()
flags a bitwise combination of the kinds of events to aggregate counts
for
Returns:
the number of events specified in the flags
If called from within the callback for an event, returns that event. The
behavior of this function is not defined when called from outside the callback
function for an event.
Sets 'tv' to the current time (as returned by gettimeofday()), looking at the
cached value in 'base' if possible, and calling gettimeofday() or
clock_gettime() as appropriate if there is no cached time. Generally, this
value will only be cached while actually processing event callbacks, and may
be very inaccuate if your callbacks take a long time to execute.
Returns 0 on success, negative on failure.
Checks if the event loop was told to abort immediately by
event_base_loopbreak(). This function will return true for an
event_base at every point after event_base_loopbreak() is
called, until the event loop is next entered.
Parameters:
eb the event_base structure returned by
event_init()
Returns:
true if event_base_loopbreak() was called on this
event base, or 0 otherwise
See also:
event_base_loopbreak()
event_base_got_exit()
Checks if the event loop was told to exit by event_base_loopexit(). This
function will return true for an event_base at every point after
event_loopexit() is called, until the event loop is next entered.
Parameters:
eb the event_base structure returned by
event_init()
Returns:
true if event_base_loopexit() was called on this
event base, or 0 otherwise
See also:
event_base_loopexit()
event_base_got_break()
Prepare an event_base to use a large number of timeouts with the same
duration. Libevent's default scheduling algorithm is optimized for having a
large number of timeouts with their durations more or less randomly
distributed. But if you have a large number of timeouts that all have the same
duration (for example, if you have a large number of connections that all have
a 10-second timeout), then you can improve Libevent's performance by telling
Libevent about it.
To do this, call this function with the common duration. It will
return a pointer to a different, opaque timeout value. (Don't depend on its
actual contents!) When you use this timeout value in event_add(),
Libevent will schedule the event more efficiently.
(This optimization probably will not be worthwhile until you have
thousands or tens of thousands of events with the same timeout.)
Wait for events to become active, and run their callbacks. This is a more
flexible version of event_base_dispatch().
By default, this loop will run the event base until either there
are no more pending or active events, or until something calls
event_base_loopbreak() or event_base_loopexit(). You can
override this behavior with the 'flags' argument.
Parameters:
eb the event_base structure returned by
event_base_new() or event_base_new_with_config()
flags any combination of EVLOOP_ONCE | EVLOOP_NONBLOCK
Returns:
0 if successful, -1 if an error occurred, or 1 if we
exited because no events were pending or active.
See also:
event_base_loopexit(),
event_base_dispatch(), EVLOOP_ONCE, EVLOOP_NONBLOCK
Abort the active event_base_loop() immediately. event_base_loop()
will abort the loop after the next event is completed;
event_base_loopbreak() is typically invoked from this event's callback.
This behavior is analogous to the 'break;' statement.
Subsequent invocations of event_base_loop() will proceed
normally.
Parameters:
eb the event_base structure returned by
event_init()
Returns:
0 if successful, or -1 if an error occurred
See also:
event_base_loopexit()
Tell the active event_base_loop() to scan for new events immediately.
Calling this function makes the currently active event_base_loop()
start the loop over again (scanning for new events) after the current event
callback finishes. If the event loop is not running, this function has no
effect.
event_base_loopbreak() is typically invoked from this
event's callback. This behavior is analogous to the 'continue;'
statement.
Subsequent invocations of event loop will proceed normally.
Parameters:
eb the event_base structure returned by
event_init()
Returns:
0 if successful, or -1 if an error occurred
See also:
event_base_loopbreak()
Exit the event loop after the specified time. The next event_base_loop()
iteration after the given timer expires will complete normally (handling all
queued events) then exit without blocking for events again.
Subsequent invocations of event_base_loop() will proceed
normally.
Parameters:
eb the event_base structure returned by
event_init()
tv the amount of time after which the loop should terminate, or NULL to
exit after running all currently active events.
Returns:
0 if successful, or -1 if an error occurred
See also:
event_base_loopbreak()
Create and return a new event_base to use with the rest of Libevent.
Returns:
a new event_base on success, or NULL on
failure.
See also:
event_base_free(),
event_base_new_with_config()
Initialize the event API. Use event_base_new_with_config() to initialize
a new event base, taking the specified configuration under consideration. The
configuration object can currently be used to avoid certain event notification
mechanisms.
Parameters:
cfg the event configuration object
Returns:
an initialized event_base that can be used to
registering events, or NULL if no event base can be created with the requested
event_config.
See also:
event_base_new(), event_base_free(),
event_init(), event_assign()
Schedule a one-time event. The function event_base_once() is similar to
event_new(). However, it schedules a callback to be called exactly
once, and does not require the caller to prepare an event structure.
Note that in Libevent 2.0 and earlier, if the event is never
triggered, the internal memory used to hold it will never be freed. In
Libevent 2.1, the internal memory will get freed by event_base_free()
if the event is never triggered. The 'arg' value, however, will not get
freed in either case--you'll need to free that on your own if you want it to
go away.
Parameters:
base an event_base
fd a file descriptor to monitor, or -1 for no fd.
events event(s) to monitor; can be any of EV_READ | EV_WRITE, or
EV_TIMEOUT
callback callback function to be invoked when the event occurs
arg an argument to be passed to the callback function
timeout the maximum amount of time to wait for the event. NULL makes an
EV_READ/EV_WRITE event make forever; NULL makes an EV_TIMEOUT event succees
immediately.
Returns:
0 if successful, or -1 if an error occurred
Set the number of different event priorities. By default Libevent schedules all
active events with the same priority. However, some time it is desirable to
process some events with a higher priority than others. For that reason,
Libevent supports strict priority queues. Active events with a lower priority
are always processed before events with a higher priority.
The number of different priorities can be set initially with the
event_base_priority_init() function. This function should be called
before the first call to event_base_dispatch(). The
event_priority_set() function can be used to assign a priority to an
event. By default, Libevent assigns the middle priority to all events unless
their priority is explicitly set.
Note that urgent-priority events can starve less-urgent events:
after running all urgent-priority callbacks, Libevent checks for more urgent
events again, before running less-urgent events. Less-urgent events will not
have their callbacks run until there are no events more urgent than them
that want to be active.
Parameters:
eb the event_base structure returned by
event_base_new()
npriorities the maximum number of priorities
Returns:
0 if successful, or -1 if an error occurred
See also:
event_priority_set()
Associate a different event base with an event. The event to be associated must
not be currently active or pending.
Parameters:
eb the event base
ev the event
Returns:
0 on success, -1 on failure.
Update cached_tv in the 'base' to the current time. You can use this function is
useful for selectively increasing the accuracy of the cached time value in
'base' during callbacks that take a long time to execute.
This function has no effect if the base is currently not in its
event loop, or if timeval caching is disabled via
EVENT_BASE_FLAG_NO_CACHE_TIME.
Returns:
0 on success, -1 on failure
Enters an event method that should be avoided into the configuration. This can
be used to avoid event mechanisms that do not support certain file descriptor
types, or for debugging to avoid certain event mechanisms. An application can
make use of multiple event bases to accommodate incompatible file descriptor
types.
Parameters:
cfg the event configuration object
method the name of the event method to avoid
Returns:
0 on success, -1 on failure.
Deallocates all memory associated with an event configuration object.
Parameters:
cfg the event configuration object to be
freed.
Allocates a new event configuration object. The event configuration object can
be used to change the behavior of an event base.
Returns:
an event_config object that can be used to store
configuration, or NULL if an error is encountered.
See also:
event_base_new_with_config(),
event_config_free(), event_config
Enters a required event method feature that the application demands. Note that
not every feature or combination of features is supported on every platform.
Code that requests features should be prepared to handle the case where
event_base_new_with_config() returns NULL, as in:
event_config_require_features(cfg, EV_FEATURE_ET);
base = event_base_new_with_config(cfg);
if (base == NULL) {
// We can't get edge-triggered behavior here.
event_config_require_features(cfg, 0);
base = event_base_new_with_config(cfg);
}
Parameters:
cfg the event configuration object
feature a bitfield of one or more event_method_feature values. Replaces
values from previous calls to this function.
Returns:
0 on success, -1 on failure.
See also:
event_method_feature,
event_base_new_with_config()
Sets one or more flags to configure what parts of the eventual event_base
will be initialized, and how they'll work.
See also:
event_base_config_flags,
event_base_new_with_config()
Record an interval and/or a number of callbacks after which the event base
should check for new events. By default, the event base will run as many
events are as activated at the higest activated priority before checking for
new events. If you configure it by setting max_interval, it will check the
time after each callback, and not allow more than max_interval to elapse
before checking for new events. If you configure it by setting max_callbacks
to a value >= 0, it will run no more than max_callbacks callbacks before
checking for new events.
This option can decrease the latency of high-priority events, and
avoid priority inversions where multiple low-priority events keep us from
polling for high-priority events, but at the expense of slightly decreasing
the throughput. Use it with caution!
Parameters:
cfg The event_base configuration object.
max_interval An interval after which Libevent should stop running
callbacks and check for more events, or NULL if there should be no such
interval.
max_callbacks A number of callbacks after which Libevent should stop
running callbacks and check for more events, or -1 if there should be no such
limit.
min_priority A priority below which max_interval and max_callbacks should
not be enforced. If this is set to 0, they are enforced for events of every
priority; if it's set to 1, they're enforced for events of priority 1 and
above, and so on.
Returns:
0 on success, -1 on failure.
Records a hint for the number of CPUs in the system. This is used for tuning
thread pools, etc, for optimal performance. In Libevent 2.0, it is only on
Windows, and only when IOCP is in use.
Parameters:
cfg the event configuration object
cpus the number of cpus
Returns:
0 on success, -1 on failure.
When debugging mode is enabled, informs Libevent that an event should no longer
be considered as assigned. When debugging mode is not enabled, does nothing.
This function must only be called on a non-added event.
See also:
event_enable_debug_mode()
Remove an event from the set of monitored events. The function
event_del() will cancel the event in the argument ev. If the event has
already executed or has never been added the call will have no effect.
Parameters:
ev an event struct to be removed from the working
set
Returns:
0 if successful, or -1 if an error occurred
See also:
event_add()
As event_del(), but always blocks while the event's callback is running
in another thread, even if the event was constructed with the EV_FINALIZE
flag. THIS IS AN EXPERIMENTAL API. IT MIGHT CHANGE BEFORE THE LIBEVENT 2.1
SERIES BECOMES STABLE.
As event_del(), but never blocks while the event's callback is running in
another thread, even if the event was constructed without the EV_FINALIZE
flag. THIS IS AN EXPERIMENTAL API. IT MIGHT CHANGE BEFORE THE LIBEVENT 2.1
SERIES BECOMES STABLE.
Turn on debugging logs and have them sent to the default log handler. This is a
global setting; if you are going to call it, you must call this before any
calls that create an event-base. You must call it before any multithreaded use
of Libevent.
Debug logs are verbose.
Parameters:
which Controls which debug messages are turned on.
This option is unused for now; for forward compatibility, you must pass in the
constant 'EVENT_DBG_ALL' to turn debugging logs on, or 'EVENT_DBG_NONE' to
turn debugging logs off.
Enable some relatively expensive debugging checks in Libevent that would
normally be turned off. Generally, these checks cause code that would
otherwise crash mysteriously to fail earlier with an assertion failure. Note
that this method MUST be called before any events or event_bases have been
created.
Debug mode can currently catch the following errors: An event is
re-assigned while it is added Any function is called on a non-assigned
event
Note that debugging mode uses memory to track every event that has
been initialized (via event_assign, event_set, or event_new) but not yet
released (via event_free or event_debug_unassign). If you want to use debug
mode, and you find yourself running out of memory, you will need to use
event_debug_unassign to explicitly stop tracking events that are no longer
considered set-up.
See also:
event_debug_unassign()
Deallocate a struct event * returned by event_new(). If the event is
pending or active, first make it non-pending and non-active.
Extract all of arguments given to construct a given event. The
event_base is copied into *base_out, the fd is copied into *fd_out, and
so on.
If any of the '_out' arguments is NULL, it will be ignored.
Return the priority of an event.
See also:
event_priority_init(),
event_get_priority()
Return the size of struct event that the Libevent library was compiled with.
This will be NO GREATER than sizeof(struct event) if you're running with the
same version of Libevent that your application was built with, but otherwise
might not.
Note that it might be SMALLER than sizeof(struct event) if some
future version of Libevent adds extra padding to the end of struct event. We
might do this to help ensure ABI-compatibility between different versions of
Libevent.
Gets all event notification mechanisms supported by Libevent. This functions
returns the event mechanism in order preferred by Libevent. Note that this
list will include all backends that Libevent has compiled-in support for, and
will not necessarily check your OS to see whether it has the required
resources.
Returns:
an array with pointers to the names of support methods.
The end of the array is indicated by a NULL pointer. If an error is
encountered NULL is returned.
Get the Libevent version. Note that this will give you the version of the
library that you're currently linked against, not the version of the headers
that you've compiled against.
Returns:
a string containing the version number of Libevent
Return a numeric representation of Libevent's version. Note that this will give
you the version of the library that you're currently linked against, not the
version of the headers you've used to compile.
The format uses one byte each for the major, minor, and patchlevel
parts of the version number. The low-order byte is unused. For example,
version 2.0.1-alpha has a numeric representation of 0x02000100
Test if an event structure might be initialized. The event_initialized()
function can be used to check if an event has been initialized.
Warning: This function is only useful for distinguishing a a
zeroed-out piece of memory from an initialized event, it can easily be
confused by uninitialized memory. Thus, it should ONLY be used to
distinguish an initialized event from zero.
Parameters:
ev an event structure to be tested
Returns:
1 if the structure might be initialized, or 0 if it has
not been initialized
Allocate and asssign a new event structure, ready to be added. The function
event_new() returns a new event that can be used in future calls to
event_add() and event_del(). The fd and events arguments
determine which conditions will trigger the event; the callback and
callback_arg arguments tell Libevent what to do when the event becomes active.
If events contains one of EV_READ, EV_WRITE, or EV_READ|EV_WRITE,
then fd is a file descriptor or socket that should get monitored for
readiness to read, readiness to write, or readiness for either operation
(respectively). If events contains EV_SIGNAL, then fd is a signal number to
wait for. If events contains none of those flags, then the event can be
triggered only by a timeout or by manual activation with
event_active(): In this case, fd must be -1.
The EV_PERSIST flag can also be passed in the events argument: it
makes event_add() persistent until event_del() is called.
The EV_ET flag is compatible with EV_READ and EV_WRITE, and
supported only by certain backends. It tells Libevent to use edge-triggered
events.
The EV_TIMEOUT flag has no effect here.
It is okay to have multiple events all listening on the same fds;
but they must either all be edge-triggered, or all not be edge triggerd.
When the event becomes active, the event loop will run the
provided callbuck function, with three arguments. The first will be the
provided fd value. The second will be a bitfield of the events that
triggered: EV_READ, EV_WRITE, or EV_SIGNAL. Here the EV_TIMEOUT flag
indicates that a timeout occurred, and EV_ET indicates that an
edge-triggered event occurred. The third event will be the callback_arg
pointer that you provide.
Parameters:
base the event base to which the event should be
attached.
fd the file descriptor or signal to be monitored, or -1.
events desired events to monitor: bitfield of EV_READ, EV_WRITE,
EV_SIGNAL, EV_PERSIST, EV_ET.
callback callback function to be invoked when the event occurs
callback_arg an argument to be passed to the callback function
Returns:
a newly allocated struct event that must later be freed
with event_free().
See also:
event_free(), event_add(),
event_del(), event_assign()
Checks if a specific event is pending or scheduled.
Parameters:
ev an event struct previously passed to
event_add()
events the requested event type; any of EV_TIMEOUT|EV_READ|
EV_WRITE|EV_SIGNAL
tv if this field is not NULL, and the event has a timeout, this field is
set to hold the time at which the timeout will expire.
Returns:
true if the event is pending on any of the events in
'what', (that is to say, it has been added), or 0 if the event is not
added.
Assign a priority to an event.
Parameters:
ev an event struct
priority the new priority to be assigned
Returns:
0 if successful, or -1 if an error occurred
See also:
event_priority_init(),
event_get_priority()
Reinitialize the event base after a fork. Some event mechanisms do not survive
across fork. The event base needs to be reinitialized with the
event_reinit() function.
Parameters:
base the event base that needs to be
re-initialized
Returns:
0 if successful, or -1 if some events could not be
re-added.
See also:
event_base_new()
Remove a timer from a pending event without removing the event itself. If the
event has a scheduled timeout, this function unschedules it but leaves the
event otherwise pending.
Parameters:
ev an event struct initialized via
event_assign() or event_new()
Returns:
0 on success, or -1 if an error occurrect.
Return a value used to specify that the event itself must be used as the
callback argument. The function event_new() takes a callback argument
which is passed to the event's callback function. To specify that the argument
to be passed to the callback function is the event that event_new()
returns, pass in the return value of event_self_cbarg() as the callback
argument for event_new().
For example:
struct event *ev = event_new(base, sock, events, callback, event_self_cbarg());
For consistency with event_new(), it is possible to pass
the return value of this function as the callback argument for
event_assign() -- this achieves the same result as passing the event
in directly.
Returns:
a value to be passed as the callback argument to
event_new() or event_assign().
See also:
event_new(), event_assign()
Override Libevent's behavior in the event of a fatal internal error. By default,
Libevent will call exit(1) if a programming error makes it impossible to
continue correct operation. This function allows you to supply another
callback instead. Note that if the function is ever invoked, something is
wrong with your program, or with Libevent: any subsequent calls to Libevent
may result in undefined behavior.
Libevent will (almost) always log an EVENT_LOG_ERR message before
calling this function; look at the last log message to see why Libevent has
died.
Redirect Libevent's log messages.
Parameters:
cb a function taking two arguments: an integer
severity between EVENT_LOG_DEBUG and EVENT_LOG_ERR, and a string. If cb is
NULL, then the default log is used.
NOTE: The function you provide must not call any other
libevent functionality. Doing so can produce undefined behavior.
Override the functions that Libevent uses for memory management. Usually,
Libevent uses the standard libc functions malloc, realloc, and free to
allocate memory. Passing replacements for those functions to
event_set_mem_functions() overrides this behavior.
Note that all memory returned from Libevent will be allocated by
the replacement functions rather than by malloc() and realloc(). Thus, if
you have replaced those functions, it will not be appropriate to free()
memory that you get from Libevent. Instead, you must use the free_fn
replacement that you provided.
Note also that if you are going to call this function, you should
do so before any call to any Libevent function that does allocation.
Otherwise, those funtions will allocate their memory using malloc(), but
then later free it using your provided free_fn.
Parameters:
malloc_fn A replacement for malloc.
realloc_fn A replacement for realloc
free_fn A replacement for free.
Release up all globally-allocated resources allocated by Libevent. This function
does not free developer-controlled resources like event_bases, events,
bufferevents, listeners, and so on. It only releases resources like global
locks that there is no other way to free.
It is not actually necessary to call this function before exit:
every resource that it frees would be released anyway on exit. It mainly
exists so that resource-leak debugging tools don't see Libevent as holding
resources at exit.
You should only call this function when no other Libevent
functions will be invoked -- e.g., when cleanly exiting a program.