[cm3/sync] Add mutex_trylock()

Allows non-blocking use in user code.
This commit is contained in:
Oliver Meier 2014-12-05 16:47:52 +01:00 committed by Karl Palsson
parent ad937e00fd
commit a2fe6c49a8
2 changed files with 20 additions and 0 deletions

View File

@ -47,6 +47,7 @@ typedef uint32_t mutex_t;
#define MUTEX_LOCKED 1
void mutex_lock(mutex_t *m);
uint32_t mutex_trylock(mutex_t *m);
void mutex_unlock(mutex_t *m);
#endif

View File

@ -61,6 +61,25 @@ void mutex_lock(mutex_t *m)
__dmb();
}
/* returns 1 if the lock was acquired */
uint32_t mutex_trylock(mutex_t *m)
{
uint32_t status = 0;
/* If the mutex is unlocked. */
if (__ldrex(m) == MUTEX_UNLOCKED) {
/* Try to lock it. */
status = __strex(MUTEX_LOCKED, m);
}
/* Execute the mysterious Data Memory Barrier instruction! */
__dmb();
/* Did we get the lock? If not then try again
* by calling this function once more. */
return status == 0;
}
void mutex_unlock(mutex_t *m)
{
/* Ensure accesses to protected resource are finished */