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/completion.h>
18 * complete: - signals a single thread waiting on this completion
19 * @x: holds the state of this particular completion
21 * This will wake up a single thread waiting on this completion. Threads will be
22 * awakened in the same order in which they were queued.
24 * See also complete_all(), wait_for_completion() and related routines.
26 * It may be assumed that this function implies a write memory barrier before
27 * changing the task state if and only if any tasks are woken up.
29 void complete(struct completion
*x
)
33 spin_lock_irqsave(&x
->wait
.lock
, flags
);
34 if (x
->done
!= UINT_MAX
)
36 __wake_up_locked(&x
->wait
, TASK_NORMAL
, 1);
37 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
39 EXPORT_SYMBOL(complete
);
42 * complete_all: - signals all threads waiting on this completion
43 * @x: holds the state of this particular completion
45 * This will wake up all threads waiting on this particular completion event.
47 * It may be assumed that this function implies a write memory barrier before
48 * changing the task state if and only if any tasks are woken up.
50 void complete_all(struct completion
*x
)
54 spin_lock_irqsave(&x
->wait
.lock
, flags
);
56 __wake_up_locked(&x
->wait
, TASK_NORMAL
, 0);
57 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
59 EXPORT_SYMBOL(complete_all
);
61 static inline long __sched
62 do_wait_for_common(struct completion
*x
,
63 long (*action
)(long), long timeout
, int state
)
66 DECLARE_WAITQUEUE(wait
, current
);
68 __add_wait_queue_tail_exclusive(&x
->wait
, &wait
);
70 if (signal_pending_state(state
, current
)) {
71 timeout
= -ERESTARTSYS
;
74 __set_current_state(state
);
75 spin_unlock_irq(&x
->wait
.lock
);
76 timeout
= action(timeout
);
77 spin_lock_irq(&x
->wait
.lock
);
78 } while (!x
->done
&& timeout
);
79 __remove_wait_queue(&x
->wait
, &wait
);
83 if (x
->done
!= UINT_MAX
)
88 static inline long __sched
89 __wait_for_common(struct completion
*x
,
90 long (*action
)(long), long timeout
, int state
)
94 spin_lock_irq(&x
->wait
.lock
);
95 timeout
= do_wait_for_common(x
, action
, timeout
, state
);
96 spin_unlock_irq(&x
->wait
.lock
);
101 wait_for_common(struct completion
*x
, long timeout
, int state
)
103 return __wait_for_common(x
, schedule_timeout
, timeout
, state
);
107 wait_for_common_io(struct completion
*x
, long timeout
, int state
)
109 return __wait_for_common(x
, io_schedule_timeout
, timeout
, state
);
113 * wait_for_completion: - waits for completion of a task
114 * @x: holds the state of this particular completion
116 * This waits to be signaled for completion of a specific task. It is NOT
117 * interruptible and there is no timeout.
119 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
120 * and interrupt capability. Also see complete().
122 void __sched
wait_for_completion(struct completion
*x
)
124 wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_UNINTERRUPTIBLE
);
126 EXPORT_SYMBOL(wait_for_completion
);
129 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
130 * @x: holds the state of this particular completion
131 * @timeout: timeout value in jiffies
133 * This waits for either a completion of a specific task to be signaled or for a
134 * specified timeout to expire. The timeout is in jiffies. It is not
137 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
138 * till timeout) if completed.
140 unsigned long __sched
141 wait_for_completion_timeout(struct completion
*x
, unsigned long timeout
)
143 return wait_for_common(x
, timeout
, TASK_UNINTERRUPTIBLE
);
145 EXPORT_SYMBOL(wait_for_completion_timeout
);
148 * wait_for_completion_io: - waits for completion of a task
149 * @x: holds the state of this particular completion
151 * This waits to be signaled for completion of a specific task. It is NOT
152 * interruptible and there is no timeout. The caller is accounted as waiting
153 * for IO (which traditionally means blkio only).
155 void __sched
wait_for_completion_io(struct completion
*x
)
157 wait_for_common_io(x
, MAX_SCHEDULE_TIMEOUT
, TASK_UNINTERRUPTIBLE
);
159 EXPORT_SYMBOL(wait_for_completion_io
);
162 * wait_for_completion_io_timeout: - waits for completion of a task (w/timeout)
163 * @x: holds the state of this particular completion
164 * @timeout: timeout value in jiffies
166 * This waits for either a completion of a specific task to be signaled or for a
167 * specified timeout to expire. The timeout is in jiffies. It is not
168 * interruptible. The caller is accounted as waiting for IO (which traditionally
171 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
172 * till timeout) if completed.
174 unsigned long __sched
175 wait_for_completion_io_timeout(struct completion
*x
, unsigned long timeout
)
177 return wait_for_common_io(x
, timeout
, TASK_UNINTERRUPTIBLE
);
179 EXPORT_SYMBOL(wait_for_completion_io_timeout
);
182 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
183 * @x: holds the state of this particular completion
185 * This waits for completion of a specific task to be signaled. It is
188 * Return: -ERESTARTSYS if interrupted, 0 if completed.
190 int __sched
wait_for_completion_interruptible(struct completion
*x
)
192 long t
= wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_INTERRUPTIBLE
);
193 if (t
== -ERESTARTSYS
)
197 EXPORT_SYMBOL(wait_for_completion_interruptible
);
200 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
201 * @x: holds the state of this particular completion
202 * @timeout: timeout value in jiffies
204 * This waits for either a completion of a specific task to be signaled or for a
205 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
207 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
208 * or number of jiffies left till timeout) if completed.
211 wait_for_completion_interruptible_timeout(struct completion
*x
,
212 unsigned long timeout
)
214 return wait_for_common(x
, timeout
, TASK_INTERRUPTIBLE
);
216 EXPORT_SYMBOL(wait_for_completion_interruptible_timeout
);
219 * wait_for_completion_killable: - waits for completion of a task (killable)
220 * @x: holds the state of this particular completion
222 * This waits to be signaled for completion of a specific task. It can be
223 * interrupted by a kill signal.
225 * Return: -ERESTARTSYS if interrupted, 0 if completed.
227 int __sched
wait_for_completion_killable(struct completion
*x
)
229 long t
= wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_KILLABLE
);
230 if (t
== -ERESTARTSYS
)
234 EXPORT_SYMBOL(wait_for_completion_killable
);
237 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
238 * @x: holds the state of this particular completion
239 * @timeout: timeout value in jiffies
241 * This waits for either a completion of a specific task to be
242 * signaled or for a specified timeout to expire. It can be
243 * interrupted by a kill signal. The timeout is in jiffies.
245 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
246 * or number of jiffies left till timeout) if completed.
249 wait_for_completion_killable_timeout(struct completion
*x
,
250 unsigned long timeout
)
252 return wait_for_common(x
, timeout
, TASK_KILLABLE
);
254 EXPORT_SYMBOL(wait_for_completion_killable_timeout
);
257 * try_wait_for_completion - try to decrement a completion without blocking
258 * @x: completion structure
260 * Return: 0 if a decrement cannot be done without blocking
261 * 1 if a decrement succeeded.
263 * If a completion is being used as a counting completion,
264 * attempt to decrement the counter without blocking. This
265 * enables us to avoid waiting if the resource the completion
266 * is protecting is not available.
268 bool try_wait_for_completion(struct completion
*x
)
274 * Since x->done will need to be locked only
275 * in the non-blocking case, we check x->done
276 * first without taking the lock so we can
277 * return early in the blocking case.
279 if (!READ_ONCE(x
->done
))
282 spin_lock_irqsave(&x
->wait
.lock
, flags
);
285 else if (x
->done
!= UINT_MAX
)
287 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
290 EXPORT_SYMBOL(try_wait_for_completion
);
293 * completion_done - Test to see if a completion has any waiters
294 * @x: completion structure
296 * Return: 0 if there are waiters (wait_for_completion() in progress)
297 * 1 if there are no waiters.
300 bool completion_done(struct completion
*x
)
302 if (!READ_ONCE(x
->done
))
306 * If ->done, we need to wait for complete() to release ->wait.lock
307 * otherwise we can end up freeing the completion before complete()
308 * is done referencing it.
310 * The RMB pairs with complete()'s RELEASE of ->wait.lock and orders
311 * the loads of ->done and ->wait.lock such that we cannot observe
312 * the lock before complete() acquires it while observing the ->done
313 * after it's acquired the lock.
316 spin_unlock_wait(&x
->wait
.lock
);
319 EXPORT_SYMBOL(completion_done
);