EVENT(3)

NAME
event_init, event_dispatch, event_loop, event_set, event_add, event_del, event_pending, event_initialized, evtimer_set, evtimer_add, evtimer_del evtimer_pending, evtimer_initialized, signal_set, signal_add, signal_del signal_pending, signal_initialized - execute a function when a specific event occurs

SYNOPSIS
#include <sys/time.h>
#include <event.h>
void
event_init();
int
event_dispatch();
int
event_loop(int flags);
void
event_set(struct event *ev, int fd, short event, void (*fn)(int, short, void *), void *arg);
int
event_add(struct event *ev, struct timeval *tv);
int
event_del(struct event *ev);
int
event_pending(struct event *ev, short event, struct timeval *tv);
int
event_initialized(struct event *ev);
void
evtimer_set(struct event *ev, void (*fn)(int, short, void *), void *arg);
void
evtimer_add(struct event *ev, struct timeval *);
void
evtimer_del(struct event *ev);
int
evtimer_pending(struct event *ev, struct timeval *tv);
int
evtimer_initialized(struct event *ev);
void
signal_set(struct event *ev, int signal, void (*fn)(int, short, void *), void *arg);
void
signal_add(struct event *ev, struct timeval *);
void
signal_del(struct event *ev);
int
signal_pending(struct event *ev, struct timeval *tv);
int
signal_initialized(struct event *ev);
int
(*event_sigcb)(void);
int
event_gotsig;

DESCRIPTION
The event API provides a mechanism to execute a function when a specific event on a file descriptor occurs or after at a given time has passed.

The event API needs to be initialized with event_init() before it can be used.

In order to process events, an application needs to call event_dispatch(). This functions only returns on error, and should replace the event core of the application program.

In order to avoid races in signal handlers, the event API provides two variables: event_sigcb and event_gotsig. A signal handler sets event_gotsig to indicate that a signal has been received. The application sets event_sigcb to a callback function. After the signal handler sets event_gotsig, event_dispatch will execute the callback function to process received signals. The callback returns 1 when no events are registered any more. It can return -1 to indicate an error to the event library, causing event_dispatch() to terminate with errno set to EINTR.

The event_loop function provides an interface for single pass execution of pending events. The flags EVLOOP_ONCE and EVLOOP_NONBLOCK are recognized.

It is the responsibility of the caller to provide these functions with pre-allocated event structures.

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.

Once initialized, the ev structure can be used repeatedly with event_add() and event_del() and does not need to be reinitialized unless the function called and/or the argument to it are to be changed.

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 has already a scheduled timeout, the old timeout will be replaced by the new one.

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.

The event_pending() function can be used to check if the event specified by event is pending to run. If EV_TIMEOUT was specified and tv is not NULL, the expiration time of the event will be returned in tv.

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

The functions evtimer_set(), evtimer_add(), evtimer_del(), evtimer_initialized(), and evtimer_pending() are abbreviations for common situations where only a timeout is required. The file descriptor passed will be 0, and the event type will be EV_TIMEOUT.

The functions signal_set(), signal_add(), signal_del(), signal_initialized(), and signal_pending() are abbreviations. The event type will be a persistent EV_SIGNAL. That means signal_set() adds EV_PERSIST.

It is possible to disable support for kqueue by setting the environment variable EVENT_NOKQUEUE.

RETURN VALUES
Upon successful completion event_add() and event_del() return 0. Otherwise, -1 is returned and the global variable errno is set to indicate the error.

SEE ALSO
timeout(9), select(2), kqueue(2)

HISTORY
The event API manpage is based on the timeout(9) manpage by Artur Grabowski.

AUTHORS
The event library was written by Niels Provos.