00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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];
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 \
00061 \
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
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
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