xref: /linux-6.15/include/linux/completion.h (revision a7967bc3)
1 #ifndef __LINUX_COMPLETION_H
2 #define __LINUX_COMPLETION_H
3 
4 /*
5  * (C) Copyright 2001 Linus Torvalds
6  *
7  * Atomic wait-for-completion handler data structures.
8  * See kernel/sched/completion.c for details.
9  */
10 
11 #include <linux/wait.h>
12 #ifdef CONFIG_LOCKDEP_COMPLETIONS
13 #include <linux/lockdep.h>
14 #endif
15 
16 /*
17  * struct completion - structure used to maintain state for a "completion"
18  *
19  * This is the opaque structure used to maintain the state for a "completion".
20  * Completions currently use a FIFO to queue threads that have to wait for
21  * the "completion" event.
22  *
23  * See also:  complete(), wait_for_completion() (and friends _timeout,
24  * _interruptible, _interruptible_timeout, and _killable), init_completion(),
25  * reinit_completion(), and macros DECLARE_COMPLETION(),
26  * DECLARE_COMPLETION_ONSTACK().
27  */
28 struct completion {
29 	unsigned int done;
30 	wait_queue_head_t wait;
31 #ifdef CONFIG_LOCKDEP_COMPLETIONS
32 	struct lockdep_map_cross map;
33 #endif
34 };
35 
36 #ifdef CONFIG_LOCKDEP_COMPLETIONS
37 static inline void complete_acquire(struct completion *x)
38 {
39 	lock_acquire_exclusive((struct lockdep_map *)&x->map, 0, 0, NULL, _RET_IP_);
40 }
41 
42 static inline void complete_release(struct completion *x)
43 {
44 	lock_release((struct lockdep_map *)&x->map, 0, _RET_IP_);
45 }
46 
47 static inline void complete_release_commit(struct completion *x)
48 {
49 	lock_commit_crosslock((struct lockdep_map *)&x->map);
50 }
51 
52 #define init_completion_map(x, m)					\
53 do {									\
54 	lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map,	\
55 			(m)->name, (m)->key, 0);				\
56 	__init_completion(x);						\
57 } while (0)
58 
59 #define init_completion(x)						\
60 do {									\
61 	static struct lock_class_key __key;				\
62 	lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map,	\
63 			"(completion)" #x,				\
64 			&__key, 0);					\
65 	__init_completion(x);						\
66 } while (0)
67 #else
68 #define init_completion_map(x, m) __init_completion(x)
69 #define init_completion(x) __init_completion(x)
70 static inline void complete_acquire(struct completion *x) {}
71 static inline void complete_release(struct completion *x) {}
72 static inline void complete_release_commit(struct completion *x) {}
73 #endif
74 
75 #ifdef CONFIG_LOCKDEP_COMPLETIONS
76 #define COMPLETION_INITIALIZER(work) \
77 	{ 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait), \
78 	STATIC_CROSS_LOCKDEP_MAP_INIT("(completion)" #work, &(work)) }
79 #else
80 #define COMPLETION_INITIALIZER(work) \
81 	{ 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
82 #endif
83 
84 #define COMPLETION_INITIALIZER_ONSTACK_MAP(work, map) \
85 	(*({ init_completion_map(&(work), &(map)); &(work); }))
86 
87 #define COMPLETION_INITIALIZER_ONSTACK(work) \
88 	(*({ init_completion(&work); &work; }))
89 
90 /**
91  * DECLARE_COMPLETION - declare and initialize a completion structure
92  * @work:  identifier for the completion structure
93  *
94  * This macro declares and initializes a completion structure. Generally used
95  * for static declarations. You should use the _ONSTACK variant for automatic
96  * variables.
97  */
98 #define DECLARE_COMPLETION(work) \
99 	struct completion work = COMPLETION_INITIALIZER(work)
100 
101 /*
102  * Lockdep needs to run a non-constant initializer for on-stack
103  * completions - so we use the _ONSTACK() variant for those that
104  * are on the kernel stack:
105  */
106 /**
107  * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
108  * @work:  identifier for the completion structure
109  *
110  * This macro declares and initializes a completion structure on the kernel
111  * stack.
112  */
113 #ifdef CONFIG_LOCKDEP
114 # define DECLARE_COMPLETION_ONSTACK(work) \
115 	struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
116 # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) \
117 	struct completion work = COMPLETION_INITIALIZER_ONSTACK_MAP(work, map)
118 #else
119 # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
120 # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) DECLARE_COMPLETION(work)
121 #endif
122 
123 /**
124  * init_completion - Initialize a dynamically allocated completion
125  * @x:  pointer to completion structure that is to be initialized
126  *
127  * This inline function will initialize a dynamically created completion
128  * structure.
129  */
130 static inline void __init_completion(struct completion *x)
131 {
132 	x->done = 0;
133 	init_waitqueue_head(&x->wait);
134 }
135 
136 /**
137  * reinit_completion - reinitialize a completion structure
138  * @x:  pointer to completion structure that is to be reinitialized
139  *
140  * This inline function should be used to reinitialize a completion structure so it can
141  * be reused. This is especially important after complete_all() is used.
142  */
143 static inline void reinit_completion(struct completion *x)
144 {
145 	x->done = 0;
146 }
147 
148 extern void wait_for_completion(struct completion *);
149 extern void wait_for_completion_io(struct completion *);
150 extern int wait_for_completion_interruptible(struct completion *x);
151 extern int wait_for_completion_killable(struct completion *x);
152 extern unsigned long wait_for_completion_timeout(struct completion *x,
153 						   unsigned long timeout);
154 extern unsigned long wait_for_completion_io_timeout(struct completion *x,
155 						    unsigned long timeout);
156 extern long wait_for_completion_interruptible_timeout(
157 	struct completion *x, unsigned long timeout);
158 extern long wait_for_completion_killable_timeout(
159 	struct completion *x, unsigned long timeout);
160 extern bool try_wait_for_completion(struct completion *x);
161 extern bool completion_done(struct completion *x);
162 
163 extern void complete(struct completion *);
164 extern void complete_all(struct completion *);
165 
166 #endif
167