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
+18 -18
View File
@@ -4,7 +4,6 @@
*/
#pragma once
#include "CoreTypes.h"
#include "Thread.h"
#include "RtosCompatSemaphore.h"
#include "Check.h"
@@ -15,9 +14,9 @@ namespace tt {
/**
* Wrapper for FreeRTOS xSemaphoreCreateMutex and xSemaphoreCreateRecursiveMutex
* Can be used in IRQ mode (within ISR context)
* Cannot be used in IRQ mode (within ISR context)
*/
class Mutex : public Lockable {
class Mutex final : public Lockable {
public:
@@ -28,35 +27,36 @@ public:
private:
SemaphoreHandle_t semaphore;
struct SemaphoreHandleDeleter {
void operator()(QueueHandle_t handleToDelete) {
assert(!TT_IS_IRQ_MODE());
vSemaphoreDelete(handleToDelete);
}
};
std::unique_ptr<std::remove_pointer_t<QueueHandle_t>, SemaphoreHandleDeleter> handle;
Type type;
public:
explicit Mutex(Type type = Type::Normal);
~Mutex() override;
/** Attempt to lock the mutex. Blocks until timeout passes or lock is acquired.
* @param[in] timeout
* @return status result
*/
TtStatus acquire(TickType_t timeout) const;
/** Attempt to unlock the mutex.
* @return status result
*/
TtStatus release() const;
~Mutex() override = default;
/** Attempt to lock the mutex. Blocks until timeout passes or lock is acquired.
* @param[in] timeout
* @return success result
*/
bool lock(TickType_t timeout) const override { return acquire(timeout) == TtStatusOk; }
bool lock(TickType_t timeout) const override;
/** Attempt to lock the mutex. Blocks until lock is acquired, without timeout.
* @return success result
*/
bool lock() const override { return lock(portMAX_DELAY); }
/** Attempt to unlock the mutex.
* @return success result
*/
bool unlock() const override { return release() == TtStatusOk; }
bool unlock() const override;
/** @return the owner of the thread */
ThreadId getOwner() const;