#include #include #include #include #include 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; };