20#include <idfxx/chrono>
23#include <idfxx/memory>
26#include <freertos/FreeRTOS.h>
27#include <freertos/task.h>
59 explicit self(context* ctx) noexcept
116 template<typename Rep, typename Period>
117 [[nodiscard]]
bool wait_for(const std::chrono::duration<Rep, Period>&
timeout) noexcept {
135 template<
typename Clock,
typename Duration>
136 [[nodiscard]]
bool wait_until(
const std::chrono::time_point<Clock, Duration>& deadline)
noexcept {
137 auto remaining = deadline - Clock::now();
138 if (remaining <=
decltype(remaining)::zero()) {
139 return _take(0) != 0;
172 template<typename Rep, typename Period>
173 [[nodiscard]] uint32_t
take_for(const std::chrono::duration<Rep, Period>&
timeout) noexcept {
192 template<
typename Clock,
typename Duration>
193 [[nodiscard]] uint32_t
take_until(
const std::chrono::time_point<Clock, Duration>& deadline)
noexcept {
194 auto remaining = deadline - Clock::now();
195 if (remaining <=
decltype(remaining)::zero()) {
206 [[nodiscard]]
unsigned int priority() const noexcept;
230 [[nodiscard]] std::
string name() const;
240 uint32_t _take(TickType_t ticks) noexcept;
250 std::string_view
name =
"task";
251 size_t stack_size = 4096;
253 std::optional<core_id> core_affinity = std::nullopt;
257#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
269 [[nodiscard]]
explicit task(
const config& cfg, std::move_only_function<
void(
self&)> task_func);
283 [[nodiscard]]
explicit task(
const config& cfg,
void (*task_func)(
self&,
void*),
void* arg);
313#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
402 [[nodiscard]] TaskHandle_t
idf_handle() const noexcept {
return _handle; }
409 [[nodiscard]]
const std::string&
name() const noexcept {
return _name; }
416 [[nodiscard]]
unsigned int priority() const noexcept;
443 [[nodiscard]]
bool joinable() const noexcept {
return _handle !=
nullptr; }
464#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
520#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
573#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
593#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
622#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
674#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
701 template<
typename Rep,
typename Period>
718 template<
typename Clock,
typename Duration>
719 void join_until(
const std::chrono::time_point<Clock, Duration>& deadline) {
751 template<
typename Rep,
typename Period>
770 template<
typename Clock,
typename Duration>
772 auto remaining = deadline - Clock::now();
773 if (remaining <=
decltype(remaining)::zero()) {
788 [[nodiscard]]
static TaskHandle_t
current_handle() noexcept {
return xTaskGetCurrentTaskHandle(); }
795 [[nodiscard]]
static std::string
current_name() {
return pcTaskGetName(
nullptr); }
798 static void trampoline(
void* arg);
803 explicit task(TaskHandle_t handle, std::string
name, context* ctx);
805 TaskHandle_t _handle;
Handle for task self-interaction.
bool wait_for(const std::chrono::duration< Rep, Period > &timeout) noexcept
Waits for a notification with a timeout (binary semaphore pattern).
unsigned int priority() const noexcept
Returns the current task priority.
bool wait_until(const std::chrono::time_point< Clock, Duration > &deadline) noexcept
Waits for a notification until a deadline (binary semaphore pattern).
bool stop_requested() const noexcept
Checks if a stop has been requested for this task.
uint32_t take_for(const std::chrono::duration< Rep, Period > &timeout) noexcept
Takes accumulated notifications with a timeout (counting semaphore pattern).
uint32_t take_until(const std::chrono::time_point< Clock, Duration > &deadline) noexcept
Takes accumulated notifications until a deadline (counting semaphore pattern).
self(const self &)=delete
TaskHandle_t idf_handle() const noexcept
Returns the FreeRTOS handle of the current task.
bool is_detached() const noexcept
Checks if the current task has been detached.
self & operator=(const self &)=delete
uint32_t take() noexcept
Takes accumulated notifications (counting semaphore pattern).
void wait() noexcept
Waits for a notification (binary semaphore pattern).
size_t stack_high_water_mark() const noexcept
Returns the minimum free stack space (in bytes) since the task started.
void set_priority(unsigned int new_priority) noexcept
Changes the current task priority.
std::string name() const
Returns the current task name.
void suspend() noexcept
Suspends the current task.
Task lifecycle management.
result< void > try_resume()
Resumes a suspended task.
void join(const std::chrono::duration< Rep, Period > &timeout)
Blocks until the task function completes or the timeout expires.
task(const config &cfg, void(*task_func)(self &, void *), void *arg)
Creates a task with a raw function pointer callback.
static TaskHandle_t current_handle() noexcept
Returns the handle of the currently executing task.
void join()
Blocks until the task function completes.
result< void > try_detach()
Releases ownership of the task.
void notify()
Sends a notification to the task.
const std::string & name() const noexcept
Returns the task name.
static result< void > try_spawn(config cfg, void(*task_func)(self &, void *), void *arg)
Creates a fire-and-forget task with a raw function pointer callback.
bool notify_from_isr() noexcept
Sends a notification to the task from ISR context.
result< void > try_set_priority(unsigned int new_priority)
Changes the task priority.
void resume()
Resumes a suspended task.
result< void > try_join()
Blocks until the task function completes.
static result< void > try_spawn(config cfg, std::move_only_function< void(self &)> task_func)
Creates a fire-and-forget task with a std::move_only_function callback.
task & operator=(const task &)=delete
void detach()
Releases ownership of the task.
void suspend()
Suspends the task.
static void spawn(config cfg, void(*task_func)(self &, void *), void *arg)
Creates a fire-and-forget task with a raw function pointer callback.
result< void > try_kill()
Immediately terminates the task without waiting for completion.
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 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.
task(const task &)=delete
void kill()
Immediately terminates the task without waiting for completion.
static result< std::unique_ptr< task > > make(config cfg, std::move_only_function< void(self &)> task_func)
Creates a task with a std::move_only_function callback.
result< void > try_notify()
Sends a notification to the task.
static std::string current_name()
Returns the name of the currently executing task.
result< void > try_join(const std::chrono::duration< Rep, Period > &timeout)
Blocks until the task function completes or the timeout expires.
bool is_completed() const noexcept
Checks if the task function has returned.
bool joinable() const noexcept
Checks if this task object owns the task.
~task()
Destroys the task, requesting stop and blocking until the task function completes.
bool request_stop() noexcept
Requests the task to stop.
bool resume_from_isr() noexcept
Resumes a suspended task from ISR context.
result< void > try_suspend()
Suspends the task.
static result< std::unique_ptr< task > > make(config cfg, void(*task_func)(self &, void *), void *arg)
Creates a task with a raw function pointer callback.
task & operator=(task &&)=delete
TaskHandle_t idf_handle() const noexcept
Returns the underlying FreeRTOS task handle.
void set_priority(unsigned int new_priority)
Changes the task priority.
unsigned int priority() const noexcept
Returns the current task priority.
task(const config &cfg, std::move_only_function< void(self &)> task_func)
Creates a task with a std::move_only_function callback.
void join_until(const std::chrono::time_point< Clock, Duration > &deadline)
Blocks until the task function completes or the deadline is reached.
constexpr TickType_t ticks(const std::chrono::duration< Rep, Period > &d)
Converts a std::chrono duration to TickType_t ticks.
memory_type
Memory region type for heap allocations.
@ internal
Internal DRAM (default)
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
@ timeout
Operation timed out.
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Task configuration parameters.