scsi: ufs: Use the resource-managed function to add devfreq device
[linux/fpc-iii.git] / kernel / sched / completion.c
blob8d0f35debf35657689908a4b37df7230ba7d6710
1 /*
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.h>
15 #include <linux/completion.h>
17 /**
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)
31 unsigned long flags;
33 spin_lock_irqsave(&x->wait.lock, flags);
34 x->done++;
35 __wake_up_locked(&x->wait, TASK_NORMAL, 1);
36 spin_unlock_irqrestore(&x->wait.lock, flags);
38 EXPORT_SYMBOL(complete);
40 /**
41 * complete_all: - signals all threads waiting on this completion
42 * @x: holds the state of this particular completion
44 * This will wake up all threads waiting on this particular completion event.
46 * It may be assumed that this function implies a write memory barrier before
47 * changing the task state if and only if any tasks are woken up.
49 void complete_all(struct completion *x)
51 unsigned long flags;
53 spin_lock_irqsave(&x->wait.lock, flags);
54 x->done += UINT_MAX/2;
55 __wake_up_locked(&x->wait, TASK_NORMAL, 0);
56 spin_unlock_irqrestore(&x->wait.lock, flags);
58 EXPORT_SYMBOL(complete_all);
60 static inline long __sched
61 do_wait_for_common(struct completion *x,
62 long (*action)(long), long timeout, int state)
64 if (!x->done) {
65 DECLARE_WAITQUEUE(wait, current);
67 __add_wait_queue_tail_exclusive(&x->wait, &wait);
68 do {
69 if (signal_pending_state(state, current)) {
70 timeout = -ERESTARTSYS;
71 break;
73 __set_current_state(state);
74 spin_unlock_irq(&x->wait.lock);
75 timeout = action(timeout);
76 spin_lock_irq(&x->wait.lock);
77 } while (!x->done && timeout);
78 __remove_wait_queue(&x->wait, &wait);
79 if (!x->done)
80 return timeout;
82 x->done--;
83 return timeout ?: 1;
86 static inline long __sched
87 __wait_for_common(struct completion *x,
88 long (*action)(long), long timeout, int state)
90 might_sleep();
92 spin_lock_irq(&x->wait.lock);
93 timeout = do_wait_for_common(x, action, timeout, state);
94 spin_unlock_irq(&x->wait.lock);
95 return timeout;
98 static long __sched
99 wait_for_common(struct completion *x, long timeout, int state)
101 return __wait_for_common(x, schedule_timeout, timeout, state);
104 static long __sched
105 wait_for_common_io(struct completion *x, long timeout, int state)
107 return __wait_for_common(x, io_schedule_timeout, timeout, state);
111 * wait_for_completion: - waits for completion of a task
112 * @x: holds the state of this particular completion
114 * This waits to be signaled for completion of a specific task. It is NOT
115 * interruptible and there is no timeout.
117 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
118 * and interrupt capability. Also see complete().
120 void __sched wait_for_completion(struct completion *x)
122 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
124 EXPORT_SYMBOL(wait_for_completion);
127 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
128 * @x: holds the state of this particular completion
129 * @timeout: timeout value in jiffies
131 * This waits for either a completion of a specific task to be signaled or for a
132 * specified timeout to expire. The timeout is in jiffies. It is not
133 * interruptible.
135 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
136 * till timeout) if completed.
138 unsigned long __sched
139 wait_for_completion_timeout(struct completion *x, unsigned long timeout)
141 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
143 EXPORT_SYMBOL(wait_for_completion_timeout);
146 * wait_for_completion_io: - waits for completion of a task
147 * @x: holds the state of this particular completion
149 * This waits to be signaled for completion of a specific task. It is NOT
150 * interruptible and there is no timeout. The caller is accounted as waiting
151 * for IO (which traditionally means blkio only).
153 void __sched wait_for_completion_io(struct completion *x)
155 wait_for_common_io(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
157 EXPORT_SYMBOL(wait_for_completion_io);
160 * wait_for_completion_io_timeout: - waits for completion of a task (w/timeout)
161 * @x: holds the state of this particular completion
162 * @timeout: timeout value in jiffies
164 * This waits for either a completion of a specific task to be signaled or for a
165 * specified timeout to expire. The timeout is in jiffies. It is not
166 * interruptible. The caller is accounted as waiting for IO (which traditionally
167 * means blkio only).
169 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
170 * till timeout) if completed.
172 unsigned long __sched
173 wait_for_completion_io_timeout(struct completion *x, unsigned long timeout)
175 return wait_for_common_io(x, timeout, TASK_UNINTERRUPTIBLE);
177 EXPORT_SYMBOL(wait_for_completion_io_timeout);
180 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
181 * @x: holds the state of this particular completion
183 * This waits for completion of a specific task to be signaled. It is
184 * interruptible.
186 * Return: -ERESTARTSYS if interrupted, 0 if completed.
188 int __sched wait_for_completion_interruptible(struct completion *x)
190 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
191 if (t == -ERESTARTSYS)
192 return t;
193 return 0;
195 EXPORT_SYMBOL(wait_for_completion_interruptible);
198 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
199 * @x: holds the state of this particular completion
200 * @timeout: timeout value in jiffies
202 * This waits for either a completion of a specific task to be signaled or for a
203 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
205 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
206 * or number of jiffies left till timeout) if completed.
208 long __sched
209 wait_for_completion_interruptible_timeout(struct completion *x,
210 unsigned long timeout)
212 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
214 EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
217 * wait_for_completion_killable: - waits for completion of a task (killable)
218 * @x: holds the state of this particular completion
220 * This waits to be signaled for completion of a specific task. It can be
221 * interrupted by a kill signal.
223 * Return: -ERESTARTSYS if interrupted, 0 if completed.
225 int __sched wait_for_completion_killable(struct completion *x)
227 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
228 if (t == -ERESTARTSYS)
229 return t;
230 return 0;
232 EXPORT_SYMBOL(wait_for_completion_killable);
235 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
236 * @x: holds the state of this particular completion
237 * @timeout: timeout value in jiffies
239 * This waits for either a completion of a specific task to be
240 * signaled or for a specified timeout to expire. It can be
241 * interrupted by a kill signal. The timeout is in jiffies.
243 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
244 * or number of jiffies left till timeout) if completed.
246 long __sched
247 wait_for_completion_killable_timeout(struct completion *x,
248 unsigned long timeout)
250 return wait_for_common(x, timeout, TASK_KILLABLE);
252 EXPORT_SYMBOL(wait_for_completion_killable_timeout);
255 * try_wait_for_completion - try to decrement a completion without blocking
256 * @x: completion structure
258 * Return: 0 if a decrement cannot be done without blocking
259 * 1 if a decrement succeeded.
261 * If a completion is being used as a counting completion,
262 * attempt to decrement the counter without blocking. This
263 * enables us to avoid waiting if the resource the completion
264 * is protecting is not available.
266 bool try_wait_for_completion(struct completion *x)
268 unsigned long flags;
269 int ret = 1;
272 * Since x->done will need to be locked only
273 * in the non-blocking case, we check x->done
274 * first without taking the lock so we can
275 * return early in the blocking case.
277 if (!READ_ONCE(x->done))
278 return 0;
280 spin_lock_irqsave(&x->wait.lock, flags);
281 if (!x->done)
282 ret = 0;
283 else
284 x->done--;
285 spin_unlock_irqrestore(&x->wait.lock, flags);
286 return ret;
288 EXPORT_SYMBOL(try_wait_for_completion);
291 * completion_done - Test to see if a completion has any waiters
292 * @x: completion structure
294 * Return: 0 if there are waiters (wait_for_completion() in progress)
295 * 1 if there are no waiters.
298 bool completion_done(struct completion *x)
300 if (!READ_ONCE(x->done))
301 return false;
304 * If ->done, we need to wait for complete() to release ->wait.lock
305 * otherwise we can end up freeing the completion before complete()
306 * is done referencing it.
308 * The RMB pairs with complete()'s RELEASE of ->wait.lock and orders
309 * the loads of ->done and ->wait.lock such that we cannot observe
310 * the lock before complete() acquires it while observing the ->done
311 * after it's acquired the lock.
313 smp_rmb();
314 spin_unlock_wait(&x->wait.lock);
315 return true;
317 EXPORT_SYMBOL(completion_done);