Completion Synchronization APIs¶
Overview¶
Completion provides a lightweight synchronization mechanism for one-shot event notification.
It is commonly used when one execution context must wait until another execution context finishes an operation.
Typical use cases include:
- Hardware initialization
- Firmware loading
- DMA completion
- Worker startup synchronization
- Driver probe synchronization
init_completion()¶
Prototype¶
Parameters¶
| Parameter | Description |
|---|---|
| x | Completion object |
Description¶
Initialize a completion object.
The completion state becomes:
Example¶
reinit_completion()¶
Prototype¶
Parameters¶
| Parameter | Description |
|---|---|
| x | Completion object |
Description¶
Reset a completion object after it has already completed.
This allows the completion object to be reused.
Example¶
Notes¶
Completion state persists after completion.
Without reinitialization, future waiters return immediately.
wait_for_completion()¶
Prototype¶
Parameters¶
| Parameter | Description |
|---|---|
| x | Completion object |
Description¶
Block until completion is signaled.
Example¶
Notes¶
The caller sleeps until:
or
is executed.
wait_for_completion_timeout()¶
Prototype¶
Parameters¶
| Parameter | Description |
|---|---|
| x | Completion object |
| timeout | Timeout in jiffies |
Return Value¶
| Return | Meaning |
|---|---|
| 0 | Timeout |
| > 0 | Completed |
Example¶
Notes¶
Timeout only indicates that completion did not occur.
The worker responsible for signaling completion may still be running.
complete()¶
Prototype¶
Parameters¶
| Parameter | Description |
|---|---|
| x | Completion object |
Description¶
Signal completion and wake a single waiter.
Example¶
Notes¶
If multiple waiters are blocked:
wakes only one waiter.
complete_all()¶
Prototype¶
Parameters¶
| Parameter | Description |
|---|---|
| x | Completion object |
Description¶
Signal completion and wake all waiters.
Example¶
Notes¶
All waiters blocked on the completion object are awakened.
Common Pitfalls¶
Forgetting Timeout Handling¶
Bad:
If the completion event never occurs, the caller blocks forever.
Consider:
when waiting for hardware or firmware.
Assuming Timeout Means Worker Exit¶
Bad assumption:
Timeout only means:
Worker cleanup must be handled separately.
Forgetting reinit_completion()¶
After:
future waiters return immediately.
Use:
before reusing a completion object.
Related APIs¶
- wait_event()
- wait_event_timeout()
- wake_up()
- kthread_run()
- kthread_stop()
Related Labs¶
- Day68 - Kernel Threads and Wait Queues
- Day69 - Completion Synchronization