diff --git a/src/omv/common/mutex.c b/src/omv/common/mutex.c index 323e9d307..9728e7f46 100644 --- a/src/omv/common/mutex.c +++ b/src/omv/common/mutex.c @@ -10,6 +10,7 @@ */ #include "mutex.h" #include "cmsis_gcc.h" +#include "py/mphal.h" // This is a standard implementation of mutexs on ARM processors following the ARM guide. // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0321a/BIHEJCHB.html @@ -45,8 +46,8 @@ int mutex_try_lock(mutex_t *mutex, uint32_t tid) { volatile int locked = 1; - // If mutex is already locked by the current thread then - // release the Kraken err.. the mutex, else attempt to lock it. + // If mutex is already locked by the current thread + // then release the the mutex, else attempt to lock it. if (mutex->tid == tid) { mutex_unlock(mutex, tid); } else if (__LDREXW(&mutex->lock) == 0) { @@ -63,6 +64,18 @@ int mutex_try_lock(mutex_t *mutex, uint32_t tid) return (locked == 0); } +int mutex_lock_timeout(mutex_t *mutex, uint32_t tid, uint32_t timeout) +{ + mp_uint_t tick_start = mp_hal_ticks_ms(); + while ((mp_hal_ticks_ms() - tick_start) >= timeout) { + if (mutex_try_lock(mutex, tid)) { + return 1; + } + __WFI(); + } + return 0; +} + void mutex_unlock(mutex_t *mutex, uint32_t tid) { if (mutex->tid == tid) { diff --git a/src/omv/common/mutex.h b/src/omv/common/mutex.h index bacaee9c8..02b93e100 100644 --- a/src/omv/common/mutex.h +++ b/src/omv/common/mutex.h @@ -21,5 +21,6 @@ typedef volatile struct { void mutex_init(mutex_t *mutex); void mutex_lock(mutex_t *mutex, uint32_t tid); int mutex_try_lock(mutex_t *mutex, uint32_t tid); +int mutex_lock_timeout(mutex_t *mutex, uint32_t tid, uint32_t timeout); void mutex_unlock(mutex_t *mutex, uint32_t tid); #endif /* __MUTEX_H__ */