2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-09-29 01:24:55 -03:00
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
#include "Semaphores.h"
|
|
|
|
|
2013-09-28 18:49:30 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
using namespace Linux;
|
|
|
|
|
2018-08-19 22:09:05 -03:00
|
|
|
// construct a semaphore
|
|
|
|
Semaphore::Semaphore()
|
|
|
|
{
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
pthread_mutexattr_init(&attr);
|
2023-07-17 00:30:10 -03:00
|
|
|
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
|
2018-08-19 22:09:05 -03:00
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
pthread_mutex_init(&_lock, &attr);
|
|
|
|
}
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
bool Semaphore::give()
|
2013-09-28 18:49:30 -03:00
|
|
|
{
|
|
|
|
return pthread_mutex_unlock(&_lock) == 0;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
bool Semaphore::take(uint32_t timeout_ms)
|
2013-09-28 18:49:30 -03:00
|
|
|
{
|
2017-04-28 21:07:05 -03:00
|
|
|
if (timeout_ms == HAL_SEMAPHORE_BLOCK_FOREVER) {
|
2013-09-28 18:49:30 -03:00
|
|
|
return pthread_mutex_lock(&_lock) == 0;
|
|
|
|
}
|
2013-10-07 21:23:22 -03:00
|
|
|
if (take_nonblocking()) {
|
|
|
|
return true;
|
2013-09-28 18:49:30 -03:00
|
|
|
}
|
2015-11-19 23:10:58 -04:00
|
|
|
uint64_t start = AP_HAL::micros64();
|
2013-10-07 21:23:22 -03:00
|
|
|
do {
|
|
|
|
hal.scheduler->delay_microseconds(200);
|
|
|
|
if (take_nonblocking()) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-19 23:10:58 -04:00
|
|
|
} while ((AP_HAL::micros64() - start) < timeout_ms*1000);
|
2013-10-07 21:23:22 -03:00
|
|
|
return false;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
bool Semaphore::take_nonblocking()
|
2013-09-28 18:49:30 -03:00
|
|
|
{
|
|
|
|
return pthread_mutex_trylock(&_lock) == 0;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
2018-08-19 22:09:05 -03:00
|
|
|
|
2023-12-29 17:30:57 -04:00
|
|
|
/*
|
|
|
|
binary semaphore using pthread condition variables
|
|
|
|
*/
|
|
|
|
|
|
|
|
BinarySemaphore::BinarySemaphore(bool initial_state) :
|
|
|
|
AP_HAL::BinarySemaphore(initial_state)
|
|
|
|
{
|
|
|
|
pthread_cond_init(&cond, NULL);
|
|
|
|
pending = initial_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BinarySemaphore::wait(uint32_t timeout_us)
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(mtx);
|
|
|
|
if (!pending) {
|
|
|
|
struct timespec ts;
|
|
|
|
if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ts.tv_sec += timeout_us/1000000UL;
|
|
|
|
ts.tv_nsec += (timeout_us % 1000000U) * 1000UL;
|
|
|
|
if (ts.tv_nsec >= 1000000000L) {
|
|
|
|
ts.tv_sec++;
|
|
|
|
ts.tv_nsec -= 1000000000L;
|
|
|
|
}
|
|
|
|
if (pthread_cond_timedwait(&cond, &mtx._lock, &ts) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pending = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BinarySemaphore::wait_blocking(void)
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(mtx);
|
|
|
|
if (!pending) {
|
|
|
|
if (pthread_cond_wait(&cond, &mtx._lock) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pending = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySemaphore::signal(void)
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(mtx);
|
|
|
|
if (!pending) {
|
|
|
|
pending = true;
|
|
|
|
pthread_cond_signal(&cond);
|
|
|
|
}
|
|
|
|
}
|