From 016f074d7e1998514aedc79b8343113baa087ca2 Mon Sep 17 00:00:00 2001 From: Mihai Renea Date: Mon, 8 Apr 2024 16:09:31 +0200 Subject: [PATCH] sys/isrpipe/read_timeout: don't set up timer if data is available --- sys/isrpipe/read_timeout/read_timeout.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/sys/isrpipe/read_timeout/read_timeout.c b/sys/isrpipe/read_timeout/read_timeout.c index 3a5c6b98a090..2f546a734657 100644 --- a/sys/isrpipe/read_timeout/read_timeout.c +++ b/sys/isrpipe/read_timeout/read_timeout.c @@ -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; } }