Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:49:12 2009

## Asterisk developer's documentation

# poll.c File Reference

`#include <unistd.h>`
`#include <sys/types.h>`
`#include <sys/time.h>`
`#include <assert.h>`
`#include <string.h>`
`#include "asterisk/poll-compat.h"`

Include dependency graph for poll.c:

Go to the source code of this file.

## Defines

#define | MAX(a, b) ((a) > (b) ? (a) : (b))

## Functions

static int | map_poll_spec (struct pollfd *pArray, unsigned long n_fds, fd_set *pReadSet, fd_set *pWriteSet, fd_set *pExceptSet)
static void | map_select_results (struct pollfd *pArray, unsigned long n_fds, fd_set *pReadSet, fd_set *pWriteSet, fd_set *pExceptSet)
static struct timeval * | map_timeout (int poll_timeout, struct timeval *pSelTimeout)
int | poll (structpollfd *pArray, unsigned long n_fds, int timeout)

## Define Documentation

#define MAX | ( | a,
|  | b |  | ) | ((a) > (b) ? (a) : (b))

Definition at line 87 of file poll.c.

Referenced by handle_response_register(), map_poll_spec(), and sound_thread().

## Function Documentation

static int map_poll_spec | ( | struct pollfd * | pArray,
|  | unsigned long | n_fds,
|  | fd_set * | pReadSet,
|  | fd_set * | pWriteSet,
|  | fd_set * | pExceptSet |
| ) |  |  | `[static]`

Definition at line 103 of file poll.c.

References pollfd::events, pollfd::fd, MAX, POLLIN, POLLOUT, and POLLPRI.

Referenced by poll().

    00110 {
    00111     register unsigned long  i;                   /* loop control */
    00112     register struct      pollfd *pCur;        /* current array element */
    00113     register int      max_fd = -1;         /* return value */
    00114
    00115     /*
    00116        Map the poll() structures into the file descriptor sets required
    00117        by select().
    00118     */
    00119     for (i = 0, pCur = pArray; i < n_fds; i++, pCur++)
    00120     {
    00121         /* Skip any bad FDs in the array. */
    00122
    00123         if (pCur->fd < 0)
    00124             continue;
    00125
    00126    if (pCur->events & POLLIN)
    00127    {
    00128        /* "Input Ready" notification desired. */
    00129        FD_SET (pCur->fd, pReadSet);
    00130    }
    00131
    00132    if (pCur->events & POLLOUT)
    00133    {
    00134        /* "Output Possible" notification desired. */
    00135        FD_SET (pCur->fd, pWriteSet);
    00136    }
    00137
    00138    if (pCur->events & POLLPRI)
    00139    {
    00140        /*
    00141           "Exception Occurred" notification desired.  (Exceptions
    00142           include out of band data.
    00143        */
    00144        FD_SET (pCur->fd, pExceptSet);
    00145    }
    00146
    00147    max_fd = MAX (max_fd, pCur->fd);
    00148     }
    00149
    00150     return max_fd;
    00151 }

static void map_select_results | ( | struct pollfd * | pArray,
|  | unsigned long | n_fds,
|  | fd_set * | pReadSet,
|  | fd_set * | pWriteSet,
|  | fd_set * | pExceptSet |
| ) |  |  | `[static]`

Definition at line 221 of file poll.c.

References pollfd::fd, POLLIN, POLLOUT, POLLPRI, and pollfd::revents.

Referenced by poll().

    00228 {
    00229     register unsigned long  i;                   /* loop control */
    00230     register struct      pollfd *pCur;        /* current array element */
    00231
    00232     for (i = 0, pCur = pArray; i < n_fds; i++, pCur++)
    00233     {
    00234         /* Skip any bad FDs in the array. */
    00235
    00236         if (pCur->fd < 0)
    00237             continue;
    00238
    00239    /* Exception events take priority over input events. */
    00240
    00241    pCur->revents = 0;
    00242    if (FD_ISSET (pCur->fd, pExceptSet))
    00243        pCur->revents |= POLLPRI;
    00244
    00245    else if (FD_ISSET (pCur->fd, pReadSet))
    00246        pCur->revents |= POLLIN;
    00247
    00248    if (FD_ISSET (pCur->fd, pWriteSet))
    00249        pCur->revents |= POLLOUT;
    00250     }
    00251
    00252     return;
    00253 }

