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

Generated on Sat Aug 6 00:39:32 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7