wait.h 36 KB

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