Blame view

include/linux/wait.h 41.2 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
  #ifndef _LINUX_WAIT_H
  #define _LINUX_WAIT_H
fb869b6e9   Ingo Molnar   sched/wait: Clean...
3
4
5
  /*
   * Linux wait queue related types and methods
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
8
  #include <linux/list.h>
  #include <linux/stddef.h>
  #include <linux/spinlock.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
  #include <asm/current.h>
607ca46e9   David Howells   UAPI: (Scripted) ...
10
  #include <uapi/linux/wait.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
  
  typedef struct __wait_queue wait_queue_t;
7d4787214   Peter Zijlstra   sched: Rename syn...
13
14
  typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15

61ada528d   Peter Zijlstra   sched/wait: Provi...
16
17
18
  /* __wait_queue::flags */
  #define WQ_FLAG_EXCLUSIVE	0x01
  #define WQ_FLAG_WOKEN		0x02
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  struct __wait_queue {
fb869b6e9   Ingo Molnar   sched/wait: Clean...
20
  	unsigned int		flags;
fb869b6e9   Ingo Molnar   sched/wait: Clean...
21
22
23
  	void			*private;
  	wait_queue_func_t	func;
  	struct list_head	task_list;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
26
  };
  
  struct wait_bit_key {
fb869b6e9   Ingo Molnar   sched/wait: Clean...
27
28
29
  	void			*flags;
  	int			bit_nr;
  #define WAIT_ATOMIC_T_BIT_NR	-1
cbbce8220   NeilBrown   SCHED: add some "...
30
  	unsigned long		timeout;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
  };
  
  struct wait_bit_queue {
fb869b6e9   Ingo Molnar   sched/wait: Clean...
34
35
  	struct wait_bit_key	key;
  	wait_queue_t		wait;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
38
  };
  
  struct __wait_queue_head {
fb869b6e9   Ingo Molnar   sched/wait: Clean...
39
40
  	spinlock_t		lock;
  	struct list_head	task_list;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
  };
  typedef struct __wait_queue_head wait_queue_head_t;
8c65b4a60   Tim Schmielau   [PATCH] fix remai...
43
  struct task_struct;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
46
47
48
49
  
  /*
   * Macros for declaration and initialisaton of the datatypes
   */
  
  #define __WAITQUEUE_INITIALIZER(name, tsk) {				\
c43dc2fd8   Benjamin LaHaise   [PATCH] aio: make...
50
  	.private	= tsk,						\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
54
55
56
57
  	.func		= default_wake_function,			\
  	.task_list	= { NULL, NULL } }
  
  #define DECLARE_WAITQUEUE(name, tsk)					\
  	wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
  
  #define __WAIT_QUEUE_HEAD_INITIALIZER(name) {				\
e4d919188   Ingo Molnar   [PATCH] lockdep: ...
58
  	.lock		= __SPIN_LOCK_UNLOCKED(name.lock),		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
61
62
63
64
65
  	.task_list	= { &(name).task_list, &(name).task_list } }
  
  #define DECLARE_WAIT_QUEUE_HEAD(name) \
  	wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
  
  #define __WAIT_BIT_KEY_INITIALIZER(word, bit)				\
  	{ .flags = word, .bit_nr = bit, }
