mirror of
git://sigrok.org/libserialport
synced 2023-08-10 21:13:24 +03:00
Move commonly used start flag into timeout helpers.
This commit is contained in:
parent
9d1ca7c855
commit
bd72614f08
64
serialport.c
64
serialport.c
@ -69,7 +69,7 @@ struct timeout {
|
|||||||
unsigned int ms, limit_ms;
|
unsigned int ms, limit_ms;
|
||||||
struct time start, now, end, delta, delta_max;
|
struct time start, now, end, delta, delta_max;
|
||||||
struct timeval delta_tv;
|
struct timeval delta_tv;
|
||||||
bool overflow;
|
bool calls_started, overflow;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void time_get(struct time *time)
|
static void time_get(struct time *time)
|
||||||
@ -173,6 +173,8 @@ static void timeout_start(struct timeout *timeout, unsigned int timeout_ms)
|
|||||||
time_add(&timeout->start, &timeout->delta, &timeout->end);
|
time_add(&timeout->start, &timeout->delta, &timeout->end);
|
||||||
/* Disable limit unless timeout_limit() called. */
|
/* Disable limit unless timeout_limit() called. */
|
||||||
timeout->limit_ms = 0;
|
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 void timeout_limit(struct timeout *timeout, unsigned int limit_ms)
|
||||||
@ -184,6 +186,9 @@ static void timeout_limit(struct timeout *timeout, unsigned int limit_ms)
|
|||||||
|
|
||||||
static bool timeout_check(struct timeout *timeout)
|
static bool timeout_check(struct timeout *timeout)
|
||||||
{
|
{
|
||||||
|
if (!timeout->calls_started)
|
||||||
|
return false;
|
||||||
|
|
||||||
if (timeout->ms == 0)
|
if (timeout->ms == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -196,6 +201,11 @@ static bool timeout_check(struct timeout *timeout)
|
|||||||
return time_greater(&timeout->now, &timeout->end);
|
return time_greater(&timeout->now, &timeout->end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void timeout_update(struct timeout *timeout)
|
||||||
|
{
|
||||||
|
timeout->calls_started = true;
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
static struct timeval *timeout_timeval(struct timeout *timeout)
|
static struct timeval *timeout_timeval(struct timeout *timeout)
|
||||||
{
|
{
|
||||||
@ -983,7 +993,6 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
|
|||||||
size_t bytes_written = 0;
|
size_t bytes_written = 0;
|
||||||
unsigned char *ptr = (unsigned char *) buf;
|
unsigned char *ptr = (unsigned char *) buf;
|
||||||
struct timeout timeout;
|
struct timeout timeout;
|
||||||
int started = 0;
|
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
@ -994,16 +1003,14 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
|
|||||||
|
|
||||||
/* Loop until we have written the requested number of bytes. */
|
/* Loop until we have written the requested number of bytes. */
|
||||||
while (bytes_written < count) {
|
while (bytes_written < count) {
|
||||||
/*
|
|
||||||
* Check timeout only if we have run select() at least once,
|
if (timeout_check(&timeout))
|
||||||
* to avoid any issues if a short timeout is reached before
|
|
||||||
* select() is even run.
|
|
||||||
*/
|
|
||||||
if (started && timeout_check(&timeout))
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
result = select(port->fd + 1, NULL, &fds, NULL, timeout_timeval(&timeout));
|
result = select(port->fd + 1, NULL, &fds, NULL, timeout_timeval(&timeout));
|
||||||
started = 1;
|
|
||||||
|
timeout_update(&timeout);
|
||||||
|
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
DEBUG("select() call was interrupted, repeating");
|
DEBUG("select() call was interrupted, repeating");
|
||||||
@ -1191,7 +1198,6 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
|
|||||||
size_t bytes_read = 0;
|
size_t bytes_read = 0;
|
||||||
unsigned char *ptr = (unsigned char *) buf;
|
unsigned char *ptr = (unsigned char *) buf;
|
||||||
struct timeout timeout;
|
struct timeout timeout;
|
||||||
int started = 0;
|
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
@ -1202,17 +1208,15 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
|
|||||||
|
|
||||||
/* Loop until we have the requested number of bytes. */
|
/* Loop until we have the requested number of bytes. */
|
||||||
while (bytes_read < count) {
|
while (bytes_read < count) {
|
||||||
/*
|
|
||||||
* Check timeout only if we have run select() at least once,
|
if (timeout_check(&timeout))
|
||||||
* to avoid any issues if a short timeout is reached before
|
|
||||||
* select() is even run.
|
|
||||||
*/
|
|
||||||
if (started && timeout_check(&timeout))
|
|
||||||
/* Timeout has expired. */
|
/* Timeout has expired. */
|
||||||
break;
|
break;
|
||||||
|
|
||||||
result = select(port->fd + 1, &fds, NULL, NULL, timeout_timeval(&timeout));
|
result = select(port->fd + 1, &fds, NULL, NULL, timeout_timeval(&timeout));
|
||||||
started = 1;
|
|
||||||
|
timeout_update(&timeout);
|
||||||
|
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
DEBUG("select() call was interrupted, repeating");
|
DEBUG("select() call was interrupted, repeating");
|
||||||
@ -1317,7 +1321,6 @@ SP_API enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf,
|
|||||||
#else
|
#else
|
||||||
size_t bytes_read = 0;
|
size_t bytes_read = 0;
|
||||||
struct timeout timeout;
|
struct timeout timeout;
|
||||||
int started = 0;
|
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
@ -1328,17 +1331,15 @@ SP_API enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf,
|
|||||||
|
|
||||||
/* Loop until we have at least one byte, or timeout is reached. */
|
/* Loop until we have at least one byte, or timeout is reached. */
|
||||||
while (bytes_read == 0) {
|
while (bytes_read == 0) {
|
||||||
/*
|
|
||||||
* Check timeout only if we have run select() at least once,
|
if (timeout_check(&timeout))
|
||||||
* to avoid any issues if a short timeout is reached before
|
|
||||||
* select() is even run.
|
|
||||||
*/
|
|
||||||
if (started && timeout_check(&timeout))
|
|
||||||
/* Timeout has expired. */
|
/* Timeout has expired. */
|
||||||
break;
|
break;
|
||||||
|
|
||||||
result = select(port->fd + 1, &fds, NULL, NULL, timeout_timeval(&timeout));
|
result = select(port->fd + 1, &fds, NULL, NULL, timeout_timeval(&timeout));
|
||||||
started = 1;
|
|
||||||
|
timeout_update(&timeout);
|
||||||
|
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
DEBUG("select() call was interrupted, repeating");
|
DEBUG("select() call was interrupted, repeating");
|
||||||
@ -1589,7 +1590,7 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set,
|
|||||||
RETURN_OK();
|
RETURN_OK();
|
||||||
#else
|
#else
|
||||||
struct timeout timeout;
|
struct timeout timeout;
|
||||||
int started = 0, result;
|
int result;
|
||||||
struct pollfd *pollfds;
|
struct pollfd *pollfds;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
@ -1613,18 +1614,15 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set,
|
|||||||
|
|
||||||
/* Loop until an event occurs. */
|
/* Loop until an event occurs. */
|
||||||
while (1) {
|
while (1) {
|
||||||
/*
|
|
||||||
* Check timeout only if we have run poll() at least once,
|
if (timeout_check(&timeout)) {
|
||||||
* to avoid any issues if a short timeout is reached before
|
|
||||||
* poll() is even run.
|
|
||||||
*/
|
|
||||||
if (started && timeout_check(&timeout)) {
|
|
||||||
DEBUG("Wait timed out");
|
DEBUG("Wait timed out");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = poll(pollfds, event_set->count, timeout_remaining_ms(&timeout) || -1);
|
result = poll(pollfds, event_set->count, timeout_remaining_ms(&timeout) || -1);
|
||||||
started = 1;
|
|
||||||
|
timeout_update(&timeout);
|
||||||
|
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
|
Loading…
Reference in New Issue
Block a user