Skip to content

Commit

Permalink
core: mutex: add support timeout condvar
Browse files Browse the repository at this point in the history
Add support timeout condvar based on timeout notify

Signed-off-by: Gavin Liu <gavin.liu@mediatek.com>
  • Loading branch information
gavin-liu106 committed Jan 31, 2024
1 parent 6a09eb4 commit 4af44be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions core/include/kernel/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down
22 changes: 18 additions & 4 deletions core/kernel/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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

0 comments on commit 4af44be

Please sign in to comment.