Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
poll.c File Reference
#include "asterisk.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include "asterisk/utils.h"
#include "asterisk/poll-compat.h"
Include dependency graph for poll.c:

Go to the source code of this file.

Functions

int ast_poll2 (struct pollfd *pArray, unsigned long n_fds, struct timeval *tv)
 Same as poll(2), except the time is specified in microseconds and the tv argument is modified to indicate the time remaining. More...
 

Variables

unsigned int ast_FD_SETSIZE = FD_SETSIZE
 

Function Documentation

◆ ast_poll2()

int ast_poll2 ( struct pollfd *  pArray,
unsigned long  n_fds,
struct timeval *  tv 
)

Same as poll(2), except the time is specified in microseconds and the tv argument is modified to indicate the time remaining.

Definition at line 268 of file poll.c.

References ast_select(), ast_tv(), ast_tvadd(), ast_tvdiff_ms(), ast_tvnow(), ast_tvsub(), FD_ZERO, and NULL.

Referenced by AST_TEST_DEFINE(), and do_monitor().

269 {
270 #if !defined(AST_POLL_COMPAT)
271  struct timeval start = ast_tvnow();
272 #if defined(HAVE_PPOLL)
273  struct timespec ts = { tv ? tv->tv_sec : 0, tv ? tv->tv_usec * 1000 : 0 };
274  int res = ppoll(pArray, n_fds, tv ? &ts : NULL, NULL);
275 #else
276  int res = poll(pArray, n_fds, tv ? tv->tv_sec * 1000 + tv->tv_usec / 1000 : -1);
277 #endif
278  struct timeval after = ast_tvnow();
279  if (res > 0 && tv && ast_tvdiff_ms(ast_tvadd(*tv, start), after) > 0) {
280  *tv = ast_tvsub(*tv, ast_tvsub(after, start));
281  } else if (res > 0 && tv) {
282  *tv = ast_tv(0, 0);
283  }
284  return res;
285 #else
286  ast_fdset read_descs, write_descs, except_descs;
287  int ready_descriptors, max_fd = 0;
288 
289  FD_ZERO(&read_descs);
290  FD_ZERO(&write_descs);
291  FD_ZERO(&except_descs);
292 
293  if (pArray) {
294  max_fd = map_poll_spec(pArray, n_fds, &read_descs, &write_descs, &except_descs);
295  }
296 
297  ready_descriptors = ast_select(max_fd + 1, &read_descs, &write_descs, &except_descs, tv);
298 
299  if (ready_descriptors >= 0) {
300  map_select_results(pArray, n_fds, &read_descs, &write_descs, &except_descs);
301  }
302 
303  return ready_descriptors;
304 #endif
305 }
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
#define NULL
Definition: resample.c:96
#define FD_ZERO(a)
Definition: select.h:49
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
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2283
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:226
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: extconf.c:2298

Variable Documentation

◆ ast_FD_SETSIZE

unsigned int ast_FD_SETSIZE = FD_SETSIZE

Definition at line 86 of file poll.c.

Referenced by main().