mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
sys/ztimer: Add ztimer_mutex_lock_timeout()
This commit is contained in:
parent
1feacd1b88
commit
71a0c53df2
@ -236,6 +236,7 @@
|
||||
|
||||
#include "sched.h"
|
||||
#include "msg.h"
|
||||
#include "mutex.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -507,6 +508,19 @@ void ztimer_set_wakeup(ztimer_clock_t *clock, ztimer_t *timer, uint32_t offset,
|
||||
void ztimer_set_timeout_flag(ztimer_clock_t *clock, ztimer_t *timer,
|
||||
uint32_t timeout);
|
||||
|
||||
/**
|
||||
* @brief Try to lock the given mutex, but give up after @p timeout
|
||||
*
|
||||
* @param[in] clock ztimer clock to operate on
|
||||
* @param[in,out] mutex Mutex object to lock
|
||||
* @param[in] timeout timeout after which to give up
|
||||
*
|
||||
* @retval 0 Success, caller has the mutex
|
||||
* @retval -ECANCELED Failed to obtain mutex within @p timeout
|
||||
*/
|
||||
int ztimer_mutex_lock_timeout(ztimer_clock_t *clock, mutex_t *mutex,
|
||||
uint32_t timeout);
|
||||
|
||||
/**
|
||||
* @brief Update ztimer clock head list offset
|
||||
*
|
||||
|
@ -163,3 +163,26 @@ void ztimer_set_wakeup(ztimer_clock_t *clock, ztimer_t *timer, uint32_t offset,
|
||||
|
||||
ztimer_set(clock, timer, offset);
|
||||
}
|
||||
|
||||
static void timeout_cb(void *arg) {
|
||||
mutex_cancel(arg);
|
||||
}
|
||||
|
||||
int ztimer_mutex_lock_timeout(ztimer_clock_t *clock, mutex_t *mutex,
|
||||
uint32_t timeout)
|
||||
{
|
||||
if (mutex_trylock(mutex)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
mutex_cancel_t mc = mutex_cancel_init(mutex);
|
||||
ztimer_t t = { .callback = timeout_cb, .arg = &mc };
|
||||
|
||||
ztimer_set(clock, &t, timeout);
|
||||
if (mutex_lock_cancelable(&mc)) {
|
||||
return -ECANCELED;
|
||||
}
|
||||
|
||||
ztimer_remove(clock, &t);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user