C++ refactoring (#91)
This commit is contained in:
committed by
GitHub
parent
d06137ba76
commit
ca5d4b8226
@@ -2,127 +2,89 @@
|
||||
#include "Check.h"
|
||||
#include "CoreDefines.h"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#else
|
||||
#include "FreeRTOS.h"
|
||||
#include "semphr.h"
|
||||
#endif
|
||||
|
||||
namespace tt {
|
||||
|
||||
Semaphore* tt_semaphore_alloc(uint32_t max_count, uint32_t initial_count) {
|
||||
Semaphore::Semaphore(uint32_t maxCount, uint32_t initialCount) {
|
||||
tt_assert(!TT_IS_IRQ_MODE());
|
||||
tt_assert((max_count > 0U) && (initial_count <= max_count));
|
||||
tt_assert((maxCount > 0U) && (initialCount <= maxCount));
|
||||
|
||||
SemaphoreHandle_t hSemaphore = nullptr;
|
||||
if (max_count == 1U) {
|
||||
hSemaphore = xSemaphoreCreateBinary();
|
||||
if ((hSemaphore != nullptr) && (initial_count != 0U)) {
|
||||
if (xSemaphoreGive(hSemaphore) != pdPASS) {
|
||||
vSemaphoreDelete(hSemaphore);
|
||||
hSemaphore = nullptr;
|
||||
if (maxCount == 1U) {
|
||||
handle = xSemaphoreCreateBinary();
|
||||
if ((handle != nullptr) && (initialCount != 0U)) {
|
||||
if (xSemaphoreGive(handle) != pdPASS) {
|
||||
vSemaphoreDelete(handle);
|
||||
handle = nullptr;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
hSemaphore = xSemaphoreCreateCounting(max_count, initial_count);
|
||||
handle = xSemaphoreCreateCounting(maxCount, initialCount);
|
||||
}
|
||||
|
||||
tt_check(hSemaphore);
|
||||
|
||||
return (Semaphore*)hSemaphore;
|
||||
tt_check(handle);
|
||||
}
|
||||
|
||||
void tt_semaphore_free(Semaphore* instance) {
|
||||
tt_assert(instance);
|
||||
Semaphore::~Semaphore() {
|
||||
tt_assert(!TT_IS_IRQ_MODE());
|
||||
|
||||
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
||||
|
||||
vSemaphoreDelete(hSemaphore);
|
||||
vSemaphoreDelete(handle);
|
||||
}
|
||||
|
||||
TtStatus tt_semaphore_acquire(Semaphore* instance, uint32_t timeout) {
|
||||
tt_assert(instance);
|
||||
|
||||
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
||||
TtStatus status;
|
||||
BaseType_t yield;
|
||||
|
||||
status = TtStatusOk;
|
||||
|
||||
TtStatus Semaphore::acquire(uint32_t timeout) const {
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (timeout != 0U) {
|
||||
status = TtStatusErrorParameter;
|
||||
return TtStatusErrorParameter;
|
||||
} else {
|
||||
yield = pdFALSE;
|
||||
BaseType_t yield = pdFALSE;
|
||||
|
||||
if (xSemaphoreTakeFromISR(hSemaphore, &yield) != pdPASS) {
|
||||
status = TtStatusErrorResource;
|
||||
if (xSemaphoreTakeFromISR(handle, &yield) != pdPASS) {
|
||||
return TtStatusErrorResource;
|
||||
} else {
|
||||
portYIELD_FROM_ISR(yield);
|
||||
return TtStatusOk;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (xSemaphoreTake(hSemaphore, (TickType_t)timeout) != pdPASS) {
|
||||
if (xSemaphoreTake(handle, (TickType_t)timeout) != pdPASS) {
|
||||
if (timeout != 0U) {
|
||||
status = TtStatusErrorTimeout;
|
||||
return TtStatusErrorTimeout;
|
||||
} else {
|
||||
status = TtStatusErrorResource;
|
||||
return TtStatusErrorResource;
|
||||
}
|
||||
} else {
|
||||
return TtStatusOk;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
TtStatus tt_semaphore_release(Semaphore* instance) {
|
||||
tt_assert(instance);
|
||||
|
||||
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
||||
TtStatus stat;
|
||||
BaseType_t yield;
|
||||
|
||||
stat = TtStatusOk;
|
||||
|
||||
TtStatus Semaphore::release() const {
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
yield = pdFALSE;
|
||||
BaseType_t yield = pdFALSE;
|
||||
|
||||
if (xSemaphoreGiveFromISR(hSemaphore, &yield) != pdTRUE) {
|
||||
stat = TtStatusErrorResource;
|
||||
if (xSemaphoreGiveFromISR(handle, &yield) != pdTRUE) {
|
||||
return TtStatusErrorResource;
|
||||
} else {
|
||||
portYIELD_FROM_ISR(yield);
|
||||
return TtStatusOk;
|
||||
}
|
||||
} else {
|
||||
if (xSemaphoreGive(hSemaphore) != pdPASS) {
|
||||
stat = TtStatusErrorResource;
|
||||
if (xSemaphoreGive(handle) != pdPASS) {
|
||||
return TtStatusErrorResource;
|
||||
} else {
|
||||
return TtStatusOk;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return execution status */
|
||||
return (stat);
|
||||
}
|
||||
|
||||
uint32_t tt_semaphore_get_count(Semaphore* instance) {
|
||||
tt_assert(instance);
|
||||
|
||||
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
||||
uint32_t count;
|
||||
|
||||
uint32_t Semaphore::getCount() const {
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
// TODO: uxSemaphoreGetCountFromISR is not supported on esp-idf 5.1.2 - perhaps later on?
|
||||
#ifdef uxSemaphoreGetCountFromISR
|
||||
count = (uint32_t)uxSemaphoreGetCountFromISR(hSemaphore);
|
||||
return uxSemaphoreGetCountFromISR(handle);
|
||||
#else
|
||||
count = (uint32_t)uxQueueMessagesWaitingFromISR((QueueHandle_t)hSemaphore);
|
||||
return uxQueueMessagesWaitingFromISR((QueueHandle_t)hSemaphore);
|
||||
#endif
|
||||
} else {
|
||||
count = (uint32_t)uxSemaphoreGetCount(hSemaphore);
|
||||
return uxSemaphoreGetCount(handle);
|
||||
}
|
||||
|
||||
/* Return number of tokens */
|
||||
return (count);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user