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>
This commit is contained in:
Jerome Forissier
2025-04-18 16:09:35 +02:00
committed by Tom Rini
parent f938479617
commit b01735b448
2 changed files with 87 additions and 0 deletions

View File

@@ -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;
}