cb65537ee   David Howells   Add wait_on_atomi...
66
67
  #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p)				\
  	{ .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
f07fdec50   Peter Zijlstra   lockdep/waitqueue...
68
  extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
2fc391112   Peter Zijlstra   locking, sched: G...
69
70
71
72
73
  
  #define init_waitqueue_head(q)				\
  	do {						\
  		static struct lock_class_key __key;	\
  							\
f07fdec50   Peter Zijlstra   lockdep/waitqueue...
74
  		__init_waitqueue_head((q), #q, &__key);	\
2fc391112   Peter Zijlstra   locking, sched: G...
75
  	} while (0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76

7259f0d05   Peter Zijlstra   [PATCH] lockdep: ...
77
78
79
80
81
82
83
84
  #ifdef CONFIG_LOCKDEP
  # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
  	({ init_waitqueue_head(&name); name; })
  # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
  	wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
  #else
  # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
  static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
  {
fb869b6e9   Ingo Molnar   sched/wait: Clean...
87
88
89
  	q->flags	= 0;
  	q->private	= p;
  	q->func		= default_wake_function;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
  }
fb869b6e9   Ingo Molnar   sched/wait: Clean...
91
92
  static inline void
  init_waitqueue_func_entry(wait_queue_t *q, wait_queue_func_t func)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
  {
fb869b6e9   Ingo Molnar   sched/wait: Clean...
94
95
96
  	q->flags	= 0;
  	q->private	= NULL;
  	q->func		= func;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
  }
69e51e92a   Peter Zijlstra   sched/wait: Docum...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  /**
   * waitqueue_active -- locklessly test for waiters on the queue
   * @q: the waitqueue to test for waiters
   *
   * returns true if the wait list is not empty
   *
   * NOTE: this function is lockless and requires care, incorrect usage _will_
   * lead to sporadic and non-obvious failure.
   *
   * Use either while holding wait_queue_head_t::lock or when used for wakeups
   * with an extra smp_mb() like:
   *
   *      CPU0 - waker                    CPU1 - waiter
   *
   *                                      for (;;) {
   *      @cond = true;                     prepare_to_wait(&wq, &wait, state);
   *      smp_mb();                         // smp_mb() from set_current_state()
   *      if (waitqueue_active(wq))         if (@cond)
   *        wake_up(wq);                      break;
   *                                        schedule();
   *                                      }
   *                                      finish_wait(&wq, &wait);
   *
   * Because without the explicit smp_mb() it's possible for the
   * waitqueue_active() load to get hoisted over the @cond store such that we'll
   * observe an empty wait list while the waiter might not observe @cond.
   *
   * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
   * which (when the lock is uncontended) are of roughly equal cost.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
129
130
131
  static inline int waitqueue_active(wait_queue_head_t *q)
  {
  	return !list_empty(&q->task_list);
  }
1ce0bf50a   Herbert Xu   net: Generalise w...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  /**
   * wq_has_sleeper - check if there are any waiting processes
   * @wq: wait queue head
   *
   * Returns true if wq has waiting processes
   *
   * Please refer to the comment for waitqueue_active.
   */
  static inline bool wq_has_sleeper(wait_queue_head_t *wq)
  {
  	/*
  	 * We need to be sure we are in sync with the
  	 * add_wait_queue modifications to the wait queue.
  	 *
  	 * This memory barrier should be paired with one on the
  	 * waiting side.
  	 */
  	smp_mb();
  	return waitqueue_active(wq);
  }
b3c975286   Harvey Harrison   include/linux: Re...
152
153
154
  extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
  extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
  extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
157
158
159
160
161
162
163
  
  static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
  {
  	list_add(&new->task_list, &head->task_list);
  }
  
  /*
   * Used for wake-one threads:
   */
fb869b6e9   Ingo Molnar   sched/wait: Clean...
164
165
  static inline void
  __add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
a93d2f174   Changli Gao   sched, wait: Use ...
166
167
168
169
  {
  	wait->flags |= WQ_FLAG_EXCLUSIVE;
  	__add_wait_queue(q, wait);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
  static inline void __add_wait_queue_tail(wait_queue_head_t *head,
a93d2f174   Changli Gao   sched, wait: Use ...
171
  					 wait_queue_t *new)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172
173
174
  {
  	list_add_tail(&new->task_list, &head->task_list);
  }
fb869b6e9   Ingo Molnar   sched/wait: Clean...
175
176
  static inline void
  __add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
a93d2f174   Changli Gao   sched, wait: Use ...
177
178
179
180
  {
  	wait->flags |= WQ_FLAG_EXCLUSIVE;
  	__add_wait_queue_tail(q, wait);
  }
fb869b6e9   Ingo Molnar   sched/wait: Clean...
181
182
  static inline void
  __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
185
  {
  	list_del(&old->task_list);
  }
dfd01f026   Peter Zijlstra   sched/wait: Fix t...
186
  typedef int wait_bit_action_f(struct wait_bit_key *, int mode);
b3c975286   Harvey Harrison   include/linux: Re...
187
  void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
ac5be6b47   Andrea Arcangeli   userfaultfd: reve...
188
  void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
fb869b6e9   Ingo Molnar   sched/wait: Clean...
189
  void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
63b200116   Thomas Gleixner   sched/wait: Add _...
190
  void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
4ede816ac   Davide Libenzi   epoll keyed wakeu...
191
  void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
b3c975286   Harvey Harrison   include/linux: Re...
192
  void __wake_up_bit(wait_queue_head_t *, void *, int);
c1221321b   NeilBrown   sched: Allow wait...
193
194
  int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
  int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
b3c975286   Harvey Harrison   include/linux: Re...
195
  void wake_up_bit(void *, int);
cb65537ee   David Howells   Add wait_on_atomi...
196
  void wake_up_atomic_t(atomic_t *);
c1221321b   NeilBrown   sched: Allow wait...
197
  int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
cbbce8220   NeilBrown   SCHED: add some "...
198
  int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
c1221321b   NeilBrown   sched: Allow wait...
199
  int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
cb65537ee   David Howells   Add wait_on_atomi...
200
  int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
b3c975286   Harvey Harrison   include/linux: Re...
201
  wait_queue_head_t *bit_waitqueue(void *, int);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202

e64d66c8e   Matthew Wilcox   wait: Use TASK_NO...
203
204
205
  #define wake_up(x)			__wake_up(x, TASK_NORMAL, 1, NULL)
  #define wake_up_nr(x, nr)		__wake_up(x, TASK_NORMAL, nr, NULL)
  #define wake_up_all(x)			__wake_up(x, TASK_NORMAL, 0, NULL)
63b200116   Thomas Gleixner   sched/wait: Add _...
206
207
  #define wake_up_locked(x)		__wake_up_locked((x), TASK_NORMAL, 1)
  #define wake_up_all_locked(x)		__wake_up_locked((x), TASK_NORMAL, 0)
e64d66c8e   Matthew Wilcox   wait: Use TASK_NO...
208

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
210
211
  #define wake_up_interruptible(x)	__wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
  #define wake_up_interruptible_nr(x, nr)	__wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
  #define wake_up_interruptible_all(x)	__wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
e64d66c8e   Matthew Wilcox   wait: Use TASK_NO...
212
  #define wake_up_interruptible_sync(x)	__wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213

0ccf831cb   Peter Zijlstra   lockdep: annotate...
214
  /*
c0da37753   Davide Libenzi   epoll keyed wakeu...
215
   * Wakeup macros to be used to report events to the targets.
0ccf831cb   Peter Zijlstra   lockdep: annotate...
216
   */
fb869b6e9   Ingo Molnar   sched/wait: Clean...
217
  #define wake_up_poll(x, m)						\
c0da37753   Davide Libenzi   epoll keyed wakeu...
218
  	__wake_up(x, TASK_NORMAL, 1, (void *) (m))
fb869b6e9   Ingo Molnar   sched/wait: Clean...
219
  #define wake_up_locked_poll(x, m)					\
ac5be6b47   Andrea Arcangeli   userfaultfd: reve...
220
  	__wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
fb869b6e9   Ingo Molnar   sched/wait: Clean...
221
  #define wake_up_interruptible_poll(x, m)				\
c0da37753   Davide Libenzi   epoll keyed wakeu...
222
223
224
  	__wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
  #define wake_up_interruptible_sync_poll(x, m)				\
  	__wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
0ccf831cb   Peter Zijlstra   lockdep: annotate...
225

35a2af94c   Peter Zijlstra   sched/wait: Make ...
226
  #define ___wait_cond_timeout(condition)					\
2953ef246   Peter Zijlstra   sched/wait: Chang...
227
  ({									\
fb869b6e9   Ingo Molnar   sched/wait: Clean...
228
229
230
231
  	bool __cond = (condition);					\
  	if (__cond && !__ret)						\
  		__ret = 1;						\
  	__cond || !__ret;						\
2953ef246   Peter Zijlstra   sched/wait: Chang...
232
  })
c2d816443   Oleg Nesterov   sched/wait: Intro...
233
234
235
  #define ___wait_is_interruptible(state)					\
  	(!__builtin_constant_p(state) ||				\
  		state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE)	\
41a1431b1   Peter Zijlstra   sched/wait: Intro...
236

0176beaff   Oleg Nesterov   sched/wait: Intro...
237
  extern void init_wait_entry(wait_queue_t *__wait, int flags);
8b32201de   Peter Zijlstra   wait: explain the...
238
239
240
241
242
243
244
245
246
247
248
  /*
   * The below macro ___wait_event() has an explicit shadow of the __ret
   * variable when used from the wait_event_*() macros.
   *
   * This is so that both can use the ___wait_cond_timeout() construct
   * to wrap the condition.
   *
   * The type inconsistency of the wait_event_*() __ret variable is also
   * on purpose; we use long where we can return timeout values and int
   * otherwise.
   */
41a1431b1   Peter Zijlstra   sched/wait: Intro...
249
  #define ___wait_event(wq, condition, state, exclusive, ret, cmd)	\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
250
  ({									\
41a1431b1   Peter Zijlstra   sched/wait: Intro...
251
  	__label__ __out;						\
c2d816443   Oleg Nesterov   sched/wait: Intro...
252
  	wait_queue_t __wait;						\
8b32201de   Peter Zijlstra   wait: explain the...
253
  	long __ret = ret;	/* explicit shadow */			\
41a1431b1   Peter Zijlstra   sched/wait: Intro...
254
  									\
0176beaff   Oleg Nesterov   sched/wait: Intro...
255
  	init_wait_entry(&__wait, exclusive ? WQ_FLAG_EXCLUSIVE : 0);	\
41a1431b1   Peter Zijlstra   sched/wait: Intro...
256
  	for (;;) {							\
c2d816443   Oleg Nesterov   sched/wait: Intro...
257
  		long __int = prepare_to_wait_event(&wq, &__wait, state);\
41a1431b1   Peter Zijlstra   sched/wait: Intro...
258
259
260
261
  									\
  		if (condition)						\
  			break;						\
  									\
c2d816443   Oleg Nesterov   sched/wait: Intro...
262
263
  		if (___wait_is_interruptible(state) && __int) {		\
  			__ret = __int;					\
b1ea06a90   Oleg Nesterov   sched/wait: Avoid...
264
  			goto __out;					\
41a1431b1   Peter Zijlstra   sched/wait: Intro...
265
266
267
268
269
  		}							\
  									\
  		cmd;							\
  	}								\
  	finish_wait(&wq, &__wait);					\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
270
271
  __out:	__ret;								\
  })
41a1431b1   Peter Zijlstra   sched/wait: Intro...
272

fb869b6e9   Ingo Molnar   sched/wait: Clean...
273
  #define __wait_event(wq, condition)					\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
274
275
  	(void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
  			    schedule())
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276
277
278
279
280
281
282
283
284
285
286
287
288
  
  /**
   * wait_event - sleep until a condition gets true
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   *
   * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
   * @condition evaluates to true. The @condition is checked each time
   * the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   */
fb869b6e9   Ingo Molnar   sched/wait: Clean...
289
  #define wait_event(wq, condition)					\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290
  do {									\
e22b886a8   Peter Zijlstra   sched/wait: Add m...
291
  	might_sleep();							\
fb869b6e9   Ingo Molnar   sched/wait: Clean...
292
  	if (condition)							\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293
294
295
  		break;							\
  	__wait_event(wq, condition);					\
  } while (0)
2c5612465   Peter Zijlstra   block: Simplify b...
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  #define __io_wait_event(wq, condition)					\
  	(void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
  			    io_schedule())
  
  /*
   * io_wait_event() -- like wait_event() but with io_schedule()
   */
  #define io_wait_event(wq, condition)					\
  do {									\
  	might_sleep();							\
  	if (condition)							\
  		break;							\
  	__io_wait_event(wq, condition);					\
  } while (0)
36df04bc5   Peter Zijlstra   sched/wait: Reimp...
310
311
312
313
314
  #define __wait_event_freezable(wq, condition)				\
  	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0,		\
  			    schedule(); try_to_freeze())
  
  /**
f4bcfa1da   Stafford Horne   sched/wait: Fix w...
315
   * wait_event_freezable - sleep (or freeze) until a condition gets true
36df04bc5   Peter Zijlstra   sched/wait: Reimp...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   *
   * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
   * to system load) until the @condition evaluates to true. The
   * @condition is checked each time the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   */
  #define wait_event_freezable(wq, condition)				\
  ({									\
  	int __ret = 0;							\
  	might_sleep();							\
  	if (!(condition))						\
  		__ret = __wait_event_freezable(wq, condition);		\
  	__ret;								\
  })
35a2af94c   Peter Zijlstra   sched/wait: Make ...
334
335
336
337
  #define __wait_event_timeout(wq, condition, timeout)			\
  	___wait_event(wq, ___wait_cond_timeout(condition),		\
  		      TASK_UNINTERRUPTIBLE, 0, timeout,			\
  		      __ret = schedule_timeout(__ret))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
339
340
341
342
343
344
345
346
347
348
349
350
351
  
  /**
   * wait_event_timeout - sleep until a condition gets true or a timeout elapses
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   * @timeout: timeout, in jiffies
   *
   * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
   * @condition evaluates to true. The @condition is checked each time
   * the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
6b44f5190   Scot Doyle   sched/wait: Docum...
352
353
354
355
356
   * Returns:
   * 0 if the @condition evaluated to %false after the @timeout elapsed,
   * 1 if the @condition evaluated to %true after the @timeout elapsed,
   * or the remaining jiffies (at least 1) if the @condition evaluated
   * to %true before the @timeout elapsed.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
357
358
359
360
   */
  #define wait_event_timeout(wq, condition, timeout)			\
  ({									\
  	long __ret = timeout;						\
e22b886a8   Peter Zijlstra   sched/wait: Add m...
361
  	might_sleep();							\
8922915b3   Oleg Nesterov   sched/wait: Add _...
362
  	if (!___wait_cond_timeout(condition))				\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
363
  		__ret = __wait_event_timeout(wq, condition, timeout);	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
  	__ret;								\
  })
36df04bc5   Peter Zijlstra   sched/wait: Reimp...
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  #define __wait_event_freezable_timeout(wq, condition, timeout)		\
  	___wait_event(wq, ___wait_cond_timeout(condition),		\
  		      TASK_INTERRUPTIBLE, 0, timeout,			\
  		      __ret = schedule_timeout(__ret); try_to_freeze())
  
  /*
   * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
   * increasing load and is freezable.
   */
  #define wait_event_freezable_timeout(wq, condition, timeout)		\
  ({									\
  	long __ret = timeout;						\
  	might_sleep();							\
  	if (!___wait_cond_timeout(condition))				\
  		__ret = __wait_event_freezable_timeout(wq, condition, timeout);	\
  	__ret;								\
  })
9f3520c31   Yuanhan Liu   wait: introduce w...
383
384
385
386
387
388
389
390
391
392
393
394
  #define __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2)		\
  	(void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 1, 0,	\
  			    cmd1; schedule(); cmd2)
  /*
   * Just like wait_event_cmd(), except it sets exclusive flag
   */
  #define wait_event_exclusive_cmd(wq, condition, cmd1, cmd2)		\
  do {									\
  	if (condition)							\
  		break;							\
  	__wait_event_exclusive_cmd(wq, condition, cmd1, cmd2);		\
  } while (0)
82e06c811   Shaohua Li   wait: add wait_ev...
395
396
397
398
399
400
401
402
  #define __wait_event_cmd(wq, condition, cmd1, cmd2)			\
  	(void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
  			    cmd1; schedule(); cmd2)
  
  /**
   * wait_event_cmd - sleep until a condition gets true
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
f434f7afa   Masanari Iida   sched: Fix warnin...
403
404
   * @cmd1: the command will be executed before sleep
   * @cmd2: the command will be executed after sleep
82e06c811   Shaohua Li   wait: add wait_ev...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
   *
   * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
   * @condition evaluates to true. The @condition is checked each time
   * the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   */
  #define wait_event_cmd(wq, condition, cmd1, cmd2)			\
  do {									\
  	if (condition)							\
  		break;							\
  	__wait_event_cmd(wq, condition, cmd1, cmd2);			\
  } while (0)
35a2af94c   Peter Zijlstra   sched/wait: Make ...
419
420
  #define __wait_event_interruptible(wq, condition)			\
  	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0,		\
f13f4c41c   Peter Zijlstra   sched/wait: Colla...
421
  		      schedule())
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
  
  /**
   * wait_event_interruptible - sleep until a condition gets true
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   *
   * The process is put to sleep (TASK_INTERRUPTIBLE) until the
   * @condition evaluates to true or a signal is received.
   * The @condition is checked each time the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * The function will return -ERESTARTSYS if it was interrupted by a
   * signal and 0 if @condition evaluated to true.
   */
  #define wait_event_interruptible(wq, condition)				\
  ({									\
  	int __ret = 0;							\
e22b886a8   Peter Zijlstra   sched/wait: Add m...
441
  	might_sleep();							\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442
  	if (!(condition))						\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
443
  		__ret = __wait_event_interruptible(wq, condition);	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
445
  	__ret;								\
  })
35a2af94c   Peter Zijlstra   sched/wait: Make ...
446
447
448
449
  #define __wait_event_interruptible_timeout(wq, condition, timeout)	\
  	___wait_event(wq, ___wait_cond_timeout(condition),		\
  		      TASK_INTERRUPTIBLE, 0, timeout,			\
  		      __ret = schedule_timeout(__ret))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
451
452
453
454
455
456
457
458
459
460
461
462
463
  
  /**
   * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   * @timeout: timeout, in jiffies
   *
   * The process is put to sleep (TASK_INTERRUPTIBLE) until the
   * @condition evaluates to true or a signal is received.
   * The @condition is checked each time the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
4c663cfc5   Imre Deak   wait: fix false t...
464
   * Returns:
6b44f5190   Scot Doyle   sched/wait: Docum...
465
466
467
468
469
   * 0 if the @condition evaluated to %false after the @timeout elapsed,
   * 1 if the @condition evaluated to %true after the @timeout elapsed,
   * the remaining jiffies (at least 1) if the @condition evaluated
   * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
   * interrupted by a signal.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
471
472
473
   */
  #define wait_event_interruptible_timeout(wq, condition, timeout)	\
  ({									\
  	long __ret = timeout;						\
e22b886a8   Peter Zijlstra   sched/wait: Add m...
474
  	might_sleep();							\
8922915b3   Oleg Nesterov   sched/wait: Add _...
475
  	if (!___wait_cond_timeout(condition))				\
fb869b6e9   Ingo Molnar   sched/wait: Clean...
476
  		__ret = __wait_event_interruptible_timeout(wq,		\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
477
  						condition, timeout);	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
479
  	__ret;								\
  })
774a08b35   Kent Overstreet   wait: add wait_ev...
480
481
482
  #define __wait_event_hrtimeout(wq, condition, timeout, state)		\
  ({									\
  	int __ret = 0;							\
774a08b35   Kent Overstreet   wait: add wait_ev...
483
484
485
486
487
488
489
490
491
492
  	struct hrtimer_sleeper __t;					\
  									\
  	hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC,		\
  			      HRTIMER_MODE_REL);			\
  	hrtimer_init_sleeper(&__t, current);				\
  	if ((timeout).tv64 != KTIME_MAX)				\
  		hrtimer_start_range_ns(&__t.timer, timeout,		\
  				       current->timer_slack_ns,		\
  				       HRTIMER_MODE_REL);		\
  									\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
493
  	__ret = ___wait_event(wq, condition, state, 0, 0,		\
774a08b35   Kent Overstreet   wait: add wait_ev...
494
495
496
497
  		if (!__t.task) {					\
  			__ret = -ETIME;					\
  			break;						\
  		}							\
ebdc195f2   Peter Zijlstra   sched/wait: Colla...
498
  		schedule());						\
774a08b35   Kent Overstreet   wait: add wait_ev...
499
500
501
  									\
  	hrtimer_cancel(&__t.timer);					\
  	destroy_hrtimer_on_stack(&__t.timer);				\
774a08b35   Kent Overstreet   wait: add wait_ev...
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
  	__ret;								\
  })
  
  /**
   * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   * @timeout: timeout, as a ktime_t
   *
   * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
   * @condition evaluates to true or a signal is received.
   * The @condition is checked each time the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * The function returns 0 if @condition became true, or -ETIME if the timeout
   * elapsed.
   */
  #define wait_event_hrtimeout(wq, condition, timeout)			\
  ({									\
  	int __ret = 0;							\
e22b886a8   Peter Zijlstra   sched/wait: Add m...
524
  	might_sleep();							\
774a08b35   Kent Overstreet   wait: add wait_ev...
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
  	if (!(condition))						\
  		__ret = __wait_event_hrtimeout(wq, condition, timeout,	\
  					       TASK_UNINTERRUPTIBLE);	\
  	__ret;								\
  })
  
  /**
   * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   * @timeout: timeout, as a ktime_t
   *
   * The process is put to sleep (TASK_INTERRUPTIBLE) until the
   * @condition evaluates to true or a signal is received.
   * The @condition is checked each time the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * The function returns 0 if @condition became true, -ERESTARTSYS if it was
   * interrupted by a signal, or -ETIME if the timeout elapsed.
   */
  #define wait_event_interruptible_hrtimeout(wq, condition, timeout)	\
  ({									\
  	long __ret = 0;							\
e22b886a8   Peter Zijlstra   sched/wait: Add m...
550
  	might_sleep();							\
774a08b35   Kent Overstreet   wait: add wait_ev...
551
552
553
554
555
  	if (!(condition))						\
  		__ret = __wait_event_hrtimeout(wq, condition, timeout,	\
  					       TASK_INTERRUPTIBLE);	\
  	__ret;								\
  })
35a2af94c   Peter Zijlstra   sched/wait: Make ...
556
557
  #define __wait_event_interruptible_exclusive(wq, condition)		\
  	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,		\
48c252171   Peter Zijlstra   sched/wait: Colla...
558
  		      schedule())
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
560
561
562
  
  #define wait_event_interruptible_exclusive(wq, condition)		\
  ({									\
  	int __ret = 0;							\
e22b886a8   Peter Zijlstra   sched/wait: Add m...
563
  	might_sleep();							\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
  	if (!(condition))						\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
565
  		__ret = __wait_event_interruptible_exclusive(wq, condition);\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
567
  	__ret;								\
  })
