clk: samsung: Fix double add of syscore ops after driver rebind
[deliverable/linux.git] / include / linux / wait.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_WAIT_H
2#define _LINUX_WAIT_H
fb869b6e
IM
3/*
4 * Linux wait queue related types and methods
5 */
1da177e4
LT
6#include <linux/list.h>
7#include <linux/stddef.h>
8#include <linux/spinlock.h>
1da177e4 9#include <asm/current.h>
607ca46e 10#include <uapi/linux/wait.h>
1da177e4
LT
11
12typedef struct __wait_queue wait_queue_t;
7d478721
PZ
13typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
14int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
1da177e4
LT
15
16struct __wait_queue {
fb869b6e 17 unsigned int flags;
1da177e4 18#define WQ_FLAG_EXCLUSIVE 0x01
fb869b6e
IM
19 void *private;
20 wait_queue_func_t func;
21 struct list_head task_list;
1da177e4
LT
22};
23
24struct wait_bit_key {
fb869b6e
IM
25 void *flags;
26 int bit_nr;
27#define WAIT_ATOMIC_T_BIT_NR -1
cbbce822 28 unsigned long timeout;
1da177e4
LT
29};
30
31struct wait_bit_queue {
fb869b6e
IM
32 struct wait_bit_key key;
33 wait_queue_t wait;
1da177e4
LT
34};
35
36struct __wait_queue_head {
fb869b6e
IM
37 spinlock_t lock;
38 struct list_head task_list;
1da177e4
LT
39};
40typedef struct __wait_queue_head wait_queue_head_t;
41
8c65b4a6 42struct task_struct;
1da177e4
LT
43
44/*
45 * Macros for declaration and initialisaton of the datatypes
46 */
47
48#define __WAITQUEUE_INITIALIZER(name, tsk) { \
c43dc2fd 49 .private = tsk, \
1da177e4
LT
50 .func = default_wake_function, \
51 .task_list = { NULL, NULL } }
52
53#define DECLARE_WAITQUEUE(name, tsk) \
54 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
55
56#define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
e4d91918 57 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
1da177e4
LT
58 .task_list = { &(name).task_list, &(name).task_list } }
59
60#define DECLARE_WAIT_QUEUE_HEAD(name) \
61 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
62
63#define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
64 { .flags = word, .bit_nr = bit, }
65
cb65537e
DH
66#define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
67 { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
68
f07fdec5 69extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
2fc39111
PZ
70
71#define init_waitqueue_head(q) \
72 do { \
73 static struct lock_class_key __key; \
74 \
f07fdec5 75 __init_waitqueue_head((q), #q, &__key); \
2fc39111 76 } while (0)
1da177e4 77
7259f0d0
PZ
78#ifdef CONFIG_LOCKDEP
79# define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
80 ({ init_waitqueue_head(&name); name; })
81# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
82 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
83#else
84# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
85#endif
86
1da177e4
LT
87static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
88{
fb869b6e
IM
89 q->flags = 0;
90 q->private = p;
91 q->func = default_wake_function;
1da177e4
LT
92}
93
fb869b6e
IM
94static inline void
95init_waitqueue_func_entry(wait_queue_t *q, wait_queue_func_t func)
1da177e4 96{
fb869b6e
IM
97 q->flags = 0;
98 q->private = NULL;
99 q->func = func;
1da177e4
LT
100}
101
102static inline int waitqueue_active(wait_queue_head_t *q)
103{
104 return !list_empty(&q->task_list);
105}
106
b3c97528
HH
107extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
108extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
109extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
1da177e4
LT
110
111static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
112{
113 list_add(&new->task_list, &head->task_list);
114}
115
116/*
117 * Used for wake-one threads:
118 */
fb869b6e
IM
119static inline void
120__add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
a93d2f17
CG
121{
122 wait->flags |= WQ_FLAG_EXCLUSIVE;
123 __add_wait_queue(q, wait);
124}
125
1da177e4 126static inline void __add_wait_queue_tail(wait_queue_head_t *head,
a93d2f17 127 wait_queue_t *new)
1da177e4
LT
128{
129 list_add_tail(&new->task_list, &head->task_list);
130}
131
fb869b6e
IM
132static inline void
133__add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
a93d2f17
CG
134{
135 wait->flags |= WQ_FLAG_EXCLUSIVE;
136 __add_wait_queue_tail(q, wait);
137}
138
fb869b6e
IM
139static inline void
140__remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
1da177e4
LT
141{
142 list_del(&old->task_list);
143}
144
c1221321 145typedef int wait_bit_action_f(struct wait_bit_key *);
b3c97528 146void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
4ede816a 147void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
fb869b6e 148void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
63b20011 149void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
4ede816a 150void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
b3c97528 151void __wake_up_bit(wait_queue_head_t *, void *, int);
c1221321
N
152int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
153int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
b3c97528 154void wake_up_bit(void *, int);
cb65537e 155void wake_up_atomic_t(atomic_t *);
c1221321 156int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
cbbce822 157int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
c1221321 158int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
cb65537e 159int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
b3c97528 160wait_queue_head_t *bit_waitqueue(void *, int);
1da177e4 161
e64d66c8
MW
162#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
163#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
164#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
63b20011
TG
165#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
166#define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
e64d66c8 167
1da177e4
LT
168#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
169#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
170#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
e64d66c8 171#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
1da177e4 172
0ccf831c 173/*
c0da3775 174 * Wakeup macros to be used to report events to the targets.
0ccf831c 175 */
fb869b6e 176#define wake_up_poll(x, m) \
c0da3775 177 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
fb869b6e 178#define wake_up_locked_poll(x, m) \
c0da3775 179 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
fb869b6e 180#define wake_up_interruptible_poll(x, m) \
c0da3775
DL
181 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
182#define wake_up_interruptible_sync_poll(x, m) \
183 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
0ccf831c 184
35a2af94 185#define ___wait_cond_timeout(condition) \
2953ef24 186({ \
fb869b6e
IM
187 bool __cond = (condition); \
188 if (__cond && !__ret) \
189 __ret = 1; \
190 __cond || !__ret; \
2953ef24
PZ
191})
192
c2d81644
ON
193#define ___wait_is_interruptible(state) \
194 (!__builtin_constant_p(state) || \
195 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
41a1431b 196
8b32201d
PZ
197/*
198 * The below macro ___wait_event() has an explicit shadow of the __ret
199 * variable when used from the wait_event_*() macros.
200 *
201 * This is so that both can use the ___wait_cond_timeout() construct
202 * to wrap the condition.
203 *
204 * The type inconsistency of the wait_event_*() __ret variable is also
205 * on purpose; we use long where we can return timeout values and int
206 * otherwise.
207 */
208
41a1431b 209#define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
35a2af94 210({ \
41a1431b 211 __label__ __out; \
c2d81644 212 wait_queue_t __wait; \
8b32201d 213 long __ret = ret; /* explicit shadow */ \
41a1431b 214 \
c2d81644
ON
215 INIT_LIST_HEAD(&__wait.task_list); \
216 if (exclusive) \
217 __wait.flags = WQ_FLAG_EXCLUSIVE; \
218 else \
219 __wait.flags = 0; \
220 \
41a1431b 221 for (;;) { \
c2d81644 222 long __int = prepare_to_wait_event(&wq, &__wait, state);\
41a1431b
PZ
223 \
224 if (condition) \
225 break; \
226 \
c2d81644
ON
227 if (___wait_is_interruptible(state) && __int) { \
228 __ret = __int; \
41a1431b 229 if (exclusive) { \
fb869b6e
IM
230 abort_exclusive_wait(&wq, &__wait, \
231 state, NULL); \
41a1431b
PZ
232 goto __out; \
233 } \
234 break; \
235 } \
236 \
237 cmd; \
238 } \
239 finish_wait(&wq, &__wait); \
35a2af94
PZ
240__out: __ret; \
241})
41a1431b 242
fb869b6e 243#define __wait_event(wq, condition) \
35a2af94
PZ
244 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
245 schedule())
1da177e4
LT
246
247/**
248 * wait_event - sleep until a condition gets true
249 * @wq: the waitqueue to wait on
250 * @condition: a C expression for the event to wait for
251 *
252 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
253 * @condition evaluates to true. The @condition is checked each time
254 * the waitqueue @wq is woken up.
255 *
256 * wake_up() has to be called after changing any variable that could
257 * change the result of the wait condition.
258 */
fb869b6e 259#define wait_event(wq, condition) \
1da177e4 260do { \
fb869b6e 261 if (condition) \
1da177e4
LT
262 break; \
263 __wait_event(wq, condition); \
264} while (0)
265
35a2af94
PZ
266#define __wait_event_timeout(wq, condition, timeout) \
267 ___wait_event(wq, ___wait_cond_timeout(condition), \
268 TASK_UNINTERRUPTIBLE, 0, timeout, \
269 __ret = schedule_timeout(__ret))
1da177e4
LT
270
271/**
272 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
273 * @wq: the waitqueue to wait on
274 * @condition: a C expression for the event to wait for
275 * @timeout: timeout, in jiffies
276 *
277 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
278 * @condition evaluates to true. The @condition is checked each time
279 * the waitqueue @wq is woken up.
280 *
281 * wake_up() has to be called after changing any variable that could
282 * change the result of the wait condition.
283 *
6b44f519
SD
284 * Returns:
285 * 0 if the @condition evaluated to %false after the @timeout elapsed,
286 * 1 if the @condition evaluated to %true after the @timeout elapsed,
287 * or the remaining jiffies (at least 1) if the @condition evaluated
288 * to %true before the @timeout elapsed.
1da177e4
LT
289 */
290#define wait_event_timeout(wq, condition, timeout) \
291({ \
292 long __ret = timeout; \
8922915b 293 if (!___wait_cond_timeout(condition)) \
35a2af94 294 __ret = __wait_event_timeout(wq, condition, timeout); \
1da177e4
LT
295 __ret; \
296})
297
82e06c81
SL
298#define __wait_event_cmd(wq, condition, cmd1, cmd2) \
299 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
300 cmd1; schedule(); cmd2)
301
302/**
303 * wait_event_cmd - sleep until a condition gets true
304 * @wq: the waitqueue to wait on
305 * @condition: a C expression for the event to wait for
f434f7af
MI
306 * @cmd1: the command will be executed before sleep
307 * @cmd2: the command will be executed after sleep
82e06c81
SL
308 *
309 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
310 * @condition evaluates to true. The @condition is checked each time
311 * the waitqueue @wq is woken up.
312 *
313 * wake_up() has to be called after changing any variable that could
314 * change the result of the wait condition.
315 */
316#define wait_event_cmd(wq, condition, cmd1, cmd2) \
317do { \
318 if (condition) \
319 break; \
320 __wait_event_cmd(wq, condition, cmd1, cmd2); \
321} while (0)
322
35a2af94
PZ
323#define __wait_event_interruptible(wq, condition) \
324 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
f13f4c41 325 schedule())
1da177e4
LT
326
327/**
328 * wait_event_interruptible - sleep until a condition gets true
329 * @wq: the waitqueue to wait on
330 * @condition: a C expression for the event to wait for
331 *
332 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
333 * @condition evaluates to true or a signal is received.
334 * The @condition is checked each time the waitqueue @wq is woken up.
335 *
336 * wake_up() has to be called after changing any variable that could
337 * change the result of the wait condition.
338 *
339 * The function will return -ERESTARTSYS if it was interrupted by a
340 * signal and 0 if @condition evaluated to true.
341 */
342#define wait_event_interruptible(wq, condition) \
343({ \
344 int __ret = 0; \
345 if (!(condition)) \
35a2af94 346 __ret = __wait_event_interruptible(wq, condition); \
1da177e4
LT
347 __ret; \
348})
349
35a2af94
PZ
350#define __wait_event_interruptible_timeout(wq, condition, timeout) \
351 ___wait_event(wq, ___wait_cond_timeout(condition), \
352 TASK_INTERRUPTIBLE, 0, timeout, \
353 __ret = schedule_timeout(__ret))
1da177e4
LT
354
355/**
356 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
357 * @wq: the waitqueue to wait on
358 * @condition: a C expression for the event to wait for
359 * @timeout: timeout, in jiffies
360 *
361 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
362 * @condition evaluates to true or a signal is received.
363 * The @condition is checked each time the waitqueue @wq is woken up.
364 *
365 * wake_up() has to be called after changing any variable that could
366 * change the result of the wait condition.
367 *
4c663cfc 368 * Returns:
6b44f519
SD
369 * 0 if the @condition evaluated to %false after the @timeout elapsed,
370 * 1 if the @condition evaluated to %true after the @timeout elapsed,
371 * the remaining jiffies (at least 1) if the @condition evaluated
372 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
373 * interrupted by a signal.
1da177e4
LT
374 */
375#define wait_event_interruptible_timeout(wq, condition, timeout) \
376({ \
377 long __ret = timeout; \
8922915b 378 if (!___wait_cond_timeout(condition)) \
fb869b6e 379 __ret = __wait_event_interruptible_timeout(wq, \
35a2af94 380 condition, timeout); \
1da177e4
LT
381 __ret; \
382})
383
774a08b3
KO
384#define __wait_event_hrtimeout(wq, condition, timeout, state) \
385({ \
386 int __ret = 0; \
774a08b3
KO
387 struct hrtimer_sleeper __t; \
388 \
389 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
390 HRTIMER_MODE_REL); \
391 hrtimer_init_sleeper(&__t, current); \
392 if ((timeout).tv64 != KTIME_MAX) \
393 hrtimer_start_range_ns(&__t.timer, timeout, \
394 current->timer_slack_ns, \
395 HRTIMER_MODE_REL); \
396 \
35a2af94 397 __ret = ___wait_event(wq, condition, state, 0, 0, \
774a08b3
KO
398 if (!__t.task) { \
399 __ret = -ETIME; \
400 break; \
401 } \
ebdc195f 402 schedule()); \
774a08b3
KO
403 \
404 hrtimer_cancel(&__t.timer); \
405 destroy_hrtimer_on_stack(&__t.timer); \
774a08b3
KO
406 __ret; \
407})
408
409/**
410 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
411 * @wq: the waitqueue to wait on
412 * @condition: a C expression for the event to wait for
413 * @timeout: timeout, as a ktime_t
414 *
415 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
416 * @condition evaluates to true or a signal is received.
417 * The @condition is checked each time the waitqueue @wq is woken up.
418 *
419 * wake_up() has to be called after changing any variable that could
420 * change the result of the wait condition.
421 *
422 * The function returns 0 if @condition became true, or -ETIME if the timeout
423 * elapsed.
424 */
425#define wait_event_hrtimeout(wq, condition, timeout) \
426({ \
427 int __ret = 0; \
428 if (!(condition)) \
429 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
430 TASK_UNINTERRUPTIBLE); \
431 __ret; \
432})
433
434/**
435 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
436 * @wq: the waitqueue to wait on
437 * @condition: a C expression for the event to wait for
438 * @timeout: timeout, as a ktime_t
439 *
440 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
441 * @condition evaluates to true or a signal is received.
442 * The @condition is checked each time the waitqueue @wq is woken up.
443 *
444 * wake_up() has to be called after changing any variable that could
445 * change the result of the wait condition.
446 *
447 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
448 * interrupted by a signal, or -ETIME if the timeout elapsed.
449 */
450#define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
451({ \
452 long __ret = 0; \
453 if (!(condition)) \
454 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
455 TASK_INTERRUPTIBLE); \
456 __ret; \
457})
458
35a2af94
PZ
459#define __wait_event_interruptible_exclusive(wq, condition) \
460 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
48c25217 461 schedule())
1da177e4
LT
462
463#define wait_event_interruptible_exclusive(wq, condition) \
464({ \
465 int __ret = 0; \
466 if (!(condition)) \
35a2af94 467 __ret = __wait_event_interruptible_exclusive(wq, condition);\
1da177e4
LT
468 __ret; \
469})
470
22c43c81
MN
471
472#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
473({ \
474 int __ret = 0; \
475 DEFINE_WAIT(__wait); \
476 if (exclusive) \
477 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
478 do { \
479 if (likely(list_empty(&__wait.task_list))) \
480 __add_wait_queue_tail(&(wq), &__wait); \
481 set_current_state(TASK_INTERRUPTIBLE); \
482 if (signal_pending(current)) { \
483 __ret = -ERESTARTSYS; \
484 break; \
485 } \
486 if (irq) \
487 spin_unlock_irq(&(wq).lock); \
488 else \
489 spin_unlock(&(wq).lock); \
490 schedule(); \
491 if (irq) \
492 spin_lock_irq(&(wq).lock); \
493 else \
494 spin_lock(&(wq).lock); \
495 } while (!(condition)); \
496 __remove_wait_queue(&(wq), &__wait); \
497 __set_current_state(TASK_RUNNING); \
498 __ret; \
499})
500
501
502/**
503 * wait_event_interruptible_locked - sleep until a condition gets true
504 * @wq: the waitqueue to wait on
505 * @condition: a C expression for the event to wait for
506 *
507 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
508 * @condition evaluates to true or a signal is received.
509 * The @condition is checked each time the waitqueue @wq is woken up.
510 *
511 * It must be called with wq.lock being held. This spinlock is
512 * unlocked while sleeping but @condition testing is done while lock
513 * is held and when this macro exits the lock is held.
514 *
515 * The lock is locked/unlocked using spin_lock()/spin_unlock()
516 * functions which must match the way they are locked/unlocked outside
517 * of this macro.
518 *
519 * wake_up_locked() has to be called after changing any variable that could
520 * change the result of the wait condition.
521 *
522 * The function will return -ERESTARTSYS if it was interrupted by a
523 * signal and 0 if @condition evaluated to true.
524 */
525#define wait_event_interruptible_locked(wq, condition) \
526 ((condition) \
527 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
528
529/**
530 * wait_event_interruptible_locked_irq - sleep until a condition gets true
531 * @wq: the waitqueue to wait on
532 * @condition: a C expression for the event to wait for
533 *
534 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
535 * @condition evaluates to true or a signal is received.
536 * The @condition is checked each time the waitqueue @wq is woken up.
537 *
538 * It must be called with wq.lock being held. This spinlock is
539 * unlocked while sleeping but @condition testing is done while lock
540 * is held and when this macro exits the lock is held.
541 *
542 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
543 * functions which must match the way they are locked/unlocked outside
544 * of this macro.
545 *
546 * wake_up_locked() has to be called after changing any variable that could
547 * change the result of the wait condition.
548 *
549 * The function will return -ERESTARTSYS if it was interrupted by a
550 * signal and 0 if @condition evaluated to true.
551 */
552#define wait_event_interruptible_locked_irq(wq, condition) \
553 ((condition) \
554 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
555
556/**
557 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
558 * @wq: the waitqueue to wait on
559 * @condition: a C expression for the event to wait for
560 *
561 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
562 * @condition evaluates to true or a signal is received.
563 * The @condition is checked each time the waitqueue @wq is woken up.
564 *
565 * It must be called with wq.lock being held. This spinlock is
566 * unlocked while sleeping but @condition testing is done while lock
567 * is held and when this macro exits the lock is held.
568 *
569 * The lock is locked/unlocked using spin_lock()/spin_unlock()
570 * functions which must match the way they are locked/unlocked outside
571 * of this macro.
572 *
573 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
574 * set thus when other process waits process on the list if this
575 * process is awaken further processes are not considered.
576 *
577 * wake_up_locked() has to be called after changing any variable that could
578 * change the result of the wait condition.
579 *
580 * The function will return -ERESTARTSYS if it was interrupted by a
581 * signal and 0 if @condition evaluated to true.
582 */
583#define wait_event_interruptible_exclusive_locked(wq, condition) \
584 ((condition) \
585 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
586
587/**
588 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
589 * @wq: the waitqueue to wait on
590 * @condition: a C expression for the event to wait for
591 *
592 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
593 * @condition evaluates to true or a signal is received.
594 * The @condition is checked each time the waitqueue @wq is woken up.
595 *
596 * It must be called with wq.lock being held. This spinlock is
597 * unlocked while sleeping but @condition testing is done while lock
598 * is held and when this macro exits the lock is held.
599 *
600 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
601 * functions which must match the way they are locked/unlocked outside
602 * of this macro.
603 *
604 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
605 * set thus when other process waits process on the list if this
606 * process is awaken further processes are not considered.
607 *
608 * wake_up_locked() has to be called after changing any variable that could
609 * change the result of the wait condition.
610 *
611 * The function will return -ERESTARTSYS if it was interrupted by a
612 * signal and 0 if @condition evaluated to true.
613 */
614#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
615 ((condition) \
616 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
617
618
35a2af94
PZ
619#define __wait_event_killable(wq, condition) \
620 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
1411d5a7
MW
621
622/**
623 * wait_event_killable - sleep until a condition gets true
624 * @wq: the waitqueue to wait on
625 * @condition: a C expression for the event to wait for
626 *
627 * The process is put to sleep (TASK_KILLABLE) until the
628 * @condition evaluates to true or a signal is received.
629 * The @condition is checked each time the waitqueue @wq is woken up.
630 *
631 * wake_up() has to be called after changing any variable that could
632 * change the result of the wait condition.
633 *
634 * The function will return -ERESTARTSYS if it was interrupted by a
635 * signal and 0 if @condition evaluated to true.
636 */
637#define wait_event_killable(wq, condition) \
638({ \
639 int __ret = 0; \
640 if (!(condition)) \
35a2af94 641 __ret = __wait_event_killable(wq, condition); \
1411d5a7
MW
642 __ret; \
643})
644
eed8c02e
LC
645
646#define __wait_event_lock_irq(wq, condition, lock, cmd) \
35a2af94
PZ
647 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
648 spin_unlock_irq(&lock); \
649 cmd; \
650 schedule(); \
651 spin_lock_irq(&lock))
eed8c02e
LC
652
653/**
654 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
655 * condition is checked under the lock. This
656 * is expected to be called with the lock
657 * taken.
658 * @wq: the waitqueue to wait on
659 * @condition: a C expression for the event to wait for
660 * @lock: a locked spinlock_t, which will be released before cmd
661 * and schedule() and reacquired afterwards.
662 * @cmd: a command which is invoked outside the critical section before
663 * sleep
664 *
665 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
666 * @condition evaluates to true. The @condition is checked each time
667 * the waitqueue @wq is woken up.
668 *
669 * wake_up() has to be called after changing any variable that could
670 * change the result of the wait condition.
671 *
672 * This is supposed to be called while holding the lock. The lock is
673 * dropped before invoking the cmd and going to sleep and is reacquired
674 * afterwards.
675 */
676#define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
677do { \
678 if (condition) \
679 break; \
680 __wait_event_lock_irq(wq, condition, lock, cmd); \
681} while (0)
682
683/**
684 * wait_event_lock_irq - sleep until a condition gets true. The
685 * condition is checked under the lock. This
686 * is expected to be called with the lock
687 * taken.
688 * @wq: the waitqueue to wait on
689 * @condition: a C expression for the event to wait for
690 * @lock: a locked spinlock_t, which will be released before schedule()
691 * and reacquired afterwards.
692 *
693 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
694 * @condition evaluates to true. The @condition is checked each time
695 * the waitqueue @wq is woken up.
696 *
697 * wake_up() has to be called after changing any variable that could
698 * change the result of the wait condition.
699 *
700 * This is supposed to be called while holding the lock. The lock is
701 * dropped before going to sleep and is reacquired afterwards.
702 */
703#define wait_event_lock_irq(wq, condition, lock) \
704do { \
705 if (condition) \
706 break; \
707 __wait_event_lock_irq(wq, condition, lock, ); \
708} while (0)
709
710
35a2af94 711#define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
fb869b6e 712 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
35a2af94
PZ
713 spin_unlock_irq(&lock); \
714 cmd; \
715 schedule(); \
8fbd88fa 716 spin_lock_irq(&lock))
eed8c02e
LC
717
718/**
719 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
720 * The condition is checked under the lock. This is expected to
721 * be called with the lock taken.
722 * @wq: the waitqueue to wait on
723 * @condition: a C expression for the event to wait for
724 * @lock: a locked spinlock_t, which will be released before cmd and
725 * schedule() and reacquired afterwards.
726 * @cmd: a command which is invoked outside the critical section before
727 * sleep
728 *
729 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
730 * @condition evaluates to true or a signal is received. The @condition is
731 * checked each time the waitqueue @wq is woken up.
732 *
733 * wake_up() has to be called after changing any variable that could
734 * change the result of the wait condition.
735 *
736 * This is supposed to be called while holding the lock. The lock is
737 * dropped before invoking the cmd and going to sleep and is reacquired
738 * afterwards.
739 *
740 * The macro will return -ERESTARTSYS if it was interrupted by a signal
741 * and 0 if @condition evaluated to true.
742 */
743#define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
744({ \
745 int __ret = 0; \
eed8c02e 746 if (!(condition)) \
fb869b6e 747 __ret = __wait_event_interruptible_lock_irq(wq, \
35a2af94 748 condition, lock, cmd); \
eed8c02e
LC
749 __ret; \
750})
751
752/**
753 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
754 * The condition is checked under the lock. This is expected
755 * to be called with the lock taken.
756 * @wq: the waitqueue to wait on
757 * @condition: a C expression for the event to wait for
758 * @lock: a locked spinlock_t, which will be released before schedule()
759 * and reacquired afterwards.
760 *
761 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
762 * @condition evaluates to true or signal is received. The @condition is
763 * checked each time the waitqueue @wq is woken up.
764 *
765 * wake_up() has to be called after changing any variable that could
766 * change the result of the wait condition.
767 *
768 * This is supposed to be called while holding the lock. The lock is
769 * dropped before going to sleep and is reacquired afterwards.
770 *
771 * The macro will return -ERESTARTSYS if it was interrupted by a signal
772 * and 0 if @condition evaluated to true.
773 */
774#define wait_event_interruptible_lock_irq(wq, condition, lock) \
775({ \
776 int __ret = 0; \
eed8c02e 777 if (!(condition)) \
35a2af94 778 __ret = __wait_event_interruptible_lock_irq(wq, \
92ec1180 779 condition, lock,); \
eed8c02e
LC
780 __ret; \
781})
782
fb869b6e
IM
783#define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
784 lock, timeout) \
35a2af94 785 ___wait_event(wq, ___wait_cond_timeout(condition), \
7d716456 786 TASK_INTERRUPTIBLE, 0, timeout, \
35a2af94
PZ
787 spin_unlock_irq(&lock); \
788 __ret = schedule_timeout(__ret); \
a1dc6852 789 spin_lock_irq(&lock));
d79ff142
MP
790
791/**
fb869b6e
IM
792 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
793 * true or a timeout elapses. The condition is checked under
794 * the lock. This is expected to be called with the lock taken.
d79ff142
MP
795 * @wq: the waitqueue to wait on
796 * @condition: a C expression for the event to wait for
797 * @lock: a locked spinlock_t, which will be released before schedule()
798 * and reacquired afterwards.
799 * @timeout: timeout, in jiffies
800 *
801 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
802 * @condition evaluates to true or signal is received. The @condition is
803 * checked each time the waitqueue @wq is woken up.
804 *
805 * wake_up() has to be called after changing any variable that could
806 * change the result of the wait condition.
807 *
808 * This is supposed to be called while holding the lock. The lock is
809 * dropped before going to sleep and is reacquired afterwards.
810 *
811 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
812 * was interrupted by a signal, and the remaining jiffies otherwise
813 * if the condition evaluated to true before the timeout elapsed.
814 */
815#define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
816 timeout) \
817({ \
35a2af94 818 long __ret = timeout; \
8922915b 819 if (!___wait_cond_timeout(condition)) \
35a2af94
PZ
820 __ret = __wait_event_interruptible_lock_irq_timeout( \
821 wq, condition, lock, timeout); \
d79ff142
MP
822 __ret; \
823})
824
1da177e4
LT
825/*
826 * Waitqueues which are removed from the waitqueue_head at wakeup time
827 */
b3c97528
HH
828void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
829void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
c2d81644 830long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
b3c97528 831void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
fb869b6e 832void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
1da177e4
LT
833int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
834int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
835
bf368e4e 836#define DEFINE_WAIT_FUNC(name, function) \
1da177e4 837 wait_queue_t name = { \
c43dc2fd 838 .private = current, \
bf368e4e 839 .func = function, \
7e43c84e 840 .task_list = LIST_HEAD_INIT((name).task_list), \
1da177e4
LT
841 }
842
bf368e4e
ED
843#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
844
1da177e4
LT
845#define DEFINE_WAIT_BIT(name, word, bit) \
846 struct wait_bit_queue name = { \
847 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
848 .wait = { \
c43dc2fd 849 .private = current, \
1da177e4
LT
850 .func = wake_bit_function, \
851 .task_list = \
852 LIST_HEAD_INIT((name).wait.task_list), \
853 }, \
854 }
855
856#define init_wait(wait) \
857 do { \
c43dc2fd 858 (wait)->private = current; \
1da177e4
LT
859 (wait)->func = autoremove_wake_function; \
860 INIT_LIST_HEAD(&(wait)->task_list); \
231d0aef 861 (wait)->flags = 0; \
1da177e4
LT
862 } while (0)
863
74316201 864
c1221321
N
865extern int bit_wait(struct wait_bit_key *);
866extern int bit_wait_io(struct wait_bit_key *);
cbbce822
N
867extern int bit_wait_timeout(struct wait_bit_key *);
868extern int bit_wait_io_timeout(struct wait_bit_key *);
74316201 869
1da177e4
LT
870/**
871 * wait_on_bit - wait for a bit to be cleared
872 * @word: the word being waited on, a kernel virtual address
873 * @bit: the bit of the word being waited on
1da177e4
LT
874 * @mode: the task state to sleep in
875 *
876 * There is a standard hashed waitqueue table for generic use. This
877 * is the part of the hashtable's accessor API that waits on a bit.
878 * For instance, if one were to have waiters on a bitflag, one would
879 * call wait_on_bit() in threads waiting for the bit to clear.
880 * One uses wait_on_bit() where one is waiting for the bit to clear,
881 * but has no intention of setting it.
74316201
N
882 * Returned value will be zero if the bit was cleared, or non-zero
883 * if the process received a signal and the mode permitted wakeup
884 * on that signal.
885 */
886static inline int
887wait_on_bit(void *word, int bit, unsigned mode)
888{
889 if (!test_bit(bit, word))
890 return 0;
891 return out_of_line_wait_on_bit(word, bit,
892 bit_wait,
893 mode);
894}
895
896/**
897 * wait_on_bit_io - wait for a bit to be cleared
898 * @word: the word being waited on, a kernel virtual address
899 * @bit: the bit of the word being waited on
900 * @mode: the task state to sleep in
901 *
902 * Use the standard hashed waitqueue table to wait for a bit
903 * to be cleared. This is similar to wait_on_bit(), but calls
904 * io_schedule() instead of schedule() for the actual waiting.
905 *
906 * Returned value will be zero if the bit was cleared, or non-zero
907 * if the process received a signal and the mode permitted wakeup
908 * on that signal.
909 */
910static inline int
911wait_on_bit_io(void *word, int bit, unsigned mode)
912{
913 if (!test_bit(bit, word))
914 return 0;
915 return out_of_line_wait_on_bit(word, bit,
916 bit_wait_io,
917 mode);
918}
919
920/**
921 * wait_on_bit_action - wait for a bit to be cleared
922 * @word: the word being waited on, a kernel virtual address
923 * @bit: the bit of the word being waited on
924 * @action: the function used to sleep, which may take special actions
925 * @mode: the task state to sleep in
926 *
927 * Use the standard hashed waitqueue table to wait for a bit
928 * to be cleared, and allow the waiting action to be specified.
929 * This is like wait_on_bit() but allows fine control of how the waiting
930 * is done.
931 *
932 * Returned value will be zero if the bit was cleared, or non-zero
933 * if the process received a signal and the mode permitted wakeup
934 * on that signal.
1da177e4 935 */
fb869b6e 936static inline int
c1221321 937wait_on_bit_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
1da177e4
LT
938{
939 if (!test_bit(bit, word))
940 return 0;
941 return out_of_line_wait_on_bit(word, bit, action, mode);
942}
943
944/**
945 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
946 * @word: the word being waited on, a kernel virtual address
947 * @bit: the bit of the word being waited on
1da177e4
LT
948 * @mode: the task state to sleep in
949 *
950 * There is a standard hashed waitqueue table for generic use. This
951 * is the part of the hashtable's accessor API that waits on a bit
952 * when one intends to set it, for instance, trying to lock bitflags.
953 * For instance, if one were to have waiters trying to set bitflag
954 * and waiting for it to clear before setting it, one would call
955 * wait_on_bit() in threads waiting to be able to set the bit.
956 * One uses wait_on_bit_lock() where one is waiting for the bit to
957 * clear with the intention of setting it, and when done, clearing it.
74316201
N
958 *
959 * Returns zero if the bit was (eventually) found to be clear and was
960 * set. Returns non-zero if a signal was delivered to the process and
961 * the @mode allows that signal to wake the process.
962 */
963static inline int
964wait_on_bit_lock(void *word, int bit, unsigned mode)
965{
966 if (!test_and_set_bit(bit, word))
967 return 0;
968 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
969}
970
971/**
972 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
973 * @word: the word being waited on, a kernel virtual address
974 * @bit: the bit of the word being waited on
975 * @mode: the task state to sleep in
976 *
977 * Use the standard hashed waitqueue table to wait for a bit
978 * to be cleared and then to atomically set it. This is similar
979 * to wait_on_bit(), but calls io_schedule() instead of schedule()
980 * for the actual waiting.
981 *
982 * Returns zero if the bit was (eventually) found to be clear and was
983 * set. Returns non-zero if a signal was delivered to the process and
984 * the @mode allows that signal to wake the process.
985 */
986static inline int
987wait_on_bit_lock_io(void *word, int bit, unsigned mode)
988{
989 if (!test_and_set_bit(bit, word))
990 return 0;
991 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
992}
993
994/**
995 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
996 * @word: the word being waited on, a kernel virtual address
997 * @bit: the bit of the word being waited on
998 * @action: the function used to sleep, which may take special actions
999 * @mode: the task state to sleep in
1000 *
1001 * Use the standard hashed waitqueue table to wait for a bit
1002 * to be cleared and then to set it, and allow the waiting action
1003 * to be specified.
1004 * This is like wait_on_bit() but allows fine control of how the waiting
1005 * is done.
1006 *
1007 * Returns zero if the bit was (eventually) found to be clear and was
1008 * set. Returns non-zero if a signal was delivered to the process and
1009 * the @mode allows that signal to wake the process.
1da177e4 1010 */
fb869b6e 1011static inline int
c1221321 1012wait_on_bit_lock_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
1da177e4
LT
1013{
1014 if (!test_and_set_bit(bit, word))
1015 return 0;
1016 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1017}
cb65537e
DH
1018
1019/**
1020 * wait_on_atomic_t - Wait for an atomic_t to become 0
1021 * @val: The atomic value being waited on, a kernel virtual address
1022 * @action: the function used to sleep, which may take special actions
1023 * @mode: the task state to sleep in
1024 *
1025 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1026 * the purpose of getting a waitqueue, but we set the key to a bit number
1027 * outside of the target 'word'.
1028 */
1029static inline
1030int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1031{
1032 if (atomic_read(val) == 0)
1033 return 0;
1034 return out_of_line_wait_on_atomic_t(val, action, mode);
1035}
fb869b6e
IM
1036
1037#endif /* _LINUX_WAIT_H */
This page took 1.190833 seconds and 5 git commands to generate.