Skip to content

Commit 1810367

Browse files
authored
feat(task): Add espp::task::run_on_core_non_blocking static method (#331)
1 parent 6703d78 commit 1810367

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

components/task/example/main/task_example.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,29 @@ extern "C" void app_main(void) {
530530
//! [run on core example]
531531
}
532532

533+
{
534+
//! [run on core nonblocking example]
535+
logger.info("espp::task::run_on_core non-blocking example: main thread core ID: {}",
536+
xPortGetCoreID());
537+
// NOTE: in these examples, because we're logging with libfmt in the
538+
// function to be run, we need a little more than the default 2k stack size,
539+
// so we're using 3k.
540+
541+
// test running a function which takes a while to complete
542+
auto task_fn = []() -> void {
543+
fmt::print("[{0}] Task running on core {0}\n", xPortGetCoreID());
544+
std::this_thread::sleep_for(1s);
545+
fmt::print("[{0}] Task done!\n", xPortGetCoreID());
546+
};
547+
espp::task::run_on_core_non_blocking(task_fn, 0, 3 * 1024);
548+
espp::task::run_on_core_non_blocking(task_fn, 1);
549+
fmt::print("Started tasks on cores 0 and 1\n");
550+
551+
// sleep for a bit to let the tasks run
552+
std::this_thread::sleep_for(2s);
553+
//! [run on core nonblocking example]
554+
}
555+
533556
logger.info("Task example complete!");
534557

535558
while (true) {

components/task/include/run_on_core.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,46 @@ static auto run_on_core(const auto &f, int core_id, size_t stack_size_bytes = 20
8282
}
8383
}
8484
}
85+
86+
/// Run the given function on the specific core without blocking the calling thread
87+
/// @details This function will run the given function on the specified core,
88+
/// without blocking the calling thread / context. A new thread is
89+
/// spawned for the function even if the requested core is the same as
90+
/// the core on which the calling thread is running.
91+
/// @param f The function to run
92+
/// @param core_id The core to run the function on
93+
/// @param stack_size_bytes The stack size to allocate for the function
94+
/// @param priority The priority of the task
95+
/// @note This function is only available on ESP32
96+
/// @note If you provide a core_id < 0, the thread will not be pinned to any
97+
/// specific core, instead the scheduler will decide which core to run
98+
/// the thread on
99+
/// @note If you provide a core_id >= configNUM_CORES, the function will run on
100+
/// the last core
101+
static void run_on_core_non_blocking(const auto &f, int core_id, size_t stack_size_bytes = 2048,
102+
size_t priority = 5) {
103+
// Otherwise run the function on the desired core
104+
if (core_id > configNUM_CORES - 1) {
105+
// If the core id is larger than the number of cores, run on the last core
106+
core_id = configNUM_CORES - 1;
107+
}
108+
auto thread_config = esp_pthread_get_default_config();
109+
thread_config.thread_name = "run_on_core_thread";
110+
if (core_id >= 0)
111+
thread_config.pin_to_core = core_id;
112+
thread_config.stack_size = stack_size_bytes;
113+
thread_config.prio = priority;
114+
// this will set the config for the next created thread
115+
auto err = esp_pthread_set_cfg(&thread_config);
116+
if (err != ESP_OK) {
117+
// failed to set the config, can't create the thread; simply run the function
118+
// on the current core
119+
f();
120+
return;
121+
}
122+
auto thread = std::thread(f);
123+
thread.detach();
124+
}
85125
#endif
86126
} // namespace task
87127
} // namespace espp

components/task/include/task.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ namespace espp {
4444
*
4545
* \section run_on_core_ex1 Run on Core Example
4646
* \snippet task_example.cpp run on core example
47+
* \section run_on_core_ex2 Run on Core (Non-Blocking) Example
48+
* \snippet task_example.cpp run on core nonblocking example
4749
*/
4850
class Task : public espp::BaseComponent {
4951
public:

0 commit comments

Comments
 (0)