@@ -7,27 +7,35 @@ namespace task {
7
7
#if defined(ESP_PLATFORM) || defined(_DOXYGEN_)
8
8
// / Run the given function on the specific core, then return the result (if any)
9
9
// / @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.
16
17
// / @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
18
20
// / @param stack_size_bytes The stack size to allocate for the function
19
21
// / @param priority The priority of the task
20
22
// / @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
24
31
// / @note If you provide a core_id >= configNUM_CORES, the function will run on
25
32
// / the last core
26
33
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
31
39
return f ();
32
40
} else {
33
41
// 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
103
111
// / the function has completed, regardless of the core it is run on.
104
112
// / @param f The function to run
105
113
// / @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
109
122
// / @note If you provide a core_id >= configNUM_CORES, the function will run on
110
123
// / 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 ) {
112
126
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 );
114
128
}
115
129
116
130
// / 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
168
182
// / the core on which the calling thread is running.
169
183
// / @param f The function to run
170
184
// / @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
172
186
// / @note If you provide a core_id < 0, the thread will not be pinned to any
173
187
// / specific core, instead the scheduler will decide which core to run
174
188
// / the thread on
0 commit comments