@@ -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
@@ -95,22 +103,29 @@ static auto run_on_core(const auto &f, int core_id, size_t stack_size_bytes = 20
95
103
96
104
// / Run the given function on the specific core, then return the result (if any)
97
105
// / @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.
104
113
// / @param f The function to run
105
114
// / @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
109
123
// / @note If you provide a core_id >= configNUM_CORES, the function will run on
110
124
// / 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 ) {
112
127
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 );
114
129
}
115
130
116
131
// / 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
123
138
// / @param stack_size_bytes The stack size to allocate for the function
124
139
// / @param priority The priority of the task
125
140
// / @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
127
142
// / @note If you provide a core_id < 0, the thread will not be pinned to any
128
143
// / specific core, instead the scheduler will decide which core to run
129
144
// / the thread on
@@ -168,7 +183,7 @@ static void run_on_core_non_blocking(const auto &f, int core_id, size_t stack_si
168
183
// / the core on which the calling thread is running.
169
184
// / @param f The function to run
170
185
// / @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
172
187
// / @note If you provide a core_id < 0, the thread will not be pinned to any
173
188
// / specific core, instead the scheduler will decide which core to run
174
189
// / the thread on
0 commit comments