include/event2/event.h File Reference

Core functions for waiting for and receiving events, and using event bases. More...

#include <event-config.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <event2/util.h>

Go to the source code of this file.

Defines

#define _EVENT_LOG_DEBUG   0
#define _EVENT_LOG_ERR   3
#define _EVENT_LOG_MSG   1
#define _EVENT_LOG_WARN   2
#define EV_ET   0x20
 Select edge-triggered behavior, if supported by the backend.
#define EV_PERSIST   0x10
 Persistent event: won't get removed automatically when activated.
#define EV_READ   0x02
#define EV_SIGNAL   0x08
#define EV_TIMEOUT   0x01
#define EV_WRITE   0x04
#define EVENT_FD(ev)   ((int)event_get_fd(ev))
#define event_get_signal(ev)   ((int)event_get_fd(ev))
 Get the signal number assigned to an event.
#define event_initialized(ev)   _event_initialized((ev), 1)
 Test if an event structure has been initialized.
#define EVENT_SIGNAL(ev)   ((int)event_get_fd(ev))
#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_del(ev)   event_del(ev)
#define evsignal_initialized(ev)   _event_initialized((ev), 0)
#define evsignal_new(b, x, cb, arg)   event_new(b, x, EV_SIGNAL|EV_PERSIST, cb, arg)
#define evsignal_pending(ev, tv)   event_pending(ev, EV_SIGNAL, tv)
#define evsignal_set(ev, x, cb, arg)   event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
#define evtimer_add(ev, tv)   event_add(ev, tv)
 Add a timer event.
#define evtimer_assign(ev, b, cb, arg)   event_assign(ev, b, -1, 0, cb, arg)
#define evtimer_del(ev)   event_del(ev)
 Delete a timer event.
#define evtimer_initialized(ev)   _event_initialized((ev), 0)
#define evtimer_new(b, cb, arg)   event_new(b, -1, 0, cb, arg)
#define evtimer_pending(ev, tv)   event_pending(ev, EV_TIMEOUT, tv)
#define evtimer_set(ev, cb, arg)   event_set(ev, -1, 0, cb, arg)
 Define a timer event.
#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.
#define EVLOOP_NONBLOCK   0x02
 Do not block.
#define EVLOOP_ONCE   0x01
 event_loop() flags

Typedefs

typedef void(* event_log_cb )(int severity, const char *msg)

Enumerations

enum  event_base_config_flag { EVENT_BASE_FLAG_NOLOCK = 0x01 }
enum  event_method_feature { EV_FEATURE_ET = 0x01, EV_FEATURE_O1 = 0x02, EV_FEATURE_FDS = 0x04 }

Functions

int _event_initialized (struct event *, int check_fd)
void event_active (struct event *, int, short)
 Make an event active.
int event_add (struct event *, const struct timeval *)
 Add an event to the set of monitored events.
void event_assign (struct event *, struct event_base *, evutil_socket_t, short, void(*)(evutil_socket_t, short, void *), void *)
 Prepare an event structure to be added.
int event_base_dispatch (struct event_base *)
 Threadsafe event dispatching loop.
void event_base_dump_events (struct event_base *, FILE *)
void event_base_free (struct event_base *)
 Deallocate all memory associated with an event_base, and free the base.
enum event_method_feature event_base_get_features (struct event_base *base)
 Return a bitmask of the features implemented by an event base.
const char * event_base_get_method (struct event_base *)
 Get the kernel event notification mechanism used by libevent.
int event_base_loop (struct event_base *, int)
 Handle events (threadsafe version).
int event_base_loopbreak (struct event_base *)
 Abort the active event_base_loop() immediately.
int event_base_loopexit (struct event_base *, const struct timeval *)
 Exit the event loop after the specified time (threadsafe variant).
struct event_base * event_base_new (void)
 Initialize the event API.
struct event_base * event_base_new_with_config (struct event_config *cfg)
 Initialize the event API.
