unsigned int ms, limit_ms;
struct time start, now, end, delta, delta_max;
struct timeval delta_tv;
- bool overflow;
+ bool calls_started, overflow;
};
static void time_get(struct time *time)
time_add(&timeout->start, &timeout->delta, &timeout->end);
/* Disable limit unless timeout_limit() called. */
timeout->limit_ms = 0;
+ /* First blocking call has not yet been made. */
+ timeout->calls_started = false;
}
static void timeout_limit(struct timeout *timeout, unsigned int limit_ms)
static bool timeout_check(struct timeout *timeout)
{
+ if (!timeout->calls_started)
+ return false;
+
if (timeout->ms == 0)
return false;
return time_greater(&timeout->now, &timeout->end);
}
+static void timeout_update(struct timeout *timeout)
+{
+ timeout->calls_started = true;
+}
+
#ifndef _WIN32
static struct timeval *timeout_timeval(struct timeout *timeout)
{
size_t bytes_written = 0;
unsigned char *ptr = (unsigned char *) buf;
struct timeout timeout;
- int started = 0;
fd_set fds;
int result;
/* Loop until we have written the requested number of bytes. */
while (bytes_written < count) {
- /*
- * Check timeout only if we have run select() at least once,
- * to avoid any issues if a short timeout is reached before
- * select() is even run.
- */
- if (started && timeout_check(&timeout))
+
+ if (timeout_check(&timeout))
break;
result = select(port->fd + 1, NULL, &fds, NULL, timeout_timeval(&timeout));
- started = 1;
+
+ timeout_update(&timeout);
+
if (result < 0) {
if (errno == EINTR) {
DEBUG("select() call was interrupted, repeating");
size_t bytes_read = 0;
unsigned char *ptr = (unsigned char *) buf;
struct timeout timeout;
- int started = 0;
fd_set fds;
int result;
/* Loop until we have the requested number of bytes. */
while (bytes_read < count) {
- /*
- * Check timeout only if we have run select() at least once,
- * to avoid any issues if a short timeout is reached before
- * select() is even run.
- */
- if (started && timeout_check(&timeout))
+
+ if (timeout_check(&timeout))
/* Timeout has expired. */
break;
result = select(port->fd + 1, &fds, NULL, NULL, timeout_timeval(&timeout));
- started = 1;
+
+ timeout_update(&timeout);
+
if (result < 0) {
if (errno == EINTR) {
DEBUG("select() call was interrupted, repeating");
#else
size_t bytes_read = 0;
struct timeout timeout;
- int started = 0;
fd_set fds;
int result;
/* Loop until we have at least one byte, or timeout is reached. */
while (bytes_read == 0) {
- /*
- * Check timeout only if we have run select() at least once,
- * to avoid any issues if a short timeout is reached before
- * select() is even run.
- */
- if (started && timeout_check(&timeout))
+
+ if (timeout_check(&timeout))
/* Timeout has expired. */
break;
result = select(port->fd + 1, &fds, NULL, NULL, timeout_timeval(&timeout));
- started = 1;
+
+ timeout_update(&timeout);
+
if (result < 0) {
if (errno == EINTR) {
DEBUG("select() call was interrupted, repeating");
RETURN_OK();
#else
struct timeout timeout;
- int started = 0, result;
+ int result;
struct pollfd *pollfds;
unsigned int i;
/* Loop until an event occurs. */
while (1) {
- /*
- * Check timeout only if we have run poll() at least once,
- * to avoid any issues if a short timeout is reached before
- * poll() is even run.
- */
- if (started && timeout_check(&timeout)) {
+
+ if (timeout_check(&timeout)) {
DEBUG("Wait timed out");
break;
}
result = poll(pollfds, event_set->count, timeout_remaining_ms(&timeout) || -1);
- started = 1;
+
+ timeout_update(&timeout);
if (result < 0) {
if (errno == EINTR) {