ISR-to-task notification pattern in FreeRTOS - Time & Space Complexity
We want to understand how the time cost changes when an interrupt signals a task using notifications.
How does the number of notifications affect the time taken by the system?
Analyze the time complexity of the following code snippet.
void ISR_Handler(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(TaskHandle, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
void TaskFunction(void *pvParameters) {
for (;;) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
// Process notification
}
}
This code shows an interrupt service routine sending notifications to a task, which waits and processes them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The task waits in a loop for notifications and processes each one.
- How many times: The loop runs once per notification received, repeating indefinitely.
Each notification triggers one loop iteration in the task to process it.
| Input Size (notifications) | Approx. Operations |
|---|---|
| 10 | 10 task loop iterations |
| 100 | 100 task loop iterations |
| 1000 | 1000 task loop iterations |
Pattern observation: The number of operations grows directly with the number of notifications.
Time Complexity: O(n)
This means the time to process notifications grows linearly with how many notifications arrive.
[X] Wrong: "The task processes all notifications instantly, so time does not grow with more notifications."
[OK] Correct: Each notification causes the task to run once, so more notifications mean more processing time.
Understanding how tasks handle notifications from interrupts helps you explain real-time system responsiveness and workload management clearly.
"What if the task processes multiple notifications in one loop iteration? How would the time complexity change?"
Practice
Solution
Step 1: Understand ISR and task roles
ISRs handle hardware events quickly, tasks do longer processing.Step 2: Identify notification purpose
The pattern lets ISR tell a task to run without delay.Final Answer:
To allow an interrupt to quickly notify a task to run -> Option AQuick Check:
ISR notifies task [OK]
- Thinking ISR creates tasks
- Believing tasks pause interrupts
- Confusing disabling interrupts with notifications
Solution
Step 1: Identify ISR notification function
Inside ISR, use vTaskNotifyGiveFromISR() to notify tasks.Step 2: Differentiate task functions
ulTaskNotifyTake() is used in tasks, not ISRs.Final Answer:
vTaskNotifyGiveFromISR() -> Option AQuick Check:
Notify from ISR = vTaskNotifyGiveFromISR() [OK]
- Using ulTaskNotifyTake() inside ISR
- Calling xTaskCreate() in ISR
- Using vTaskDelay() inside ISR
void ISR_Handler() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(taskHandle, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
void TaskFunction(void *pvParameters) {
for(;;) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
printf("Task notified!\n");
}
}Solution
Step 1: Analyze ISR notification call
ISR calls vTaskNotifyGiveFromISR() to notify the task each time it runs.Step 2: Check task notification wait
Task waits indefinitely with ulTaskNotifyTake(), then prints on notification.Final Answer:
Task notified! (printed each time ISR runs) -> Option BQuick Check:
ISR notifies task -> prints each time [OK]
- Assuming task never wakes
- Confusing function parameters
- Thinking output prints only once
void ISR_Handler() {
vTaskNotifyGive(taskHandle);
}
void TaskFunction(void *pvParameters) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
printf("Notified\n");
}Solution
Step 1: Check ISR notification function
vTaskNotifyGive() is not safe inside ISR; must use vTaskNotifyGiveFromISR().Step 2: Verify task code correctness
TaskFunction can have single call; infinite loop is common but not error here.Final Answer:
Using vTaskNotifyGive() inside ISR instead of vTaskNotifyGiveFromISR() -> Option DQuick Check:
ISR notification function must end with FromISR [OK]
- Calling task functions inside ISR
- Ignoring FromISR suffix importance
- Assuming printf() is disallowed in tasks
Solution
Step 1: Understand multiple ISR notifications
Each ISR calls vTaskNotifyGiveFromISR() to increment notification count.Step 2: Task processes all notifications
Task uses ulTaskNotifyTake(pdTRUE, portMAX_DELAY) in a loop to clear all counts and process each notification.Final Answer:
Use ulTaskNotifyTake() with clear count and call vTaskNotifyGiveFromISR() in each ISR; task loops to process all notifications -> Option CQuick Check:
Multiple ISR notifications require counting and looping [OK]
- Assuming one notification covers all ISR events
- Using non-FromISR functions in ISR
- Ignoring notification count clearing
Ready to prove it?
Pass the test and this pattern moves unknown
