mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Add mutex lock_timeout
This commit is contained in:
parent
8ce2bb6d9b
commit
be53435a8f
@ -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) {
|
||||
|
||||
@ -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__ */
|
||||
|
||||
Loading…
Reference in New Issue
Block a user