Asterisk - The Open Source Telephony Project  18.5.0
select.h
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * Tilghman Lesher <tlesher AT digium DOT com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!\file
20  * \brief Bitfield expansions for ast_select
21  */
22 
23 #ifndef __AST_SELECT_H
24 #define __AST_SELECT_H
25 
26 #include <sys/time.h>
27 #include <sys/select.h>
28 #include <errno.h>
29 
30 #include "asterisk/compat.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 extern unsigned int ast_FD_SETSIZE;
37 
38 #if !defined(HAVE_VARIABLE_FDSET) && defined(CONFIGURE_RAN_AS_ROOT)
39 #define ast_fdset fd_set
40 #else
41 #define ast_FDMAX 32768
42 typedef struct {
43  TYPEOF_FD_SET_FDS_BITS fds_bits[ast_FDMAX / 8 / SIZEOF_FD_SET_FDS_BITS]; /* 32768 bits */
44 } ast_fdset;
45 
46 #define _bitsize(a) (sizeof(a) * 8)
47 
48 #undef FD_ZERO
49 #define FD_ZERO(a) \
50  do { \
51  TYPEOF_FD_SET_FDS_BITS *bytes = (TYPEOF_FD_SET_FDS_BITS *) a; \
52  int i; \
53  for (i = 0; i < ast_FDMAX / _bitsize(TYPEOF_FD_SET_FDS_BITS); i++) { \
54  bytes[i] = 0; \
55  } \
56  } while (0)
57 #undef FD_SET
58 #define FD_SET(fd, fds) \
59  do { \
60  TYPEOF_FD_SET_FDS_BITS *bytes = (TYPEOF_FD_SET_FDS_BITS *) fds; \
61  if (fd < ast_FDMAX) { \
62  bytes[fd / _bitsize(*bytes)] |= ((TYPEOF_FD_SET_FDS_BITS) 1) << (fd % _bitsize(*bytes)); \
63  } else { \
64  fprintf(stderr, "FD %d exceeds the maximum size of ast_fdset!\n", fd); \
65  } \
66  } while (0)
67 #endif /* HAVE_VARIABLE_FDSET */
68 
69 /*! \brief Waits for activity on a group of channels
70  * \param nfds the maximum number of file descriptors in the sets
71  * \param rfds file descriptors to check for read availability
72  * \param wfds file descriptors to check for write availability
73  * \param efds file descriptors to check for exceptions (OOB data)
74  * \param tvp timeout while waiting for events
75  * This is the same as a standard select(), except it guarantees the
76  * behaviour where the passed struct timeval is updated with how much
77  * time was not slept while waiting for the specified events
78  */
79 static inline int ast_select(int nfds, ast_fdset *rfds, ast_fdset *wfds, ast_fdset *efds, struct timeval *tvp)
80 {
81 #ifdef __linux__
82  return select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, tvp);
83 #else
84  int save_errno = 0;
85  if (tvp) {
86  struct timeval tv, tvstart, tvend, tvlen;
87  int res;
88 
89  tv = *tvp;
90  gettimeofday(&tvstart, NULL);
91  res = select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, tvp);
92  save_errno = errno;
93  gettimeofday(&tvend, NULL);
94  timersub(&tvend, &tvstart, &tvlen);
95  timersub(&tv, &tvlen, tvp);
96  if (tvp->tv_sec < 0 || (tvp->tv_sec == 0 && tvp->tv_usec < 0)) {
97  tvp->tv_sec = 0;
98  tvp->tv_usec = 0;
99  }
100  errno = save_errno;
101  return res;
102  }
103  else
104  return select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, NULL);
105 #endif
106 }
107 
108 #ifdef __cplusplus
109 }
110 #endif
111 
112 #endif /* __AST_SELECT_H */
#define ast_FDMAX
Definition: select.h:41
#define NULL
Definition: resample.c:96
void timersub(struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
#define SIZEOF_FD_SET_FDS_BITS
Definition: autoconfig.h:1408
static int ast_select(int nfds, ast_fdset *rfds, ast_fdset *wfds, ast_fdset *efds, struct timeval *tvp)
Waits for activity on a group of channels.
Definition: select.h:79
#define TYPEOF_FD_SET_FDS_BITS
Definition: autoconfig.h:1437
unsigned int ast_FD_SETSIZE
Definition: poll.c:86
int errno
General Definitions for Asterisk top level program Included by asterisk.h to handle platform-specific...