Mon Jun 27 16:50:56 2011

Asterisk developer's documentation


select.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2010, Digium, Inc.
00005  *
00006  * Tilghman Lesher <tlesher AT digium DOT com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*!\file
00020  * \brief Bitfield expansions for ast_select
00021  */
00022 
00023 #ifndef __AST_SELECT_H
00024 #define __AST_SELECT_H
00025 
00026 #include <sys/time.h>
00027 #include <sys/select.h>
00028 #include <errno.h>
00029 
00030 #include "asterisk/compat.h"
00031 
00032 #ifdef __cplusplus
00033 extern "C" {
00034 #endif
00035 
00036 extern unsigned int ast_FD_SETSIZE;
00037 
00038 #if !defined(HAVE_VARIABLE_FDSET) && defined(CONFIGURE_RAN_AS_ROOT)
00039 #define ast_fdset fd_set
00040 #else
00041 typedef struct {
00042    TYPEOF_FD_SET_FDS_BITS fds_bits[4096 / SIZEOF_FD_SET_FDS_BITS]; /* 32768 bits */
00043 } ast_fdset;
00044 
00045 #undef FD_ZERO
00046 #define FD_ZERO(a) \
00047    do { \
00048       TYPEOF_FD_SET_FDS_BITS *bytes = (TYPEOF_FD_SET_FDS_BITS *) a; \
00049       int i; \
00050       for (i = 0; i < sizeof(*(a)) / SIZEOF_FD_SET_FDS_BITS; i++) { \
00051          bytes[i] = 0; \
00052       } \
00053    } while (0)
00054 #undef FD_SET
00055 #define FD_SET(fd, fds) \
00056    do { \
00057       TYPEOF_FD_SET_FDS_BITS *bytes = (TYPEOF_FD_SET_FDS_BITS *) fds; \
00058       if (fd / sizeof(*bytes) + ((fd + 1) % sizeof(*bytes) ? 1 : 0) < sizeof(*(fds))) { \
00059          bytes[fd / (sizeof(*bytes) * 8)] |= ((TYPEOF_FD_SET_FDS_BITS) 1) << (fd % (sizeof(*bytes) * 8)); \
00060       } else { \
00061          fprintf(stderr, "FD %d exceeds the maximum size of ast_fdset!\n", fd); \
00062       } \
00063    } while (0)
00064 #endif /* HAVE_VARIABLE_FDSET */
00065 
00066 /*! \brief Waits for activity on a group of channels 
00067  * \param nfds the maximum number of file descriptors in the sets
00068  * \param rfds file descriptors to check for read availability
00069  * \param wfds file descriptors to check for write availability
00070  * \param efds file descriptors to check for exceptions (OOB data)
00071  * \param tvp timeout while waiting for events
00072  * This is the same as a standard select(), except it guarantees the
00073  * behaviour where the passed struct timeval is updated with how much
00074  * time was not slept while waiting for the specified events
00075  */
00076 static inline int ast_select(int nfds, ast_fdset *rfds, ast_fdset *wfds, ast_fdset *efds, struct timeval *tvp)
00077 {
00078 #ifdef __linux__
00079    return select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, tvp);
00080 #else
00081    int save_errno = 0;
00082    if (tvp) {
00083       struct timeval tv, tvstart, tvend, tvlen;
00084       int res;
00085 
00086       tv = *tvp;
00087       gettimeofday(&tvstart, NULL);
00088       res = select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, tvp);
00089       save_errno = errno;
00090       gettimeofday(&tvend, NULL);
00091       timersub(&tvend, &tvstart, &tvlen);
00092       timersub(&tv, &tvlen, tvp);
00093       if (tvp->tv_sec < 0 || (tvp->tv_sec == 0 && tvp->tv_usec < 0)) {
00094          tvp->tv_sec = 0;
00095          tvp->tv_usec = 0;
00096       }
00097       errno = save_errno;
00098       return res;
00099    }
00100    else
00101       return select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, NULL);
00102 #endif
00103 }
00104 
00105 #ifdef __cplusplus
00106 }
00107 #endif
00108 
00109 #endif /* __AST_SELECT_H */

Generated on Mon Jun 27 16:50:56 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7