2 * Generic wait-for-completion handler;
4 * It differs from semaphores in that their default case is the opposite,
5 * wait_for_completion default blocks whereas semaphore default non-block. The
6 * interface also makes it easy to 'complete' multiple waiting threads,
7 * something which isn't entirely natural for semaphores.
9 * But more importantly, the primitive documents the usage. Semaphores would
10 * typically be used for exclusion which gives rise to priority inversion.
11 * Waiting for completion is a typically sync point, but not an exclusion point.
14 #include <linux/sched/signal.h>
15 #include <linux/sched/debug.h>
16 #include <linux/completion.h>
19 * complete: - signals a single thread waiting on this completion
20 * @x: holds the state of this particular completion
22 * This will wake up a single thread waiting on this completion. Threads will be
23 * awakened in the same order in which they were queued.
25 * See also complete_all(), wait_for_completion() and related routines.
27 * It may be assumed that this function implies a write memory barrier before
28 * changing the task state if and only if any tasks are woken up.
30 void complete(struct completion *x)
34 spin_lock_irqsave(&x->wait.lock, flags);
37 * Perform commit of crossrelease here.
39 complete_release_commit(x);
41 if (x->done != UINT_MAX)
43 __wake_up_locked(&x->wait, TASK_NORMAL, 1);
44 spin_unlock_irqrestore(&x->wait.lock, flags);
46 EXPORT_SYMBOL(complete);
49 * complete_all: - signals all threads waiting on this completion
50 * @x: holds the state of this particular completion
52 * This will wake up all threads waiting on this particular completion event.
54 * It may be assumed that this function implies a write memory barrier before
55 * changing the task state if and only if any tasks are woken up.
57 * Since complete_all() sets the completion of @x permanently to done
58 * to allow multiple waiters to finish, a call to reinit_completion()
59 * must be used on @x if @x is to be used again. The code must make
60 * sure that all waiters have woken and finished before reinitializing
61 * @x. Also note that the function completion_done() can not be used
62 * to know if there are still waiters after complete_all() has been called.
64 void complete_all(struct completion *x)
68 spin_lock_irqsave(&x->wait.lock, flags);
70 __wake_up_locked(&x->wait, TASK_NORMAL, 0);
71 spin_unlock_irqrestore(&x->wait.lock, flags);
73 EXPORT_SYMBOL(complete_all);
75 static inline long __sched
76 do_wait_for_common(struct completion *x,
77 long (*action)(long), long timeout, int state)
80 DECLARE_WAITQUEUE(wait, current);
82 __add_wait_queue_entry_tail_exclusive(&x->wait, &wait);
84 if (signal_pending_state(state, current)) {
85 timeout = -ERESTARTSYS;
88 __set_current_state(state);
89 spin_unlock_irq(&x->wait.lock);
90 timeout = action(timeout);
91 spin_lock_irq(&x->wait.lock);
92 } while (!x->done && timeout);
93 __remove_wait_queue(&x->wait, &wait);
97 if (x->done != UINT_MAX)
102 static inline long __sched
103 __wait_for_common(struct completion *x,
104 long (*action)(long), long timeout, int state)
110 spin_lock_irq(&x->wait.lock);
111 timeout = do_wait_for_common(x, action, timeout, state);
112 spin_unlock_irq(&x->wait.lock);
120 wait_for_common(struct completion *x, long timeout, int state)
122 return __wait_for_common(x, schedule_timeout, timeout, state);
126 wait_for_common_io(struct completion *x, long timeout, int state)
128 return __wait_for_common(x, io_schedule_timeout, timeout, state);
132 * wait_for_completion: - waits for completion of a task
133 * @x: holds the state of this particular completion
135 * This waits to be signaled for completion of a specific task. It is NOT
136 * interruptible and there is no timeout.
138 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
139 * and interrupt capability. Also see complete().
141 void __sched wait_for_completion(struct completion *x)
143 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
145 EXPORT_SYMBOL(wait_for_completion);
148 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
149 * @x: holds the state of this particular completion
150 * @timeout: timeout value in jiffies
152 * This waits for either a completion of a specific task to be signaled or for a
153 * specified timeout to expire. The timeout is in jiffies. It is not
156 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
157 * till timeout) if completed.
159 unsigned long __sched
160 wait_for_completion_timeout(struct completion *x, unsigned long timeout)
162 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
164 EXPORT_SYMBOL(wait_for_completion_timeout);
167 * wait_for_completion_io: - waits for completion of a task
168 * @x: holds the state of this particular completion
170 * This waits to be signaled for completion of a specific task. It is NOT
171 * interruptible and there is no timeout. The caller is accounted as waiting
172 * for IO (which traditionally means blkio only).
174 void __sched wait_for_completion_io(struct completion *x)
176 wait_for_common_io(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
178 EXPORT_SYMBOL(wait_for_completion_io);
181 * wait_for_completion_io_timeout: - waits for completion of a task (w/timeout)
182 * @x: holds the state of this particular completion
183 * @timeout: timeout value in jiffies
185 * This waits for either a completion of a specific task to be signaled or for a
186 * specified timeout to expire. The timeout is in jiffies. It is not
187 * interruptible. The caller is accounted as waiting for IO (which traditionally
190 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
191 * till timeout) if completed.
193 unsigned long __sched
194 wait_for_completion_io_timeout(struct completion *x, unsigned long timeout)
196 return wait_for_common_io(x, timeout, TASK_UNINTERRUPTIBLE);
198 EXPORT_SYMBOL(wait_for_completion_io_timeout);
201 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
202 * @x: holds the state of this particular completion
204 * This waits for completion of a specific task to be signaled. It is
207 * Return: -ERESTARTSYS if interrupted, 0 if completed.
209 int __sched wait_for_completion_interruptible(struct completion *x)
211 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
212 if (t == -ERESTARTSYS)
216 EXPORT_SYMBOL(wait_for_completion_interruptible);
219 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
220 * @x: holds the state of this particular completion
221 * @timeout: timeout value in jiffies
223 * This waits for either a completion of a specific task to be signaled or for a
224 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
226 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
227 * or number of jiffies left till timeout) if completed.
230 wait_for_completion_interruptible_timeout(struct completion *x,
231 unsigned long timeout)
233 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
235 EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
238 * wait_for_completion_killable: - waits for completion of a task (killable)
239 * @x: holds the state of this particular completion
241 * This waits to be signaled for completion of a specific task. It can be
242 * interrupted by a kill signal.
244 * Return: -ERESTARTSYS if interrupted, 0 if completed.
246 int __sched wait_for_completion_killable(struct completion *x)
248 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
249 if (t == -ERESTARTSYS)
253 EXPORT_SYMBOL(wait_for_completion_killable);
256 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
257 * @x: holds the state of this particular completion
258 * @timeout: timeout value in jiffies
260 * This waits for either a completion of a specific task to be
261 * signaled or for a specified timeout to expire. It can be
262 * interrupted by a kill signal. The timeout is in jiffies.
264 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
265 * or number of jiffies left till timeout) if completed.
268 wait_for_completion_killable_timeout(struct completion *x,
269 unsigned long timeout)
271 return wait_for_common(x, timeout, TASK_KILLABLE);
273 EXPORT_SYMBOL(wait_for_completion_killable_timeout);
276 * try_wait_for_completion - try to decrement a completion without blocking
277 * @x: completion structure
279 * Return: 0 if a decrement cannot be done without blocking
280 * 1 if a decrement succeeded.
282 * If a completion is being used as a counting completion,
283 * attempt to decrement the counter without blocking. This
284 * enables us to avoid waiting if the resource the completion
285 * is protecting is not available.
287 bool try_wait_for_completion(struct completion *x)
293 * Since x->done will need to be locked only
294 * in the non-blocking case, we check x->done
295 * first without taking the lock so we can
296 * return early in the blocking case.
298 if (!READ_ONCE(x->done))
301 spin_lock_irqsave(&x->wait.lock, flags);
304 else if (x->done != UINT_MAX)
306 spin_unlock_irqrestore(&x->wait.lock, flags);
309 EXPORT_SYMBOL(try_wait_for_completion);
312 * completion_done - Test to see if a completion has any waiters
313 * @x: completion structure
315 * Return: 0 if there are waiters (wait_for_completion() in progress)
316 * 1 if there are no waiters.
318 * Note, this will always return true if complete_all() was called on @X.
320 bool completion_done(struct completion *x)
324 if (!READ_ONCE(x->done))
328 * If ->done, we need to wait for complete() to release ->wait.lock
329 * otherwise we can end up freeing the completion before complete()
330 * is done referencing it.
332 spin_lock_irqsave(&x->wait.lock, flags);
333 spin_unlock_irqrestore(&x->wait.lock, flags);
336 EXPORT_SYMBOL(completion_done);