static struct timeval* map_timeout | ( | int | poll_timeout,
|  | struct timeval * | pSelTimeout |
| ) |  |  | `[static, read]`

Definition at line 157 of file poll.c.

Referenced by poll().

    00161 {
    00162     struct timeval *pResult;
    00163
    00164     /*
    00165        Map the poll() timeout value into a select() timeout.  The possible
    00166        values of the poll() timeout value, and their meanings, are:
    00167
    00168        VALUE   MEANING
    00169
    00170        -1   wait indefinitely (until signal occurs)
    00171         0   return immediately, don't block
    00172        >0   wait specified number of milliseconds
    00173
    00174        select() uses a "struct timeval", which specifies the timeout in
    00175        seconds and microseconds, so the milliseconds value has to be mapped
    00176        accordingly.
    00177     */
    00178
    00179     assert (pSelTimeout != (struct timeval *) NULL);
    00180
    00181     switch (poll_timeout)
    00182     {
    00183    case -1:
    00184        /*
    00185           A NULL timeout structure tells select() to wait indefinitely.
    00186        */
    00187        pResult = (struct timeval *) NULL;
    00188        break;
    00189
    00190    case 0:
    00191        /*
    00192           "Return immediately" (test) is specified by all zeros in
    00193           a timeval structure.
    00194        */
    00195        pSelTimeout->tv_sec  = 0;
    00196        pSelTimeout->tv_usec = 0;
    00197        pResult = pSelTimeout;
    00198        break;
    00199
    00200    default:
    00201        /* Wait the specified number of milliseconds. */
    00202        pSelTimeout->tv_sec  = poll_timeout / 1000; /* get seconds */
    00203        poll_timeout        %= 1000;                /* remove seconds */
    00204        pSelTimeout->tv_usec = poll_timeout * 1000; /* get microseconds */
    00205        pResult = pSelTimeout;
    00206        break;
    00207     }
    00208
    00209
    00210     return pResult;
    00211 }

int poll | ( | struct pollfd * | pArray,
|  | unsigned long | n_fds,
|  | int | timeout |
| ) |  |  |

Definition at line 264 of file poll.c.

References map_poll_spec(), map_select_results(), and map_timeout().

Referenced by ast_carefulwrite(), ast_el_read_char(), ast_io_wait(), ast_remotecontrol(), ast_wait_for_input(), ast_waitfor_n_fd(), ast_waitfor_nandfds(), do_monitor(), get_input(), launch_netscript(), listener(), misdn_tasks_thread_func(), monitor_sig_flags(), netconsole(), timed_read(), zt_sendtext(), and zt_setoption().

    00270 {
    00271     fd_set  read_descs;                          /* input file descs */
    00272     fd_set  write_descs;                         /* output file descs */
    00273     fd_set  except_descs;                        /* exception descs */
    00274     struct  timeval stime;                       /* select() timeout value */
    00275     int      ready_descriptors;                   /* function result */
    00276     int      max_fd;                              /* maximum fd value */
    00277     struct  timeval *pTimeout;                   /* actually passed */
    00278
    00279     FD_ZERO (&read_descs);
    00280     FD_ZERO (&write_descs);
    00281     FD_ZERO (&except_descs);
    00282
    00283     assert (pArray != (struct pollfd *) NULL);
    00284
    00285     /* Map the poll() file descriptor list in the select() data structures. */
    00286
    00287     max_fd = map_poll_spec (pArray, n_fds,
    00288              &read_descs, &write_descs, &except_descs);
    00289
    00290     /* Map the poll() timeout value in the select() timeout structure. */
    00291
    00292     pTimeout = map_timeout (timeout, &stime);
    00293
    00294     /* Make the select() call. */
    00295
    00296     ready_descriptors = select (max_fd + 1, &read_descs, &write_descs,
    00297             &except_descs, pTimeout);
    00298
    00299     if (ready_descriptors >= 0)
    00300     {
    00301    map_select_results (pArray, n_fds,
    00302              &read_descs, &write_descs, &except_descs);
    00303     }
    00304
    00305     return ready_descriptors;
    00306 }