Wed Jan 8 2020 09:49:51

Asterisk developer's documentation


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  /* 32bit: FD / 32 + ((FD + 1) % 32 ? 1 : 0) < 1024 */ \
62  /* 64bit: FD / 64 + ((FD + 1) % 64 ? 1 : 0) < 512 */ \
63  if (fd / _bitsize(*bytes) + ((fd + 1) % _bitsize(*bytes) ? 1 : 0) < sizeof(*(fds)) / SIZEOF_FD_SET_FDS_BITS) { \
64  bytes[fd / _bitsize(*bytes)] |= ((TYPEOF_FD_SET_FDS_BITS) 1) << (fd % _bitsize(*bytes)); \
65  } else { \
66  fprintf(stderr, "FD %d exceeds the maximum size of ast_fdset!\n", fd); \
67  } \
68  } while (0)
69 #endif /* HAVE_VARIABLE_FDSET */
70 
71 /*! \brief Waits for activity on a group of channels
72  * \param nfds the maximum number of file descriptors in the sets
73  * \param rfds file descriptors to check for read availability
74  * \param wfds file descriptors to check for write availability
75  * \param efds file descriptors to check for exceptions (OOB data)
76  * \param tvp timeout while waiting for events
77  * This is the same as a standard select(), except it guarantees the
78  * behaviour where the passed struct timeval is updated with how much
79  * time was not slept while waiting for the specified events
80  */
81 static inline int ast_select(int nfds, ast_fdset *rfds, ast_fdset *wfds, ast_fdset *efds, struct timeval *tvp)
82 {
83 #ifdef __linux__
84  return select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, tvp);
85 #else
86  int save_errno = 0;
87  if (tvp) {
88  struct timeval tv, tvstart, tvend, tvlen;
89  int res;
90 
91  tv = *tvp;
92  gettimeofday(&tvstart, NULL);
93  res = select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, tvp);
94  save_errno = errno;
95  gettimeofday(&tvend, NULL);
96  timersub(&tvend, &tvstart, &tvlen);
97  timersub(&tv, &tvlen, tvp);
98  if (tvp->tv_sec < 0 || (tvp->tv_sec == 0 && tvp->tv_usec < 0)) {
99  tvp->tv_sec = 0;
100  tvp->tv_usec = 0;
101  }
102  errno = save_errno;
103  return res;
104  }
105  else
106  return select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, NULL);
107 #endif
108 }
109 
110 #ifdef __cplusplus
111 }
112 #endif
113 
114 #endif /* __AST_SELECT_H */
#define ast_FDMAX
Definition: select.h:41
#define SIZEOF_FD_SET_FDS_BITS
Definition: autoconfig.h:1167
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:81
#define TYPEOF_FD_SET_FDS_BITS
Definition: autoconfig.h:1196
void timersub(struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
unsigned int ast_FD_SETSIZE
Definition: poll.c:86
General Definitions for Asterisk top level program Included by asterisk.h to handle platform-specific...
int errno