6a0fb3067   Al Viro   new helper: wait_...
568
569
570
571
572
573
574
575
576
577
578
579
  #define __wait_event_killable_exclusive(wq, condition)			\
  	___wait_event(wq, condition, TASK_KILLABLE, 1, 0,		\
  		      schedule())
  
  #define wait_event_killable_exclusive(wq, condition)			\
  ({									\
  	int __ret = 0;							\
  	might_sleep();							\
  	if (!(condition))						\
  		__ret = __wait_event_killable_exclusive(wq, condition);	\
  	__ret;								\
  })
22c43c81a   Michal Nazarewicz   wait_event_interr...
580

36df04bc5   Peter Zijlstra   sched/wait: Reimp...
581
582
583
584
585
586
587
588
589
590
591
592
  #define __wait_event_freezable_exclusive(wq, condition)			\
  	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,		\
  			schedule(); try_to_freeze())
  
  #define wait_event_freezable_exclusive(wq, condition)			\
  ({									\
  	int __ret = 0;							\
  	might_sleep();							\
  	if (!(condition))						\
  		__ret = __wait_event_freezable_exclusive(wq, condition);\
  	__ret;								\
  })
22c43c81a   Michal Nazarewicz   wait_event_interr...
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
  #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
  ({									\
  	int __ret = 0;							\
  	DEFINE_WAIT(__wait);						\
  	if (exclusive)							\
  		__wait.flags |= WQ_FLAG_EXCLUSIVE;			\
  	do {								\
  		if (likely(list_empty(&__wait.task_list)))		\
  			__add_wait_queue_tail(&(wq), &__wait);		\
  		set_current_state(TASK_INTERRUPTIBLE);			\
  		if (signal_pending(current)) {				\
  			__ret = -ERESTARTSYS;				\
  			break;						\
  		}							\
  		if (irq)						\
  			spin_unlock_irq(&(wq).lock);			\
  		else							\
  			spin_unlock(&(wq).lock);			\
  		schedule();						\
  		if (irq)						\
  			spin_lock_irq(&(wq).lock);			\
  		else							\
  			spin_lock(&(wq).lock);				\
  	} while (!(condition));						\
  	__remove_wait_queue(&(wq), &__wait);				\
  	__set_current_state(TASK_RUNNING);				\
  	__ret;								\
  })
  
  
  /**
   * wait_event_interruptible_locked - sleep until a condition gets true
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   *
   * The process is put to sleep (TASK_INTERRUPTIBLE) until the
   * @condition evaluates to true or a signal is received.
   * The @condition is checked each time the waitqueue @wq is woken up.
   *
   * It must be called with wq.lock being held.  This spinlock is
   * unlocked while sleeping but @condition testing is done while lock
   * is held and when this macro exits the lock is held.
   *
   * The lock is locked/unlocked using spin_lock()/spin_unlock()
   * functions which must match the way they are locked/unlocked outside
   * of this macro.
   *
   * wake_up_locked() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * The function will return -ERESTARTSYS if it was interrupted by a
   * signal and 0 if @condition evaluated to true.
   */
  #define wait_event_interruptible_locked(wq, condition)			\
  	((condition)							\
  	 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
  
  /**
   * wait_event_interruptible_locked_irq - sleep until a condition gets true
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   *
   * The process is put to sleep (TASK_INTERRUPTIBLE) until the
   * @condition evaluates to true or a signal is received.
   * The @condition is checked each time the waitqueue @wq is woken up.
   *
   * It must be called with wq.lock being held.  This spinlock is
   * unlocked while sleeping but @condition testing is done while lock
   * is held and when this macro exits the lock is held.
   *
   * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
   * functions which must match the way they are locked/unlocked outside
   * of this macro.
   *
   * wake_up_locked() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * The function will return -ERESTARTSYS if it was interrupted by a
   * signal and 0 if @condition evaluated to true.
   */
  #define wait_event_interruptible_locked_irq(wq, condition)		\
  	((condition)							\
  	 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
  
  /**
   * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   *
   * The process is put to sleep (TASK_INTERRUPTIBLE) until the
   * @condition evaluates to true or a signal is received.
   * The @condition is checked each time the waitqueue @wq is woken up.
   *
   * It must be called with wq.lock being held.  This spinlock is
   * unlocked while sleeping but @condition testing is done while lock
   * is held and when this macro exits the lock is held.
   *
   * The lock is locked/unlocked using spin_lock()/spin_unlock()
   * functions which must match the way they are locked/unlocked outside
   * of this macro.
   *
   * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
   * set thus when other process waits process on the list if this
   * process is awaken further processes are not considered.
   *
   * wake_up_locked() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * The function will return -ERESTARTSYS if it was interrupted by a
   * signal and 0 if @condition evaluated to true.
   */
  #define wait_event_interruptible_exclusive_locked(wq, condition)	\
  	((condition)							\
  	 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
  
  /**
   * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   *
   * The process is put to sleep (TASK_INTERRUPTIBLE) until the
   * @condition evaluates to true or a signal is received.
   * The @condition is checked each time the waitqueue @wq is woken up.
   *
   * It must be called with wq.lock being held.  This spinlock is
   * unlocked while sleeping but @condition testing is done while lock
   * is held and when this macro exits the lock is held.
   *
   * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
   * functions which must match the way they are locked/unlocked outside
   * of this macro.
   *
   * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
   * set thus when other process waits process on the list if this
   * process is awaken further processes are not considered.
   *
   * wake_up_locked() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * The function will return -ERESTARTSYS if it was interrupted by a
   * signal and 0 if @condition evaluated to true.
   */
  #define wait_event_interruptible_exclusive_locked_irq(wq, condition)	\
  	((condition)							\
  	 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
35a2af94c   Peter Zijlstra   sched/wait: Make ...
738
739
  #define __wait_event_killable(wq, condition)				\
  	___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
1411d5a7f   Matthew Wilcox   Add wait_event_ki...
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
  
  /**
   * wait_event_killable - sleep until a condition gets true
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   *
   * The process is put to sleep (TASK_KILLABLE) until the
   * @condition evaluates to true or a signal is received.
   * The @condition is checked each time the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * The function will return -ERESTARTSYS if it was interrupted by a
   * signal and 0 if @condition evaluated to true.
   */
  #define wait_event_killable(wq, condition)				\
  ({									\
  	int __ret = 0;							\
e22b886a8   Peter Zijlstra   sched/wait: Add m...
759
  	might_sleep();							\
1411d5a7f   Matthew Wilcox   Add wait_event_ki...
760
  	if (!(condition))						\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
761
  		__ret = __wait_event_killable(wq, condition);		\
1411d5a7f   Matthew Wilcox   Add wait_event_ki...
762
763
  	__ret;								\
  })
eed8c02e6   Lukas Czerner   wait: add wait_ev...
764
765
  
  #define __wait_event_lock_irq(wq, condition, lock, cmd)			\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
766
767
768
769
770
  	(void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
  			    spin_unlock_irq(&lock);			\
  			    cmd;					\
  			    schedule();					\
  			    spin_lock_irq(&lock))
eed8c02e6   Lukas Czerner   wait: add wait_ev...
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
  
  /**
   * wait_event_lock_irq_cmd - sleep until a condition gets true. The
   *			     condition is checked under the lock. This
   *			     is expected to be called with the lock
   *			     taken.
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   * @lock: a locked spinlock_t, which will be released before cmd
   *	  and schedule() and reacquired afterwards.
   * @cmd: a command which is invoked outside the critical section before
   *	 sleep
   *
   * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
   * @condition evaluates to true. The @condition is checked each time
   * the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * This is supposed to be called while holding the lock. The lock is
   * dropped before invoking the cmd and going to sleep and is reacquired
   * afterwards.
   */
  #define wait_event_lock_irq_cmd(wq, condition, lock, cmd)		\
  do {									\
  	if (condition)							\
  		break;							\
  	__wait_event_lock_irq(wq, condition, lock, cmd);		\
  } while (0)
  
  /**
   * wait_event_lock_irq - sleep until a condition gets true. The
   *			 condition is checked under the lock. This
   *			 is expected to be called with the lock
   *			 taken.
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   * @lock: a locked spinlock_t, which will be released before schedule()
   *	  and reacquired afterwards.
   *
   * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
   * @condition evaluates to true. The @condition is checked each time
   * the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * This is supposed to be called while holding the lock. The lock is
   * dropped before going to sleep and is reacquired afterwards.
   */
  #define wait_event_lock_irq(wq, condition, lock)			\
  do {									\
  	if (condition)							\
  		break;							\
  	__wait_event_lock_irq(wq, condition, lock, );			\
  } while (0)
35a2af94c   Peter Zijlstra   sched/wait: Make ...
828
  #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd)	\
fb869b6e9   Ingo Molnar   sched/wait: Clean...
829
  	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0,		\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
830
831
832
  		      spin_unlock_irq(&lock);				\
  		      cmd;						\
  		      schedule();					\
8fbd88fa1   Peter Zijlstra   sched/wait: Colla...
833
  		      spin_lock_irq(&lock))
eed8c02e6   Lukas Czerner   wait: add wait_ev...
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
  
  /**
   * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
   *		The condition is checked under the lock. This is expected to
   *		be called with the lock taken.
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   * @lock: a locked spinlock_t, which will be released before cmd and
   *	  schedule() and reacquired afterwards.
   * @cmd: a command which is invoked outside the critical section before
   *	 sleep
   *
   * The process is put to sleep (TASK_INTERRUPTIBLE) until the
   * @condition evaluates to true or a signal is received. The @condition is
   * checked each time the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * This is supposed to be called while holding the lock. The lock is
   * dropped before invoking the cmd and going to sleep and is reacquired
   * afterwards.
   *
   * The macro will return -ERESTARTSYS if it was interrupted by a signal
   * and 0 if @condition evaluated to true.
   */
  #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd)	\
  ({									\
  	int __ret = 0;							\
eed8c02e6   Lukas Czerner   wait: add wait_ev...
863
  	if (!(condition))						\
fb869b6e9   Ingo Molnar   sched/wait: Clean...
864
  		__ret = __wait_event_interruptible_lock_irq(wq,		\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
865
  						condition, lock, cmd);	\
eed8c02e6   Lukas Czerner   wait: add wait_ev...
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
  	__ret;								\
  })
  
  /**
   * wait_event_interruptible_lock_irq - sleep until a condition gets true.
   *		The condition is checked under the lock. This is expected
   *		to be called with the lock taken.
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   * @lock: a locked spinlock_t, which will be released before schedule()
   *	  and reacquired afterwards.
   *
   * The process is put to sleep (TASK_INTERRUPTIBLE) until the
   * @condition evaluates to true or signal is received. The @condition is
   * checked each time the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * This is supposed to be called while holding the lock. The lock is
   * dropped before going to sleep and is reacquired afterwards.
   *
   * The macro will return -ERESTARTSYS if it was interrupted by a signal
   * and 0 if @condition evaluated to true.
   */
  #define wait_event_interruptible_lock_irq(wq, condition, lock)		\
  ({									\
  	int __ret = 0;							\
eed8c02e6   Lukas Czerner   wait: add wait_ev...
894
  	if (!(condition))						\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
895
  		__ret = __wait_event_interruptible_lock_irq(wq,		\
92ec11809   Thierry Reding   sched/wait: Fix b...
896
  						condition, lock,);	\
eed8c02e6   Lukas Czerner   wait: add wait_ev...
897
898
  	__ret;								\
  })
fb869b6e9   Ingo Molnar   sched/wait: Clean...
899
900
  #define __wait_event_interruptible_lock_irq_timeout(wq, condition,	\
  						    lock, timeout)	\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
901
  	___wait_event(wq, ___wait_cond_timeout(condition),		\
7d716456a   Heiko Carstens   sched/wait: Fix _...
902
  		      TASK_INTERRUPTIBLE, 0, timeout,			\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
903
904
  		      spin_unlock_irq(&lock);				\
  		      __ret = schedule_timeout(__ret);			\
a1dc6852a   Peter Zijlstra   sched/wait: Colla...
905
  		      spin_lock_irq(&lock));
d79ff1426   Martin Peschke   [SCSI] zfcp: fix ...
906
907
  
  /**
fb869b6e9   Ingo Molnar   sched/wait: Clean...
908
909
910
   * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
   *		true or a timeout elapses. The condition is checked under
   *		the lock. This is expected to be called with the lock taken.
d79ff1426   Martin Peschke   [SCSI] zfcp: fix ...
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
   * @wq: the waitqueue to wait on
   * @condition: a C expression for the event to wait for
   * @lock: a locked spinlock_t, which will be released before schedule()
   *	  and reacquired afterwards.
   * @timeout: timeout, in jiffies
   *
   * The process is put to sleep (TASK_INTERRUPTIBLE) until the
   * @condition evaluates to true or signal is received. The @condition is
   * checked each time the waitqueue @wq is woken up.
   *
   * wake_up() has to be called after changing any variable that could
   * change the result of the wait condition.
   *
   * This is supposed to be called while holding the lock. The lock is
   * dropped before going to sleep and is reacquired afterwards.
   *
   * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
   * was interrupted by a signal, and the remaining jiffies otherwise
   * if the condition evaluated to true before the timeout elapsed.
   */
  #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock,	\
  						  timeout)		\
  ({									\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
934
  	long __ret = timeout;						\
8922915b3   Oleg Nesterov   sched/wait: Add _...
935
  	if (!___wait_cond_timeout(condition))				\
35a2af94c   Peter Zijlstra   sched/wait: Make ...
936
937
  		__ret = __wait_event_interruptible_lock_irq_timeout(	\
  					wq, condition, lock, timeout);	\
d79ff1426   Martin Peschke   [SCSI] zfcp: fix ...
938
939
  	__ret;								\
  })
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
940
941
942
  /*
   * Waitqueues which are removed from the waitqueue_head at wakeup time
   */
b3c975286   Harvey Harrison   include/linux: Re...
943
944
  void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
  void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
c2d816443   Oleg Nesterov   sched/wait: Intro...
945
  long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
b3c975286   Harvey Harrison   include/linux: Re...
946
  void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
61ada528d   Peter Zijlstra   sched/wait: Provi...
947
948
  long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
  int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
949
950
  int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
bf368e4e7   Eric Dumazet   net: Avoid extra ...
951
  #define DEFINE_WAIT_FUNC(name, function)				\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
952
  	wait_queue_t name = {						\
c43dc2fd8   Benjamin LaHaise   [PATCH] aio: make...
953
  		.private	= current,				\
bf368e4e7   Eric Dumazet   net: Avoid extra ...
954
  		.func		= function,				\
7e43c84e3   Paolo 'Blaisorblade' Giarrusso   [PATCH] Cleanup D...
955
  		.task_list	= LIST_HEAD_INIT((name).task_list),	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
956
  	}
bf368e4e7   Eric Dumazet   net: Avoid extra ...
957
  #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
958
959
960
961
  #define DEFINE_WAIT_BIT(name, word, bit)				\
  	struct wait_bit_queue name = {					\
  		.key = __WAIT_BIT_KEY_INITIALIZER(word, bit),		\
  		.wait	= {						\
c43dc2fd8   Benjamin LaHaise   [PATCH] aio: make...
962
  			.private	= current,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
963
964
965
966
967
968
969
970
  			.func		= wake_bit_function,		\
  			.task_list	=				\
  				LIST_HEAD_INIT((name).wait.task_list),	\
  		},							\
  	}
  
  #define init_wait(wait)							\
  	do {								\
c43dc2fd8   Benjamin LaHaise   [PATCH] aio: make...
971
  		(wait)->private = current;				\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
972
973
  		(wait)->func = autoremove_wake_function;		\
  		INIT_LIST_HEAD(&(wait)->task_list);			\
231d0aefd   Evgeny Kuznetsov   wait: using unini...
974
  		(wait)->flags = 0;					\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
975
  	} while (0)
743162013   NeilBrown   sched: Remove pro...
976

dfd01f026   Peter Zijlstra   sched/wait: Fix t...
977
978
979
980
  extern int bit_wait(struct wait_bit_key *, int);
  extern int bit_wait_io(struct wait_bit_key *, int);
  extern int bit_wait_timeout(struct wait_bit_key *, int);
  extern int bit_wait_io_timeout(struct wait_bit_key *, int);
743162013   NeilBrown   sched: Remove pro...
981

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982
983
984
985
  /**
   * wait_on_bit - wait for a bit to be cleared
   * @word: the word being waited on, a kernel virtual address
   * @bit: the bit of the word being waited on
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
986
987
988
989
990
991
992
993
   * @mode: the task state to sleep in
   *
   * There is a standard hashed waitqueue table for generic use. This
   * is the part of the hashtable's accessor API that waits on a bit.
   * For instance, if one were to have waiters on a bitflag, one would
   * call wait_on_bit() in threads waiting for the bit to clear.
   * One uses wait_on_bit() where one is waiting for the bit to clear,
   * but has no intention of setting it.
743162013   NeilBrown   sched: Remove pro...
994
995
996
997
998
   * Returned value will be zero if the bit was cleared, or non-zero
   * if the process received a signal and the mode permitted wakeup
   * on that signal.
   */
  static inline int
7e6059878   Palmer Dabbelt   sched/wait: Chang...
999
  wait_on_bit(unsigned long *word, int bit, unsigned mode)
743162013   NeilBrown   sched: Remove pro...
1000
  {
e22b886a8   Peter Zijlstra   sched/wait: Add m...
1001
  	might_sleep();
743162013   NeilBrown   sched: Remove pro...
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
  	if (!test_bit(bit, word))
  		return 0;
  	return out_of_line_wait_on_bit(word, bit,
  				       bit_wait,
  				       mode);
  }
  
  /**
   * wait_on_bit_io - wait for a bit to be cleared
   * @word: the word being waited on, a kernel virtual address
   * @bit: the bit of the word being waited on
   * @mode: the task state to sleep in
   *
   * Use the standard hashed waitqueue table to wait for a bit
   * to be cleared.  This is similar to wait_on_bit(), but calls
   * io_schedule() instead of schedule() for the actual waiting.
   *
   * Returned value will be zero if the bit was cleared, or non-zero
   * if the process received a signal and the mode permitted wakeup
   * on that signal.
   */
  static inline int
7e6059878   Palmer Dabbelt   sched/wait: Chang...
1024
  wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
743162013   NeilBrown   sched: Remove pro...
1025
  {
e22b886a8   Peter Zijlstra   sched/wait: Add m...
1026
  	might_sleep();
743162013   NeilBrown   sched: Remove pro...
1027
1028
1029
1030
1031
1032
1033
1034
  	if (!test_bit(bit, word))
  		return 0;
  	return out_of_line_wait_on_bit(word, bit,
  				       bit_wait_io,
  				       mode);
  }
  
  /**
44fc0e5ee   Johan Hedberg   sched/wait: Intro...
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
   * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
   * @word: the word being waited on, a kernel virtual address
   * @bit: the bit of the word being waited on
   * @mode: the task state to sleep in
   * @timeout: timeout, in jiffies
   *
   * Use the standard hashed waitqueue table to wait for a bit
   * to be cleared. This is similar to wait_on_bit(), except also takes a
   * timeout parameter.
   *
   * Returned value will be zero if the bit was cleared before the
   * @timeout elapsed, or non-zero if the @timeout elapsed or process
   * received a signal and the mode permitted wakeup on that signal.
   */
  static inline int
7e6059878   Palmer Dabbelt   sched/wait: Chang...
1050
1051
  wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
  		    unsigned long timeout)
