Skip to content

Commit

Permalink
feat(task): update task to use base config as storage for configurati…
Browse files Browse the repository at this point in the history
…on members and update example
  • Loading branch information
finger563 committed Mar 11, 2024
1 parent d04fa57 commit f902bed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 39 deletions.
8 changes: 7 additions & 1 deletion components/task/example/main/task_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ extern "C" void app_main(void) {
return false;
};
std::string task_name = fmt::format("Task {}", i);
auto task = espp::Task::make_unique({.name = task_name, .callback = task_fn});
auto task = espp::Task::make_unique({
.callback = task_fn,
.task_config =
{
.name = task_name,
},
});
tasks[i] = std::move(task);
tasks[i]->start();
}
Expand Down
59 changes: 21 additions & 38 deletions components/task/include/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,8 @@ class Task : public BaseComponent {
* or mutex in the callback.
*/
struct SimpleConfig {
std::string_view name; /**< Name of the task */
simple_callback_fn callback; /**< Callback function */
size_t stack_size_bytes{4 * 1024}; /**< Stack Size (B) allocated to the task. */
size_t priority{0}; /**< Priority of the task, 0 is lowest priority on ESP / FreeRTOS. */
int core_id{-1}; /**< Core ID of the task, -1 means it is not pinned to any core. */
simple_callback_fn callback; /**< Callback function */
BaseConfig task_config; /**< Base configuration for the task. */
Logger::Verbosity log_level{Logger::Verbosity::WARN}; /**< Log verbosity for the task. */
};

Expand All @@ -134,29 +131,27 @@ class Task : public BaseComponent {
: BaseComponent(config.name, config.log_level)
, name_(config.name)
, callback_(config.callback)
, stack_size_bytes_(config.stack_size_bytes)
, priority_(config.priority)
, core_id_(config.core_id) {}
, config_({config.name, config.stack_size_bytes, config.priority, config.core_id}) {}

/**
* @brief Construct a new Task object using the SimpleConfig struct.
* @param config SimpleConfig struct to initialize the Task with.
*/
explicit Task(const SimpleConfig &config)
: BaseComponent(config.name, config.log_level)
, name_(config.name)
: BaseComponent(config.task_config.name, config.log_level)
, name_(config.task_config.name)
, simple_callback_(config.callback)
, stack_size_bytes_(config.stack_size_bytes)
, priority_(config.priority)
, core_id_(config.core_id) {}
, config_(config.task_config) {}

/**
* @brief Construct a new Task object using the AdvancedConfig struct.
* @param config AdvancedConfig struct to initialize the Task with.
*/
explicit Task(const AdvancedConfig &config)
: BaseComponent(config.task_config.name, config.log_level)
, name_(config.task_config.name)
, callback_(config.callback)
, stack_size_bytes_(config.task_config.stack_size_bytes)
, priority_(config.task_config.priority)
, core_id_(config.task_config.core_id) {}
, config_(config.task_config) {}

/**
* @brief Get a unique pointer to a new task created with \p config.
Expand Down Expand Up @@ -219,15 +214,16 @@ class Task : public BaseComponent {
#if defined(ESP_PLATFORM)
auto thread_config = esp_pthread_get_default_config();
thread_config.thread_name = name_.c_str();
if (core_id_ >= 0)
thread_config.pin_to_core = core_id_;
if (core_id_ >= portNUM_PROCESSORS) {
auto core_id = config_.core_id;
if (core_id >= 0)
thread_config.pin_to_core = core_id;
if (core_id >= portNUM_PROCESSORS) {
logger_.error("core_id ({}) is larger than portNUM_PROCESSORS ({}), cannot create Task '{}'",
core_id_, portNUM_PROCESSORS, name_);
core_id, portNUM_PROCESSORS, name_);
return false;
}
thread_config.stack_size = stack_size_bytes_;
thread_config.prio = priority_;
thread_config.stack_size = config_.stack_size_bytes;
thread_config.prio = config_.priority;
// this will set the config for the next created thread
auto err = esp_pthread_set_cfg(&thread_config);
if (err == ESP_ERR_NO_MEM) {
Expand All @@ -239,7 +235,7 @@ class Task : public BaseComponent {
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/pthread.html?highlight=esp_pthread_set_cfg#_CPPv419esp_pthread_set_cfgPK17esp_pthread_cfg_t
logger_.error(
"Configured stack size ({}) is less than PTHREAD_STACK_MIN ({}), cannot create Task '{}'",
stack_size_bytes_, PTHREAD_STACK_MIN, name_);
config_.stack_size_bytes, PTHREAD_STACK_MIN, name_);
return false;
}
#endif
Expand Down Expand Up @@ -357,23 +353,10 @@ class Task : public BaseComponent {
*/
simple_callback_fn simple_callback_;

// NOTE: the below parameters are only used on ESP / FreeRTOS platform
/**
* @brief On ESP platform, the amount of bytes allocated to the Task stack.
*/
size_t stack_size_bytes_;

/**
* @brief On ESP platform, the priority of the task, with 0 being lowest
* priority.
*/
size_t priority_;

/**
* @brief On ESP platform, the core id that the task is pinned to. If -1,
* then the task is not pinned to a core.
* @brief Configuration for the task.
*/
int core_id_;
BaseConfig config_;

std::atomic<bool> started_{false};
std::condition_variable cv_;
Expand Down

0 comments on commit f902bed

Please sign in to comment.