diff --git a/sys/include/ztimer.h b/sys/include/ztimer.h index 3ee7356463..30b9428532 100644 --- a/sys/include/ztimer.h +++ b/sys/include/ztimer.h @@ -776,6 +776,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 Unlock mutex after @p timeout + * + * This function will unlock the given mutex after the timeout has passed. + * + * @param[in] clock ztimer clock to operate on + * @param[in] timer timer struct to use + * @param[in] timeout timeout in ztimer_clock's ticks + * @param[in] mutex mutex to unlock after timeout + */ +void ztimer_mutex_unlock(ztimer_clock_t *clock, ztimer_t *timer, + uint32_t timeout, mutex_t *mutex); + /** * @brief Try to lock the given mutex, but give up after @p timeout * diff --git a/sys/ztimer/util.c b/sys/ztimer/util.c index c01f8802fa..277f4854be 100644 --- a/sys/ztimer/util.c +++ b/sys/ztimer/util.c @@ -144,6 +144,18 @@ void ztimer_set_timeout_flag(ztimer_clock_t *clock, ztimer_t *t, } #endif +void ztimer_mutex_unlock(ztimer_clock_t *clock, ztimer_t *timer, uint32_t offset, + mutex_t *mutex) +{ + unsigned state = irq_disable(); + + timer->callback = _callback_unlock_mutex; + timer->arg = (void *)mutex; + + irq_restore(state); + ztimer_set(clock, timer, offset); +} + static void _callback_wakeup(void *arg) { thread_wakeup((kernel_pid_t)((intptr_t)arg));