Thu Mar 25 12:09:37 2010

Asterisk developer's documentation


poll.c

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   $Id: poll.c 182810 2009-03-18 02:09:13Z russell $
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               Macros
00088 \*---------------------------------------------------------------------------*/
00089 
00090 #ifndef MAX
00091 #define MAX(a,b)  ((a) > (b) ? (a) : (b))
00092 #endif
00093 
00094 /*---------------------------------------------------------------------------*\
00095               Private Functions
00096 \*---------------------------------------------------------------------------*/
00097 
00098 static int map_poll_spec
00099 #if __STDC__ > 0
00100          (struct pollfd *pArray,
00101            unsigned long  n_fds,
00102            fd_set        *pReadSet,
00103            fd_set        *pWriteSet,
00104            fd_set        *pExceptSet)
00105 #else
00106           (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
00107            struct pollfd *pArray;
00108            unsigned long  n_fds;
00109            fd_set        *pReadSet;
00110            fd_set        *pWriteSet;
00111            fd_set        *pExceptSet;
00112 #endif
00113 {
00114     register unsigned long  i;                   /* loop control */
00115     register struct      pollfd *pCur;        /* current array element */
00116     register int      max_fd = -1;         /* return value */
00117 
00118     /*
00119        Map the poll() structures into the file descriptor sets required
00120        by select().
00121     */
00122     for (i = 0, pCur = pArray; i < n_fds; i++, pCur++)
00123     {
00124         /* Skip any bad FDs in the array. */
00125 
00126         if (pCur->fd < 0)
00127             continue;
00128 
00129    if (pCur->events & POLLIN)
00130    {
00131        /* "Input Ready" notification desired. */
00132        FD_SET (pCur->fd, pReadSet);
00133    }
00134 
00135    if (pCur->events & POLLOUT)
00136    {
00137        /* "Output Possible" notification desired. */
00138        FD_SET (pCur->fd, pWriteSet);
00139    }
00140 
00141    if (pCur->events & POLLPRI)
00142    {
00143        /*
00144           "Exception Occurred" notification desired.  (Exceptions
00145           include out of band data.
00146        */
00147        FD_SET (pCur->fd, pExceptSet);
00148    }
00149 
00150    max_fd = MAX (max_fd, pCur->fd);
00151     }
00152 
00153     return max_fd;
00154 }
00155 
00156 static struct timeval *map_timeout
00157 #if __STDC__ > 0
00158          (int poll_timeout, struct timeval *pSelTimeout)
00159 #else
00160          (poll_timeout, pSelTimeout)
00161           int             poll_timeout;
00162           struct timeval *pSelTimeout;
00163 #endif
00164 {
00165     struct timeval *pResult;
00166 
00167     /*
00168        Map the poll() timeout value into a select() timeout.  The possible
00169        values of the poll() timeout value, and their meanings, are:
00170 
00171        VALUE   MEANING
00172 
00173        -1   wait indefinitely (until signal occurs)
00174         0   return immediately, don't block
00175        >0   wait specified number of milliseconds
00176 
00177        select() uses a "struct timeval", which specifies the timeout in
00178        seconds and microseconds, so the milliseconds value has to be mapped
00179        accordingly.
00180     */
00181 
00182     assert (pSelTimeout != (struct timeval *) NULL);
00183 
00184     switch (poll_timeout)
00185     {
00186    case -1:
00187        /*
00188           A NULL timeout structure tells select() to wait indefinitely.
00189        */
00190        pResult = (struct timeval *) NULL;
00191        break;
00192 
00193    case 0:
00194        /*
00195           "Return immediately" (test) is specified by all zeros in
00196           a timeval structure.
00197        */
00198        pSelTimeout->tv_sec  = 0;
00199        pSelTimeout->tv_usec = 0;
00200        pResult = pSelTimeout;
00201        break;
00202 
00203    default:
00204        /* Wait the specified number of milliseconds. */
00205        pSelTimeout->tv_sec  = poll_timeout / 1000; /* get seconds */
00206        poll_timeout        %= 1000;                /* remove seconds */
00207        pSelTimeout->tv_usec = poll_timeout * 1000; /* get microseconds */
00208        pResult = pSelTimeout;
00209        break;
00210     }
00211 
00212 
00213     return pResult;
00214 }
00215 
00216 static void map_select_results
00217 #if __STDC__ > 0
00218           (struct pollfd *pArray,
00219            unsigned long  n_fds,
00220            fd_set        *pReadSet,
00221            fd_set        *pWriteSet,
00222            fd_set        *pExceptSet)
00223 #else
00224           (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
00225            struct pollfd *pArray;
00226            unsigned long  n_fds;
00227            fd_set        *pReadSet;
00228            fd_set        *pWriteSet;
00229            fd_set        *pExceptSet;
00230 #endif
00231 {
00232     register unsigned long  i;                   /* loop control */
00233     register struct      pollfd *pCur;        /* current array element */
00234 
00235     for (i = 0, pCur = pArray; i < n_fds; i++, pCur++)
00236     {
00237         /* Skip any bad FDs in the array. */
00238 
00239         if (pCur->fd < 0)
00240             continue;
00241 
00242    /* Exception events take priority over input events. */
00243 
00244    pCur->revents = 0;
00245    if (FD_ISSET (pCur->fd, pExceptSet))
00246        pCur->revents |= POLLPRI;
00247 
00248    else if (FD_ISSET (pCur->fd, pReadSet))
00249        pCur->revents |= POLLIN;
00250 
00251    if (FD_ISSET (pCur->fd, pWriteSet))
00252        pCur->revents |= POLLOUT;
00253     }
00254 
00255     return;
00256 }
00257 
00258 /*---------------------------------------------------------------------------*\
00259               Public Functions
00260 \*---------------------------------------------------------------------------*/
00261 
00262 int ast_internal_poll(struct pollfd *pArray, unsigned long n_fds, int timeout)
00263 {
00264     fd_set  read_descs;                          /* input file descs */
00265     fd_set  write_descs;                         /* output file descs */
00266     fd_set  except_descs;                        /* exception descs */
00267     struct  timeval stime;                       /* select() timeout value */
00268     int      ready_descriptors;                   /* function result */
00269     int      max_fd = 0;                          /* maximum fd value */
00270     struct  timeval *pTimeout;                   /* actually passed */
00271 
00272     FD_ZERO (&read_descs);
00273     FD_ZERO (&write_descs);
00274     FD_ZERO (&except_descs);
00275 
00276     /* Map the poll() file descriptor list in the select() data structures. */
00277 
00278    if (pArray) {
00279       max_fd = map_poll_spec (pArray, n_fds,
00280             &read_descs, &write_descs, &except_descs);
00281    }
00282 
00283     /* Map the poll() timeout value in the select() timeout structure. */
00284 
00285     pTimeout = map_timeout (timeout, &stime);
00286 
00287     /* Make the select() call. */
00288 
00289     ready_descriptors = select (max_fd + 1, &read_descs, &write_descs,
00290             &except_descs, pTimeout);
00291 
00292     if (ready_descriptors >= 0)
00293     {
00294    map_select_results (pArray, n_fds,
00295              &read_descs, &write_descs, &except_descs);
00296     }
00297 
00298     return ready_descriptors;
00299 }
00300 
00301 #endif /* AST_POLL_COMPAT */

Generated on Thu Mar 25 12:09:37 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7