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:
committed by
GitHub
parent
c2edbad0fb
commit
686f7cce83
@@ -6,7 +6,7 @@
|
||||
|
||||
namespace tt {
|
||||
|
||||
static void timer_callback(TimerHandle_t hTimer) {
|
||||
void Timer::onCallback(TimerHandle_t hTimer) {
|
||||
auto* timer = static_cast<Timer*>(pvTimerGetTimerID(hTimer));
|
||||
|
||||
if (timer != nullptr) {
|
||||
@@ -14,54 +14,59 @@ static void timer_callback(TimerHandle_t hTimer) {
|
||||
}
|
||||
}
|
||||
|
||||
Timer::Timer(Type type, Callback callback, std::shared_ptr<void> callbackContext) {
|
||||
assert((!TT_IS_ISR()) && (callback != nullptr));
|
||||
|
||||
this->callback = callback;
|
||||
this->callbackContext = std::move(callbackContext);
|
||||
static inline TimerHandle_t createTimer(Timer::Type type, void* timerId, TimerCallbackFunction_t callback) {
|
||||
assert(timerId != nullptr);
|
||||
assert(callback != nullptr);
|
||||
|
||||
UBaseType_t reload;
|
||||
if (type == Type::Once) {
|
||||
if (type == Timer::Type::Once) {
|
||||
reload = pdFALSE;
|
||||
} else {
|
||||
reload = pdTRUE;
|
||||
}
|
||||
|
||||
this->timerHandle = xTimerCreate(nullptr, portMAX_DELAY, (BaseType_t)reload, this, timer_callback);
|
||||
assert(this->timerHandle);
|
||||
return xTimerCreate(nullptr, portMAX_DELAY, (BaseType_t)reload, timerId, callback);
|
||||
}
|
||||
|
||||
Timer::Timer(Type type, Callback callback, std::shared_ptr<void> callbackContext) :
|
||||
callback(callback),
|
||||
callbackContext(std::move(callbackContext)),
|
||||
handle(createTimer(type, this, onCallback))
|
||||
{
|
||||
assert(!TT_IS_ISR());
|
||||
assert(handle != nullptr);
|
||||
}
|
||||
|
||||
Timer::~Timer() {
|
||||
assert(!TT_IS_ISR());
|
||||
tt_check(xTimerDelete(timerHandle, portMAX_DELAY) == pdPASS);
|
||||
}
|
||||
|
||||
bool Timer::start(TickType_t interval) {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(interval < portMAX_DELAY);
|
||||
return xTimerChangePeriod(timerHandle, interval, portMAX_DELAY) == pdPASS;
|
||||
return xTimerChangePeriod(handle.get(), interval, portMAX_DELAY) == pdPASS;
|
||||
}
|
||||
|
||||
bool Timer::restart(TickType_t interval) {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(interval < portMAX_DELAY);
|
||||
return xTimerChangePeriod(timerHandle, interval, portMAX_DELAY) == pdPASS &&
|
||||
xTimerReset(timerHandle, portMAX_DELAY) == pdPASS;
|
||||
return xTimerChangePeriod(handle.get(), interval, portMAX_DELAY) == pdPASS &&
|
||||
xTimerReset(handle.get(), portMAX_DELAY) == pdPASS;
|
||||
}
|
||||
|
||||
bool Timer::stop() {
|
||||
assert(!TT_IS_ISR());
|
||||
return xTimerStop(timerHandle, portMAX_DELAY) == pdPASS;
|
||||
return xTimerStop(handle.get(), portMAX_DELAY) == pdPASS;
|
||||
}
|
||||
|
||||
bool Timer::isRunning() {
|
||||
assert(!TT_IS_ISR());
|
||||
return xTimerIsTimerActive(timerHandle) == pdTRUE;
|
||||
return xTimerIsTimerActive(handle.get()) == pdTRUE;
|
||||
}
|
||||
|
||||
TickType_t Timer::getExpireTime() {
|
||||
assert(!TT_IS_ISR());
|
||||
return xTimerGetExpiryTime(timerHandle);
|
||||
return xTimerGetExpiryTime(handle.get());
|
||||
}
|
||||
|
||||
bool Timer::setPendingCallback(PendingCallback callback, void* callbackContext, uint32_t callbackArg, TickType_t timeout) {
|
||||
|
||||
Reference in New Issue
Block a user