Mon Mar 19 11:30:30 2012

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 #define ast_FDMAX 32768
00042 typedef struct {
00043    TYPEOF_FD_SET_FDS_BITS fds_bits[ast_FDMAX / 8 / SIZEOF_FD_SET_FDS_BITS]; /* 32768 bits */
00044 } ast_fdset;
00045 
00046 #define _bitsize(a)  (sizeof(a) * 8)
00047 
00048 #undef FD_ZERO
00049 #define FD_ZERO(a) \
00050    do { \
00051       TYPEOF_FD_SET_FDS_BITS *bytes = (TYPEOF_FD_SET_FDS_BITS *) a; \
00052       int i; \
00053       for (i = 0; i < ast_FDMAX / _bitsize(TYPEOF_FD_SET_FDS_BITS); i++) { \
00054          bytes[i] = 0; \
00055       } \
00056    } while (0)
00057 #undef FD_SET
00058 #define FD_SET(fd, fds) \
00059    do { \
00060       TYPEOF_FD_SET_FDS_BITS *bytes = (TYPEOF_FD_SET_FDS_BITS *) fds; \
00061       /* 32bit: FD / 32 + ((FD + 1) % 32 ? 1 : 0) < 1024 */ \
00062       /* 64bit: FD / 64 + ((FD + 1) % 64 ? 1 : 0) < 512 */ \
00063       if (fd / _bitsize(*bytes) + ((fd + 1) % _bitsize(*bytes) ? 1 : 0) < sizeof(*(fds)) / SIZEOF_FD_SET_FDS_BITS) { \
00064          bytes[fd / _bitsize(*bytes)] |= ((TYPEOF_FD_SET_FDS_BITS) 1) << (fd % _bitsize(*bytes)); \
00065       } else { \
00066          fprintf(stderr, "FD %d exceeds the maximum size of ast_fdset!\n", fd); \
00067       } \
00068    } while (0)
00069 #endif /* HAVE_VARIABLE_FDSET */
00070 
00071 /*! \brief Waits for activity on a group of channels 
00072  * \param nfds the maximum number of file descriptors in the sets
00073  * \param rfds file descriptors to check for read availability
00074  * \param wfds file descriptors to check for write availability
00075  * \param efds file descriptors to check for exceptions (OOB data)
00076  * \param tvp timeout while waiting for events
00077  * This is the same as a standard select(), except it guarantees the
00078  * behaviour where the passed struct timeval is updated with how much
00079  * time was not slept while waiting for the specified events
00080  */
00081 static inline int ast_select(int nfds, ast_fdset *rfds, ast_fdset *wfds, ast_fdset *efds, struct timeval *tvp)
00082 {
00083 #ifdef __linux__
00084    return select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, tvp);
00085 #else
00086    int save_errno = 0;
00087    if (tvp) {
00088       struct timeval tv, tvstart, tvend, tvlen;
00089       int res;
00090 
00091       tv = *tvp;
00092       gettimeofday(&tvstart, NULL);
00093       res = select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, tvp);
00094       save_errno = errno;
00095       gettimeofday(&tvend, NULL);
00096       timersub(&tvend, &tvstart, &tvlen);
00097       timersub(&tv, &tvlen, tvp);
00098       if (tvp->tv_sec < 0 || (tvp->tv_sec == 0 && tvp->tv_usec < 0)) {
00099          tvp->tv_sec = 0;
00100          tvp->tv_usec = 0;
00101       }
00102       errno = save_errno;
00103       return res;
00104    }
00105    else
00106       return select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, NULL);
00107 #endif
00108 }
00109 
00110 #ifdef __cplusplus
00111 }
00112 #endif
00113 
00114 #endif /* __AST_SELECT_H */

Generated on Mon Mar 19 11:30:30 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7