From 4af44bee2c4a0567d77d76cbf73a4bb0ea75c536 Mon Sep 17 00:00:00 2001 From: Gavin Liu Date: Wed, 31 Jan 2024 14:00:12 +0800 Subject: [PATCH] core: mutex: add support timeout condvar Add support timeout condvar based on timeout notify Signed-off-by: Gavin Liu --- core/include/kernel/mutex.h | 6 ++++++ core/kernel/mutex.c | 22 ++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/core/include/kernel/mutex.h b/core/include/kernel/mutex.h index c701188d79c..fa9e12c68f3 100644 --- a/core/include/kernel/mutex.h +++ b/core/include/kernel/mutex.h @@ -95,10 +95,16 @@ void condvar_broadcast_debug(struct condvar *cv, const char *fname, int lineno); void condvar_wait_debug(struct condvar *cv, struct mutex *m, const char *fname, int lineno); #define condvar_wait(cv, m) condvar_wait_debug((cv), (m), __FILE__, __LINE__) + +uint32_t condvar_wait_timeout_debug(struct condvar *cv, struct mutex *m, uint32_t tmo, + const char *fname, int lineno); +#define condvar_wait_timeout(cv, m, tmo) \ + condvar_wait_timeout_debug((cv), (m), tmo, __FILE__, __LINE__) #else void condvar_signal(struct condvar *cv); void condvar_broadcast(struct condvar *cv); void condvar_wait(struct condvar *cv, struct mutex *m); +uint32_t condvar_wait_timeout(struct condvar *cv, struct mutex *m, uint32_t tmo); #endif #endif /*__KERNEL_MUTEX_H*/ diff --git a/core/kernel/mutex.c b/core/kernel/mutex.c index 45028c43181..5fab31b287d 100644 --- a/core/kernel/mutex.c +++ b/core/kernel/mutex.c @@ -393,13 +393,14 @@ void condvar_broadcast(struct condvar *cv) } #endif /*CFG_MUTEX_DEBUG*/ -static void __condvar_wait(struct condvar *cv, struct mutex *m, +static uint32_t __condvar_wait_timeout(struct condvar *cv, struct mutex *m, uint32_t tmo, const char *fname, int lineno) { uint32_t old_itr_status; struct wait_queue_elem wqe; short old_state; short new_state; + uint32_t ret = 0; mutex_unlock_check(m); @@ -434,23 +435,36 @@ static void __condvar_wait(struct condvar *cv, struct mutex *m, if (!new_state) wq_wake_next(&m->wq, m, fname, lineno); - wq_wait_final(&m->wq, &wqe, m, fname, lineno); + ret = wq_wait_final_timeout(&m->wq, &wqe, tmo, m, fname, lineno); if (old_state > 0) mutex_read_lock(m); else mutex_lock(m); + + return ret; } #ifdef CFG_MUTEX_DEBUG void condvar_wait_debug(struct condvar *cv, struct mutex *m, const char *fname, int lineno) { - __condvar_wait(cv, m, fname, lineno); + __condvar_wait_timeout(cv, m, 0, fname, lineno); +} + +uint32_t condvar_wait_timeout_debug(struct condvar *cv, struct mutex *m, uint32_t tmo, + const char *fname, int lineno) +{ + return __condvar_wait_timeout(cv, m, tmo, fname, lineno); } #else void condvar_wait(struct condvar *cv, struct mutex *m) { - __condvar_wait(cv, m, NULL, -1); + __condvar_wait_timeout(cv, m, 0, NULL, -1); +} + +uint32_t condvar_wait_timeout(struct condvar *cv, struct mutex *m, uint32_t tmo) +{ + return __condvar_wait_timeout(cv, m, tmo, NULL, -1); } #endif