int event_base_once (struct event_base *, evutil_socket_t, short, void(*)(evutil_socket_t, short, void *), void *, const struct timeval *)
 Schedule a one-time event.
int event_base_priority_init (struct event_base *, int)
 Set the number of different event priorities (threadsafe variant).
int event_base_set (struct event_base *, struct event *)
 Associate a different event base with 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.
void event_config_free (struct event_config *cfg)
 Deallocates all memory associated with an event configuration object.
struct event_config * event_config_new (void)
 Allocates a new event configuration object.
int event_config_require_features (struct event_config *cfg, enum event_method_feature feature)
 Enters a required event method feature that the application demands.
int event_config_set_flag (struct event_config *cfg, enum event_base_config_flag flag)
 Sets a flag to configure what parts of the eventual event_base will be initialized, and how they'll work.
int event_del (struct event *)
 Remove an event from the set of monitored events.
void event_free (struct event *)
 Deallocate a struct event * returned by event_new().
evutil_socket_t event_get_fd (struct event *ev)
 Get the socket assigned to an event.
const char ** event_get_supported_methods (void)
 Gets all event notification mechanisms supported by libevent.
const char * event_get_version (void)
 Get the libevent version.
ev_uint32_t event_get_version_number (void)
 Return a numeric representation of Libevent's version.
struct event * event_new (struct event_base *, evutil_socket_t, short, void(*)(evutil_socket_t, short, void *), void *)
 Create and allocate a new event structure, ready to be added.
int event_pending (struct event *, short, struct timeval *)
 Checks if a specific event is pending or scheduled.
int event_priority_set (struct event *, int)
 Assign a priority to an event.
int event_reinit (struct event_base *base)
 Reinitialized the event base after a fork.
void event_set (struct event *, evutil_socket_t, short, void(*)(evutil_socket_t, short, void *), void *)
 Prepare an event structure to be added.
void event_set_log_callback (event_log_cb cb)
 Redirect libevent's log messages.
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.


Detailed Description

Core functions for waiting for and receiving events, and using event bases.


Define Documentation

#define EV_ET   0x20

Select edge-triggered behavior, if supported by the backend.

#define EV_PERSIST   0x10

Persistent event: won't get removed automatically when activated.

#define event_initialized ( ev   )     _event_initialized((ev), 1)

Test if an event structure has been initialized.

The event_initialized() macro can be used to check if an event has been initialized.

Parameters:
ev an event structure to be tested
Returns:
1 if the structure has been initialized, or 0 if it has not been initialized

#define EVLOOP_NONBLOCK   0x02

Do not block.

#define EVLOOP_ONCE   0x01

event_loop() flags

Block at most once.

#define evtimer_add ( ev,
tv   )     event_add(ev, tv)

Add a timer event.

Parameters:
ev the event struct
tv timeval struct

#define evtimer_del ( ev   )     event_del(ev)

Delete a timer event.

Parameters:
ev the event struct to be disabled

#define evtimer_set ( ev,
cb,
arg   )     event_set(ev, -1, 0, cb, arg)

Define a timer event.

Parameters:
ev event struct to be modified
cb callback function
arg argument that will be passed to the callback function

#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.


Function Documentation

void event_active ( struct event *  ,
int  ,
short   
)

Make an event active.

Parameters:
ev an event to make active.
res a set of flags to pass to the event's callback.
ncalls 

int event_add ( struct event *  ,
const struct timeval *   
)

Add an event to the set of monitored events.

The function event_add() schedules the execution of the ev event when the event specified in event_set() occurs or in at least the time specified in the tv. If tv is NULL, no timeout occurs and the function will only be called if a matching event occurs on the file descriptor. The event in the ev argument must be already initialized by event_set() and may not be used in calls to event_set() until it has timed out or been removed with event_del(). If the event in the ev argument already has a scheduled timeout, the old timeout will be replaced by the new one.

