Each time we run through the poll loop, we check all file descriptors
that we were waiting on to see if there is data available. However,
this requires a system call and poll already provides information on
which FDs caused the wakeup so it is inefficient as the number of
active FDs grows. This provides a way to check whether a given FD
has data.
poll_fd_wait(sock->fd, events);
}
poll_fd_wait(sock->fd, events);
}
+/* Checks whether this socket caused a wakeup in the previous run of the poll
+ * loop. */
+short int
+nl_sock_woke(const struct nl_sock *sock)
+{
+ return poll_fd_woke(sock->fd);
+}
+
/* Returns the PID associated with this socket. */
uint32_t
nl_sock_pid(const struct nl_sock *sock)
/* Returns the PID associated with this socket. */
uint32_t
nl_sock_pid(const struct nl_sock *sock)
int nl_sock_drain(struct nl_sock *);
void nl_sock_wait(const struct nl_sock *, short int events);
int nl_sock_drain(struct nl_sock *);
void nl_sock_wait(const struct nl_sock *, short int events);
+short int nl_sock_woke(const struct nl_sock *);
uint32_t nl_sock_pid(const struct nl_sock *);
uint32_t nl_sock_pid(const struct nl_sock *);
/* Location where waiter created. */
static const char *timeout_where;
/* Location where waiter created. */
static const char *timeout_where;
+/* Array of file descriptors from last run of poll_block(). */
+static struct pollfd *pollfds;
+
+/* Allocated size of pollfds. */
+static size_t max_pollfds;
+
+/* Current number of elements in pollfds. */
+static int n_pollfds;
+
static struct poll_waiter *new_waiter(int fd, short int events,
const char *where);
static struct poll_waiter *new_waiter(int fd, short int events,
const char *where);
- static struct pollfd *pollfds;
- static size_t max_pollfds;
-
struct poll_waiter *pw, *next;
struct poll_waiter *pw, *next;
- int n_waiters, n_pollfds;
int retval;
/* Register fatal signal events before actually doing any real work for
int retval;
/* Register fatal signal events before actually doing any real work for
+
+/* Checks whether the given file descriptor caused the poll loop to wake up
+ * in the previous iteration. If it did, returns a bitmask of the events
+ * that caused the wakeup. Otherwise returns 0;
+ */
+short int
+poll_fd_woke(int fd)
+{
+ int i;
+ short int events = 0;
+
+ for (i = 0; i < n_pollfds; i++) {
+ if (pollfds[i].fd == fd) {
+ events |= pollfds[i].revents;
+ }
+ }
+
+ return events;
+}
\f
/* Creates and returns a new poll_waiter for 'fd' and 'events'. */
static struct poll_waiter *
\f
/* Creates and returns a new poll_waiter for 'fd' and 'events'. */
static struct poll_waiter *
/* Cancel a file descriptor callback or event. */
void poll_cancel(struct poll_waiter *);
/* Cancel a file descriptor callback or event. */
void poll_cancel(struct poll_waiter *);
+/* Checks whether the file descriptor caused a wakeup. */
+short int poll_fd_woke(int fd);
+
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
}
#endif