poller

Section: C Library Functions (3)
Updated: August 2010
Index Return to Main Contents
 

NAME

poller - front-end for waiting for I/O functions  

SYNOPSIS

#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);
 

DESCRIPTION

The poller library is a simple front-end for the waiting for I/O functions (poll(2), epoll(7)). It simplifies adding/removing file descriptors to/from the poll's input when polling many times, for example when a server is waiting for a reply from clients and listening for a connection.

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:

POLLERIN
Equivalent to POLLIN
POLLERPRI
Equivalent to POLLPRI
POLLEROUT
Equivalent to POLLOUT
POLLERHUP
Equivalent to POLLHUP
POLLERERR
Equivalent to POLLERR
 

RETURN VALUE

poller_create() returns the new poller instance identifier (a non-negative integer on success), -1 if an error has occured.

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.  

ERRORS

EINTR
A signal occured before any requested event.
ENOMEM
There was no space to allocate tables for the poller.
EEXIST
File descriptor fd already registered in the poller.
ENOENT
Poller inst does not exist or file descriptor fd not found in the poller.
EINVAL
The number of file descriptors registered in the poller exceeds the RLIMIT_NOFILE value.
EBADF
Bad file descriptor fd.
EPERM
File descriptor fd does not support polling.
 

SEE ALSO

poll(2), epoll(7), select(2)  

COPYRIGHT

Copyright © 2010 Marek Behun
License LGPLv3+: GNU LGPL version 3 or later <http://gnu.org/licenses/lgpl.html>
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
 

Index

NAME
SYNOPSIS
DESCRIPTION
RETURN VALUE
ERRORS
SEE ALSO
COPYRIGHT

This document was created by man2html, using the manual pages.
Time: 22:51:33 GMT, August 08, 2010