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
+13 -13
View File
@@ -4,7 +4,7 @@
#include "Statusbar.h"
#include "Mutex.h"
#include "Pubsub.h"
#include "PubSub.h"
#include "TactilityCore.h"
#include "lvgl/Style.h"
@@ -43,10 +43,10 @@ typedef struct {
lv_obj_t* time;
lv_obj_t* icons[STATUSBAR_ICON_LIMIT];
lv_obj_t* battery_icon;
PubSubSubscription* pubsub_subscription;
PubSub::SubscriptionHandle pubsub_subscription;
} Statusbar;
static bool statusbar_lock(uint32_t timeoutTicks) {
static bool statusbar_lock(uint32_t timeoutTicks = portMAX_DELAY) {
return statusbar_data.mutex.lock(timeoutTicks);
}
@@ -83,7 +83,7 @@ static void onUpdateTime(TT_UNUSED std::shared_ptr<void> context) {
statusbar_data.time_update_timer->start(getNextUpdateTime());
// Notify widget
tt_pubsub_publish(statusbar_data.pubsub, nullptr);
statusbar_data.pubsub->publish(nullptr);
} else {
statusbar_data.time_update_timer->start(pdMS_TO_TICKS(60000U));
}
@@ -132,7 +132,7 @@ static void statusbar_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
lv_obj_remove_flag(obj, LV_OBJ_FLAG_SCROLLABLE);
LV_TRACE_OBJ_CREATE("finished");
auto* statusbar = (Statusbar*)obj;
statusbar->pubsub_subscription = tt_pubsub_subscribe(statusbar_data.pubsub, &statusbar_pubsub_event, statusbar);
statusbar->pubsub_subscription = statusbar_data.pubsub->subscribe(&statusbar_pubsub_event, statusbar);
if (!statusbar_data.time_update_timer->isRunning()) {
statusbar_data.time_update_timer->start(50 / portTICK_PERIOD_MS);
@@ -145,7 +145,7 @@ static void statusbar_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
static void statusbar_destructor(TT_UNUSED const lv_obj_class_t* class_p, lv_obj_t* obj) {
auto* statusbar = (Statusbar*)obj;
tt_pubsub_unsubscribe(statusbar_data.pubsub, statusbar->pubsub_subscription);
statusbar_data.pubsub->unsubscribe(statusbar->pubsub_subscription);
}
static void update_icon(lv_obj_t* image, const StatusbarIcon* icon) {
@@ -181,7 +181,7 @@ lv_obj_t* statusbar_create(lv_obj_t* parent) {
obj_set_style_bg_invisible(left_spacer);
lv_obj_set_flex_grow(left_spacer, 1);
statusbar_lock(TtWaitForever);
statusbar_lock(portMAX_DELAY);
for (int i = 0; i < STATUSBAR_ICON_LIMIT; ++i) {
auto* image = lv_image_create(obj);
lv_obj_set_size(image, STATUSBAR_ICON_SIZE, STATUSBAR_ICON_SIZE);
@@ -233,7 +233,7 @@ static void statusbar_event(TT_UNUSED const lv_obj_class_t* class_p, lv_event_t*
}
int8_t statusbar_icon_add(const std::string& image) {
statusbar_lock(TtWaitForever);
statusbar_lock();
int8_t result = -1;
for (int8_t i = 0; i < STATUSBAR_ICON_LIMIT; ++i) {
if (!statusbar_data.icons[i].claimed) {
@@ -245,7 +245,7 @@ int8_t statusbar_icon_add(const std::string& image) {
break;
}
}
tt_pubsub_publish(statusbar_data.pubsub, nullptr);
statusbar_data.pubsub->publish(nullptr);
statusbar_unlock();
return result;
}
@@ -257,12 +257,12 @@ int8_t statusbar_icon_add() {
void statusbar_icon_remove(int8_t id) {
TT_LOG_I(TAG, "id %d: remove", id);
tt_check(id >= 0 && id < STATUSBAR_ICON_LIMIT);
statusbar_lock(TtWaitForever);
statusbar_lock();
StatusbarIcon* icon = &statusbar_data.icons[id];
icon->claimed = false;
icon->visible = false;
icon->image = "";
tt_pubsub_publish(statusbar_data.pubsub, nullptr);
statusbar_data.pubsub->publish(nullptr);
statusbar_unlock();
}
@@ -273,7 +273,7 @@ void statusbar_icon_set_image(int8_t id, const std::string& image) {
StatusbarIcon* icon = &statusbar_data.icons[id];
tt_check(icon->claimed);
icon->image = image;
tt_pubsub_publish(statusbar_data.pubsub, nullptr);
statusbar_data.pubsub->publish(nullptr);
statusbar_unlock();
}
}
@@ -285,7 +285,7 @@ void statusbar_icon_set_visibility(int8_t id, bool visible) {
StatusbarIcon* icon = &statusbar_data.icons[id];
tt_check(icon->claimed);
icon->visible = visible;
tt_pubsub_publish(statusbar_data.pubsub, nullptr);
statusbar_data.pubsub->publish(nullptr);
statusbar_unlock();
}
}