|
idfxx 1.0.0
Modern C++23 components for ESP-IDF
|
Task lifecycle management. More...
Classes | |
| struct | config |
| Task configuration parameters. More... | |
| class | self |
| Handle for task self-interaction. More... | |
Public Member Functions | |
| task (const config &cfg, std::move_only_function< void(self &)> task_func) | |
| Creates a task with a std::move_only_function callback. | |
| ~task () | |
| Destroys the task, requesting stop and blocking until the task function completes. | |
| task (const task &)=delete | |
| task & | operator= (const task &)=delete |
| task (task &&other) noexcept | |
| task & | operator= (task &&other) noexcept |
| TaskHandle_t | idf_handle () const noexcept |
| Returns the underlying FreeRTOS task handle. | |
| const std::string & | name () const noexcept |
| Returns the task name. | |
| task_priority | priority () const noexcept |
| Returns the current task priority. | |
| size_t | stack_high_water_mark () const noexcept |
| Returns the minimum free stack space (in bytes) since the task started. | |
| bool | is_completed () const noexcept |
| Checks if the task function has returned. | |
| bool | joinable () const noexcept |
| Checks if this task object owns the task. | |
| bool | request_stop () noexcept |
| Requests the task to stop. | |
| bool | stop_requested () const noexcept |
| Checks if a stop has been requested for this task. | |
| void | suspend () |
| Suspends the task. | |
| void | resume () |
| Resumes a suspended task. | |
| result< void > | try_suspend () |
| Suspends the task. | |
| result< void > | try_resume () |
| Resumes a suspended task. | |
| bool | resume_from_isr () noexcept |
| Resumes a suspended task from ISR context. | |
| void | notify () |
| Sends a notification to the task. | |
| result< void > | try_notify () |
| Sends a notification to the task. | |
| bool | notify_from_isr () noexcept |
| Sends a notification to the task from ISR context. | |
| void | set_priority (task_priority new_priority) |
| Changes the task priority. | |
| result< void > | try_set_priority (task_priority new_priority) |
| Changes the task priority. | |
| void | detach () |
| Releases ownership of the task. | |
| result< void > | try_detach () |
| Releases ownership of the task. | |
| void | kill () |
| Immediately terminates the task without waiting for completion. | |
| result< void > | try_kill () |
| Immediately terminates the task without waiting for completion. | |
| void | join () |
| Blocks until the task function completes. | |
| template<typename Rep , typename Period > | |
| void | join (const std::chrono::duration< Rep, Period > &timeout) |
| Blocks until the task function completes or the timeout expires. | |
| template<typename Clock , typename Duration > | |
| void | join_until (const std::chrono::time_point< Clock, Duration > &deadline) |
| Blocks until the task function completes or the deadline is reached. | |
| result< void > | try_join () |
| Blocks until the task function completes. | |
| template<typename Rep , typename Period > | |
| result< void > | try_join (const std::chrono::duration< Rep, Period > &timeout) |
| Blocks until the task function completes or the timeout expires. | |
| template<typename Clock , typename Duration > | |
| result< void > | try_join_until (const std::chrono::time_point< Clock, Duration > &deadline) |
| Blocks until the task function completes or the deadline is reached. | |
Static Public Member Functions | |
| static void | spawn (config cfg, std::move_only_function< void(self &)> task_func) |
| Creates a fire-and-forget task with a std::move_only_function callback. | |
| static TaskHandle_t | current_handle () noexcept |
| Returns the handle of the currently executing task. | |
| static std::string | current_name () |
| Returns the name of the currently executing task. | |
Task lifecycle management.
Manages a task with automatic cleanup on destruction.
Tasks are non-copyable and move-only. Result-returning methods on a moved-from object return errc::invalid_state. Simple accessors return default/null values.
Creates a task with a std::move_only_function callback.
The task starts executing immediately after construction.
| cfg | Task configuration. |
| task_func | Function to execute in the task context. Receives a self reference for task self-interaction. |
| std::bad_alloc | if memory allocation fails. |
| idfxx::task::~task | ( | ) |
Destroys the task, requesting stop and blocking until the task function completes.
If the task is still joinable (not detached), the destructor calls request_stop() and then blocks until the task function returns, similar to std::jthread. If the task is suspended, it is automatically resumed so it can observe the stop request and exit. The destructor repeatedly resumes the task to ensure it doesn't suspend again. Tasks that check stop_requested() in their loops will exit cleanly.
|
noexcept |
|
inlinestaticnoexcept |
|
inlinestatic |
|
inline |
Releases ownership of the task.
After detaching, the task becomes self-managing and cleans up its resources automatically when the task function returns. The task object becomes non-joinable and the destructor becomes a no-op.
| std::system_error | if the task has already been detached. |
Definition at line 511 of file task.hpp.
References try_detach(), and idfxx::unwrap().
|
inlinenoexcept |
Returns the underlying FreeRTOS task handle.
|
noexcept |
Checks if the task function has returned.
|
inline |
Blocks until the task function completes.
Waits indefinitely for the task to finish. After a successful join, the task becomes non-joinable. Prefer join() with a timeout to avoid blocking indefinitely on tasks that may not return promptly.
| std::system_error | if the task has been detached or already joined (idfxx::errc::invalid_state), or if called from within the task itself (std::errc::resource_deadlock_would_occur). |
Definition at line 594 of file task.hpp.
References try_join(), and idfxx::unwrap().
Blocks until the task function completes or the timeout expires.
| Rep | The representation type of the duration. |
| Period | The period type of the duration. |
| timeout | Maximum time to wait for the task to complete. |
| std::system_error | if the task has been detached or already joined (idfxx::errc::invalid_state), if called from within the task itself (std::errc::resource_deadlock_would_occur), or if the timeout expires (idfxx::errc::timeout). |
Definition at line 609 of file task.hpp.
References idfxx::timeout, try_join(), and idfxx::unwrap().
Blocks until the task function completes or the deadline is reached.
| Clock | The clock type. |
| Duration | The duration type of the time point. |
| deadline | The time point at which to stop waiting. |
| std::system_error | if the task has been detached or already joined (idfxx::errc::invalid_state), if called from within the task itself (std::errc::resource_deadlock_would_occur), or if the deadline is reached (idfxx::errc::timeout). |
Definition at line 626 of file task.hpp.
References try_join_until(), and idfxx::unwrap().
|
inlinenoexcept |
|
inline |
Immediately terminates the task without waiting for completion.
Immediately terminates the task. After killing, the task becomes non-joinable.
These consequences can lead to resource leaks, deadlocks, and undefined behavior in the rest of the system. Prefer using request_stop() and join() to allow the task to complete gracefully, or allow the destructor to block.
If the task function has already returned by the time this method is called, this method simply cleans up resources.
| std::system_error | if the task has been detached, already joined, or if called from within the task itself. |
Definition at line 553 of file task.hpp.
References try_kill(), and idfxx::unwrap().
|
inlinenoexcept |
|
inline |
Sends a notification to the task.
Increments the task's notification value, waking the task if it is blocked in wait() or take(). Can be called multiple times before the task receives — each call increments the notification count.
Uses notification index 0, which is reserved for this purpose.
| std::system_error | if the task has been detached or completed. |
Definition at line 440 of file task.hpp.
References try_notify(), and idfxx::unwrap().
|
noexcept |
Sends a notification to the task from ISR context.
Increments the task's notification value, waking the task if it is blocked in wait() or take().
Uses notification index 0, which is reserved for this purpose.
|
noexcept |
Returns the current task priority.
|
noexcept |
Requests the task to stop.
Requests the task to stop cooperatively. The task function can observe this via self::stop_requested(). Also requested automatically by the destructor before joining.
|
inline |
Resumes a suspended task.
| std::system_error | if the task has been detached or completed. |
Definition at line 388 of file task.hpp.
References try_resume(), and idfxx::unwrap().
|
noexcept |
Resumes a suspended task from ISR context.
|
inline |
Changes the task priority.
| new_priority | The new priority level. |
| std::system_error | if the task has been detached or completed. |
Definition at line 488 of file task.hpp.
References try_set_priority(), and idfxx::unwrap().
|
static |
Creates a fire-and-forget task with a std::move_only_function callback.
The task starts executing immediately and cleans up its resources when complete.
| cfg | Task configuration. |
| task_func | Function to execute in the task context. Receives a self reference for task self-interaction. |
| std::bad_alloc | if memory allocation fails. |
|
noexcept |
Returns the minimum free stack space (in bytes) since the task started.
This is useful for tuning stack sizes — a value approaching zero indicates potential stack overflow.
|
noexcept |
Checks if a stop has been requested for this task.
|
inline |
Suspends the task.
A suspended task will not run until resume() is called.
| std::system_error | if the task has been detached or completed. |
Definition at line 380 of file task.hpp.
References try_suspend(), and idfxx::unwrap().
Releases ownership of the task.
After detaching, the task becomes self-managing and cleans up its resources automatically when the task function returns. The task object becomes non-joinable and the destructor becomes a no-op.
If the task has already completed, ownership is released and resources are cleaned up immediately.
| invalid_state | The task has already been detached. |
Referenced by detach().
Blocks until the task function completes.
Waits indefinitely for the task to finish. After a successful join, the task becomes non-joinable. Prefer try_join() with a timeout to avoid blocking indefinitely on tasks that may not return promptly.
| invalid_state | The task has been detached or already joined. |
| std::errc::resource_deadlock_would_occur | Called from within the task itself. |
Blocks until the task function completes or the timeout expires.
After a successful join, the task becomes non-joinable.
| Rep | The representation type of the duration. |
| Period | The period type of the duration. |
| timeout | Maximum time to wait for the task to complete. |
| invalid_state | The task has been detached or already joined. |
| std::errc::resource_deadlock_would_occur | Called from within the task itself. |
| timeout | The task did not complete within the specified duration. The task remains joinable and can be joined again. |
Definition at line 659 of file task.hpp.
References idfxx::chrono::ticks(), and idfxx::timeout.
|
inline |
Blocks until the task function completes or the deadline is reached.
After a successful join, the task becomes non-joinable.
| Clock | The clock type. |
| Duration | The duration type of the time point. |
| deadline | The time point at which to stop waiting. |
| invalid_state | The task has been detached or already joined. |
| std::errc::resource_deadlock_would_occur | Called from within the task itself. |
| timeout | The task did not complete before the deadline. The task remains joinable and can be joined again. |
Definition at line 678 of file task.hpp.
References idfxx::chrono::ticks().
Referenced by join_until().
Immediately terminates the task without waiting for completion.
Immediately terminates the task. After killing, the task becomes non-joinable.
These consequences can lead to resource leaks, deadlocks, and undefined behavior in the rest of the system. Prefer using request_stop() and try_join() to allow the task to complete gracefully, or allow the destructor to block.
If the task function has already returned by the time this method is called, this method simply cleans up resources.
| invalid_state | The task has been detached, already joined, or called from within the task itself. |
Referenced by kill().
Sends a notification to the task.
Increments the task's notification value, waking the task if it is blocked in wait() or take(). Can be called multiple times before the task receives — each call increments the notification count.
Uses notification index 0, which is reserved for this purpose.
| invalid_state | The task has been detached or completed. |
Referenced by notify().
Resumes a suspended task.
| invalid_state | The task has been detached or completed. |
Referenced by resume().
| result< void > idfxx::task::try_set_priority | ( | task_priority | new_priority | ) |
Changes the task priority.
| new_priority | The new priority level. |
| invalid_state | The task has been detached or completed. |
Referenced by set_priority().