Skip to content

Commit 7570c48

Browse files
committed
feat(task): Allow run_on_core callers to ensure a separate task is created
* Update `espp::task::run_on_core` (blocking) APIs to have a `bool ensure_task = false` parameter which (if true) will ensure that the function is run in a separate task, regardless of the requested core and current core id. * Update `espp::task::run_on_core` (blocking) APIs so that `core_id = -1` (do not care) parameter will still spawn a separate task, instead of running in the same context as the calling function.
1 parent 8e84d34 commit 7570c48

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

components/task/include/run_on_core.hpp

+34-20
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,35 @@ namespace task {
77
#if defined(ESP_PLATFORM) || defined(_DOXYGEN_)
88
/// Run the given function on the specific core, then return the result (if any)
99
/// @details This function will run the given function on the specified core,
10-
/// then return the result (if any). If the provided core is the same
11-
/// as the current core, the function will run directly. If the
12-
/// provided core is different, the function will be run on the
13-
/// specified core and the result will be returned to the calling
14-
/// thread. Note that this function will block the calling thread until
15-
/// the function has completed, regardless of the core it is run on.
10+
/// then return the result (if any). If the provided core is the same as
11+
/// the current core, the function will run directly (unless ensure_task
12+
/// is set to true). If the provided core is different, the function
13+
/// will be run on the specified core and the result will be returned to
14+
/// the calling thread. Note that this function will block the calling
15+
/// thread until the function has completed, regardless of the core it
16+
/// is run on.
1617
/// @param f The function to run
17-
/// @param core_id The core to run the function on
18+
/// @param core_id The core to run the function on. -1 means the scheduler will
19+
/// decide which core to run the function on
1820
/// @param stack_size_bytes The stack size to allocate for the function
1921
/// @param priority The priority of the task
2022
/// @param name The name of the task
21-
/// @note This function is only available on ESP32
22-
/// @note If you provide a core_id < 0, the function will run on the current
23-
/// core (same core as the caller)
23+
/// @param ensure_task If true, the function will be run in a separate task,
24+
/// regardless of the core id. If false, the function will be run
25+
/// directly if the core id is the same as the current core, otherwise
26+
/// it will be run in a separate task
27+
/// @note If the provided core id is the same as the current core, the function
28+
/// will run directly - unless ensure_task is true, in which case the
29+
/// function will run in a separate task
30+
/// @note This function is only available on the ESP platform
2431
/// @note If you provide a core_id >= configNUM_CORES, the function will run on
2532
/// the last core
2633
static auto run_on_core(const auto &f, int core_id, size_t stack_size_bytes = 2048,
27-
size_t priority = 5, const std::string &name = "run_on_core_task") {
28-
if (core_id < 0 || core_id == xPortGetCoreID()) {
29-
// If no core id specified or we are already executing on the desired core,
30-
// run the function directly
34+
size_t priority = 5, const std::string &name = "run_on_core_task",
35+
bool ensure_task = false) {
36+
if (!ensure_task && core_id == xPortGetCoreID()) {
37+
// If we are already executing on the desired core and ensure task is false,
38+
// then simply run the function directly
3139
return f();
3240
} else {
3341
// Otherwise run the function on the desired core
@@ -103,14 +111,20 @@ static auto run_on_core(const auto &f, int core_id, size_t stack_size_bytes = 20
103111
/// the function has completed, regardless of the core it is run on.
104112
/// @param f The function to run
105113
/// @param task_config The task configuration
106-
/// @note This function is only available on ESP32
107-
/// @note If you provide a core_id < 0, the function will run on the current
108-
/// core (same core as the caller)
114+
/// @param ensure_task If true, the function will be run in a separate task,
115+
/// regardless of the core id. If false, the function will be run
116+
/// directly if the core id is the same as the current core, otherwise
117+
/// it will be run in a separate task
118+
/// @note This function is only available on the ESP platform
119+
/// @note If the provided core id is the same as the current core, the function
120+
/// will run directly - unless ensure_task is true, in which case the
121+
/// function will run in a separate task
109122
/// @note If you provide a core_id >= configNUM_CORES, the function will run on
110123
/// the last core
111-
static auto run_on_core(const auto &f, const espp::Task::BaseConfig &task_config) {
124+
static auto run_on_core(const auto &f, const espp::Task::BaseConfig &task_config,
125+
bool ensure_task = false) {
112126
return run_on_core(f, task_config.core_id, task_config.stack_size_bytes, task_config.priority,
113-
task_config.name);
127+
task_config.name, ensure_task);
114128
}
115129

116130
/// Run the given function on the specific core without blocking the calling thread
@@ -168,7 +182,7 @@ static void run_on_core_non_blocking(const auto &f, int core_id, size_t stack_si
168182
/// the core on which the calling thread is running.
169183
/// @param f The function to run
170184
/// @param task_config The task configuration
171-
/// @note This function is only available on ESP32
185+
/// @note This function is only available on the ESP platform
172186
/// @note If you provide a core_id < 0, the thread will not be pinned to any
173187
/// specific core, instead the scheduler will decide which core to run
174188
/// the thread on

0 commit comments

Comments
 (0)