2013-01-03 19:41:36 -04:00
|
|
|
|
|
|
|
#include "Semaphores.h"
|
|
|
|
|
2013-01-09 17:32:34 -04:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SMACCM
|
|
|
|
|
2013-01-10 17:52:30 -04:00
|
|
|
#include <task.h>
|
|
|
|
|
2013-01-03 19:41:36 -04:00
|
|
|
using namespace SMACCM;
|
|
|
|
|
2013-01-10 17:16:43 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
/** Return true if this thread already holds "sem". */
|
|
|
|
static bool already_held(xSemaphoreHandle sem)
|
|
|
|
{
|
|
|
|
xTaskHandle self = xTaskGetCurrentTaskHandle();
|
|
|
|
return xSemaphoreGetMutexHolder(sem) == self;
|
|
|
|
}
|
|
|
|
|
2013-01-03 19:41:36 -04:00
|
|
|
SMACCMSemaphore::SMACCMSemaphore()
|
|
|
|
: m_semaphore(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMACCMSemaphore::init()
|
|
|
|
{
|
|
|
|
m_semaphore = xSemaphoreCreateMutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SMACCMSemaphore::take(uint32_t timeout_ms)
|
|
|
|
{
|
|
|
|
portTickType delay;
|
|
|
|
|
2013-01-10 17:16:43 -04:00
|
|
|
if (already_held(m_semaphore))
|
|
|
|
hal.scheduler->panic("PANIC: Recursive semaphore take.");
|
|
|
|
|
2013-01-03 19:41:36 -04:00
|
|
|
if (timeout_ms == HAL_SEMAPHORE_BLOCK_FOREVER)
|
|
|
|
delay = portMAX_DELAY;
|
|
|
|
else
|
|
|
|
delay = timeout_ms / portTICK_RATE_MS;
|
|
|
|
|
|
|
|
return xSemaphoreTake(m_semaphore, delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SMACCMSemaphore::take_nonblocking()
|
|
|
|
{
|
2013-01-10 17:16:43 -04:00
|
|
|
if (already_held(m_semaphore))
|
|
|
|
hal.scheduler->panic("PANIC: Recursive semaphore take.");
|
|
|
|
|
2013-01-03 19:41:36 -04:00
|
|
|
return xSemaphoreTake(m_semaphore, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SMACCMSemaphore::give()
|
|
|
|
{
|
|
|
|
return xSemaphoreGive(m_semaphore);
|
|
|
|
}
|
2013-01-09 17:32:34 -04:00
|
|
|
|
|
|
|
#endif // CONFIG_HAL_BOARD == HAL_BOARD_SMACCM
|