diff options
author | Jerome Forissier <jerome.forissier@linaro.org> | 2025-04-18 16:09:35 +0200 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2025-04-23 13:19:44 -0600 |
commit | b01735b448bfdd39bb0b0f8f28db149d8a088e55 (patch) | |
tree | 310f4c328af5fd18463bf293966f51ba7dd5617e /lib | |
parent | f9384796179abcc7e5796815c79b2137f5f83b12 (diff) |
uthread: add uthread_mutex
Add struct uthread_mutex and uthread_mutex_lock(),
uthread_mutex_trylock(), uthread_mutex_unlock() to protect shared data
structures from concurrent modifications.
Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/uthread.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/uthread.c b/lib/uthread.c index aa264b1d95f..062fca7d209 100644 --- a/lib/uthread.c +++ b/lib/uthread.c @@ -8,6 +8,7 @@ */ #include <compiler.h> +#include <linux/errno.h> #include <linux/kernel.h> #include <linux/list.h> #include <malloc.h> @@ -136,3 +137,29 @@ bool uthread_grp_done(unsigned int grp_id) return true; } + +int uthread_mutex_lock(struct uthread_mutex *mutex) +{ + while (mutex->state == UTHREAD_MUTEX_LOCKED) + uthread_schedule(); + + mutex->state = UTHREAD_MUTEX_LOCKED; + return 0; +} + +int uthread_mutex_trylock(struct uthread_mutex *mutex) +{ + if (mutex->state == UTHREAD_MUTEX_UNLOCKED) { + mutex->state = UTHREAD_MUTEX_LOCKED; + return 0; + } + + return -EBUSY; +} + +int uthread_mutex_unlock(struct uthread_mutex *mutex) +{ + mutex->state = UTHREAD_MUTEX_UNLOCKED; + + return 0; +} |