Skip to content

Commit

Permalink
sys/isrpipe/read_timeout: don't set up timer if data is available
Browse files Browse the repository at this point in the history
  • Loading branch information
derMihai committed Apr 8, 2024
1 parent e6bd633 commit 016f074
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions sys/isrpipe/read_timeout/read_timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ static void _cb(void *arg)

int isrpipe_read_timeout(isrpipe_t *isrpipe, uint8_t *buffer, size_t count, uint32_t timeout)
{
int res;
int res = tsrb_get(&isrpipe->tsrb, buffer, count);
if (res > 0) {
return res;
}

_isrpipe_timeout_t _timeout = { .mutex = &isrpipe->mutex, .flag = 0 };

ztimer_t timer = { .callback = _cb, .arg = &_timeout };

ztimer_set(ZTIMER_USEC, &timer, timeout);
while (!(res = tsrb_get(&isrpipe->tsrb, buffer, count))) {
while ((res = tsrb_get(&isrpipe->tsrb, buffer, count)) == 0) {
mutex_lock(&isrpipe->mutex);
if (_timeout.flag) {
res = -ETIMEDOUT;
break;
/* timer was consumed */
return -ETIMEDOUT;
}
}

Expand Down

0 comments on commit 016f074

Please sign in to comment.