TactilityCore improvements (#187)

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`
This commit is contained in:
Ken Van Hoeylandt
2025-01-25 17:29:11 +01:00
committed by GitHub
parent c2edbad0fb
commit 686f7cce83
60 changed files with 711 additions and 831 deletions
+21 -53
View File
@@ -22,88 +22,56 @@ namespace tt {
#define tt_mutex_info(mutex, text)
#endif
Mutex::Mutex(Type type) : type(type) {
tt_mutex_info(data, "alloc");
static inline SemaphoreHandle_t createSemaphoreHandle(Mutex::Type type) {
switch (type) {
case Type::Normal:
semaphore = xSemaphoreCreateMutex();
break;
case Type::Recursive:
semaphore = xSemaphoreCreateRecursiveMutex();
break;
case Mutex::Type::Normal:
return xSemaphoreCreateMutex();
case Mutex::Type::Recursive:
return xSemaphoreCreateRecursiveMutex();
default:
tt_crash("Mutex type unknown/corrupted");
}
assert(semaphore != nullptr);
}
Mutex::~Mutex() {
assert(!TT_IS_IRQ_MODE());
vSemaphoreDelete(semaphore);
semaphore = nullptr; // If the mutex is used after release, this might help debugging
Mutex::Mutex(Type type) : handle(createSemaphoreHandle(type)), type(type) {
tt_mutex_info(data, "alloc");
assert(handle != nullptr);
}
TtStatus Mutex::acquire(TickType_t timeout) const {
bool Mutex::lock(TickType_t timeout) const {
assert(!TT_IS_IRQ_MODE());
assert(semaphore != nullptr);
assert(handle != nullptr);
tt_mutex_info(mutex, "acquire");
switch (type) {
case Type::Normal:
if (xSemaphoreTake(semaphore, timeout) != pdPASS) {
if (timeout != 0U) {
return TtStatusErrorTimeout;
} else {
return TtStatusErrorResource;
}
} else {
return TtStatusOk;
}
return xSemaphoreTake(handle.get(), timeout) == pdPASS;
case Type::Recursive:
if (xSemaphoreTakeRecursive(semaphore, timeout) != pdPASS) {
if (timeout != 0U) {
return TtStatusErrorTimeout;
} else {
return TtStatusErrorResource;
}
} else {
return TtStatusOk;
}
return xSemaphoreTakeRecursive(handle.get(), timeout) == pdPASS;
default:
tt_crash("mutex type unknown/corrupted");
tt_crash();
}
}
TtStatus Mutex::release() const {
bool Mutex::unlock() const {
assert(!TT_IS_IRQ_MODE());
assert(semaphore);
assert(handle != nullptr);
tt_mutex_info(mutex, "release");
switch (type) {
case Type::Normal: {
if (xSemaphoreGive(semaphore) != pdPASS) {
return TtStatusErrorResource;
} else {
return TtStatusOk;
}
}
case Type::Normal:
return xSemaphoreGive(handle.get()) == pdPASS;
case Type::Recursive:
if (xSemaphoreGiveRecursive(semaphore) != pdPASS) {
return TtStatusErrorResource;
} else {
return TtStatusOk;
}
return xSemaphoreGiveRecursive(handle.get()) == pdPASS;
default:
tt_crash("mutex type unknown/corrupted");
tt_crash();
}
}
ThreadId Mutex::getOwner() const {
assert(!TT_IS_IRQ_MODE());
assert(semaphore);
return (ThreadId)xSemaphoreGetMutexHolder(semaphore);
assert(handle != nullptr);
return (ThreadId)xSemaphoreGetMutexHolder(handle.get());
}
} // namespace