2015-12-20 16:33:11 -04:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
|
|
|
|
#include "Semaphores.h"
|
2019-09-02 03:00:16 -03:00
|
|
|
#include "Scheduler.h"
|
2015-12-20 16:33:11 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
using namespace HALSITL;
|
|
|
|
|
2018-08-19 22:18:27 -03:00
|
|
|
// construct a semaphore
|
|
|
|
Semaphore::Semaphore()
|
|
|
|
{
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
pthread_mutexattr_init(&attr);
|
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
pthread_mutex_init(&_lock, &attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-09 08:23:23 -04:00
|
|
|
bool Semaphore::give()
|
2015-12-20 16:33:11 -04:00
|
|
|
{
|
2020-11-12 03:45:09 -04:00
|
|
|
take_count--;
|
2018-08-19 22:18:27 -03:00
|
|
|
if (pthread_mutex_unlock(&_lock) != 0) {
|
|
|
|
AP_HAL::panic("Bad semaphore usage");
|
|
|
|
}
|
2020-11-12 03:45:09 -04:00
|
|
|
if (take_count == 0) {
|
|
|
|
owner = (pthread_t)-1;
|
|
|
|
}
|
2018-08-19 22:18:27 -03:00
|
|
|
return true;
|
2015-12-20 16:33:11 -04:00
|
|
|
}
|
|
|
|
|
2021-02-01 12:26:28 -04:00
|
|
|
void Semaphore::check_owner() const
|
2020-10-11 22:24:49 -03:00
|
|
|
{
|
|
|
|
// should probably make sure we're holding the semaphore here....
|
|
|
|
if (owner != pthread_self()) {
|
|
|
|
AP_HAL::panic("Wrong owner");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 08:23:23 -04:00
|
|
|
bool Semaphore::take(uint32_t timeout_ms)
|
2015-12-20 16:33:11 -04:00
|
|
|
{
|
2017-04-28 21:07:28 -03:00
|
|
|
if (timeout_ms == HAL_SEMAPHORE_BLOCK_FOREVER) {
|
2020-10-11 22:24:49 -03:00
|
|
|
if (pthread_mutex_lock(&_lock) == 0) {
|
|
|
|
owner = pthread_self();
|
2020-11-12 03:45:09 -04:00
|
|
|
take_count++;
|
2020-10-11 22:24:49 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-12-20 16:33:11 -04:00
|
|
|
}
|
|
|
|
if (take_nonblocking()) {
|
2020-10-11 22:24:49 -03:00
|
|
|
owner = pthread_self();
|
2015-12-20 16:33:11 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
uint64_t start = AP_HAL::micros64();
|
|
|
|
do {
|
2019-09-02 03:00:16 -03:00
|
|
|
Scheduler::from(hal.scheduler)->set_in_semaphore_take_wait(true);
|
2015-12-20 16:33:11 -04:00
|
|
|
hal.scheduler->delay_microseconds(200);
|
2019-09-02 03:00:16 -03:00
|
|
|
Scheduler::from(hal.scheduler)->set_in_semaphore_take_wait(false);
|
2015-12-20 16:33:11 -04:00
|
|
|
if (take_nonblocking()) {
|
2020-10-11 22:24:49 -03:00
|
|
|
owner = pthread_self();
|
2015-12-20 16:33:11 -04:00
|
|
|
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
|
|
|
{
|
2020-11-12 03:45:09 -04:00
|
|
|
if (pthread_mutex_trylock(&_lock) == 0) {
|
|
|
|
owner = pthread_self();
|
|
|
|
take_count++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-12-20 16:33:11 -04:00
|
|
|
}
|
|
|
|
|
2017-01-09 08:23:23 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|