avr-fw-modules/core/src/mutex_lock.c

52 lines
872 B
C
Executable File

#include <sys/mutex.h>
#include <sys/atomic.h>
#include <sys/systick.h>
#include <sys/errno.h>
#include <stdlib.h>
APICALL mutex_lock(MUTEX *mutex)
{
if (!mutex)
return -1;
while (1)
{
{
ATOMIC
if ((mutex->thread == NULL) || (mutex->thread == current_thread()))
{
mutex->thread = current_thread();
mutex->locked++;
return 0;
};
};
wait_ms(0);
};
};
APICALL mutex_lock_timeout(MUTEX *mutex,int32_t timeout){
systick_t timeend = systick_ticks() + timeout;
if (!mutex)
return -1;
while ( (timeout == -1) || (systick_ticks() < timeend) ){
{
ATOMIC
if (((volatile avrThread*)mutex->thread == NULL) || ((volatile avrThread*)mutex->thread == (volatile avrThread*)current_thread()))
{
mutex->thread = current_thread();
mutex->locked++;
return 0;
};
};
wait_ms(0);
};
return -ETIMEOUT;
};