Skip to content

Commit 4ee42c8

Browse files
authored
feat(task): Allow run_on_core callers to ensure a separate task is created (#358)
* 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. * minor update to commnet * consistency
1 parent 8e84d34 commit 4ee42c8

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

components/task/include/run_on_core.hpp

+42-27
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
@@ -95,22 +103,29 @@ static auto run_on_core(const auto &f, int core_id, size_t stack_size_bytes = 20
95103

96104
/// Run the given function on the specific core, then return the result (if any)
97105
/// @details This function will run the given function on the specified core,
98-
/// then return the result (if any). If the provided core is the same
99-
/// as the current core, the function will run directly. If the
100-
/// provided core is different, the function will be run on the
101-
/// specified core and the result will be returned to the calling
102-
/// thread. Note that this function will block the calling thread until
103-
/// the function has completed, regardless of the core it is run on.
106+
/// then return the result (if any). If the provided core is the same as
107+
/// the current core, the function will run directly (unless ensure_task
108+
/// is set to true). If the provided core is different, the function
109+
/// will be run on the specified core and the result will be returned to
110+
/// the calling thread. Note that this function will block the calling
111+
/// thread until the function has completed, regardless of the core it
112+
/// is run on.
104113
/// @param f The function to run
105114
/// @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)
115+
/// @param ensure_task If true, the function will be run in a separate task,
116+
/// regardless of the core id. If false, the function will be run
117+
/// directly if the core id is the same as the current core, otherwise
118+
/// it will be run in a separate task
119+
/// @note This function is only available on the ESP platform
120+
/// @note If the provided core id is the same as the current core, the function
121+
/// will run directly - unless ensure_task is true, in which case the
122+
/// function will run in a separate task
109123
/// @note If you provide a core_id >= configNUM_CORES, the function will run on
110124
/// the last core
111-
static auto run_on_core(const auto &f, const espp::Task::BaseConfig &task_config) {
125+
static auto run_on_core(const auto &f, const espp::Task::BaseConfig &task_config,
126+
bool ensure_task = false) {
112127
return run_on_core(f, task_config.core_id, task_config.stack_size_bytes, task_config.priority,
113-
task_config.name);
128+
task_config.name, ensure_task);
114129
}
115130

116131
/// Run the given function on the specific core without blocking the calling thread
@@ -123,7 +138,7 @@ static auto run_on_core(const auto &f, const espp::Task::BaseConfig &task_config
123138
/// @param stack_size_bytes The stack size to allocate for the function
124139
/// @param priority The priority of the task
125140
/// @param name The name of the task
126-
/// @note This function is only available on ESP32
141+
/// @note This function is only available on the ESP platform
127142
/// @note If you provide a core_id < 0, the thread will not be pinned to any
128143
/// specific core, instead the scheduler will decide which core to run
129144
/// the thread on
@@ -168,7 +183,7 @@ static void run_on_core_non_blocking(const auto &f, int core_id, size_t stack_si
168183
/// the core on which the calling thread is running.
169184
/// @param f The function to run
170185
/// @param task_config The task configuration
171-
/// @note This function is only available on ESP32
186+
/// @note This function is only available on the ESP platform
172187
/// @note If you provide a core_id < 0, the thread will not be pinned to any
173188
/// specific core, instead the scheduler will decide which core to run
174189
/// the thread on

0 commit comments

Comments
 (0)