Tue Mar 2 17:31:51 2010

Asterisk developer's documentation


poll.c

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   $Id: poll.c 238497 2010-01-07 21:17:32Z tilghman $
00003 
00004   NAME
00005 
00006    poll - select(2)-based poll() emulation function for BSD systems.
00007 
00008   SYNOPSIS
00009    #include "poll.h"
00010 
00011    struct pollfd
00012    {
00013        int     fd;
00014        short   events;
00015        short   revents;
00016    }
00017 
00018    int poll (struct pollfd *pArray, unsigned long n_fds, int timeout)
00019 
00020   DESCRIPTION
00021 
00022    This file, and the accompanying "poll.h", implement the System V
00023    poll(2) system call for BSD systems (which typically do not provide
00024    poll()).  Poll() provides a method for multiplexing input and output
00025    on multiple open file descriptors; in traditional BSD systems, that
00026    capability is provided by select().  While the semantics of select()
00027    differ from those of poll(), poll() can be readily emulated in terms
00028    of select() -- which is how this function is implemented.
00029 
00030   REFERENCES
00031    Stevens, W. Richard. Unix Network Programming.  Prentice-Hall, 1990.
00032 
00033   NOTES
00034    1. This software requires an ANSI C compiler.
00035 
00036   LICENSE
00037 
00038    This software is released under the following license:
00039 
00040       Copyright (c) 1995-2002 Brian M. Clapper
00041       All rights reserved.
00042 
00043       Redistribution and use in source and binary forms are
00044       permitted provided that: (1) source distributions retain
00045       this entire copyright notice and comment; (2) modifications
00046       made to the software are prominently mentioned, and a copy
00047       of the original software (or a pointer to its location) are
00048       included; and (3) distributions including binaries display
00049       the following acknowledgement: "This product includes
00050       software developed by Brian M. Clapper <bmc@clapper.org>"
00051       in the documentation or other materials provided with the
00052       distribution. The name of the author may not be used to
00053       endorse or promote products derived from this software
00054       without specific prior written permission.
00055 
00056       THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS
00057       OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
00058       IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00059       PARTICULAR PURPOSE.
00060 
00061    Effectively, this means you can do what you want with the software
00062    except remove this notice or take advantage of the author's name.
00063    If you modify the software and redistribute your modified version,
00064    you must indicate that your version is a modification of the
00065    original, and you must provide either a pointer to or a copy of the
00066    original.
00067 \*---------------------------------------------------------------------------*/
00068 
00069 
00070 /*---------------------------------------------------------------------------*\
00071              Includes
00072 \*---------------------------------------------------------------------------*/
00073 
00074 #include "asterisk.h"
00075 
00076 #include <unistd.h>             /* standard Unix definitions */
00077 #include <sys/types.h>                       /* system types */
00078 #include <sys/time.h>                        /* time definitions */
00079 #include <assert.h>                          /* assertion macros */
00080 #include <string.h>                          /* string functions */
00081 
00082 #include "asterisk/poll-compat.h"                            /* this package */
00083 
00084 #ifdef AST_POLL_COMPAT
00085 
00086 /*---------------------------------------------------------------------------*\
00087               Private Functions
00088 \*---------------------------------------------------------------------------*/
00089 
00090 static int map_poll_spec
00091 #if __STDC__ > 0
00092          (struct pollfd *pArray,
00093            unsigned long  n_fds,
00094            fd_set        *pReadSet,
00095            fd_set        *pWriteSet,
00096            fd_set        *pExceptSet)
00097 #else
00098           (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
00099            struct pollfd *pArray;
00100            unsigned long  n_fds;
00101            fd_set        *pReadSet;
00102            fd_set        *pWriteSet;
00103            fd_set        *pExceptSet;
00104 #endif
00105 {
00106    register unsigned long  i;                   /* loop control */
00107    register struct       pollfd *pCur;        /* current array element */
00108    register int       max_fd = -1;         /* return value */
00109 
00110    /*!\note
00111     * Map the poll() structures into the file descriptor sets required
00112     * by select().
00113     */
00114    for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
00115       /* Skip any bad FDs in the array. */
00116 
00117       if (pCur->fd < 0)
00118          continue;
00119 
00120       if (pCur->events & POLLIN) {
00121          /* "Input Ready" notification desired. */
00122          FD_SET(pCur->fd, pReadSet);
00123       }
00124 
00125       if (pCur->events & POLLOUT) {
00126          /* "Output Possible" notification desired. */
00127          FD_SET(pCur->fd, pWriteSet);
00128       }
00129 
00130       if (pCur->events & POLLPRI) {
00131          /*!\note
00132           * "Exception Occurred" notification desired.  (Exceptions
00133           * include out of band data.)
00134           */
00135          FD_SET(pCur->fd, pExceptSet);
00136       }
00137 
00138       max_fd = MAX(max_fd, pCur->fd);
00139    }
00140 
00141    return max_fd;
00142 }
00143 
00144 static struct timeval *map_timeout
00145 #if __STDC__ > 0
00146          (int poll_timeout, struct timeval *pSelTimeout)
00147 #else
00148          (poll_timeout, pSelTimeout)
00149           int             poll_timeout;
00150           struct timeval *pSelTimeout;
00151 #endif
00152 {
00153    struct timeval *pResult;
00154 
00155    /*!\note
00156     * Map the poll() timeout value into a select() timeout.  The possible
00157     * values of the poll() timeout value, and their meanings, are:
00158     *
00159     * VALUE MEANING
00160     *
00161     * -1 wait indefinitely (until signal occurs)
00162     *  0 return immediately, don't block
00163     * >0 wait specified number of milliseconds
00164     *
00165     * select() uses a "struct timeval", which specifies the timeout in
00166     * seconds and microseconds, so the milliseconds value has to be mapped
00167     * accordingly.
00168     */
00169 
00170    assert(pSelTimeout != (struct timeval *) NULL);
00171 
00172    switch (poll_timeout) {
00173    case -1:
00174       /*
00175        * A NULL timeout structure tells select() to wait indefinitely.
00176        */
00177       pResult = (struct timeval *) NULL;
00178       break;
00179 
00180    case 0:
00181       /*
00182        * "Return immediately" (test) is specified by all zeros in
00183        * a timeval structure.
00184        */
00185       pSelTimeout->tv_sec  = 0;
00186       pSelTimeout->tv_usec = 0;
00187       pResult = pSelTimeout;
00188       break;
00189 
00190    default:
00191       /* Wait the specified number of milliseconds. */
00192       pSelTimeout->tv_sec  = poll_timeout / 1000; /* get seconds */
00193       poll_timeout        %= 1000;                /* remove seconds */
00194       pSelTimeout->tv_usec = poll_timeout * 1000; /* get microseconds */
00195       pResult = pSelTimeout;
00196       break;
00197    }
00198 
00199    return pResult;
00200 }
00201 
00202 static void map_select_results
00203 #if __STDC__ > 0
00204           (struct pollfd *pArray,
00205            unsigned long  n_fds,
00206            fd_set        *pReadSet,
00207            fd_set        *pWriteSet,
00208            fd_set        *pExceptSet)
00209 #else
00210           (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
00211            struct pollfd *pArray;
00212            unsigned long  n_fds;
00213            fd_set        *pReadSet;
00214            fd_set        *pWriteSet;
00215            fd_set        *pExceptSet;
00216 #endif
00217 {
00218    register unsigned long  i;                   /* loop control */
00219    register struct       pollfd *pCur;        /* current array element */
00220 
00221    for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
00222       /* Skip any bad FDs in the array. */
00223 
00224       if (pCur->fd < 0) {
00225          continue;
00226       }
00227 
00228       /* Exception events take priority over input events. */
00229 
00230       pCur->revents = 0;
00231       if (FD_ISSET (pCur->fd, pExceptSet)) {
00232          pCur->revents |= POLLPRI;
00233       } else if (FD_ISSET (pCur->fd, pReadSet)) {
00234          pCur->revents |= POLLIN;
00235       }
00236 
00237       if (FD_ISSET (pCur->fd, pWriteSet)) {
00238          pCur->revents |= POLLOUT;
00239       }
00240    }
00241 
00242    return;
00243 }
00244 
00245 /*---------------------------------------------------------------------------*\
00246               Public Functions
00247 \*---------------------------------------------------------------------------*/
00248 
00249 int ast_internal_poll(struct pollfd *pArray, unsigned long n_fds, int timeout)
00250 {
00251    fd_set  read_descs;                          /* input file descs */
00252    fd_set  write_descs;                         /* output file descs */
00253    fd_set  except_descs;                        /* exception descs */
00254    struct  timeval stime;                       /* select() timeout value */
00255    int       ready_descriptors;                   /* function result */
00256    int       max_fd = 0;                          /* maximum fd value */
00257    struct  timeval *pTimeout;                   /* actually passed */
00258 
00259    FD_ZERO (&read_descs);
00260    FD_ZERO (&write_descs);
00261    FD_ZERO (&except_descs);
00262 
00263    /* Map the poll() file descriptor list in the select() data structures. */
00264 
00265    if (pArray) {
00266       max_fd = map_poll_spec (pArray, n_fds,
00267             &read_descs, &write_descs, &except_descs);
00268    }
00269 
00270    /* Map the poll() timeout value in the select() timeout structure. */
00271    pTimeout = map_timeout(timeout, &stime);
00272 
00273    /* Make the select() call. */
00274    ready_descriptors = select(max_fd + 1, &read_descs, &write_descs,
00275             &except_descs, pTimeout);
00276 
00277    if (ready_descriptors >= 0) {
00278       map_select_results(pArray, n_fds, &read_descs, &write_descs, &except_descs);
00279    }
00280 
00281    return ready_descriptors;
00282 }
00283 
00284 #endif /* AST_POLL_COMPAT */

Generated on 2 Mar 2010 for Asterisk - the Open Source PBX by  doxygen 1.6.1