44fc0e5ee   Johan Hedberg   sched/wait: Intro...
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
  {
  	might_sleep();
  	if (!test_bit(bit, word))
  		return 0;
  	return out_of_line_wait_on_bit_timeout(word, bit,
  					       bit_wait_timeout,
  					       mode, timeout);
  }
  
  /**
743162013   NeilBrown   sched: Remove pro...
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
   * wait_on_bit_action - wait for a bit to be cleared
   * @word: the word being waited on, a kernel virtual address
   * @bit: the bit of the word being waited on
   * @action: the function used to sleep, which may take special actions
   * @mode: the task state to sleep in
   *
   * Use the standard hashed waitqueue table to wait for a bit
   * to be cleared, and allow the waiting action to be specified.
   * This is like wait_on_bit() but allows fine control of how the waiting
   * is done.
   *
   * Returned value will be zero if the bit was cleared, or non-zero
   * if the process received a signal and the mode permitted wakeup
   * on that signal.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1076
   */
fb869b6e9   Ingo Molnar   sched/wait: Clean...
1077
  static inline int
7e6059878   Palmer Dabbelt   sched/wait: Chang...
1078
1079
  wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
  		   unsigned mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1080
  {
e22b886a8   Peter Zijlstra   sched/wait: Add m...
1081
  	might_sleep();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1082
1083
1084
1085
1086
1087
1088
1089
1090
  	if (!test_bit(bit, word))
  		return 0;
  	return out_of_line_wait_on_bit(word, bit, action, mode);
  }
  
  /**
   * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
   * @word: the word being waited on, a kernel virtual address
   * @bit: the bit of the word being waited on
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
   * @mode: the task state to sleep in
   *
   * There is a standard hashed waitqueue table for generic use. This
   * is the part of the hashtable's accessor API that waits on a bit
   * when one intends to set it, for instance, trying to lock bitflags.
   * For instance, if one were to have waiters trying to set bitflag
   * and waiting for it to clear before setting it, one would call
   * wait_on_bit() in threads waiting to be able to set the bit.
   * One uses wait_on_bit_lock() where one is waiting for the bit to
   * clear with the intention of setting it, and when done, clearing it.
743162013   NeilBrown   sched: Remove pro...
1101
1102
1103
1104
1105
1106
   *
   * Returns zero if the bit was (eventually) found to be clear and was
   * set.  Returns non-zero if a signal was delivered to the process and
   * the @mode allows that signal to wake the process.
   */
  static inline int
