idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
Scheduling Utilities

Delay and yield functions for task scheduling. More...

Namespaces

namespace  idfxx
 

Functions

template<typename Rep , typename Period >
void idfxx::delay (const std::chrono::duration< Rep, Period > &duration)
 Delay for the specified duration.
 
template<typename Clock , typename Duration >
void idfxx::delay_until (const std::chrono::time_point< Clock, Duration > &target)
 Delays until the specified time point.
 
void idfxx::yield () noexcept
 Yields execution to other ready tasks of equal priority.
 
void idfxx::yield_from_isr (bool higher_priority_task_woken=true) noexcept
 Requests a context switch from ISR context.
 

Detailed Description

Delay and yield functions for task scheduling.

Provides scheduling primitives for the current task context:

Function Documentation

◆ delay()

template<typename Rep , typename Period >
void idfxx::delay ( const std::chrono::duration< Rep, Period > &  duration)

Delay for the specified duration.

Automatically selects the appropriate delay method:

  • For durations < 10ms: uses ets_delay_us (busy-wait, microsecond precision)
  • For durations >= 10ms: uses vTaskDelay (yields to scheduler, tick-based)
Template Parameters
RepThe representation type of the duration.
PeriodThe period type of the duration.
Parameters
durationThe duration to delay for.
Note
Must not be called from ISR context. Debug builds will assert on ISR context.
Zero or negative durations return immediately.

Definition at line 43 of file sched.hpp.

References idfxx::delay().

Referenced by idfxx::delay(), and idfxx::delay_until().

◆ delay_until()

template<typename Clock , typename Duration >
void idfxx::delay_until ( const std::chrono::time_point< Clock, Duration > &  target)

Delays until the specified time point.

Computes the remaining time from Clock::now() to the target time point and delays for that duration. If the target time has already passed, returns immediately without delaying.

This is useful for periodic timing loops where execution time between iterations should not cause drift:

auto next = idfxx::chrono::tick_clock::now() + 100ms;
while (true) {
next += 100ms;
// Runs every 100ms regardless of execution time
}
void delay_until(const std::chrono::time_point< Clock, Duration > &target)
Delays until the specified time point.
Definition sched.hpp:77
static time_point now() noexcept
Returns the current tick count as a time_point.
Definition chrono.hpp:55
Template Parameters
ClockThe clock type (must provide Clock::now()).
DurationThe duration type of the time point.
Parameters
targetThe time point to delay until.
Note
Must not be called from ISR context.

Definition at line 77 of file sched.hpp.

References idfxx::delay().

◆ yield()

void idfxx::yield ( )
inlinenoexcept

Yields execution to other ready tasks of equal priority.

Definition at line 87 of file sched.hpp.

◆ yield_from_isr()

void idfxx::yield_from_isr ( bool  higher_priority_task_woken = true)
noexcept

Requests a context switch from ISR context.

Call this at the end of an ISR when a FreeRTOS API has indicated that a higher priority task was woken. This ensures the scheduler switches to the higher priority task immediately upon ISR completion rather than waiting for the next tick.

Parameters
higher_priority_task_wokentrue if a higher priority task was woken by a FreeRTOS call during the ISR, false otherwise. When false, no context switch is requested. Defaults to true for unconditional context switch.
void IRAM_ATTR my_isr() {
bool need_yield = worker_task->resume_from_isr();
idfxx::yield_from_isr(need_yield);
}
void yield_from_isr(bool higher_priority_task_woken=true) noexcept
Requests a context switch from ISR context.