Skip to content

Commit

Permalink
feat(task): Add espp::task::run_on_core_non_blocking static method (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
finger563 authored Sep 23, 2024
1 parent 6703d78 commit 1810367
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
23 changes: 23 additions & 0 deletions components/task/example/main/task_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,29 @@ extern "C" void app_main(void) {
//! [run on core example]
}

{
//! [run on core nonblocking example]
logger.info("espp::task::run_on_core non-blocking example: main thread core ID: {}",
xPortGetCoreID());
// NOTE: in these examples, because we're logging with libfmt in the
// function to be run, we need a little more than the default 2k stack size,
// so we're using 3k.

// test running a function which takes a while to complete
auto task_fn = []() -> void {
fmt::print("[{0}] Task running on core {0}\n", xPortGetCoreID());
std::this_thread::sleep_for(1s);
fmt::print("[{0}] Task done!\n", xPortGetCoreID());
};
espp::task::run_on_core_non_blocking(task_fn, 0, 3 * 1024);
espp::task::run_on_core_non_blocking(task_fn, 1);
fmt::print("Started tasks on cores 0 and 1\n");

// sleep for a bit to let the tasks run
std::this_thread::sleep_for(2s);
//! [run on core nonblocking example]
}

logger.info("Task example complete!");

while (true) {
Expand Down
40 changes: 40 additions & 0 deletions components/task/include/run_on_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,46 @@ static auto run_on_core(const auto &f, int core_id, size_t stack_size_bytes = 20
}
}
}

/// Run the given function on the specific core without blocking the calling thread
/// @details This function will run the given function on the specified core,
/// without blocking the calling thread / context. A new thread is
/// spawned for the function even if the requested core is the same as
/// the core on which the calling thread is running.
/// @param f The function to run
/// @param core_id The core to run the function on
/// @param stack_size_bytes The stack size to allocate for the function
/// @param priority The priority of the task
/// @note This function is only available on ESP32
/// @note If you provide a core_id < 0, the thread will not be pinned to any
/// specific core, instead the scheduler will decide which core to run
/// the thread on
/// @note If you provide a core_id >= configNUM_CORES, the function will run on
/// the last core
static void run_on_core_non_blocking(const auto &f, int core_id, size_t stack_size_bytes = 2048,
size_t priority = 5) {
// Otherwise run the function on the desired core
if (core_id > configNUM_CORES - 1) {
// If the core id is larger than the number of cores, run on the last core
core_id = configNUM_CORES - 1;
}
auto thread_config = esp_pthread_get_default_config();
thread_config.thread_name = "run_on_core_thread";
if (core_id >= 0)
thread_config.pin_to_core = core_id;
thread_config.stack_size = stack_size_bytes;
thread_config.prio = priority;
// this will set the config for the next created thread
auto err = esp_pthread_set_cfg(&thread_config);
if (err != ESP_OK) {
// failed to set the config, can't create the thread; simply run the function
// on the current core
f();
return;
}
auto thread = std::thread(f);
thread.detach();
}
#endif
} // namespace task
} // namespace espp
2 changes: 2 additions & 0 deletions components/task/include/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace espp {
*
* \section run_on_core_ex1 Run on Core Example
* \snippet task_example.cpp run on core example
* \section run_on_core_ex2 Run on Core (Non-Blocking) Example
* \snippet task_example.cpp run on core nonblocking example
*/
class Task : public espp::BaseComponent {
public:
Expand Down

0 comments on commit 1810367

Please sign in to comment.