ardupilot/libraries/AP_HAL_SITL/Semaphores.cpp

40 lines
800 B
C++
Raw Normal View History

2015-12-20 16:33:11 -04:00
#include <AP_HAL/AP_HAL.h>
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
#include "Semaphores.h"
extern const AP_HAL::HAL& hal;
using namespace HALSITL;
2017-01-09 08:23:23 -04:00
bool Semaphore::give()
2015-12-20 16:33:11 -04:00
{
return pthread_mutex_unlock(&_lock) == 0;
}
2017-01-09 08:23:23 -04:00
bool Semaphore::take(uint32_t timeout_ms)
2015-12-20 16:33:11 -04:00
{
if (timeout_ms == HAL_SEMAPHORE_BLOCK_FOREVER) {
2015-12-20 16:33:11 -04:00
return pthread_mutex_lock(&_lock) == 0;
}
if (take_nonblocking()) {
return true;
}
uint64_t start = AP_HAL::micros64();
do {
hal.scheduler->delay_microseconds(200);
if (take_nonblocking()) {
return true;
}
2017-01-09 08:23:23 -04:00
} while ((AP_HAL::micros64() - start) < timeout_ms * 1000);
2015-12-20 16:33:11 -04:00
return false;
}
2017-01-09 08:23:23 -04:00
bool Semaphore::take_nonblocking()
2015-12-20 16:33:11 -04:00
{
return pthread_mutex_trylock(&_lock) == 0;
}
2017-01-09 08:23:23 -04:00
#endif // CONFIG_HAL_BOARD