7e6059878   Palmer Dabbelt   sched/wait: Chang...
1107
  wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
743162013   NeilBrown   sched: Remove pro...
1108
  {
e22b886a8   Peter Zijlstra   sched/wait: Add m...
1109
  	might_sleep();
743162013   NeilBrown   sched: Remove pro...
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
  	if (!test_and_set_bit(bit, word))
  		return 0;
  	return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
  }
  
  /**
   * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
   * @word: the word being waited on, a kernel virtual address
   * @bit: the bit of the word being waited on
   * @mode: the task state to sleep in
   *
   * Use the standard hashed waitqueue table to wait for a bit
   * to be cleared and then to atomically set it.  This is similar
   * to wait_on_bit(), but calls io_schedule() instead of schedule()
   * for the actual waiting.
   *
   * Returns zero if the bit was (eventually) found to be clear and was
   * set.  Returns non-zero if a signal was delivered to the process and
   * the @mode allows that signal to wake the process.
   */
  static inline int
7e6059878   Palmer Dabbelt   sched/wait: Chang...
1131
  wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
743162013   NeilBrown   sched: Remove pro...
1132
  {
e22b886a8   Peter Zijlstra   sched/wait: Add m...
1133
  	might_sleep();
743162013   NeilBrown   sched: Remove pro...
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
  	if (!test_and_set_bit(bit, word))
  		return 0;
  	return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
  }
  
  /**
   * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
   * @word: the word being waited on, a kernel virtual address
   * @bit: the bit of the word being waited on
   * @action: the function used to sleep, which may take special actions
   * @mode: the task state to sleep in
   *
   * Use the standard hashed waitqueue table to wait for a bit
   * to be cleared and then to set it, and allow the waiting action
   * to be specified.
   * This is like wait_on_bit() but allows fine control of how the waiting
   * is done.
   *
   * Returns zero if the bit was (eventually) found to be clear and was
   * set.  Returns non-zero if a signal was delivered to the process and
   * the @mode allows that signal to wake the process.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1155
   */
