1 // SPDX-License-Identifier: GPL-2.0
3 * Generic wait-for-completion handler;
5 * It differs from semaphores in that their default case is the opposite,
6 * wait_for_completion default blocks whereas semaphore default non-block. The
7 * interface also makes it easy to 'complete' multiple waiting threads,
8 * something which isn't entirely natural for semaphores.
10 * But more importantly, the primitive documents the usage. Semaphores would
11 * typically be used for exclusion which gives rise to priority inversion.
12 * Waiting for completion is a typically sync point, but not an exclusion point.
15 #include <linux/sched/signal.h>
16 #include <linux/sched/debug.h>
17 #include <linux/completion.h>
20 * complete: - signals a single thread waiting on this completion
21 * @x: holds the state of this particular completion
23 * This will wake up a single thread waiting on this completion. Threads will be
24 * awakened in the same order in which they were queued.
26 * See also complete_all(), wait_for_completion() and related routines.
28 * It may be assumed that this function implies a write memory barrier before
29 * changing the task state if and only if any tasks are woken up.
31 void complete(struct completion *x)
35 spin_lock_irqsave(&x->wait.lock, flags);
38 * Perform commit of crossrelease here.
40 complete_release_commit(x);
42 if (x->done != UINT_MAX)
44 __wake_up_locked(&x->wait, TASK_NORMAL, 1);
45 spin_unlock_irqrestore(&x->wait.lock, flags);
47 EXPORT_SYMBOL(complete);
50 * complete_all: - signals all threads waiting on this completion
51 * @x: holds the state of this particular completion
53 * This will wake up all threads waiting on this particular completion event.
55 * It may be assumed that this function implies a write memory barrier before
56 * changing the task state if and only if any tasks are woken up.
58 * Since complete_all() sets the completion of @x permanently to done
59 * to allow multiple waiters to finish, a call to reinit_completion()
60 * must be used on @x if @x is to be used again. The code must make
61 * sure that all waiters have woken and finished before reinitializing
62 * @x. Also note that the function completion_done() can not be used
63 * to know if there are still waiters after complete_all() has been called.
65 void complete_all(struct completion *x)
69 spin_lock_irqsave(&x->wait.lock, flags);
71 __wake_up_locked(&x->wait, TASK_NORMAL, 0);
72 spin_unlock_irqrestore(&x->wait.lock, flags);
74 EXPORT_SYMBOL(complete_all);
76 static inline long __sched
77 do_wait_for_common(struct completion *x,
78 long (*action)(long), long timeout, int state)
81 DECLARE_WAITQUEUE(wait, current);
83 __add_wait_queue_entry_tail_exclusive(&x->wait, &wait);
85 if (signal_pending_state(state, current)) {
86 timeout = -ERESTARTSYS;
89 __set_current_state(state);
90 spin_unlock_irq(&x->wait.lock);
91 timeout = action(timeout);
92 spin_lock_irq(&x->wait.lock);
93 } while (!x->done && timeout);
94 __remove_wait_queue(&x->wait, &wait);
98 if (x->done != UINT_MAX)
103 static inline long __sched
104 __wait_for_common(struct completion *x,
105 long (*action)(long), long timeout, int state)
111 spin_lock_irq(&x->wait.lock);
112 timeout = do_wait_for_common(x, action, timeout, state);
113 spin_unlock_irq(&x->wait.lock);
121 wait_for_common(struct completion *x, long timeout, int state)
123 return __wait_for_common(x, schedule_timeout, timeout, state);
127 wait_for_common_io(struct completion *x, long timeout, int state)
129 return __wait_for_common(x, io_schedule_timeout, timeout, state);
133 * wait_for_completion: - waits for completion of a task
134 * @x: holds the state of this particular completion
136 * This waits to be signaled for completion of a specific task. It is NOT
137 * interruptible and there is no timeout.
139 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
140 * and interrupt capability. Also see complete().
142 void __sched wait_for_completion(struct completion *x)
144 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
146 EXPORT_SYMBOL(wait_for_completion);
149 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
150 * @x: holds the state of this particular completion
151 * @timeout: timeout value in jiffies
153 * This waits for either a completion of a specific task to be signaled or for a
154 * specified timeout to expire. The timeout is in jiffies. It is not
157 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
158 * till timeout) if completed.
160 unsigned long __sched
161 wait_for_completion_timeout(struct completion *x, unsigned long timeout)
163 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
165 EXPORT_SYMBOL(wait_for_completion_timeout);
168 * wait_for_completion_io: - waits for completion of a task
169 * @x: holds the state of this particular completion
171 * This waits to be signaled for completion of a specific task. It is NOT
172 * interruptible and there is no timeout. The caller is accounted as waiting
173 * for IO (which traditionally means blkio only).
175 void __sched wait_for_completion_io(struct completion *x)
177 wait_for_common_io(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
179 EXPORT_SYMBOL(wait_for_completion_io);
182 * wait_for_completion_io_timeout: - waits for completion of a task (w/timeout)
183 * @x: holds the state of this particular completion
184 * @timeout: timeout value in jiffies
186 * This waits for either a completion of a specific task to be signaled or for a
187 * specified timeout to expire. The timeout is in jiffies. It is not
188 * interruptible. The caller is accounted as waiting for IO (which traditionally
191 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
192 * till timeout) if completed.
194 unsigned long __sched
195 wait_for_completion_io_timeout(struct completion *x, unsigned long timeout)
197 return wait_for_common_io(x, timeout, TASK_UNINTERRUPTIBLE);
199 EXPORT_SYMBOL(wait_for_completion_io_timeout);
202 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
203 * @x: holds the state of this particular completion
205 * This waits for completion of a specific task to be signaled. It is
208 * Return: -ERESTARTSYS if interrupted, 0 if completed.
210 int __sched wait_for_completion_interruptible(struct completion *x)
212 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
213 if (t == -ERESTARTSYS)
217 EXPORT_SYMBOL(wait_for_completion_interruptible);
220 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
221 * @x: holds the state of this particular completion
222 * @timeout: timeout value in jiffies
224 * This waits for either a completion of a specific task to be signaled or for a
225 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
227 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
228 * or number of jiffies left till timeout) if completed.
231 wait_for_completion_interruptible_timeout(struct completion *x,
232 unsigned long timeout)
234 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
236 EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
239 * wait_for_completion_killable: - waits for completion of a task (killable)
240 * @x: holds the state of this particular completion
242 * This waits to be signaled for completion of a specific task. It can be
243 * interrupted by a kill signal.
245 * Return: -ERESTARTSYS if interrupted, 0 if completed.
247 int __sched wait_for_completion_killable(struct completion *x)
249 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
250 if (t == -ERESTARTSYS)
254 EXPORT_SYMBOL(wait_for_completion_killable);
257 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
258 * @x: holds the state of this particular completion
259 * @timeout: timeout value in jiffies
261 * This waits for either a completion of a specific task to be
262 * signaled or for a specified timeout to expire. It can be
263 * interrupted by a kill signal. The timeout is in jiffies.
265 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
266 * or number of jiffies left till timeout) if completed.
269 wait_for_completion_killable_timeout(struct completion *x,
270 unsigned long timeout)
272 return wait_for_common(x, timeout, TASK_KILLABLE);
274 EXPORT_SYMBOL(wait_for_completion_killable_timeout);
277 * try_wait_for_completion - try to decrement a completion without blocking
278 * @x: completion structure
280 * Return: 0 if a decrement cannot be done without blocking
281 * 1 if a decrement succeeded.
283 * If a completion is being used as a counting completion,
284 * attempt to decrement the counter without blocking. This
285 * enables us to avoid waiting if the resource the completion
286 * is protecting is not available.
288 bool try_wait_for_completion(struct completion *x)
294 * Since x->done will need to be locked only
295 * in the non-blocking case, we check x->done
296 * first without taking the lock so we can
297 * return early in the blocking case.
299 if (!READ_ONCE(x->done))
302 spin_lock_irqsave(&x->wait.lock, flags);
305 else if (x->done != UINT_MAX)
307 spin_unlock_irqrestore(&x->wait.lock, flags);
310 EXPORT_SYMBOL(try_wait_for_completion);
313 * completion_done - Test to see if a completion has any waiters
314 * @x: completion structure
316 * Return: 0 if there are waiters (wait_for_completion() in progress)
317 * 1 if there are no waiters.
319 * Note, this will always return true if complete_all() was called on @X.
321 bool completion_done(struct completion *x)
325 if (!READ_ONCE(x->done))
329 * If ->done, we need to wait for complete() to release ->wait.lock
330 * otherwise we can end up freeing the completion before complete()
331 * is done referencing it.
333 spin_lock_irqsave(&x->wait.lock, flags);
334 spin_unlock_irqrestore(&x->wait.lock, flags);
337 EXPORT_SYMBOL(completion_done);