lib/closure.c
Source file repositories/reference/linux-study-clean/lib/closure.c
File Facts
- System
- Linux kernel
- Corpus path
lib/closure.c- Extension
.c- Size
- 6914 bytes
- Lines
- 298
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/closure.hlinux/debugfs.hlinux/export.hlinux/rcupdate.hlinux/seq_file.hlinux/sched/debug.h
Detected Declarations
struct closure_syncerfunction closure_put_after_sub_checksfunction closure_put_after_subfunction closure_subfunction closure_putfunction __closure_wake_upfunction closure_wake_upfunction CLOSURE_CALLBACKfunction __closure_syncfunction closure_get_not_zerofunction __closure_sync_timeoutfunction closure_debug_createfunction closure_debug_destroyfunction debug_showfunction list_for_each_entryfunction closure_debug_initexport closure_subexport closure_putexport __closure_wake_upexport closure_waitexport __closure_syncexport closure_return_syncexport __closure_sync_timeoutexport closure_debug_createexport closure_debug_destroy
Annotated Snippet
struct closure_syncer {
struct task_struct *task;
int done;
};
static CLOSURE_CALLBACK(closure_sync_fn)
{
struct closure *cl = container_of(ws, struct closure, work);
struct closure_syncer *s = cl->s;
struct task_struct *p;
rcu_read_lock();
p = READ_ONCE(s->task);
s->done = 1;
wake_up_process(p);
rcu_read_unlock();
}
void __sched __closure_sync(struct closure *cl)
{
struct closure_syncer s = { .task = current };
cl->s = &s;
continue_at(cl, closure_sync_fn, NULL);
while (1) {
set_current_state(TASK_UNINTERRUPTIBLE);
if (s.done)
break;
schedule();
}
__set_current_state(TASK_RUNNING);
}
EXPORT_SYMBOL(__closure_sync);
/*
* closure_return_sync - finish running a closure, synchronously (i.e. waiting
* for outstanding get()s to finish) and returning once closure refcount is 0.
*
* Unlike closure_sync() this doesn't reinit the ref to 1; subsequent
* closure_get_not_zero() calls waill fail.
*/
void __sched closure_return_sync(struct closure *cl)
{
struct closure_syncer s = { .task = current };
cl->s = &s;
set_closure_fn(cl, closure_sync_fn, NULL);
unsigned flags = atomic_sub_return_release(1 + CLOSURE_RUNNING - CLOSURE_DESTRUCTOR,
&cl->remaining);
closure_put_after_sub_checks(flags);
if (unlikely(flags & CLOSURE_REMAINING_MASK)) {
while (1) {
set_current_state(TASK_UNINTERRUPTIBLE);
if (s.done)
break;
schedule();
}
__set_current_state(TASK_RUNNING);
}
if (cl->parent)
closure_put(cl->parent);
}
EXPORT_SYMBOL(closure_return_sync);
int __sched __closure_sync_timeout(struct closure *cl, unsigned long timeout)
{
struct closure_syncer s = { .task = current };
int ret = 0;
cl->s = &s;
continue_at(cl, closure_sync_fn, NULL);
while (1) {
set_current_state(TASK_UNINTERRUPTIBLE);
if (s.done)
break;
if (!timeout) {
/*
* Carefully undo the continue_at() - but only if it
* hasn't completed, i.e. the final closure_put() hasn't
* happened yet:
*/
unsigned old, new, v = atomic_read(&cl->remaining);
Annotation
- Immediate include surface: `linux/closure.h`, `linux/debugfs.h`, `linux/export.h`, `linux/rcupdate.h`, `linux/seq_file.h`, `linux/sched/debug.h`.
- Detected declarations: `struct closure_syncer`, `function closure_put_after_sub_checks`, `function closure_put_after_sub`, `function closure_sub`, `function closure_put`, `function __closure_wake_up`, `function closure_wake_up`, `function CLOSURE_CALLBACK`, `function __closure_sync`, `function closure_get_not_zero`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.