1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/event: add event_wait_timeout_ztimer()

This commit is contained in:
Hauke Petersen 2021-01-18 17:37:02 +01:00
parent fa9a297e7c
commit 91859fc261
2 changed files with 58 additions and 7 deletions

View File

@ -29,7 +29,7 @@
#include "clist.h"
#include "thread.h"
#ifdef MODULE_XTIMER
#if IS_USED(MODULE_XTIMER)
#include "xtimer.h"
#endif
@ -96,8 +96,8 @@ event_t *event_wait_multi(event_queue_t *queues, size_t n_queues)
return result;
}
#ifdef MODULE_XTIMER
static event_t *_wait_timeout(event_queue_t *queue, xtimer_t *timer)
#if IS_USED(MODULE_XTIMER) || IS_USED(MODULE_ZTIMER)
static event_t *_wait_timeout(event_queue_t *queue)
{
assert(queue);
event_t *result;
@ -106,10 +106,19 @@ static event_t *_wait_timeout(event_queue_t *queue, xtimer_t *timer)
do {
result = event_get(queue);
if (result == NULL) {
flags = thread_flags_wait_any(THREAD_FLAG_EVENT | THREAD_FLAG_TIMEOUT);
flags = thread_flags_wait_any(THREAD_FLAG_EVENT |
THREAD_FLAG_TIMEOUT);
}
} while ((result == NULL) && (flags & THREAD_FLAG_EVENT));
return result;
}
#endif
#if IS_USED(MODULE_XTIMER)
static event_t *_wait_timeout_xtimer(event_queue_t *queue, xtimer_t *timer)
{
event_t *result = _wait_timeout(queue);
if (result) {
xtimer_remove(timer);
}
@ -121,15 +130,34 @@ event_t *event_wait_timeout(event_queue_t *queue, uint32_t timeout)
{
xtimer_t timer;
thread_flags_clear(THREAD_FLAG_TIMEOUT);
xtimer_set_timeout_flag(&timer, timeout);
return _wait_timeout(queue, &timer);
return _wait_timeout_xtimer(queue, &timer);
}
event_t *event_wait_timeout64(event_queue_t *queue, uint64_t timeout)
{
xtimer_t timer;
thread_flags_clear(THREAD_FLAG_TIMEOUT);
xtimer_set_timeout_flag64(&timer, timeout);
return _wait_timeout(queue, &timer);
return _wait_timeout_xtimer(queue, &timer);
}
#endif
#if IS_USED(MODULE_ZTIMER)
event_t *event_wait_timeout_ztimer(event_queue_t *queue,
ztimer_clock_t *clock, uint32_t timeout)
{
ztimer_t timer;
event_t *result;
thread_flags_clear(THREAD_FLAG_TIMEOUT);
ztimer_set_timeout_flag(clock, &timer, timeout);
result = _wait_timeout(queue);
if (result) {
ztimer_remove(clock, &timer);
}
return result;
}
#endif

View File

@ -106,6 +106,10 @@
#include "thread_flags.h"
#include "ptrtag.h"
#if IS_USED(MODULE_ZTIMER)
#include "ztimer.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
@ -333,7 +337,7 @@ static inline event_t *event_wait(event_queue_t *queue)
return event_wait_multi(queue, 1);
}
#if defined(MODULE_XTIMER) || defined(DOXYGEN)
#if IS_USED(MODULE_XTIMER) || defined(DOXYGEN)
/**
* @brief Get next event from event queue, blocking until timeout expires
*
@ -357,6 +361,25 @@ event_t *event_wait_timeout(event_queue_t *queue, uint32_t timeout);
event_t *event_wait_timeout64(event_queue_t *queue, uint64_t timeout);
#endif
#if IS_USED(MODULE_ZTIMER) || defined(DOXYGEN)
/**
* @brief Get next event from event queue, blocking until timeout expires
*
* This function is the same as event_wait_timeout() with the difference that it
* uses ztimer instead of xtimer as timer backend.
*
* @param[in] queue queue to query for an event
* @param[in] clock ztimer clock to use
* @param[in] timeout maximum time to wait for an event, time unit depends
* on the used ztimer clock
*
* @return pointer to next event if event was taken from the queue
* @return NULL if timeout expired before an event was posted
*/
event_t *event_wait_timeout_ztimer(event_queue_t *queue,
ztimer_clock_t *clock, uint32_t timeout);
#endif
/**
* @brief Simple event loop with multiple queues
*