Parameters:
ev an event struct initialized via event_set()
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_set()

void event_assign ( struct event *  ,
struct event_base *  ,
evutil_socket_t  ,
short  ,
void(*)(evutil_socket_t, short, void *)  ,
void *   
)

Prepare an 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(). The event will be prepared to call the function specified by the fn argument with an int argument indicating the file descriptor, a short argument indicating the type of event, and a void * argument given in the arg argument. The fd indicates the file descriptor that should be monitored for events. The events can be either EV_READ, EV_WRITE, or both. Indicating that an application can read or write from the file descriptor respectively without blocking.

The function fn will be called with the file descriptor that triggered the event and the type of event which will be either EV_TIMEOUT, EV_SIGNAL, EV_READ, or EV_WRITE. The additional flag EV_PERSIST makes an event_add() persistent until event_del() has been called.

Note that using event_assign() request that you have already allocated the event struct. Doing so will often require your code to depend on the size of the structure, and will create possible incompatibility with future versions of libevent. If this seems like a bad idea to you, use event_new() and event_free() instead.

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
event desired events to monitor; can be EV_READ and/or EV_WRITE
fn callback function to be invoked when the event occurs
arg an argument to be passed to the callback function
See also:
event_add(), event_del(), event_once()

int event_base_dispatch ( struct event_base *   ) 

Threadsafe event dispatching loop.

Parameters:
eb the event_base structure returned by event_init()
See also:
event_init(), event_dispatch()

void event_base_free ( struct event_base *   ) 

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_set as the argument to callback.

Parameters:
eb an event_base to be freed

const char* event_base_get_method ( struct event_base *   ) 

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.)

int event_base_loop ( struct event_base *  ,
int   
)

Handle events (threadsafe version).

This is a more flexible version of event_base_dispatch().

Parameters:
eb the event_base structure returned by event_init()
flags any combination of EVLOOP_ONCE | EVLOOP_NONBLOCK
Returns:
0 if successful, -1 if an error occurred, or 1 if no events were registered.
See also:
event_loopexit(), event_base_loop()

int event_base_loopbreak ( struct event_base *   ) 

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_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

int event_base_loopexit ( struct event_base *  ,
const struct timeval *   
)

Exit the event loop after the specified time (threadsafe variant).

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.
Returns:
0 if successful, or -1 if an error occurred
See also:
event_loopexit()

struct event_base* event_base_new ( void   )  [read]

Initialize the event API.

Use event_base_new() to initialize a new event base, but does not set the current_base global. If using only event_base_new(), each event added must have an event base set with event_base_set()

See also:
event_base_set(), event_base_free(), event_init(), event_base_new_with_config()

struct event_base* event_base_new_with_config ( struct event_config *  cfg  )  [read]

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()

int event_base_once ( struct event_base *  ,
evutil_socket_t  ,
short  ,
void(*)(evutil_socket_t, short, void *)  ,
void *  ,
const struct timeval *   
)

Schedule a one-time event.

The function event_base_once() is similar to event_set(). However, it schedules a callback to be called exactly once and does not require the caller to prepare an event structure.

Parameters:
base an event_base returned by event_init()
fd a file descriptor to monitor
events event(s) to monitor; can be any of EV_TIMEOUT | EV_READ | EV_WRITE
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, or NULL to wait forever
Returns:
0 if successful, or -1 if an error occurred
See also:
event_once()

int event_base_priority_init ( struct event_base *  ,
int   
)

Set the number of different event priorities (threadsafe variant).

See the description of event_priority_init() for more information.

Parameters:
eb the event_base structure returned by event_init()
npriorities the maximum number of priorities
Returns:
0 if successful, or -1 if an error occurred
See also:
event_priority_init(), event_priority_set()

int event_base_set ( struct event_base *  ,
struct event *   
)

Associate a different event base with an event.

Parameters:
eb the event base
ev the event

