#include <poller.h> int poller_create (void); int poller_destroy (int instance); int poller_add (int instance, int fd, pollerev_t events, pollerdata_t data); int poller_modify (int instance, int fd, pollerev_t events, pollerdata_t data); int poller_remove (int instance, int fd); int poller_poll (int instance, int timeout, pollercb_t callback);
To initialize the poller, call the poller_create() function. If poller is compiled to use epoll(7), then this calling will create the epoll file descriptor. Otherwise it won't do anything. If you don't need poller anymore, call poller_destroy() with the argument instance as the return value from poller_create(). It will remove file descriptors and their data from the memory, or close the epoll file descriptor, if epoll(7) is used.
A call to poller_add() adds file descriptor fd into the poller specified by instance. The field events is a bit mask specifying events the application is interested in. The field data will be sent back to the application when an event occurs on this file descriptor. Normally an information about the file descriptor or a pointer to a structure containing this file descriptor is stored in data. The type is defined as
typedef union { void * ptr; int fd; uint32_t u32; uint64_t u64; } pollerdata_t;
To modify already stored file descirptor in the poller call poller_modify() with the same arguments as poller_add().
Removing a file descriptor from the poller can be done by calling poller_remove().
To poll for events on stored file descriptors call poller_poll(). The instance argument specifies the instance of poller to be used, while the timeout argument specifies an upper limit on the time for which polling will block, in milliseconds. If you don't want to limit the time for polling, use a value of -1. The field callback should be a pointer to a function which will be called when an event occurs on a file descriptor. The prototype for this function is
void callback (pollerdata_t data, pollerev_t events);
The data argument will contain the same data which was saved into the poller with poller_add() or poller_modify() function. The field events is a bit mask specifying events which actually occured on that descriptor.
Currently supported events:
poller_destroy(), poller_add(), poller_modify() and poller_remove() return -1 if an error has occurred, 0 otherwise.
poller_poll() returns the number of file descriptors on which an event has occured. If the timeout is reached sooner, 0 is returned. If an error has occured, poller_poll() returns -1.
If an error has occured, all this functions set errno appropriately.