ISR-safe API functions (FromISR suffix) in FreeRTOS - Time & Space Complexity
When using ISR-safe API functions in FreeRTOS, it's important to know how their execution time changes as input grows.
We want to see how the cost of these functions scales when called from interrupts.
Analyze the time complexity of this ISR-safe queue send function.
void vExampleISR(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
int data = 42;
xQueueSendFromISR(xQueue, &data, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
This code sends data to a queue safely from an interrupt service routine.
Look for loops or repeated steps inside the ISR-safe function.
- Primary operation: The queue send checks and updates queue state.
- How many times: It runs a fixed number of steps, no loops over input size.
The function's work stays about the same no matter how many items are in the queue.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Constant steps |
| 100 | Constant steps |
| 1000 | Constant steps |
Pattern observation: The execution time stays constant regardless of input size.
Time Complexity: O(1)
This means the function takes about the same time no matter how big the queue is.
[X] Wrong: "The ISR-safe queue send takes longer if the queue has more items."
[OK] Correct: The function only updates pointers and flags, not scanning all items, so time stays constant.
Understanding how ISR-safe functions behave helps you write reliable real-time code and explain your choices clearly.
"What if the ISR-safe function had to copy multiple items instead of one? How would the time complexity change?"