int event_config_avoid_method ( struct event_config *  cfg,
const char *  method 
)

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. An application can make use of multiple event bases to accomodate incompatible file descriptor types.

Parameters:
cfg the event configuration object
method the event method to avoid
Returns:
0 on success, -1 on failure.

void event_config_free ( struct event_config *  cfg  ) 

Deallocates all memory associated with an event configuration object.

Parameters:
cfg the event configuration object to be freed.

struct event_config* event_config_new ( void   )  [read]

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 when an error is encountered.

int event_config_require_features ( struct event_config *  cfg,
enum event_method_feature  feature 
)

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.

int event_config_set_flag ( struct event_config *  cfg,
enum event_base_config_flag  flag 
)

Sets a flag to configure what parts of the eventual event_base will be initialized, and how they'll work.

int event_del ( struct event *   ) 

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()

const char** event_get_supported_methods ( void   ) 

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.

const char* event_get_version ( void   ) 

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

ev_uint32_t event_get_version_number ( void   ) 

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

struct event* event_new ( struct event_base *  ,
evutil_socket_t  ,
short  ,
void(*)(evutil_socket_t, short, void *)  ,
void *   
) [read]

Create and allocate a new event structure, ready to be added.

Arguments are as for event_assign; returns a newly allocated struct event * that must later be deallocated with event_free().

int event_pending ( struct event *  ,
short  ,
struct timeval *   
)

Checks if a specific event is pending or scheduled.

Parameters:
ev an event struct previously passed to event_add()
event the requested event type; any of EV_TIMEOUT|EV_READ| EV_WRITE|EV_SIGNAL
tv an alternate timeout (FIXME - is this true?)
Returns:
1 if the event is pending, or 0 if the event has not occurred

int event_priority_set ( struct event *  ,
int   
)

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()

int event_reinit ( struct event_base *  base  ) 

Reinitialized 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(), event_init()

void event_set ( struct event *  ,
evutil_socket_t  ,
short  ,
void(*)(evutil_socket_t, short, void *)  ,
void *   
)

Prepare an event structure to be added.

The function event_set() prepares the event structure ev to be used in future calls to event_add() and event_del(). The event will be prepared to call the function specified by the fn argument with an int argument indicating the file descriptor, a short argument indicating the type of event, and a void * argument given in the arg argument. The fd indicates the file descriptor that should be monitored for events. The events can be either EV_READ, EV_WRITE, or both. Indicating that an application can read or write from the file descriptor respectively without blocking.

The function fn will be called with the file descriptor that triggered the event and the type of event which will be either EV_TIMEOUT, EV_SIGNAL, EV_READ, or EV_WRITE. The additional flag EV_PERSIST makes an event_add() persistent until event_del() has been called.

For read and write events, edge-triggered behavior can be requested with the EV_ET flag. Not all backends support edge-triggered behavior. When an edge-triggered event is activated, the EV_ET flag is added to its events argument.

Parameters:
ev an event struct to be modified
fd the file descriptor to be monitored
event desired events to monitor; can be EV_READ and/or EV_WRITE
fn callback function to be invoked when the event occurs
arg an argument to be passed to the callback function
See also:
event_add(), event_del(), event_once()
Deprecated:
event_set() is not recommended for new code, because it requires a subsequent call to event_base_set() to be safe under many circumstances. Use event_assign() or event_new() instead.

void event_set_log_callback ( event_log_cb  cb  ) 

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.

void event_set_mem_functions ( void *(*)(size_t sz)  malloc_fn,
void *(*)(void *ptr, size_t sz)  realloc_fn,
void(*)(void *ptr)  free_fn 
)

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. To restore the default behavior, pass NULLs as the arguments to this function.

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 may not be appropriate to free() memory that you get from libevent.

Parameters:
malloc_fn A replacement for malloc.
realloc_fn A replacement for realloc
free_fn A replacement for free.


Generated on Fri Apr 17 21:33:55 2009 for libevent by  doxygen 1.5.7