fb869b6e9   Ingo Molnar   sched/wait: Clean...
1156
  static inline int
7e6059878   Palmer Dabbelt   sched/wait: Chang...
1157
1158
  wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
  			unsigned mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1159
  {
e22b886a8   Peter Zijlstra   sched/wait: Add m...
1160
  	might_sleep();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1161
1162
1163
1164
  	if (!test_and_set_bit(bit, word))
  		return 0;
  	return out_of_line_wait_on_bit_lock(word, bit, action, mode);
  }
cb65537ee   David Howells   Add wait_on_atomi...
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
  
  /**
   * wait_on_atomic_t - Wait for an atomic_t to become 0
   * @val: The atomic value being waited on, a kernel virtual address
   * @action: the function used to sleep, which may take special actions
   * @mode: the task state to sleep in
   *
   * Wait for an atomic_t to become 0.  We abuse the bit-wait waitqueue table for
   * the purpose of getting a waitqueue, but we set the key to a bit number
   * outside of the target 'word'.
   */
  static inline
  int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
  {
e22b886a8   Peter Zijlstra   sched/wait: Add m...
1179
  	might_sleep();
cb65537ee   David Howells   Add wait_on_atomi...
1180
1181
1182
1183
  	if (atomic_read(val) == 0)
  		return 0;
  	return out_of_line_wait_on_atomic_t(val, action, mode);
  }
fb869b6e9   Ingo Molnar   sched/wait: Clean...
1184
1185
  
  #endif /* _LINUX_WAIT_H */