Wed Jan 27 20:02:13 2016

Asterisk developer's documentation


poll.c

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

Generated on 27 Jan 2016 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1