Completion Synchronization¶
Overview¶
Completion is a Linux kernel synchronization primitive designed for one-shot event notification.
It allows one execution context to wait until another execution context signals that an operation has completed.
Common use cases include:
- Hardware initialization
- Firmware loading
- DMA completion
- Worker startup synchronization
- Probe synchronization
Completion provides a simpler alternative to wait queues when only a single completion event must be tracked.
Why Completion Exists¶
Without completion, the same behavior is often implemented using:
Completion encapsulates this pattern into a dedicated synchronization primitive.
Instead of expressing:
Wait until a condition becomes true.
Completion expresses:
Wait until an operation finishes.
Completion Lifecycle¶
Typical workflow:
The waiter blocks until another execution context signals completion.
Completion Object¶
Kernel definition:
The internal state contains:
- Completion counter
- Wait queue
Completion is built on top of Linux wait queues.
Initialization¶
Initialize a completion object:
The completion state becomes:
Waiting for Completion¶
Wait indefinitely:
The caller sleeps until completion is signaled.
Timeout Version¶
Wait with timeout:
Return value:
| Return | Meaning |
|---|---|
| 0 | Timeout |
| > 0 | Completed |
Signaling Completion¶
Signal a single waiter:
Signal all waiters:
complete() vs complete_all()¶
complete()¶
Wake a single waiter.
Example:
Result:
complete_all()¶
Wake all waiters.
Example:
Result:
Completion State Persistence¶
Completion remains in the completed state after:
or
Subsequent waiters return immediately.
Example:
returns immediately.
Reinitialization¶
Reset completion state:
After reinitialization:
blocks again until another completion event occurs.
Completion vs Wait Queue¶
| Feature | Completion | Wait Queue |
|---|---|---|
| One-shot event | Yes | Possible |
| Repeated events | No | Yes |
| Multiple conditions | No | Yes |
| State machine support | No | Yes |
| Initialization synchronization | Excellent | Acceptable |
| Event-driven state machine | Limited | Excellent |
Typical Use Cases¶
Completion¶
Recommended for:
- Firmware ready
- DMA complete
- Hardware initialization
- Worker startup synchronization
- Probe synchronization
Example:
Wait Queue¶
Recommended for:
- Producer / consumer systems
- State machines
- Event loops
- Driver event processing
- Repeated notifications
Example:
RTOS Mapping¶
For developers with RTOS experience:
| RTOS Concept | Linux Kernel Primitive |
|---|---|
| Binary Event | Completion |
| Event Group | Wait Queue |
| Counting Semaphore | Semaphore |
| Mutex | Mutex |
Completion most closely resembles a binary event used to signal that a specific operation has finished.
Common Mistakes¶
Forgetting Timeout Handling¶
Bad:
A failed device may cause the caller to block forever.
Prefer:
when waiting for hardware.
Assuming Timeout Means Worker Exit¶
Timeout only indicates:
The worker may still be running.
Worker lifetime must be handled separately.
Forgetting reinit_completion()¶
After completion occurs:
returns immediately.
Use:
before reusing the completion object.
Summary¶
Completion is a lightweight synchronization primitive for one-shot events.
Use completion when the goal is:
Wait until an operation finishes.
Use wait queues when the goal is:
Wait until a condition becomes true.