mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-18 19:03:16 +00:00
FreeRTOS handles were stored plainly and they were deleted in the destructor of classes. This meant that if a class were to be copied, the destructor would be called twice on the same handles and lead to double-free. Seha on Discord suggested to fix this by using `std::unique_ptr` with a custom deletion function. The changes affect: - Thread - Semaphore - Mutex - StreamBuffer - Timer - MessageQueue - EventFlag Thread changes: - Removal of the hack with the `Data` struct - Thread's main body is now just a private static function inside the class. - The C functions were relocated to static class members PubSub changes: - Refactored pubsub into class - Renamed files to `PubSub` instead of `Pubsub` - `PubSubSubscription` is now a private inner struct and `PubSub` only exposes `SubscriptionHandle` Lockable, ScopedLockable, Mutex: - Added `lock()` method that locks indefinitely - Remove deprecated `acquire()` and `release()` methods - Removed `TtWaitForever` in favour of `portMAX_DELAY`
168 lines
3.5 KiB
C++
168 lines
3.5 KiB
C++
#include "kernel/Kernel.h"
|
|
#include "CoreDefines.h"
|
|
#include "RtosCompatTask.h"
|
|
|
|
#ifdef ESP_PLATFORM
|
|
#include "rom/ets_sys.h"
|
|
#else
|
|
#include <cassert>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
namespace tt::kernel {
|
|
|
|
bool isRunning() {
|
|
return xTaskGetSchedulerState() != taskSCHEDULER_RUNNING;
|
|
}
|
|
|
|
bool lock() {
|
|
assert(!TT_IS_ISR());
|
|
|
|
int32_t lock;
|
|
|
|
switch (xTaskGetSchedulerState()) {
|
|
// Already suspended
|
|
case taskSCHEDULER_SUSPENDED:
|
|
return true;
|
|
|
|
case taskSCHEDULER_RUNNING:
|
|
vTaskSuspendAll();
|
|
return true;
|
|
|
|
case taskSCHEDULER_NOT_STARTED:
|
|
default:
|
|
return false;
|
|
}
|
|
|
|
/* Return previous lock state */
|
|
return (lock);
|
|
}
|
|
|
|
bool unlock() {
|
|
assert(!TT_IS_ISR());
|
|
|
|
switch (xTaskGetSchedulerState()) {
|
|
case taskSCHEDULER_SUSPENDED:
|
|
if (xTaskResumeAll() != pdTRUE) {
|
|
if (xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
|
|
case taskSCHEDULER_RUNNING:
|
|
return true;
|
|
|
|
case taskSCHEDULER_NOT_STARTED:
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool restoreLock(bool lock) {
|
|
assert(!TT_IS_ISR());
|
|
|
|
switch (xTaskGetSchedulerState()) {
|
|
case taskSCHEDULER_SUSPENDED:
|
|
case taskSCHEDULER_RUNNING:
|
|
if (lock) {
|
|
vTaskSuspendAll();
|
|
return true;
|
|
} else {
|
|
if (xTaskResumeAll() != pdTRUE) {
|
|
if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
case taskSCHEDULER_NOT_STARTED:
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
uint32_t getTickFrequency() {
|
|
/* Return frequency in hertz */
|
|
return (configTICK_RATE_HZ);
|
|
}
|
|
|
|
void delayTicks(TickType_t ticks) {
|
|
assert(!TT_IS_ISR());
|
|
if (ticks == 0U) {
|
|
taskYIELD();
|
|
} else {
|
|
vTaskDelay(ticks);
|
|
}
|
|
}
|
|
|
|
bool delayUntilTick(TickType_t tick) {
|
|
assert(!TT_IS_ISR());
|
|
|
|
TickType_t tcnt, delay;
|
|
|
|
tcnt = xTaskGetTickCount();
|
|
|
|
/* Determine remaining number of tick to delay */
|
|
delay = (TickType_t)tick - tcnt;
|
|
|
|
/* Check if target tick has not expired */
|
|
if ((delay != 0U) && (0 == (delay >> (8 * sizeof(TickType_t) - 1)))) {
|
|
if (xTaskDelayUntil(&tcnt, delay) == pdPASS) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
TickType_t getTicks() {
|
|
if (TT_IS_ISR() != 0U) {
|
|
return xTaskGetTickCountFromISR();
|
|
} else {
|
|
return xTaskGetTickCount();
|
|
}
|
|
}
|
|
|
|
TickType_t millisToTicks(uint32_t milliseconds) {
|
|
#if configTICK_RATE_HZ == 1000
|
|
return (TickType_t)milliseconds;
|
|
#else
|
|
return (TickType_t)((float)configTICK_RATE_HZ) / 1000.0f * (float)milliseconds;
|
|
#endif
|
|
}
|
|
|
|
void delayMillis(uint32_t milliseconds) {
|
|
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
|
|
if (milliseconds > 0 && milliseconds < portMAX_DELAY - 1) {
|
|
milliseconds += 1;
|
|
}
|
|
#if configTICK_RATE_HZ_RAW == 1000
|
|
tt_delay_tick(milliseconds);
|
|
#else
|
|
delayTicks(kernel::millisToTicks(milliseconds));
|
|
#endif
|
|
} else if (milliseconds > 0) {
|
|
kernel::delayMicros(milliseconds * 1000);
|
|
}
|
|
}
|
|
|
|
void delayMicros(uint32_t microseconds) {
|
|
#ifdef ESP_PLATFORM
|
|
ets_delay_us(microseconds);
|
|
#else
|
|
usleep(microseconds);
|
|
#endif
|
|
}
|
|
|
|
Platform getPlatform() {
|
|
#ifdef ESP_PLATFORM
|
|
return PlatformEsp;
|
|
#else
|
|
return PlatformSimulator;
|
|
#endif
|
|
}
|
|
|
|
} // namespace
|