Skip to content

Commit

Permalink
Merge pull request #20558 from derMihai/mir/isrpipe_timeout
Browse files Browse the repository at this point in the history
sys/isrpipe/read_timeout: don't set up timer if data is available
  • Loading branch information
benpicco authored Apr 9, 2024
2 parents 6595936 + 016f074 commit f5e6677
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 f5e6677

Please sign in to comment.