xTaskNotify() with value in FreeRTOS - Time & Space Complexity
We want to understand how the time cost changes when using xTaskNotify() with a value in FreeRTOS.
Specifically, how does the execution time grow as the number of notifications or tasks changes?
Analyze the time complexity of the following code snippet.
BaseType_t xTaskNotify(
TaskHandle_t xTaskToNotify,
uint32_t ulValue,
eNotifyAction eAction
);
This function sends a notification with a value to a specific task, updating its notification state.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Updating the notification value and checking the task state.
- How many times: This happens once per call; no loops or recursion inside
xTaskNotify().
The execution time stays about the same no matter how many notifications are sent or how many tasks exist.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Constant steps to update one task |
| 100 | Same constant steps for one task |
| 1000 | Still constant steps for one task |
Pattern observation: The time does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the time to notify a task with a value stays the same no matter how many tasks or notifications exist.
[X] Wrong: "Notifying a task takes longer if many tasks exist or many notifications are sent."
[OK] Correct: The function targets one task directly and updates its notification value in constant time, so other tasks or notifications do not affect this time.
Understanding that task notification functions run in constant time helps you reason about real-time system responsiveness and efficient task communication.
"What if xTaskNotify() was changed to notify multiple tasks at once? How would the time complexity change?"
Practice
What does the xTaskNotify() function primarily do in FreeRTOS?
Solution
Step 1: Understand the purpose of xTaskNotify()
The function is designed to send a notification value or signal from one task to another in FreeRTOS.Step 2: Compare with other task functions
Creating, deleting, or suspending tasks are done by other FreeRTOS functions, not xTaskNotify().Final Answer:
Sends a notification value from one task to another -> Option CQuick Check:
xTaskNotify() sends notification value [OK]
- Confusing xTaskNotify() with task creation functions
- Thinking it suspends or deletes tasks
- Assuming it handles task priorities
Which of the following is the correct syntax to send a notification with a value using xTaskNotify()?
BaseType_t xTaskNotify( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction );
Solution
Step 1: Identify parameter order in xTaskNotify()
The correct order is: TaskHandle_t, uint32_t value, eNotifyAction.Step 2: Match options to correct order
xTaskNotify(taskHandle, 10, eSetBits); matches the correct order: taskHandle, 10, eSetBits. Others mix parameter order incorrectly.Final Answer:
xTaskNotify(taskHandle, 10, eSetBits); -> Option AQuick Check:
Correct parameter order: task, value, action [OK]
- Swapping value and task handle parameters
- Using wrong enum for eNotifyAction
- Passing parameters in wrong order
Consider the following code snippet:
uint32_t notifiedValue = 0; // Task A sends notification xTaskNotify(taskBHandle, 0x04, eSetBits); // Task B waits for notification xTaskNotifyWait(0x00, 0xFFFFFFFF, ¬ifiedValue, portMAX_DELAY); // What is the value of notifiedValue after notification?
Solution
Step 1: Understand eSetBits action
eSetBits sets bits in the notification value. Since initial is 0, setting bit 0x04 results in notifiedValue = 0x04.Step 2: Check xTaskNotifyWait parameters
It clears bits 0x00 (none) and waits indefinitely, storing the notified bits in notifiedValue.Final Answer:
0x04 -> Option AQuick Check:
eSetBits sets bits = 0x04 [OK]
- Assuming notifiedValue remains zero
- Confusing eSetBits with eSetValueWithoutOverwrite
- Ignoring the effect of xTaskNotifyWait
Identify the error in the following code snippet using xTaskNotify():
BaseType_t status;
status = xTaskNotify(NULL, 5, eSetValueWithOverwrite);
if(status == pdPASS) {
// Notification sent
}Solution
Step 1: Check task handle parameter
xTaskNotify requires a valid task handle; NULL is invalid and causes failure.Step 2: Validate other parameters
eSetValueWithOverwrite and value 5 are valid; no return statement needed here.Final Answer:
Passing NULL as task handle causes error -> Option DQuick Check:
Task handle NULL invalid [OK]
- Passing NULL as task handle
- Thinking notification value must be zero
- Confusing eNotifyAction enums
You want to send a notification value from Task A to Task B, but only if Task B's current notification value is zero. Which eNotifyAction should you use with xTaskNotify() to achieve this?
Solution
Step 1: Understand eNotifyAction options
eSetValueWithoutOverwrite sets the notification value only if it is currently zero.Step 2: Compare with other actions
eSetValueWithOverwrite always overwrites; eIncrement adds 1; eNoAction sends no value.Final Answer:
eSetValueWithoutOverwrite -> Option BQuick Check:
Set only if zero = eSetValueWithoutOverwrite [OK]
- Using eSetValueWithOverwrite and overwriting value
- Choosing eIncrement which changes value incorrectly
- Selecting eNoAction which sends no value
Ready to prove it?
Pass the test and this pattern moves unknown
