Thread and Timer converted to class (#81)

This commit is contained in:
Ken Van Hoeylandt 2024-11-22 23:08:18 +01:00 committed by GitHub
parent 85e26636a3
commit 854fefa1a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 550 additions and 679 deletions

View File

@ -21,7 +21,7 @@ int main() {
"main", "main",
8192, 8192,
nullptr, nullptr,
tt::ThreadPriorityNormal, tt::Thread::PriorityNormal,
nullptr nullptr
); );

View File

@ -81,7 +81,7 @@ void lvgl_task_start() {
"lvgl", "lvgl",
8192, 8192,
NULL, NULL,
tt::ThreadPriorityHigh, // Should be higher than main app task tt::Thread::PriorityHigh, // Should be higher than main app task
NULL NULL
); );

View File

@ -12,7 +12,7 @@ bool m5stack_lvgl_init() {
static lv_display_t* display = nullptr; static lv_display_t* display = nullptr;
const lvgl_port_cfg_t lvgl_cfg = { const lvgl_port_cfg_t lvgl_cfg = {
.task_priority = tt::ThreadPriorityHigh, .task_priority = tt::Thread::PriorityHigh,
.task_stack = 8096, .task_stack = 8096,
.task_affinity = -1, // core pinning .task_affinity = -1, // core pinning
.task_max_sleep_ms = 500, .task_max_sleep_ms = 500,

View File

@ -104,9 +104,9 @@ lv_disp_t* ws3t_display_create() {
lvgl_mux = xSemaphoreCreateRecursiveMutex(); lvgl_mux = xSemaphoreCreateRecursiveMutex();
tt_assert(lvgl_mux); tt_assert(lvgl_mux);
tt::Thread* thread = tt::thread_alloc_ex("display_task", 8192, &display_task, nullptr); tt::Thread* thread = new tt::Thread("display_task", 8192, &display_task, nullptr);
tt::thread_set_priority(thread, tt::ThreadPriorityHigh); // TODO: try out THREAD_PRIORITY_RENDER thread->setPriority(tt::Thread::PriorityHigh); // TODO: try out THREAD_PRIORITY_RENDER
tt::thread_start(thread); thread->start();
esp_lcd_panel_handle_t panel_handle = NULL; esp_lcd_panel_handle_t panel_handle = NULL;
esp_lcd_rgb_panel_config_t panel_config = { esp_lcd_rgb_panel_config_t panel_config = {

View File

@ -14,7 +14,7 @@ bool twodotfour_lvgl_init() {
static esp_lcd_touch_handle_t touch_handle; static esp_lcd_touch_handle_t touch_handle;
const lvgl_port_cfg_t lvgl_cfg = { const lvgl_port_cfg_t lvgl_cfg = {
.task_priority = tt::ThreadPriorityHigh, .task_priority = tt::Thread::PriorityHigh,
.task_stack = 8096, .task_stack = 8096,
.task_affinity = -1, // core pinning .task_affinity = -1, // core pinning
.task_max_sleep_ms = 500, .task_max_sleep_ms = 500,

View File

@ -90,14 +90,14 @@ static int32_t gpio_task(void* context) {
static void task_start(Gpio* gpio) { static void task_start(Gpio* gpio) {
tt_assert(gpio->thread == nullptr); tt_assert(gpio->thread == nullptr);
lock(gpio); lock(gpio);
gpio->thread = thread_alloc_ex( gpio->thread = new Thread(
"gpio", "gpio",
4096, 4096,
&gpio_task, &gpio_task,
gpio gpio
); );
gpio->thread_interrupted = false; gpio->thread_interrupted = false;
thread_start(gpio->thread); gpio->thread->start();
unlock(gpio); unlock(gpio);
} }
@ -107,10 +107,10 @@ static void task_stop(Gpio* gpio) {
gpio->thread_interrupted = true; gpio->thread_interrupted = true;
unlock(gpio); unlock(gpio);
thread_join(gpio->thread); gpio->thread->join();
lock(gpio); lock(gpio);
thread_free(gpio->thread); delete gpio->thread;
gpio->thread = nullptr; gpio->thread = nullptr;
unlock(gpio); unlock(gpio);
} }

View File

@ -20,9 +20,8 @@ typedef struct {
lv_obj_t* current; lv_obj_t* current;
} AppData; } AppData;
static void app_update_ui(App app) { static void app_update_ui(void* callbackContext) {
auto* data = static_cast<AppData*>(tt_app_get_data(app)); auto* data = (AppData*)callbackContext;
bool charging_enabled = data->power->is_charging_enabled(); bool charging_enabled = data->power->is_charging_enabled();
const char* charge_state = data->power->is_charging() ? "yes" : "no"; const char* charge_state = data->power->is_charging() ? "yes" : "no";
uint8_t charge_level = data->power->get_charge_level(); uint8_t charge_level = data->power->get_charge_level();
@ -46,11 +45,10 @@ static void on_power_enabled_change(lv_event_t* event) {
auto* enable_switch = static_cast<lv_obj_t*>(lv_event_get_target(event)); auto* enable_switch = static_cast<lv_obj_t*>(lv_event_get_target(event));
if (code == LV_EVENT_VALUE_CHANGED) { if (code == LV_EVENT_VALUE_CHANGED) {
bool is_on = lv_obj_has_state(enable_switch, LV_STATE_CHECKED); bool is_on = lv_obj_has_state(enable_switch, LV_STATE_CHECKED);
App app = lv_event_get_user_data(event); auto* data = static_cast<AppData*>(lv_event_get_user_data(event));
auto* data = static_cast<AppData*>(tt_app_get_data(app));
if (data->power->is_charging_enabled() != is_on) { if (data->power->is_charging_enabled() != is_on) {
data->power->set_charging_enabled(is_on); data->power->set_charging_enabled(is_on);
app_update_ui(app); app_update_ui(data);
} }
} }
} }
@ -80,7 +78,7 @@ static void app_show(App app, lv_obj_t* parent) {
lv_obj_set_align(enable_label, LV_ALIGN_LEFT_MID); lv_obj_set_align(enable_label, LV_ALIGN_LEFT_MID);
lv_obj_t* enable_switch = lv_switch_create(switch_container); lv_obj_t* enable_switch = lv_switch_create(switch_container);
lv_obj_add_event_cb(enable_switch, on_power_enabled_change, LV_EVENT_ALL, app); lv_obj_add_event_cb(enable_switch, on_power_enabled_change, LV_EVENT_ALL, data);
lv_obj_set_align(enable_switch, LV_ALIGN_RIGHT_MID); lv_obj_set_align(enable_switch, LV_ALIGN_RIGHT_MID);
data->enable_switch = enable_switch; data->enable_switch = enable_switch;
@ -88,27 +86,27 @@ static void app_show(App app, lv_obj_t* parent) {
data->charge_level = lv_label_create(wrapper); data->charge_level = lv_label_create(wrapper);
data->current = lv_label_create(wrapper); data->current = lv_label_create(wrapper);
app_update_ui(app); app_update_ui(data);
timer_start(data->update_timer, ms_to_ticks(1000)); data->update_timer->start(ms_to_ticks(1000));
} }
static void app_hide(TT_UNUSED App app) { static void app_hide(TT_UNUSED App app) {
auto* data = static_cast<AppData*>(tt_app_get_data(app)); auto* data = static_cast<AppData*>(tt_app_get_data(app));
timer_stop(data->update_timer); data->update_timer->stop();
} }
static void app_start(App app) { static void app_start(App app) {
auto* data = static_cast<AppData*>(malloc(sizeof(AppData))); auto* data = new AppData();
tt_app_set_data(app, data); data->update_timer = new Timer(Timer::TypePeriodic, &app_update_ui, data);
data->update_timer = timer_alloc(&app_update_ui, TimerTypePeriodic, app);
data->power = get_config()->hardware->power; data->power = get_config()->hardware->power;
assert(data->power != nullptr); // The Power app only shows up on supported devices assert(data->power != nullptr); // The Power app only shows up on supported devices
tt_app_set_data(app, data);
} }
static void app_stop(App app) { static void app_stop(App app) {
auto* data = static_cast<AppData*>(tt_app_get_data(app)); auto* data = static_cast<AppData*>(tt_app_get_data(app));
timer_free(data->update_timer); delete data->update_timer;
free(data); delete data;
} }
extern const AppManifest manifest = { extern const AppManifest manifest = {

View File

@ -37,7 +37,7 @@ Gui* gui_alloc() {
auto* instance = static_cast<Gui*>(malloc(sizeof(Gui))); auto* instance = static_cast<Gui*>(malloc(sizeof(Gui)));
memset(instance, 0, sizeof(Gui)); memset(instance, 0, sizeof(Gui));
tt_check(instance != NULL); tt_check(instance != NULL);
instance->thread = thread_alloc_ex( instance->thread = new Thread(
"gui", "gui",
4096, // Last known minimum was 2800 for launching desktop 4096, // Last known minimum was 2800 for launching desktop
&gui_main, &gui_main,
@ -56,7 +56,7 @@ Gui* gui_alloc() {
void gui_free(Gui* instance) { void gui_free(Gui* instance) {
tt_assert(instance != nullptr); tt_assert(instance != nullptr);
thread_free(instance->thread); delete instance->thread;
tt_mutex_free(instance->mutex); tt_mutex_free(instance->mutex);
tt_check(lvgl::lock(1000 / portTICK_PERIOD_MS)); tt_check(lvgl::lock(1000 / portTICK_PERIOD_MS));
@ -80,7 +80,7 @@ void unlock() {
void request_draw() { void request_draw() {
tt_assert(gui); tt_assert(gui);
ThreadId thread_id = thread_get_id(gui->thread); ThreadId thread_id = gui->thread->getId();
thread_flags_set(thread_id, GUI_THREAD_FLAG_DRAW); thread_flags_set(thread_id, GUI_THREAD_FLAG_DRAW);
} }
@ -138,17 +138,17 @@ static int32_t gui_main(TT_UNUSED void* p) {
static void start(TT_UNUSED Service& service) { static void start(TT_UNUSED Service& service) {
gui = gui_alloc(); gui = gui_alloc();
thread_set_priority(gui->thread, THREAD_PRIORITY_SERVICE); gui->thread->setPriority(THREAD_PRIORITY_SERVICE);
thread_start(gui->thread); gui->thread->start();
} }
static void stop(TT_UNUSED Service& service) { static void stop(TT_UNUSED Service& service) {
lock(); lock();
ThreadId thread_id = thread_get_id(gui->thread); ThreadId thread_id = gui->thread->getId();
thread_flags_set(thread_id, GUI_THREAD_FLAG_EXIT); thread_flags_set(thread_id, GUI_THREAD_FLAG_EXIT);
thread_join(gui->thread); gui->thread->join();
thread_free(gui->thread); delete gui->thread;
unlock(); unlock();

View File

@ -31,7 +31,7 @@ static Loader* loader_alloc() {
loader_singleton = new Loader(); loader_singleton = new Loader();
loader_singleton->pubsub_internal = tt_pubsub_alloc(); loader_singleton->pubsub_internal = tt_pubsub_alloc();
loader_singleton->pubsub_external = tt_pubsub_alloc(); loader_singleton->pubsub_external = tt_pubsub_alloc();
loader_singleton->thread = thread_alloc_ex( loader_singleton->thread = new Thread(
"loader", "loader",
4096, // Last known minimum was 2400 for starting Hello World app 4096, // Last known minimum was 2400 for starting Hello World app
&loader_main, &loader_main,
@ -45,7 +45,7 @@ static Loader* loader_alloc() {
static void loader_free() { static void loader_free() {
tt_assert(loader_singleton != nullptr); tt_assert(loader_singleton != nullptr);
thread_free(loader_singleton->thread); delete loader_singleton->thread;
tt_pubsub_free(loader_singleton->pubsub_internal); tt_pubsub_free(loader_singleton->pubsub_internal);
tt_pubsub_free(loader_singleton->pubsub_external); tt_pubsub_free(loader_singleton->pubsub_external);
tt_mutex_free(loader_singleton->mutex); tt_mutex_free(loader_singleton->mutex);
@ -334,18 +334,21 @@ static void loader_start(TT_UNUSED Service& service) {
tt_check(loader_singleton == nullptr); tt_check(loader_singleton == nullptr);
loader_singleton = loader_alloc(); loader_singleton = loader_alloc();
thread_set_priority(loader_singleton->thread, THREAD_PRIORITY_SERVICE); loader_singleton->thread->setPriority(THREAD_PRIORITY_SERVICE);
thread_start(loader_singleton->thread); loader_singleton->thread->start();
} }
static void loader_stop(TT_UNUSED Service& service) { static void loader_stop(TT_UNUSED Service& service) {
tt_check(loader_singleton != nullptr); tt_check(loader_singleton != nullptr);
// Send stop signal to thread and wait for thread to finish // Send stop signal to thread and wait for thread to finish
loader_lock();
LoaderMessage message(LoaderMessageTypeServiceStop); LoaderMessage message(LoaderMessageTypeServiceStop);
loader_singleton->queue.put(&message, TtWaitForever); loader_singleton->queue.put(&message, TtWaitForever);
thread_join(loader_singleton->thread); loader_unlock();
thread_free(loader_singleton->thread);
loader_singleton->thread->join();
delete loader_singleton->thread;
loader_free(); loader_free();
loader_singleton = nullptr; loader_singleton = nullptr;

View File

@ -126,13 +126,13 @@ static int32_t screenshot_task(void* context) {
static void task_start(ScreenshotTaskData* data) { static void task_start(ScreenshotTaskData* data) {
task_lock(data); task_lock(data);
tt_check(data->thread == NULL); tt_check(data->thread == NULL);
data->thread = thread_alloc_ex( data->thread = new Thread(
"screenshot", "screenshot",
8192, 8192,
&screenshot_task, &screenshot_task,
data data
); );
thread_start(data->thread); data->thread->start();
task_unlock(data); task_unlock(data);
} }
@ -175,10 +175,10 @@ void task_stop(ScreenshotTask* task) {
data->interrupted = true; data->interrupted = true;
task_unlock(data); task_unlock(data);
thread_join(data->thread); data->thread->join();
task_lock(data); task_lock(data);
thread_free(data->thread); delete data->thread;
data->thread = nullptr; data->thread = nullptr;
task_unlock(data); task_unlock(data);
} }

View File

@ -136,7 +136,7 @@ static ServiceData* service_data_alloc() {
auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData))); auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData)));
*data = (ServiceData) { *data = (ServiceData) {
.mutex = tt_mutex_alloc(MutexTypeNormal), .mutex = tt_mutex_alloc(MutexTypeNormal),
.thread = thread_alloc(), .thread = new Thread(),
.service_interrupted = false, .service_interrupted = false,
.wifi_icon_id = lvgl::statusbar_icon_add(nullptr), .wifi_icon_id = lvgl::statusbar_icon_add(nullptr),
.wifi_last_icon = nullptr, .wifi_last_icon = nullptr,
@ -156,7 +156,7 @@ static ServiceData* service_data_alloc() {
static void service_data_free(ServiceData* data) { static void service_data_free(ServiceData* data) {
tt_mutex_free(data->mutex); tt_mutex_free(data->mutex);
thread_free(data->thread); delete data->thread;
lvgl::statusbar_icon_remove(data->wifi_icon_id); lvgl::statusbar_icon_remove(data->wifi_icon_id);
lvgl::statusbar_icon_remove(data->sdcard_icon_id); lvgl::statusbar_icon_remove(data->sdcard_icon_id);
lvgl::statusbar_icon_remove(data->power_icon_id); lvgl::statusbar_icon_remove(data->power_icon_id);
@ -188,11 +188,10 @@ static void on_start(Service& service) {
ServiceData* data = service_data_alloc(); ServiceData* data = service_data_alloc();
service.setData(data); service.setData(data);
thread_set_callback(data->thread, service_main); data->thread->setCallback(service_main, data);
thread_set_current_priority(ThreadPriorityLow); data->thread->setPriority(Thread::PriorityLow);
thread_set_stack_size(data->thread, 3000); data->thread->setStackSize(3000);
thread_set_context(data->thread, data); data->thread->start();
thread_start(data->thread);
} }
static void on_stop(Service& service) { static void on_stop(Service& service) {
@ -203,7 +202,7 @@ static void on_stop(Service& service) {
data->service_interrupted = true; data->service_interrupted = true;
service_data_unlock(data); service_data_unlock(data);
tt_mutex_release(data->mutex); tt_mutex_release(data->mutex);
thread_join(data->thread); data->thread->join();
service_data_free(data); service_data_free(data);
} }

View File

@ -1,20 +1,11 @@
#include "Thread.h" #include "Thread.h"
#include <cstdlib> #include <string>
#include <cstring>
#include "Check.h" #include "Check.h"
#include "CoreDefines.h" #include "CoreDefines.h"
#include "Kernel.h" #include "Kernel.h"
#include "Log.h" #include "Log.h"
#ifdef ESP_PLATFORM
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#else
#include "FreeRTOS.h"
#include "task.h"
#endif
namespace tt { namespace tt {
#define TAG "Thread" #define TAG "Thread"
@ -28,32 +19,15 @@ namespace tt {
#define THREAD_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_TASK_NOTIFY) - 1U)) #define THREAD_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_TASK_NOTIFY) - 1U))
#define EVENT_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_EVENT_GROUPS) - 1U)) #define EVENT_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_EVENT_GROUPS) - 1U))
static_assert(ThreadPriorityHighest <= TT_CONFIG_THREAD_MAX_PRIORITIES, "highest thread priority is higher than max priority"); static_assert(Thread::PriorityHighest <= TT_CONFIG_THREAD_MAX_PRIORITIES, "highest thread priority is higher than max priority");
static_assert(TT_CONFIG_THREAD_MAX_PRIORITIES <= configMAX_PRIORITIES, "highest tactility priority is higher than max FreeRTOS priority"); static_assert(TT_CONFIG_THREAD_MAX_PRIORITIES <= configMAX_PRIORITIES, "highest tactility priority is higher than max FreeRTOS priority");
struct Thread { void setState(Thread::Data* data, Thread::State state) {
ThreadState state; data->state = state;
int32_t ret; if (data->stateCallback) {
data->stateCallback(state, data->stateCallbackContext);
ThreadCallback callback; }
void* context; }
ThreadStateCallback state_callback;
void* state_context;
char* name;
char* appid;
ThreadPriority priority;
TaskHandle_t task_handle;
// Keep all non-alignable byte types in one place,
// this ensures that the size of this structure is minimal
bool is_static;
configSTACK_DEPTH_TYPE stack_size;
};
/** Catch threads that are trying to exit wrong way */ /** Catch threads that are trying to exit wrong way */
__attribute__((__noreturn__)) void thread_catch() { //-V1082 __attribute__((__noreturn__)) void thread_catch() { //-V1082
@ -64,238 +38,178 @@ __attribute__((__noreturn__)) void thread_catch() { //-V1082
__builtin_unreachable(); __builtin_unreachable();
} }
static void thread_set_state(Thread* thread, ThreadState state) {
tt_assert(thread);
thread->state = state;
if (thread->state_callback) {
thread->state_callback(state, thread->state_context);
}
}
static void thread_body(void* context) { static void thread_body(void* context) {
tt_assert(context); tt_assert(context);
auto* thread = static_cast<Thread*>(context); auto* data = static_cast<Thread::Data*>(context);
// Store thread instance to thread local storage // Store thread data instance to thread local storage
tt_assert(pvTaskGetThreadLocalStoragePointer(nullptr, 0) == nullptr); tt_assert(pvTaskGetThreadLocalStoragePointer(nullptr, 0) == nullptr);
vTaskSetThreadLocalStoragePointer(nullptr, 0, thread); vTaskSetThreadLocalStoragePointer(nullptr, 0, data->thread);
tt_assert(thread->state == ThreadStateStarting); tt_assert(data->state == Thread::StateStarting);
thread_set_state(thread, ThreadStateRunning); setState(data, Thread::StateRunning);
data->callbackResult = data->callback(data->callbackContext);
tt_assert(data->state == Thread::StateRunning);
thread->ret = thread->callback(thread->context); if (data->isStatic) {
tt_assert(thread->state == ThreadStateRunning);
if (thread->is_static) {
TT_LOG_I( TT_LOG_I(
TAG, TAG,
"%s static task memory will not be reclaimed", "%s static task memory will not be reclaimed",
thread->name ? thread->name : "<unnamed service>" data->name.empty() ? "<unnamed service>" : data->name.c_str()
); );
} }
thread_set_state(thread, ThreadStateStopped); setState(data, Thread::StateStopped);
vTaskSetThreadLocalStoragePointer(nullptr, 0, nullptr); vTaskSetThreadLocalStoragePointer(nullptr, 0, nullptr);
thread->task_handle = nullptr; data->taskHandle = nullptr;
vTaskDelete(nullptr); vTaskDelete(nullptr);
thread_catch(); thread_catch();
} }
Thread* thread_alloc() { Thread::Thread() :
auto* thread = static_cast<Thread*>(malloc(sizeof(Thread))); data({
// TODO: create default struct instead of using memset() .taskHandle = nullptr,
memset(thread, 0, sizeof(Thread)); .state = StateStopped,
thread->is_static = false; .callback = nullptr,
.callbackContext = nullptr,
.callbackResult = 0,
.stateCallback = nullptr,
.stateCallbackContext = nullptr,
.name = std::string(),
.priority = PriorityNormal,
.isStatic = false,
.stackSize = 0,
}) { }
Thread* parent = nullptr; Thread::Thread(
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) { const std::string& name,
// TLS is not available, if we called not from thread context configSTACK_DEPTH_TYPE stackSize,
parent = (Thread*)pvTaskGetThreadLocalStoragePointer(nullptr, 0); Callback callback,
_Nullable void* callbackContext) :
if (parent && parent->appid) { data({
thread_set_appid(thread, parent->appid); .taskHandle = nullptr,
} else { .state = StateStopped,
thread_set_appid(thread, "unknown"); .callback = callback,
} .callbackContext = callbackContext,
} else { .callbackResult = 0,
// If scheduler is not started, we are starting driver thread .stateCallback = nullptr,
thread_set_appid(thread, "driver"); .stateCallbackContext = nullptr,
} .name = name,
.priority = PriorityNormal,
return thread; .isStatic = false,
} .stackSize = stackSize,
}) { }
Thread* thread_alloc_ex(
const char* name,
uint32_t stack_size,
ThreadCallback callback,
void* context
) {
Thread* thread = thread_alloc();
thread_set_name(thread, name);
thread_set_stack_size(thread, stack_size);
thread_set_callback(thread, callback);
thread_set_context(thread, context);
return thread;
}
void thread_free(Thread* thread) {
tt_assert(thread);
Thread::~Thread() {
// Ensure that use join before free // Ensure that use join before free
tt_assert(thread->state == ThreadStateStopped); tt_assert(data.state == StateStopped);
tt_assert(thread->task_handle == nullptr); tt_assert(data.taskHandle == nullptr);
if (thread->name) free(thread->name);
if (thread->appid) free(thread->appid);
free(thread);
} }
void thread_set_name(Thread* thread, const char* name) { void Thread::setName(const std::string& newName) {
tt_assert(thread); tt_assert(data.state == StateStopped);
tt_assert(thread->state == ThreadStateStopped); data.name = newName;
if (thread->name) free(thread->name);
thread->name = name ? strdup(name) : nullptr;
} }
void thread_set_appid(Thread* thread, const char* appid) {
tt_assert(thread); void Thread::markAsStatic() {
tt_assert(thread->state == ThreadStateStopped); data.isStatic = true;
if (thread->appid) free(thread->appid);
thread->appid = appid ? strdup(appid) : nullptr;
} }
void thread_mark_as_static(Thread* thread) { bool Thread::isMarkedAsStatic() const {
thread->is_static = true; return data.isStatic;
} }
bool thread_mark_is_static(ThreadId thread_id) { void Thread::setStackSize(size_t stackSize) {
auto hTask = (TaskHandle_t)thread_id; tt_assert(data.state == StateStopped);
assert(!TT_IS_IRQ_MODE() && (hTask != nullptr)); tt_assert(stackSize % 4 == 0);
auto* thread = (Thread*)pvTaskGetThreadLocalStoragePointer(hTask, 0); data.stackSize = stackSize;
assert(thread != nullptr);
return thread->is_static;
} }
void thread_set_stack_size(Thread* thread, size_t stack_size) { void Thread::setCallback(Callback callback, _Nullable void* callbackContext) {
tt_assert(thread); tt_assert(data.state == StateStopped);
tt_assert(thread->state == ThreadStateStopped); data.callback = callback;
tt_assert(stack_size % 4 == 0); data.callbackContext = callbackContext;
thread->stack_size = stack_size;
} }
void thread_set_callback(Thread* thread, ThreadCallback callback) {
tt_assert(thread);
tt_assert(thread->state == ThreadStateStopped);
thread->callback = callback;
}
void thread_set_context(Thread* thread, void* context) { void Thread::setPriority(Priority priority) {
tt_assert(thread); tt_assert(data.state == StateStopped);
tt_assert(thread->state == ThreadStateStopped);
thread->context = context;
}
void thread_set_priority(Thread* thread, ThreadPriority priority) {
tt_assert(thread);
tt_assert(thread->state == ThreadStateStopped);
tt_assert(priority >= 0 && priority <= TT_CONFIG_THREAD_MAX_PRIORITIES); tt_assert(priority >= 0 && priority <= TT_CONFIG_THREAD_MAX_PRIORITIES);
thread->priority = priority; data.priority = priority;
} }
void thread_set_current_priority(ThreadPriority priority) {
UBaseType_t new_priority = priority ? priority : ThreadPriorityNormal; void Thread::setStateCallback(StateCallback callback, _Nullable void* callbackContext) {
vTaskPrioritySet(nullptr, new_priority); tt_assert(data.state == StateStopped);
data.stateCallback = callback;
data.stateCallbackContext = callbackContext;
} }
ThreadPriority thread_get_current_priority() { Thread::State Thread::getState() const {
return (ThreadPriority)uxTaskPriorityGet(nullptr); return data.state;
} }
void thread_set_state_callback(Thread* thread, ThreadStateCallback callback) { void Thread::start() {
tt_assert(thread); tt_assert(data.callback);
tt_assert(thread->state == ThreadStateStopped); tt_assert(data.state == StateStopped);
thread->state_callback = callback; tt_assert(data.stackSize > 0 && data.stackSize < (UINT16_MAX * sizeof(StackType_t)));
}
void thread_set_state_context(Thread* thread, void* context) { setState(&data, StateStarting);
tt_assert(thread);
tt_assert(thread->state == ThreadStateStopped);
thread->state_context = context;
}
ThreadState thread_get_state(Thread* thread) { uint32_t stack_depth = data.stackSize / sizeof(StackType_t);
tt_assert(thread); if (data.isStatic) {
return thread->state;
}
void thread_start(Thread* thread) {
tt_assert(thread);
tt_assert(thread->callback);
tt_assert(thread->state == ThreadStateStopped);
tt_assert(thread->stack_size > 0 && thread->stack_size < (UINT16_MAX * sizeof(StackType_t)));
thread_set_state(thread, ThreadStateStarting);
uint32_t stack = thread->stack_size / sizeof(StackType_t);
UBaseType_t priority = thread->priority ? thread->priority : ThreadPriorityNormal;
if (thread->is_static) {
#if configSUPPORT_STATIC_ALLOCATION == 1 #if configSUPPORT_STATIC_ALLOCATION == 1
thread->task_handle = xTaskCreateStatic( data.taskHandle = xTaskCreateStatic(
thread_body, thread_body,
thread->name, data.name.c_str(),
stack, stack_depth,
thread, &data,
priority, data.priority,
static_cast<StackType_t*>(malloc(sizeof(StackType_t) * stack)), static_cast<StackType_t*>(malloc(sizeof(StackType_t) * stack_depth)),
static_cast<StaticTask_t*>(malloc(sizeof(StaticTask_t))) static_cast<StaticTask_t*>(malloc(sizeof(StaticTask_t)))
); );
#else #else
TT_LOG_E(TAG, "static tasks are not supported by current FreeRTOS config/platform - creating regular one"); TT_LOG_E(TAG, "static tasks are not supported by current FreeRTOS config/platform - creating regular one");
BaseType_t ret = xTaskCreate( BaseType_t result = xTaskCreate(
thread_body, thread->name, stack, thread, priority, &(thread->task_handle) thread_body, data.name.c_str(), stack_depth, this, data.priority, &(data.taskHandle)
); );
tt_check(ret == pdPASS); tt_check(result == pdPASS);
#endif #endif
} else { } else {
BaseType_t ret = xTaskCreate( BaseType_t result = xTaskCreate(
thread_body, thread->name, stack, thread, priority, &(thread->task_handle) thread_body, data.name.c_str(), stack_depth, this, data.priority, &(data.taskHandle)
); );
tt_check(ret == pdPASS); tt_check(result == pdPASS);
} }
tt_check(thread->state == ThreadStateStopped || thread->task_handle); tt_check(data.state == StateStopped || data.taskHandle);
} }
bool thread_join(Thread* thread) { bool Thread::join() {
tt_assert(thread); tt_check(thread_get_current() != this);
tt_check(thread_get_current() != thread);
// !!! IMPORTANT NOTICE !!! // !!! IMPORTANT NOTICE !!!
// //
// If your thread exited, but your app stuck here: some other thread uses // If your thread exited, but your app stuck here: some other thread uses
// all cpu time, which delays kernel from releasing task handle // all cpu time, which delays kernel from releasing task handle
while (thread->task_handle) { while (data.taskHandle) {
delay_ms(10); delay_ms(10);
} }
return true; return true;
} }
ThreadId thread_get_id(Thread* thread) { ThreadId Thread::getId() {
tt_assert(thread); return data.taskHandle;
return thread->task_handle;
} }
int32_t thread_get_return_code(Thread* thread) { int32_t Thread::getReturnCode() {
tt_assert(thread); tt_assert(data.state == StateStopped);
tt_assert(thread->state == ThreadStateStopped); return data.callbackResult;
return thread->ret;
} }
ThreadId thread_get_current_id() { ThreadId thread_get_current_id() {
@ -307,11 +221,28 @@ Thread* thread_get_current() {
return thread; return thread;
} }
void thread_set_current_priority(Thread::Priority priority) {
UBaseType_t new_priority = priority ? priority : Thread::PriorityNormal;
vTaskPrioritySet(nullptr, new_priority);
}
Thread::Priority thread_get_current_priority() {
return (Thread::Priority)uxTaskPriorityGet(nullptr);
}
void thread_yield() { void thread_yield() {
tt_assert(!TT_IS_IRQ_MODE()); tt_assert(!TT_IS_IRQ_MODE());
taskYIELD(); taskYIELD();
} }
bool thread_mark_is_static(ThreadId thread_id) {
auto hTask = (TaskHandle_t)thread_id;
assert(!TT_IS_IRQ_MODE() && (hTask != nullptr));
auto* thread = (Thread*)pvTaskGetThreadLocalStoragePointer(hTask, 0);
assert(thread != nullptr);
return thread->isMarkedAsStatic();
}
uint32_t thread_flags_set(ThreadId thread_id, uint32_t flags) { uint32_t thread_flags_set(ThreadId thread_id, uint32_t flags) {
auto hTask = (TaskHandle_t)thread_id; auto hTask = (TaskHandle_t)thread_id;
uint32_t rflags; uint32_t rflags;
@ -470,20 +401,6 @@ const char* thread_get_name(ThreadId thread_id) {
return (name); return (name);
} }
const char* thread_get_appid(ThreadId thread_id) {
auto hTask = (TaskHandle_t)thread_id;
const char* appid = "system";
if (!TT_IS_IRQ_MODE() && (hTask != nullptr)) {
auto* thread = (Thread*)pvTaskGetThreadLocalStoragePointer(hTask, 0);
if (thread) {
appid = thread->appid;
}
}
return (appid);
}
uint32_t thread_get_stack_space(ThreadId thread_id) { uint32_t thread_get_stack_space(ThreadId thread_id) {
auto hTask = (TaskHandle_t)thread_id; auto hTask = (TaskHandle_t)thread_id;
uint32_t sz; uint32_t sz;

View File

@ -5,162 +5,148 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <string>
#ifdef ESP_PLATFORM
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#else
#include "FreeRTOS.h"
#include "task.h"
#endif
namespace tt { namespace tt {
/** ThreadState */ typedef TaskHandle_t ThreadId;
class Thread {
public:
typedef enum { typedef enum {
ThreadStateStopped, StateStopped,
ThreadStateStarting, StateStarting,
ThreadStateRunning, StateRunning,
} ThreadState; } State;
/** ThreadPriority */ /** ThreadPriority */
typedef enum { typedef enum {
ThreadPriorityNone = 0, /**< Uninitialized, choose system default */ PriorityNone = 0, /**< Uninitialized, choose system default */
ThreadPriorityIdle = 1, PriorityIdle = 1,
ThreadPriorityLowest = 2, PriorityLowest = 2,
ThreadPriorityLow = 3, PriorityLow = 3,
ThreadPriorityNormal = 4, PriorityNormal = 4,
ThreadPriorityHigh = 5, PriorityHigh = 5,
ThreadPriorityHigher = 6, PriorityHigher = 6,
ThreadPriorityHighest = 7 PriorityHighest = 7
} ThreadPriority; } Priority;
#define THREAD_PRIORITY_APP ThreadPriorityNormal
#define THREAD_PRIORITY_SERVICE ThreadPriorityHigh
#define THREAD_PRIORITY_RENDER ThreadPriorityHigher
#define THREAD_PRIORITY_ISR (TT_CONFIG_THREAD_MAX_PRIORITIES - 1)
/** Thread anonymous structure */
typedef struct Thread Thread;
/** ThreadId proxy type to OS low level functions */
typedef void* ThreadId;
/** ThreadCallback Your callback to run in new thread /** ThreadCallback Your callback to run in new thread
* @warning never use osThreadExit in Thread * @warning never use osThreadExit in Thread
*/ */
typedef int32_t (*ThreadCallback)(void* context); typedef int32_t (*Callback)(void* context);
/** Write to stdout callback /** Write to stdout callback
* @param data pointer to data * @param data pointer to data
* @param size data size @warning your handler must consume everything * @param size data size @warning your handler must consume everything
*/ */
typedef void (*ThreadStdoutWriteCallback)(const char* data, size_t size); typedef void (*StdoutWriteCallback)(const char* data, size_t size);
/** Thread state change callback called upon thread state change /** Thread state change callback called upon thread state change
* @param state new thread state * @param state new thread state
* @param context callback context * @param context callback context
*/ */
typedef void (*ThreadStateCallback)(ThreadState state, void* context); typedef void (*StateCallback)(State state, void* context);
/** Allocate Thread typedef struct {
* Thread* thread;
* @return Thread instance TaskHandle_t taskHandle;
*/
Thread* thread_alloc(); State state;
Callback callback;
void* callbackContext;
int32_t callbackResult;
StateCallback stateCallback;
void* stateCallbackContext;
std::string name;
Priority priority;
// Keep all non-alignable byte types in one place,
// this ensures that the size of this structure is minimal
bool isStatic;
configSTACK_DEPTH_TYPE stackSize;
} Data;
Thread();
/** Allocate Thread, shortcut version /** Allocate Thread, shortcut version
*
* @param name * @param name
* @param stack_size * @param stack_size
* @param callback * @param callback
* @param context * @param context
* @return Thread* * @return Thread*
*/ */
Thread* thread_alloc_ex( Thread(
const char* name, const std::string& name,
uint32_t stack_size, configSTACK_DEPTH_TYPE stackSize,
ThreadCallback callback, Callback callback,
void* context _Nullable void* callbackContext
); );
/** Release Thread ~Thread();
*
* @warning see tt_thread_join
*
* @param thread Thread instance
*/
void thread_free(Thread* thread);
/** Set Thread name /** Set Thread name
* *
* @param thread Thread instance
* @param name string * @param name string
*/ */
void thread_set_name(Thread* thread, const char* name); void setName(const std::string& name);
/**
* @brief Set Thread appid
* Technically, it is like a "process id", but it is not a system-wide unique identifier.
* All threads spawned by the same app will have the same appid.
*
* @param thread
* @param appid
*/
void thread_set_appid(Thread* thread, const char* appid);
/** Mark thread as service /** Mark thread as service
* The service cannot be stopped or removed, and cannot exit from the thread body * The service cannot be stopped or removed, and cannot exit from the thread body
*
* @param thread
*/ */
void thread_mark_as_static(Thread* thread); void markAsStatic();
/** Check if thread is as service
* If true, the service cannot be stopped or removed, and cannot exit from the thread body
*/
bool isMarkedAsStatic() const;
/** Set Thread stack size /** Set Thread stack size
* *
* @param thread Thread instance * @param thread Thread instance
* @param stack_size stack size in bytes * @param stackSize stack size in bytes
*/ */
void thread_set_stack_size(Thread* thread, size_t stack_size); void setStackSize(size_t stackSize);
/** Set Thread callback /** Set Thread callback
* *
* @param thread Thread instance * @param thread Thread instance
* @param callback ThreadCallback, called upon thread run * @param callback ThreadCallback, called upon thread run
* @param callbackContext what to pass to the callback
*/ */
void thread_set_callback(Thread* thread, ThreadCallback callback); void setCallback(Callback callback, _Nullable void* callbackContext = nullptr);
/** Set Thread context
*
* @param thread Thread instance
* @param context pointer to context for thread callback
*/
void thread_set_context(Thread* thread, void* context);
/** Set Thread priority /** Set Thread priority
* *
* @param thread Thread instance * @param thread Thread instance
* @param priority ThreadPriority value * @param priority ThreadPriority value
*/ */
void thread_set_priority(Thread* thread, ThreadPriority priority); void setPriority(Priority priority);
/** Set current thread priority
*
* @param priority ThreadPriority value
*/
void thread_set_current_priority(ThreadPriority priority);
/** Get current thread priority
*
* @return ThreadPriority value
*/
ThreadPriority thread_get_current_priority();
/** Set Thread state change callback /** Set Thread state change callback
* *
* @param thread Thread instance * @param thread Thread instance
* @param callback state change callback * @param callback state change callback
*/
void thread_set_state_callback(Thread* thread, ThreadStateCallback callback);
/** Set Thread state change context
*
* @param thread Thread instance
* @param context pointer to context * @param context pointer to context
*/ */
void thread_set_state_context(Thread* thread, void* context); void setStateCallback(StateCallback callback, _Nullable void* callbackContext = nullptr);
/** Get Thread state /** Get Thread state
* *
@ -168,13 +154,13 @@ void thread_set_state_context(Thread* thread, void* context);
* *
* @return thread state from ThreadState * @return thread state from ThreadState
*/ */
ThreadState thread_get_state(Thread* thread); [[nodiscard]] State getState() const;
/** Start Thread /** Start Thread
* *
* @param thread Thread instance * @param thread Thread instance
*/ */
void thread_start(Thread* thread); void start();
/** Join Thread /** Join Thread
* *
@ -183,17 +169,17 @@ void thread_start(Thread* thread);
* *
* @param thread Thread instance * @param thread Thread instance
* *
* @return bool * @return success result
*/ */
bool thread_join(Thread* thread); bool join();
/** Get FreeRTOS ThreadId for Thread instance /** Get FreeRTOS ThreadId for Thread instance
* *
* @param thread Thread instance * @param thread Thread instance
* *
* @return ThreadId or NULL * @return ThreadId or nullptr
*/ */
ThreadId thread_get_id(Thread* thread); ThreadId getId();
/** Get thread return code /** Get thread return code
* *
@ -201,7 +187,29 @@ ThreadId thread_get_id(Thread* thread);
* *
* @return return code * @return return code
*/ */
int32_t thread_get_return_code(Thread* thread); int32_t getReturnCode();
private:
Data data;
};
#define THREAD_PRIORITY_APP Thread::PriorityNormal
#define THREAD_PRIORITY_SERVICE Thread::PriorityHigh
#define THREAD_PRIORITY_RENDER Thread::PriorityHigher
#define THREAD_PRIORITY_ISR (TT_CONFIG_THREAD_MAX_PRIORITIES - 1)
/** Set current thread priority
*
* @param priority ThreadPriority value
*/
void thread_set_current_priority(Thread::Priority priority);
/** Get current thread priority
*
* @return ThreadPriority value
*/
Thread::Priority thread_get_current_priority();
/** Thread related methods that doesn't involve Thread directly */ /** Thread related methods that doesn't involve Thread directly */
@ -238,14 +246,6 @@ uint32_t thread_flags_wait(uint32_t flags, uint32_t options, uint32_t timeout);
*/ */
const char* thread_get_name(ThreadId thread_id); const char* thread_get_name(ThreadId thread_id);
/**
* @brief Get thread appid
*
* @param thread_id
* @return const char* appid
*/
const char* thread_get_appid(ThreadId thread_id);
/** /**
* @brief Get thread stack watermark * @brief Get thread stack watermark
* *

View File

@ -1,148 +1,89 @@
#include "Timer.h" #include "Timer.h"
#include "Check.h" #include "Check.h"
#include "Kernel.h" #include "Kernel.h"
#include <cstdlib>
#ifdef ESP_PLATFORM
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#else
#include "FreeRTOS.h"
#include "timers.h"
#endif
namespace tt { namespace tt {
typedef struct {
TimerCallback func;
void* context;
} TimerCallback_t;
static void timer_callback(TimerHandle_t hTimer) { static void timer_callback(TimerHandle_t hTimer) {
auto* callback = static_cast<TimerCallback_t*>(pvTimerGetTimerID(hTimer)); auto* timer = static_cast<Timer*>(pvTimerGetTimerID(hTimer));
if (callback != nullptr) { if (timer != nullptr) {
callback->func(callback->context); timer->callback(timer->callbackContext);
} }
} }
Timer* timer_alloc(TimerCallback func, TimerType type, void* context) { Timer::Timer(Type type, Callback callback, void* callbackContext) {
tt_assert((kernel_is_irq() == 0U) && (func != nullptr)); tt_assert((kernel_is_irq() == 0U) && (callback != nullptr));
auto* callback = static_cast<TimerCallback_t*>(malloc(sizeof(TimerCallback_t)));
callback->func = func; this->callback = callback;
callback->context = context; this->callbackContext = callbackContext;
UBaseType_t reload; UBaseType_t reload;
if (type == TimerTypeOnce) { if (type == TypeOnce) {
reload = pdFALSE; reload = pdFALSE;
} else { } else {
reload = pdTRUE; reload = pdTRUE;
} }
// TimerCallback function is always provided as a callback and is used to call application this->timerHandle = xTimerCreate(nullptr, portMAX_DELAY, (BaseType_t)reload, this, timer_callback);
// specified function with its context both stored in structure callb. tt_assert(this->timerHandle);
// TODO: should we use pointer to function or function directly as-is?
TimerHandle_t hTimer = xTimerCreate(nullptr, portMAX_DELAY, (BaseType_t)reload, callback, timer_callback);
tt_assert(hTimer);
/* Return timer ID */
return (Timer*)hTimer;
} }
void timer_free(Timer* instance) { Timer::~Timer() {
tt_assert(!kernel_is_irq()); tt_assert(!kernel_is_irq());
tt_assert(instance); tt_check(xTimerDelete(timerHandle, portMAX_DELAY) == pdPASS);
auto hTimer = static_cast<TimerHandle_t>(instance);
auto* callback = static_cast<TimerCallback_t*>(pvTimerGetTimerID(hTimer));
tt_check(xTimerDelete(hTimer, portMAX_DELAY) == pdPASS);
while (timer_is_running(instance)) delay_tick(2);
/* Return allocated memory to dynamic pool */
free(callback);
} }
TtStatus timer_start(Timer* instance, uint32_t ticks) { TtStatus Timer::start(uint32_t ticks) {
tt_assert(!kernel_is_irq()); tt_assert(!kernel_is_irq());
tt_assert(instance);
tt_assert(ticks < portMAX_DELAY); tt_assert(ticks < portMAX_DELAY);
auto hTimer = static_cast<TimerHandle_t>(instance); if (xTimerChangePeriod(timerHandle, ticks, portMAX_DELAY) == pdPASS) {
TtStatus stat; return TtStatusOk;
if (xTimerChangePeriod(hTimer, ticks, portMAX_DELAY) == pdPASS) {
stat = TtStatusOk;
} else { } else {
stat = TtStatusErrorResource; return TtStatusErrorResource;
}
} }
/* Return execution status */ TtStatus Timer::restart(uint32_t ticks) {
return (stat);
}
TtStatus timer_restart(Timer* instance, uint32_t ticks) {
tt_assert(!kernel_is_irq()); tt_assert(!kernel_is_irq());
tt_assert(instance);
tt_assert(ticks < portMAX_DELAY); tt_assert(ticks < portMAX_DELAY);
auto hTimer = static_cast<TimerHandle_t>(instance); if (xTimerChangePeriod(timerHandle, ticks, portMAX_DELAY) == pdPASS &&
TtStatus stat; xTimerReset(timerHandle, portMAX_DELAY) == pdPASS) {
return TtStatusOk;
if (xTimerChangePeriod(hTimer, ticks, portMAX_DELAY) == pdPASS &&
xTimerReset(hTimer, portMAX_DELAY) == pdPASS) {
stat = TtStatusOk;
} else { } else {
stat = TtStatusErrorResource; return TtStatusErrorResource;
}
} }
/* Return execution status */ TtStatus Timer::stop() {
return (stat);
}
TtStatus timer_stop(Timer* instance) {
tt_assert(!kernel_is_irq()); tt_assert(!kernel_is_irq());
tt_assert(instance); tt_check(xTimerStop(timerHandle, portMAX_DELAY) == pdPASS);
auto hTimer = static_cast<TimerHandle_t>(instance);
tt_check(xTimerStop(hTimer, portMAX_DELAY) == pdPASS);
return TtStatusOk; return TtStatusOk;
} }
uint32_t timer_is_running(Timer* instance) { bool Timer::isRunning() {
tt_assert(!kernel_is_irq()); tt_assert(!kernel_is_irq());
tt_assert(instance); return xTimerIsTimerActive(timerHandle) == pdTRUE;
auto hTimer = static_cast<TimerHandle_t>(instance);
/* Return 0: not running, 1: running */
return (uint32_t)xTimerIsTimerActive(hTimer);
} }
uint32_t timer_get_expire_time(Timer* instance) { uint32_t Timer::getExpireTime() {
tt_assert(!kernel_is_irq()); tt_assert(!kernel_is_irq());
tt_assert(instance); return (uint32_t)xTimerGetExpiryTime(timerHandle);
auto hTimer = static_cast<TimerHandle_t>(instance);
return (uint32_t)xTimerGetExpiryTime(hTimer);
} }
void timer_pending_callback(TimerPendigCallback callback, void* context, uint32_t arg) { void Timer::pendingCallback(PendigCallback callback, void* callbackContext, uint32_t arg) {
BaseType_t ret = pdFAIL; BaseType_t ret = pdFAIL;
if (kernel_is_irq()) { if (kernel_is_irq()) {
ret = xTimerPendFunctionCallFromISR(callback, context, arg, nullptr); ret = xTimerPendFunctionCallFromISR(callback, callbackContext, arg, nullptr);
} else { } else {
ret = xTimerPendFunctionCall(callback, context, arg, TtWaitForever); ret = xTimerPendFunctionCall(callback, callbackContext, arg, TtWaitForever);
} }
tt_assert(ret == pdPASS); tt_assert(ret == pdPASS);
} }
void timer_set_thread_priority(TimerThreadPriority priority) { void Timer::setThreadPriority(TimerThreadPriority priority) {
tt_assert(!kernel_is_irq()); tt_assert(!kernel_is_irq());
TaskHandle_t task_handle = xTimerGetTimerDaemonTaskHandle(); TaskHandle_t task_handle = xTimerGetTimerDaemonTaskHandle();

View File

@ -2,67 +2,73 @@
#include "CoreTypes.h" #include "CoreTypes.h"
#ifdef ESP_PLATFORM
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#else
#include "FreeRTOS.h"
#include "timers.h"
#endif
namespace tt { namespace tt {
typedef void (*TimerCallback)(void* context);
class Timer {
private:
TimerHandle_t timerHandle;
public:
typedef void (*Callback)(void* context);
typedef void (*PendigCallback)(void* context, uint32_t arg);
Callback callback;
void* callbackContext;
typedef enum { typedef enum {
TimerTypeOnce = 0, ///< One-shot timer. TypeOnce = 0, ///< One-shot timer.
TimerTypePeriodic = 1 ///< Repeating timer. TypePeriodic = 1 ///< Repeating timer.
} TimerType; } Type;
typedef void Timer; /**
/** Allocate timer
*
* @param[in] func The callback function
* @param[in] type The timer type * @param[in] type The timer type
* @param context The callback context * @param[in] callback The callback function
* * @param callbackContext The callback context
* @return The pointer to Timer instance
*/ */
Timer* timer_alloc(TimerCallback func, TimerType type, void* context); Timer(Type type, Callback callback, void* callbackContext);
/** Free timer ~Timer();
*
* @param instance The pointer to Timer instance
*/
void timer_free(Timer* instance);
/** Start timer /** Start timer
* *
* @warning This is asynchronous call, real operation will happen as soon as * @warning This is asynchronous call, real operation will happen as soon as
* timer service process this request. * timer service process this request.
* *
* @param instance The pointer to Timer instance
* @param[in] ticks The interval in ticks * @param[in] ticks The interval in ticks
*
* @return The status. * @return The status.
*/ */
TtStatus timer_start(Timer* instance, uint32_t ticks); TtStatus start(uint32_t ticks);
/** Restart timer with previous timeout value /** Restart timer with previous timeout value
* *
* @warning This is asynchronous call, real operation will happen as soon as * @warning This is asynchronous call, real operation will happen as soon as
* timer service process this request. * timer service process this request.
* *
* @param instance The pointer to Timer instance
* @param[in] ticks The interval in ticks * @param[in] ticks The interval in ticks
* *
* @return The status. * @return The status.
*/ */
TtStatus timer_restart(Timer* instance, uint32_t ticks); TtStatus restart(uint32_t ticks);
/** Stop timer /** Stop timer
* *
* @warning This is asynchronous call, real operation will happen as soon as * @warning This is asynchronous call, real operation will happen as soon as
* timer service process this request. * timer service process this request.
* *
* @param instance The pointer to Timer instance
*
* @return The status. * @return The status.
*/ */
TtStatus timer_stop(Timer* instance); TtStatus stop();
/** Is timer running /** Is timer running
* *
@ -70,11 +76,9 @@ TtStatus timer_stop(Timer* instance);
* commands are still in the queue. Please read FreeRTOS timer * commands are still in the queue. Please read FreeRTOS timer
* documentation first. * documentation first.
* *
* @param instance The pointer to Timer instance * @return true when running
*
* @return 0: not running, 1: running
*/ */
uint32_t timer_is_running(Timer* instance); bool isRunning();
/** Get timer expire time /** Get timer expire time
* *
@ -82,11 +86,9 @@ uint32_t timer_is_running(Timer* instance);
* *
* @return expire tick * @return expire tick
*/ */
uint32_t timer_get_expire_time(Timer* instance); uint32_t getExpireTime();
typedef void (*TimerPendigCallback)(void* context, uint32_t arg); void pendingCallback(PendigCallback callback, void* callbackContext, uint32_t arg);
void timer_pending_callback(TimerPendigCallback callback, void* context, uint32_t arg);
typedef enum { typedef enum {
TimerThreadPriorityNormal, /**< Lower then other threads */ TimerThreadPriorityNormal, /**< Lower then other threads */
@ -97,6 +99,12 @@ typedef enum {
* *
* @param[in] priority The priority * @param[in] priority The priority
*/ */
void timer_set_thread_priority(TimerThreadPriority priority); void setThreadPriority(TimerThreadPriority priority);
};
} // namespace } // namespace

View File

@ -22,7 +22,7 @@ static ServiceData* service_data_alloc() {
auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData))); auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData)));
*data = (ServiceData) { *data = (ServiceData) {
.mutex = tt_mutex_alloc(MutexTypeNormal), .mutex = tt_mutex_alloc(MutexTypeNormal),
.thread = thread_alloc_ex( .thread = new Thread(
"sdcard", "sdcard",
3000, // Minimum is ~2800 @ ESP-IDF 5.1.2 when ejecting sdcard 3000, // Minimum is ~2800 @ ESP-IDF 5.1.2 when ejecting sdcard
&sdcard_task, &sdcard_task,
@ -31,13 +31,13 @@ static ServiceData* service_data_alloc() {
.last_state = hal::sdcard::StateUnmounted, .last_state = hal::sdcard::StateUnmounted,
.interrupted = false .interrupted = false
}; };
thread_set_priority(data->thread, ThreadPriorityLow); data->thread->setPriority(Thread::PriorityLow);
return data; return data;
} }
static void service_data_free(ServiceData* data) { static void service_data_free(ServiceData* data) {
tt_mutex_free(data->mutex); tt_mutex_free(data->mutex);
thread_free(data->thread); delete data->thread;
} }
static void service_data_lock(ServiceData* data) { static void service_data_lock(ServiceData* data) {
@ -79,7 +79,7 @@ static void on_start(Service& service) {
if (get_hardware_config()->sdcard != nullptr) { if (get_hardware_config()->sdcard != nullptr) {
ServiceData* data = service_data_alloc(); ServiceData* data = service_data_alloc();
service.setData(data); service.setData(data);
thread_start(data->thread); data->thread->start();
} else { } else {
TT_LOG_I(TAG, "task not started due to config"); TT_LOG_I(TAG, "task not started due to config");
} }
@ -92,7 +92,7 @@ static void on_stop(Service& service) {
data->interrupted = true; data->interrupted = true;
service_data_unlock(data); service_data_unlock(data);
thread_join(data->thread); data->thread->join();
service_data_free(data); service_data_free(data);
} }

View File

@ -7,6 +7,9 @@ set(CMAKE_CXX_COMPILER g++)
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/*.cpp) file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/*.cpp)
add_executable(TactilityCoreTests EXCLUDE_FROM_ALL ${TEST_SOURCES}) add_executable(TactilityCoreTests EXCLUDE_FROM_ALL ${TEST_SOURCES})
add_definitions(-D_Nullable=)
add_definitions(-D_Nonnull=)
target_include_directories(TactilityCoreTests PRIVATE target_include_directories(TactilityCoreTests PRIVATE
${DOCTESTINC} ${DOCTESTINC}
) )

View File

@ -14,23 +14,23 @@ TEST_CASE("a mutex can block a thread") {
auto* mutex = tt_mutex_alloc(MutexTypeNormal); auto* mutex = tt_mutex_alloc(MutexTypeNormal);
tt_mutex_acquire(mutex, TtWaitForever); tt_mutex_acquire(mutex, TtWaitForever);
Thread* thread = thread_alloc_ex( Thread* thread = new Thread(
"thread", "thread",
1024, 1024,
&thread_with_mutex_parameter, &thread_with_mutex_parameter,
mutex mutex
); );
thread_start(thread); thread->start();
delay_ms(5); delay_ms(5);
CHECK_EQ(thread_get_state(thread), ThreadStateRunning); CHECK_EQ(thread->getState(), Thread::StateRunning);
tt_mutex_release(mutex); tt_mutex_release(mutex);
delay_ms(5); delay_ms(5);
CHECK_EQ(thread_get_state(thread), ThreadStateStopped); CHECK_EQ(thread->getState(), Thread::StateStopped);
thread_join(thread); thread->join();
thread_free(thread); delete thread;
tt_mutex_free(mutex); tt_mutex_free(mutex);
} }

View File

@ -25,79 +25,79 @@ static int thread_with_return_code(void* parameter) {
TEST_CASE("when a thread is started then its callback should be called") { TEST_CASE("when a thread is started then its callback should be called") {
bool has_called = false; bool has_called = false;
auto* thread = thread_alloc_ex( auto* thread = new Thread(
"immediate return task", "immediate return task",
4096, 4096,
&immediate_return_thread, &immediate_return_thread,
&has_called &has_called
); );
CHECK(!has_called); CHECK(!has_called);
thread_start(thread); thread->start();
thread_join(thread); thread->join();
thread_free(thread); delete thread;
CHECK(has_called); CHECK(has_called);
} }
TEST_CASE("a thread can be started and stopped") { TEST_CASE("a thread can be started and stopped") {
bool interrupted = false; bool interrupted = false;
auto* thread = thread_alloc_ex( auto* thread = new Thread(
"interruptable thread", "interruptable thread",
4096, 4096,
&interruptable_thread, &interruptable_thread,
&interrupted &interrupted
); );
CHECK(thread); CHECK(thread);
thread_start(thread); thread->start();
interrupted = true; interrupted = true;
thread_join(thread); thread->join();
thread_free(thread); delete thread;
} }
TEST_CASE("thread id should only be set at when thread is started") { TEST_CASE("thread id should only be set at when thread is started") {
bool interrupted = false; bool interrupted = false;
auto* thread = thread_alloc_ex( auto* thread = new Thread(
"interruptable thread", "interruptable thread",
4096, 4096,
&interruptable_thread, &interruptable_thread,
&interrupted &interrupted
); );
CHECK_EQ(thread_get_id(thread), nullptr); CHECK_EQ(thread->getId(), nullptr);
thread_start(thread); thread->start();
CHECK_NE(thread_get_id(thread), nullptr); CHECK_NE(thread->getId(), nullptr);
interrupted = true; interrupted = true;
thread_join(thread); thread->join();
CHECK_EQ(thread_get_id(thread), nullptr); CHECK_EQ(thread->getId(), nullptr);
thread_free(thread); delete thread;
} }
TEST_CASE("thread state should be correct") { TEST_CASE("thread state should be correct") {
bool interrupted = false; bool interrupted = false;
auto* thread = thread_alloc_ex( auto* thread = new Thread(
"interruptable thread", "interruptable thread",
4096, 4096,
&interruptable_thread, &interruptable_thread,
&interrupted &interrupted
); );
CHECK_EQ(thread_get_state(thread), ThreadStateStopped); CHECK_EQ(thread->getState(), Thread::StateStopped);
thread_start(thread); thread->start();
ThreadState state = thread_get_state(thread); Thread::State state = thread->getState();
CHECK((state == ThreadStateStarting || state == ThreadStateRunning)); CHECK((state == Thread::StateStarting || state == Thread::StateRunning));
interrupted = true; interrupted = true;
thread_join(thread); thread->join();
CHECK_EQ(thread_get_state(thread), ThreadStateStopped); CHECK_EQ(thread->getState(), Thread::StateStopped);
thread_free(thread); delete thread;
} }
TEST_CASE("thread id should only be set at when thread is started") { TEST_CASE("thread id should only be set at when thread is started") {
int code = 123; int code = 123;
auto* thread = thread_alloc_ex( auto* thread = new Thread(
"return code", "return code",
4096, 4096,
&thread_with_return_code, &thread_with_return_code,
&code &code
); );
thread_start(thread); thread->start();
thread_join(thread); thread->join();
CHECK_EQ(thread_get_return_code(thread), code); CHECK_EQ(thread->getReturnCode(), code);
thread_free(thread); delete thread;
} }

View File

@ -16,24 +16,25 @@ static void timer_callback_with_counter(void* context) {
TEST_CASE("a timer passes the context correctly") { TEST_CASE("a timer passes the context correctly") {
int foo = 1; int foo = 1;
auto* timer = timer_alloc(&timer_callback_with_context, TimerTypeOnce, &foo); auto* timer = new Timer(Timer::TypeOnce, &timer_callback_with_context, &foo);
timer_start(timer, 1); timer->start(1);
delay_tick(10); delay_tick(10);
timer_stop(timer); timer->stop();
timer_free(timer); delete timer;
CHECK_EQ(timer_callback_context, &foo); CHECK_EQ(timer_callback_context, &foo);
} }
TEST_CASE("TimerTypePeriodic timers can be stopped and restarted") { TEST_CASE("TimerTypePeriodic timers can be stopped and restarted") {
int counter = 0; int counter = 0;
auto* timer = timer_alloc(&timer_callback_with_counter, TimerTypePeriodic, &counter); auto* timer = new Timer(Timer::TypePeriodic, &timer_callback_with_counter, &counter);
timer_start(timer, 1); timer->start(1);
delay_tick(10); delay_tick(10);
timer_stop(timer); timer->stop();
timer->start(1);
delay_tick(10); delay_tick(10);
timer_stop(timer); timer->stop();
timer_free(timer); delete timer;
CHECK_GE(counter, 2); CHECK_GE(counter, 2);
} }
@ -41,24 +42,25 @@ TEST_CASE("TimerTypePeriodic timers can be stopped and restarted") {
TEST_CASE("TimerTypePeriodic calls the callback periodically") { TEST_CASE("TimerTypePeriodic calls the callback periodically") {
int counter = 0; int counter = 0;
int ticks_to_run = 10; int ticks_to_run = 10;
auto* timer = timer_alloc(&timer_callback_with_counter, TimerTypePeriodic, &counter); auto* timer = new Timer(Timer::TypePeriodic, &timer_callback_with_counter, &counter);
timer_start(timer, 1); timer->start(1);
delay_tick(ticks_to_run); delay_tick(ticks_to_run);
timer_stop(timer); timer->stop();
timer_free(timer); delete timer;
CHECK_EQ(counter, ticks_to_run); CHECK_EQ(counter, ticks_to_run);
} }
TEST_CASE("restarting TimerTypeOnce timers has no effect") { TEST_CASE("restarting TimerTypeOnce timers calls the callback again") {
int counter = 0; int counter = 0;
auto* timer = timer_alloc(&timer_callback_with_counter, TimerTypeOnce, &counter); auto* timer = new Timer(Timer::TypeOnce, &timer_callback_with_counter, &counter);
timer_start(timer, 1); timer->start(1);
delay_tick(10); delay_tick(10);
timer_stop(timer); timer->stop();
timer->start(1);
delay_tick(10); delay_tick(10);
timer_stop(timer); timer->stop();
timer_free(timer); delete timer;
CHECK_EQ(counter, 1); CHECK_EQ(counter, 2);
} }