wait.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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. /**
  86. * waitqueue_active -- locklessly test for waiters on the queue
  87. * @q: the waitqueue to test for waiters
  88. *
  89. * returns true if the wait list is not empty
  90. *
  91. * NOTE: this function is lockless and requires care, incorrect usage _will_
  92. * lead to sporadic and non-obvious failure.
  93. *
  94. * Use either while holding wait_queue_head_t::lock or when used for wakeups
  95. * with an extra smp_mb() like:
  96. *
  97. * CPU0 - waker CPU1 - waiter
  98. *
  99. * for (;;) {
  100. * @cond = true; prepare_to_wait(&wq, &wait, state);
  101. * smp_mb(); // smp_mb() from set_current_state()
  102. * if (waitqueue_active(wq)) if (@cond)
  103. * wake_up(wq); break;
  104. * schedule();
  105. * }
  106. * finish_wait(&wq, &wait);
  107. *
  108. * Because without the explicit smp_mb() it's possible for the
  109. * waitqueue_active() load to get hoisted over the @cond store such that we'll
  110. * observe an empty wait list while the waiter might not observe @cond.
  111. *
  112. * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
  113. * which (when the lock is uncontended) are of roughly equal cost.
  114. */
  115. static inline int waitqueue_active(wait_queue_head_t *q)
  116. {
  117. return !list_empty(&q->task_list);
  118. }
  119. /**
  120. * wq_has_sleeper - check if there are any waiting processes
  121. * @wq: wait queue head
  122. *
  123. * Returns true if wq has waiting processes
  124. *
  125. * Please refer to the comment for waitqueue_active.
  126. */
  127. static inline bool wq_has_sleeper(wait_queue_head_t *wq)
  128. {
  129. /*
  130. * We need to be sure we are in sync with the
  131. * add_wait_queue modifications to the wait queue.
  132. *
  133. * This memory barrier should be paired with one on the
  134. * waiting side.
  135. */
  136. smp_mb();
  137. return waitqueue_active(wq);
  138. }
  139. extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
  140. extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
  141. extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
  142. static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
  143. {
  144. list_add(&new->task_list, &head->task_list);
  145. }
  146. /*
  147. * Used for wake-one threads:
  148. */
  149. static inline void
  150. __add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
  151. {
  152. wait->flags |= WQ_FLAG_EXCLUSIVE;
  153. __add_wait_queue(q, wait);
  154. }
  155. static inline void __add_wait_queue_tail(wait_queue_head_t *head,
  156. wait_queue_t *new)
  157. {
  158. list_add_tail(&new->task_list, &head->task_list);
  159. }
  160. static inline void
  161. __add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
  162. {
  163. wait->flags |= WQ_FLAG_EXCLUSIVE;
  164. __add_wait_queue_tail(q, wait);
  165. }
  166. static inline void
  167. __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
  168. {
  169. list_del(&old->task_list);
  170. }
  171. typedef int wait_bit_action_f(struct wait_bit_key *, int mode);
  172. void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
  173. void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
  174. void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
  175. void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
  176. void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
  177. void __wake_up_bit(wait_queue_head_t *, void *, int);
  178. int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
  179. int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
  180. void wake_up_bit(void *, int);
  181. void wake_up_atomic_t(atomic_t *);
  182. int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
  183. int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
  184. int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
  185. int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
  186. wait_queue_head_t *bit_waitqueue(void *, int);
  187. #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
  188. #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
  189. #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
  190. #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
  191. #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
  192. #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
  193. #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
  194. #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
  195. #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
  196. /*
  197. * Wakeup macros to be used to report events to the targets.
  198. */
  199. #define wake_up_poll(x, m) \
  200. __wake_up(x, TASK_NORMAL, 1, (void *) (m))
  201. #define wake_up_locked_poll(x, m) \
  202. __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
  203. #define wake_up_interruptible_poll(x, m) \
  204. __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
  205. #define wake_up_interruptible_sync_poll(x, m) \
  206. __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
  207. #define ___wait_cond_timeout(condition) \
  208. ({ \
  209. bool __cond = (condition); \
  210. if (__cond && !__ret) \
  211. __ret = 1; \
  212. __cond || !__ret; \
  213. })
  214. #define ___wait_is_interruptible(state) \
  215. (!__builtin_constant_p(state) || \
  216. state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
  217. /*
  218. * The below macro ___wait_event() has an explicit shadow of the __ret
  219. * variable when used from the wait_event_*() macros.
  220. *
  221. * This is so that both can use the ___wait_cond_timeout() construct
  222. * to wrap the condition.
  223. *
  224. * The type inconsistency of the wait_event_*() __ret variable is also
  225. * on purpose; we use long where we can return timeout values and int
  226. * otherwise.
  227. */
  228. #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
  229. ({ \
  230. __label__ __out; \
  231. wait_queue_t __wait; \
  232. long __ret = ret; /* explicit shadow */ \
  233. \
  234. INIT_LIST_HEAD(&__wait.task_list); \
  235. if (exclusive) \
  236. __wait.flags = WQ_FLAG_EXCLUSIVE; \
  237. else \
  238. __wait.flags = 0; \
  239. \
  240. for (;;) { \
  241. long __int = prepare_to_wait_event(&wq, &__wait, state);\
  242. \
  243. if (condition) \
  244. break; \
  245. \
  246. if (___wait_is_interruptible(state) && __int) { \
  247. __ret = __int; \
  248. goto __out; \
  249. } \
  250. \
  251. cmd; \
  252. } \
  253. finish_wait(&wq, &__wait); \
  254. __out: __ret; \
  255. })
  256. #define __wait_event(wq, condition) \
  257. (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
  258. schedule())
  259. /**
  260. * wait_event - sleep until a condition gets true
  261. * @wq: the waitqueue to wait on
  262. * @condition: a C expression for the event to wait for
  263. *
  264. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  265. * @condition evaluates to true. The @condition is checked each time
  266. * the waitqueue @wq is woken up.
  267. *
  268. * wake_up() has to be called after changing any variable that could
  269. * change the result of the wait condition.
  270. */
  271. #define wait_event(wq, condition) \
  272. do { \
  273. might_sleep(); \
  274. if (condition) \
  275. break; \
  276. __wait_event(wq, condition); \
  277. } while (0)
  278. #define __io_wait_event(wq, condition) \
  279. (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
  280. io_schedule())
  281. /*
  282. * io_wait_event() -- like wait_event() but with io_schedule()
  283. */
  284. #define io_wait_event(wq, condition) \
  285. do { \
  286. might_sleep(); \
  287. if (condition) \
  288. break; \
  289. __io_wait_event(wq, condition); \
  290. } while (0)
  291. #define __wait_event_freezable(wq, condition) \
  292. ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
  293. schedule(); try_to_freeze())
  294. /**
  295. * wait_event_freezable - sleep (or freeze) until a condition gets true
  296. * @wq: the waitqueue to wait on
  297. * @condition: a C expression for the event to wait for
  298. *
  299. * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
  300. * to system load) until the @condition evaluates to true. The
  301. * @condition is checked each time the waitqueue @wq is woken up.
  302. *
  303. * wake_up() has to be called after changing any variable that could
  304. * change the result of the wait condition.
  305. */
  306. #define wait_event_freezable(wq, condition) \
  307. ({ \
  308. int __ret = 0; \
  309. might_sleep(); \
  310. if (!(condition)) \
  311. __ret = __wait_event_freezable(wq, condition); \
  312. __ret; \
  313. })
  314. #define __wait_event_timeout(wq, condition, timeout) \
  315. ___wait_event(wq, ___wait_cond_timeout(condition), \
  316. TASK_UNINTERRUPTIBLE, 0, timeout, \
  317. __ret = schedule_timeout(__ret))
  318. /**
  319. * wait_event_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_UNINTERRUPTIBLE) until the
  325. * @condition evaluates to true. The @condition is checked each time
  326. * 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. * or the remaining jiffies (at least 1) if the @condition evaluated
  335. * to %true before the @timeout elapsed.
  336. */
  337. #define wait_event_timeout(wq, condition, timeout) \
  338. ({ \
  339. long __ret = timeout; \
  340. might_sleep(); \
  341. if (!___wait_cond_timeout(condition)) \
  342. __ret = __wait_event_timeout(wq, condition, timeout); \
  343. __ret; \
  344. })
  345. #define __wait_event_freezable_timeout(wq, condition, timeout) \
  346. ___wait_event(wq, ___wait_cond_timeout(condition), \
  347. TASK_INTERRUPTIBLE, 0, timeout, \
  348. __ret = schedule_timeout(__ret); try_to_freeze())
  349. /*
  350. * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
  351. * increasing load and is freezable.
  352. */
  353. #define wait_event_freezable_timeout(wq, condition, timeout) \
  354. ({ \
  355. long __ret = timeout; \
  356. might_sleep(); \
  357. if (!___wait_cond_timeout(condition)) \
  358. __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
  359. __ret; \
  360. })
  361. #define __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
  362. (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
  363. cmd1; schedule(); cmd2)
  364. /*
  365. * Just like wait_event_cmd(), except it sets exclusive flag
  366. */
  367. #define wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
  368. do { \
  369. if (condition) \
  370. break; \
  371. __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2); \
  372. } while (0)
  373. #define __wait_event_cmd(wq, condition, cmd1, cmd2) \
  374. (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
  375. cmd1; schedule(); cmd2)
  376. /**
  377. * wait_event_cmd - sleep until a condition gets true
  378. * @wq: the waitqueue to wait on
  379. * @condition: a C expression for the event to wait for
  380. * @cmd1: the command will be executed before sleep
  381. * @cmd2: the command will be executed after sleep
  382. *
  383. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  384. * @condition evaluates to true. The @condition is checked each time
  385. * the waitqueue @wq is woken up.
  386. *
  387. * wake_up() has to be called after changing any variable that could
  388. * change the result of the wait condition.
  389. */
  390. #define wait_event_cmd(wq, condition, cmd1, cmd2) \
  391. do { \
  392. if (condition) \
  393. break; \
  394. __wait_event_cmd(wq, condition, cmd1, cmd2); \
  395. } while (0)
  396. #define __wait_event_interruptible(wq, condition) \
  397. ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
  398. schedule())
  399. /**
  400. * wait_event_interruptible - sleep until a condition gets true
  401. * @wq: the waitqueue to wait on
  402. * @condition: a C expression for the event to wait for
  403. *
  404. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  405. * @condition evaluates to true or a signal is received.
  406. * The @condition is checked each time the waitqueue @wq is woken up.
  407. *
  408. * wake_up() has to be called after changing any variable that could
  409. * change the result of the wait condition.
  410. *
  411. * The function will return -ERESTARTSYS if it was interrupted by a
  412. * signal and 0 if @condition evaluated to true.
  413. */
  414. #define wait_event_interruptible(wq, condition) \
  415. ({ \
  416. int __ret = 0; \
  417. might_sleep(); \
  418. if (!(condition)) \
  419. __ret = __wait_event_interruptible(wq, condition); \
  420. __ret; \
  421. })
  422. #define __wait_event_interruptible_timeout(wq, condition, timeout) \
  423. ___wait_event(wq, ___wait_cond_timeout(condition), \
  424. TASK_INTERRUPTIBLE, 0, timeout, \
  425. __ret = schedule_timeout(__ret))
  426. /**
  427. * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
  428. * @wq: the waitqueue to wait on
  429. * @condition: a C expression for the event to wait for
  430. * @timeout: timeout, in jiffies
  431. *
  432. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  433. * @condition evaluates to true or a signal is received.
  434. * The @condition is checked each time the waitqueue @wq is woken up.
  435. *
  436. * wake_up() has to be called after changing any variable that could
  437. * change the result of the wait condition.
  438. *
  439. * Returns:
  440. * 0 if the @condition evaluated to %false after the @timeout elapsed,
  441. * 1 if the @condition evaluated to %true after the @timeout elapsed,
  442. * the remaining jiffies (at least 1) if the @condition evaluated
  443. * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
  444. * interrupted by a signal.
  445. */
  446. #define wait_event_interruptible_timeout(wq, condition, timeout) \
  447. ({ \
  448. long __ret = timeout; \
  449. might_sleep(); \
  450. if (!___wait_cond_timeout(condition)) \
  451. __ret = __wait_event_interruptible_timeout(wq, \
  452. condition, timeout); \
  453. __ret; \
  454. })
  455. #define __wait_event_hrtimeout(wq, condition, timeout, state) \
  456. ({ \
  457. int __ret = 0; \
  458. struct hrtimer_sleeper __t; \
  459. \
  460. hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
  461. HRTIMER_MODE_REL); \
  462. hrtimer_init_sleeper(&__t, current); \
  463. if ((timeout).tv64 != KTIME_MAX) \
  464. hrtimer_start_range_ns(&__t.timer, timeout, \
  465. current->timer_slack_ns, \
  466. HRTIMER_MODE_REL); \
  467. \
  468. __ret = ___wait_event(wq, condition, state, 0, 0, \
  469. if (!__t.task) { \
  470. __ret = -ETIME; \
  471. break; \
  472. } \
  473. schedule()); \
  474. \
  475. hrtimer_cancel(&__t.timer); \
  476. destroy_hrtimer_on_stack(&__t.timer); \
  477. __ret; \
  478. })
  479. /**
  480. * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
  481. * @wq: the waitqueue to wait on
  482. * @condition: a C expression for the event to wait for
  483. * @timeout: timeout, as a ktime_t
  484. *
  485. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  486. * @condition evaluates to true or a signal is received.
  487. * The @condition is checked each time the waitqueue @wq is woken up.
  488. *
  489. * wake_up() has to be called after changing any variable that could
  490. * change the result of the wait condition.
  491. *
  492. * The function returns 0 if @condition became true, or -ETIME if the timeout
  493. * elapsed.
  494. */
  495. #define wait_event_hrtimeout(wq, condition, timeout) \
  496. ({ \
  497. int __ret = 0; \
  498. might_sleep(); \
  499. if (!(condition)) \
  500. __ret = __wait_event_hrtimeout(wq, condition, timeout, \
  501. TASK_UNINTERRUPTIBLE); \
  502. __ret; \
  503. })
  504. /**
  505. * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
  506. * @wq: the waitqueue to wait on
  507. * @condition: a C expression for the event to wait for
  508. * @timeout: timeout, as a ktime_t
  509. *
  510. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  511. * @condition evaluates to true or a signal is received.
  512. * The @condition is checked each time the waitqueue @wq is woken up.
  513. *
  514. * wake_up() has to be called after changing any variable that could
  515. * change the result of the wait condition.
  516. *
  517. * The function returns 0 if @condition became true, -ERESTARTSYS if it was
  518. * interrupted by a signal, or -ETIME if the timeout elapsed.
  519. */
  520. #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
  521. ({ \
  522. long __ret = 0; \
  523. might_sleep(); \
  524. if (!(condition)) \
  525. __ret = __wait_event_hrtimeout(wq, condition, timeout, \
  526. TASK_INTERRUPTIBLE); \
  527. __ret; \
  528. })
  529. #define __wait_event_interruptible_exclusive(wq, condition) \
  530. ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
  531. schedule())
  532. #define wait_event_interruptible_exclusive(wq, condition) \
  533. ({ \
  534. int __ret = 0; \
  535. might_sleep(); \
  536. if (!(condition)) \
  537. __ret = __wait_event_interruptible_exclusive(wq, condition);\
  538. __ret; \
  539. })
  540. #define __wait_event_killable_exclusive(wq, condition) \
  541. ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
  542. schedule())
  543. #define wait_event_killable_exclusive(wq, condition) \
  544. ({ \
  545. int __ret = 0; \
  546. might_sleep(); \
  547. if (!(condition)) \
  548. __ret = __wait_event_killable_exclusive(wq, condition); \
  549. __ret; \
  550. })
  551. #define __wait_event_freezable_exclusive(wq, condition) \
  552. ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
  553. schedule(); try_to_freeze())
  554. #define wait_event_freezable_exclusive(wq, condition) \
  555. ({ \
  556. int __ret = 0; \
  557. might_sleep(); \
  558. if (!(condition)) \
  559. __ret = __wait_event_freezable_exclusive(wq, condition);\
  560. __ret; \
  561. })
  562. #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
  563. ({ \
  564. int __ret = 0; \
  565. DEFINE_WAIT(__wait); \
  566. if (exclusive) \
  567. __wait.flags |= WQ_FLAG_EXCLUSIVE; \
  568. do { \
  569. if (likely(list_empty(&__wait.task_list))) \
  570. __add_wait_queue_tail(&(wq), &__wait); \
  571. set_current_state(TASK_INTERRUPTIBLE); \
  572. if (signal_pending(current)) { \
  573. __ret = -ERESTARTSYS; \
  574. break; \
  575. } \
  576. if (irq) \
  577. spin_unlock_irq(&(wq).lock); \
  578. else \
  579. spin_unlock(&(wq).lock); \
  580. schedule(); \
  581. if (irq) \
  582. spin_lock_irq(&(wq).lock); \
  583. else \
  584. spin_lock(&(wq).lock); \
  585. } while (!(condition)); \
  586. __remove_wait_queue(&(wq), &__wait); \
  587. __set_current_state(TASK_RUNNING); \
  588. __ret; \
  589. })
  590. /**
  591. * wait_event_interruptible_locked - sleep until a condition gets true
  592. * @wq: the waitqueue to wait on
  593. * @condition: a C expression for the event to wait for
  594. *
  595. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  596. * @condition evaluates to true or a signal is received.
  597. * The @condition is checked each time the waitqueue @wq is woken up.
  598. *
  599. * It must be called with wq.lock being held. This spinlock is
  600. * unlocked while sleeping but @condition testing is done while lock
  601. * is held and when this macro exits the lock is held.
  602. *
  603. * The lock is locked/unlocked using spin_lock()/spin_unlock()
  604. * functions which must match the way they are locked/unlocked outside
  605. * of this macro.
  606. *
  607. * wake_up_locked() has to be called after changing any variable that could
  608. * change the result of the wait condition.
  609. *
  610. * The function will return -ERESTARTSYS if it was interrupted by a
  611. * signal and 0 if @condition evaluated to true.
  612. */
  613. #define wait_event_interruptible_locked(wq, condition) \
  614. ((condition) \
  615. ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
  616. /**
  617. * wait_event_interruptible_locked_irq - sleep until a condition gets true
  618. * @wq: the waitqueue to wait on
  619. * @condition: a C expression for the event to wait for
  620. *
  621. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  622. * @condition evaluates to true or a signal is received.
  623. * The @condition is checked each time the waitqueue @wq is woken up.
  624. *
  625. * It must be called with wq.lock being held. This spinlock is
  626. * unlocked while sleeping but @condition testing is done while lock
  627. * is held and when this macro exits the lock is held.
  628. *
  629. * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
  630. * functions which must match the way they are locked/unlocked outside
  631. * of this macro.
  632. *
  633. * wake_up_locked() has to be called after changing any variable that could
  634. * change the result of the wait condition.
  635. *
  636. * The function will return -ERESTARTSYS if it was interrupted by a
  637. * signal and 0 if @condition evaluated to true.
  638. */
  639. #define wait_event_interruptible_locked_irq(wq, condition) \
  640. ((condition) \
  641. ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
  642. /**
  643. * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
  644. * @wq: the waitqueue to wait on
  645. * @condition: a C expression for the event to wait for
  646. *
  647. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  648. * @condition evaluates to true or a signal is received.
  649. * The @condition is checked each time the waitqueue @wq is woken up.
  650. *
  651. * It must be called with wq.lock being held. This spinlock is
  652. * unlocked while sleeping but @condition testing is done while lock
  653. * is held and when this macro exits the lock is held.
  654. *
  655. * The lock is locked/unlocked using spin_lock()/spin_unlock()
  656. * functions which must match the way they are locked/unlocked outside
  657. * of this macro.
  658. *
  659. * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
  660. * set thus when other process waits process on the list if this
  661. * process is awaken further processes are not considered.
  662. *
  663. * wake_up_locked() has to be called after changing any variable that could
  664. * change the result of the wait condition.
  665. *
  666. * The function will return -ERESTARTSYS if it was interrupted by a
  667. * signal and 0 if @condition evaluated to true.
  668. */
  669. #define wait_event_interruptible_exclusive_locked(wq, condition) \
  670. ((condition) \
  671. ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
  672. /**
  673. * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
  674. * @wq: the waitqueue to wait on
  675. * @condition: a C expression for the event to wait for
  676. *
  677. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  678. * @condition evaluates to true or a signal is received.
  679. * The @condition is checked each time the waitqueue @wq is woken up.
  680. *
  681. * It must be called with wq.lock being held. This spinlock is
  682. * unlocked while sleeping but @condition testing is done while lock
  683. * is held and when this macro exits the lock is held.
  684. *
  685. * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
  686. * functions which must match the way they are locked/unlocked outside
  687. * of this macro.
  688. *
  689. * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
  690. * set thus when other process waits process on the list if this
  691. * process is awaken further processes are not considered.
  692. *
  693. * wake_up_locked() has to be called after changing any variable that could
  694. * change the result of the wait condition.
  695. *
  696. * The function will return -ERESTARTSYS if it was interrupted by a
  697. * signal and 0 if @condition evaluated to true.
  698. */
  699. #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
  700. ((condition) \
  701. ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
  702. #define __wait_event_killable(wq, condition) \
  703. ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
  704. /**
  705. * wait_event_killable - sleep until a condition gets true
  706. * @wq: the waitqueue to wait on
  707. * @condition: a C expression for the event to wait for
  708. *
  709. * The process is put to sleep (TASK_KILLABLE) until the
  710. * @condition evaluates to true or a signal is received.
  711. * The @condition is checked each time the waitqueue @wq is woken up.
  712. *
  713. * wake_up() has to be called after changing any variable that could
  714. * change the result of the wait condition.
  715. *
  716. * The function will return -ERESTARTSYS if it was interrupted by a
  717. * signal and 0 if @condition evaluated to true.
  718. */
  719. #define wait_event_killable(wq, condition) \
  720. ({ \
  721. int __ret = 0; \
  722. might_sleep(); \
  723. if (!(condition)) \
  724. __ret = __wait_event_killable(wq, condition); \
  725. __ret; \
  726. })
  727. #define __wait_event_lock_irq(wq, condition, lock, cmd) \
  728. (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
  729. spin_unlock_irq(&lock); \
  730. cmd; \
  731. schedule(); \
  732. spin_lock_irq(&lock))
  733. /**
  734. * wait_event_lock_irq_cmd - sleep until a condition gets true. The
  735. * condition is checked under the lock. This
  736. * is expected to be called with the lock
  737. * 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 cmd
  741. * and schedule() and reacquired afterwards.
  742. * @cmd: a command which is invoked outside the critical section before
  743. * sleep
  744. *
  745. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  746. * @condition evaluates to true. The @condition is checked each time
  747. * the waitqueue @wq is woken up.
  748. *
  749. * wake_up() has to be called after changing any variable that could
  750. * change the result of the wait condition.
  751. *
  752. * This is supposed to be called while holding the lock. The lock is
  753. * dropped before invoking the cmd and going to sleep and is reacquired
  754. * afterwards.
  755. */
  756. #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
  757. do { \
  758. if (condition) \
  759. break; \
  760. __wait_event_lock_irq(wq, condition, lock, cmd); \
  761. } while (0)
  762. /**
  763. * wait_event_lock_irq - sleep until a condition gets true. The
  764. * condition is checked under the lock. This
  765. * is expected to be called with the lock
  766. * taken.
  767. * @wq: the waitqueue to wait on
  768. * @condition: a C expression for the event to wait for
  769. * @lock: a locked spinlock_t, which will be released before schedule()
  770. * and reacquired afterwards.
  771. *
  772. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  773. * @condition evaluates to true. The @condition is checked each time
  774. * the waitqueue @wq is woken up.
  775. *
  776. * wake_up() has to be called after changing any variable that could
  777. * change the result of the wait condition.
  778. *
  779. * This is supposed to be called while holding the lock. The lock is
  780. * dropped before going to sleep and is reacquired afterwards.
  781. */
  782. #define wait_event_lock_irq(wq, condition, lock) \
  783. do { \
  784. if (condition) \
  785. break; \
  786. __wait_event_lock_irq(wq, condition, lock, ); \
  787. } while (0)
  788. #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
  789. ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
  790. spin_unlock_irq(&lock); \
  791. cmd; \
  792. schedule(); \
  793. spin_lock_irq(&lock))
  794. /**
  795. * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
  796. * The condition is checked under the lock. This is expected to
  797. * be called with the lock taken.
  798. * @wq: the waitqueue to wait on
  799. * @condition: a C expression for the event to wait for
  800. * @lock: a locked spinlock_t, which will be released before cmd and
  801. * schedule() and reacquired afterwards.
  802. * @cmd: a command which is invoked outside the critical section before
  803. * sleep
  804. *
  805. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  806. * @condition evaluates to true or a signal is received. The @condition is
  807. * checked each time the waitqueue @wq is woken up.
  808. *
  809. * wake_up() has to be called after changing any variable that could
  810. * change the result of the wait condition.
  811. *
  812. * This is supposed to be called while holding the lock. The lock is
  813. * dropped before invoking the cmd and going to sleep and is reacquired
  814. * afterwards.
  815. *
  816. * The macro will return -ERESTARTSYS if it was interrupted by a signal
  817. * and 0 if @condition evaluated to true.
  818. */
  819. #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
  820. ({ \
  821. int __ret = 0; \
  822. if (!(condition)) \
  823. __ret = __wait_event_interruptible_lock_irq(wq, \
  824. condition, lock, cmd); \
  825. __ret; \
  826. })
  827. /**
  828. * wait_event_interruptible_lock_irq - sleep until a condition gets true.
  829. * The condition is checked under the lock. This is expected
  830. * to be called with the lock taken.
  831. * @wq: the waitqueue to wait on
  832. * @condition: a C expression for the event to wait for
  833. * @lock: a locked spinlock_t, which will be released before schedule()
  834. * and reacquired afterwards.
  835. *
  836. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  837. * @condition evaluates to true or signal is received. The @condition is
  838. * checked each time the waitqueue @wq is woken up.
  839. *
  840. * wake_up() has to be called after changing any variable that could
  841. * change the result of the wait condition.
  842. *
  843. * This is supposed to be called while holding the lock. The lock is
  844. * dropped before going to sleep and is reacquired afterwards.
  845. *
  846. * The macro will return -ERESTARTSYS if it was interrupted by a signal
  847. * and 0 if @condition evaluated to true.
  848. */
  849. #define wait_event_interruptible_lock_irq(wq, condition, lock) \
  850. ({ \
  851. int __ret = 0; \
  852. if (!(condition)) \
  853. __ret = __wait_event_interruptible_lock_irq(wq, \
  854. condition, lock,); \
  855. __ret; \
  856. })
  857. #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
  858. lock, timeout) \
  859. ___wait_event(wq, ___wait_cond_timeout(condition), \
  860. TASK_INTERRUPTIBLE, 0, timeout, \
  861. spin_unlock_irq(&lock); \
  862. __ret = schedule_timeout(__ret); \
  863. spin_lock_irq(&lock));
  864. /**
  865. * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
  866. * true or a timeout elapses. The condition is checked under
  867. * the lock. This is expected to be called with the lock taken.
  868. * @wq: the waitqueue to wait on
  869. * @condition: a C expression for the event to wait for
  870. * @lock: a locked spinlock_t, which will be released before schedule()
  871. * and reacquired afterwards.
  872. * @timeout: timeout, in jiffies
  873. *
  874. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  875. * @condition evaluates to true or signal is received. The @condition is
  876. * checked each time the waitqueue @wq is woken up.
  877. *
  878. * wake_up() has to be called after changing any variable that could
  879. * change the result of the wait condition.
  880. *
  881. * This is supposed to be called while holding the lock. The lock is
  882. * dropped before going to sleep and is reacquired afterwards.
  883. *
  884. * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
  885. * was interrupted by a signal, and the remaining jiffies otherwise
  886. * if the condition evaluated to true before the timeout elapsed.
  887. */
  888. #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
  889. timeout) \
  890. ({ \
  891. long __ret = timeout; \
  892. if (!___wait_cond_timeout(condition)) \
  893. __ret = __wait_event_interruptible_lock_irq_timeout( \
  894. wq, condition, lock, timeout); \
  895. __ret; \
  896. })
  897. /*
  898. * Waitqueues which are removed from the waitqueue_head at wakeup time
  899. */
  900. void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
  901. void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
  902. long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
  903. void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
  904. void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, void *key);
  905. long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
  906. int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  907. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  908. int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  909. #define DEFINE_WAIT_FUNC(name, function) \
  910. wait_queue_t name = { \
  911. .private = current, \
  912. .func = function, \
  913. .task_list = LIST_HEAD_INIT((name).task_list), \
  914. }
  915. #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
  916. #define DEFINE_WAIT_BIT(name, word, bit) \
  917. struct wait_bit_queue name = { \
  918. .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
  919. .wait = { \
  920. .private = current, \
  921. .func = wake_bit_function, \
  922. .task_list = \
  923. LIST_HEAD_INIT((name).wait.task_list), \
  924. }, \
  925. }
  926. #define init_wait(wait) \
  927. do { \
  928. (wait)->private = current; \
  929. (wait)->func = autoremove_wake_function; \
  930. INIT_LIST_HEAD(&(wait)->task_list); \
  931. (wait)->flags = 0; \
  932. } while (0)
  933. extern int bit_wait(struct wait_bit_key *, int);
  934. extern int bit_wait_io(struct wait_bit_key *, int);
  935. extern int bit_wait_timeout(struct wait_bit_key *, int);
  936. extern int bit_wait_io_timeout(struct wait_bit_key *, int);
  937. /**
  938. * wait_on_bit - wait for a bit to be cleared
  939. * @word: the word being waited on, a kernel virtual address
  940. * @bit: the bit of the word being waited on
  941. * @mode: the task state to sleep in
  942. *
  943. * There is a standard hashed waitqueue table for generic use. This
  944. * is the part of the hashtable's accessor API that waits on a bit.
  945. * For instance, if one were to have waiters on a bitflag, one would
  946. * call wait_on_bit() in threads waiting for the bit to clear.
  947. * One uses wait_on_bit() where one is waiting for the bit to clear,
  948. * but has no intention of setting it.
  949. * Returned value will be zero if the bit was cleared, or non-zero
  950. * if the process received a signal and the mode permitted wakeup
  951. * on that signal.
  952. */
  953. static inline int
  954. wait_on_bit(unsigned long *word, int bit, unsigned mode)
  955. {
  956. might_sleep();
  957. if (!test_bit(bit, word))
  958. return 0;
  959. return out_of_line_wait_on_bit(word, bit,
  960. bit_wait,
  961. mode);
  962. }
  963. /**
  964. * wait_on_bit_io - wait for a bit to be cleared
  965. * @word: the word being waited on, a kernel virtual address
  966. * @bit: the bit of the word being waited on
  967. * @mode: the task state to sleep in
  968. *
  969. * Use the standard hashed waitqueue table to wait for a bit
  970. * to be cleared. This is similar to wait_on_bit(), but calls
  971. * io_schedule() instead of schedule() for the actual waiting.
  972. *
  973. * Returned value will be zero if the bit was cleared, or non-zero
  974. * if the process received a signal and the mode permitted wakeup
  975. * on that signal.
  976. */
  977. static inline int
  978. wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
  979. {
  980. might_sleep();
  981. if (!test_bit(bit, word))
  982. return 0;
  983. return out_of_line_wait_on_bit(word, bit,
  984. bit_wait_io,
  985. mode);
  986. }
  987. /**
  988. * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
  989. * @word: the word being waited on, a kernel virtual address
  990. * @bit: the bit of the word being waited on
  991. * @mode: the task state to sleep in
  992. * @timeout: timeout, in jiffies
  993. *
  994. * Use the standard hashed waitqueue table to wait for a bit
  995. * to be cleared. This is similar to wait_on_bit(), except also takes a
  996. * timeout parameter.
  997. *
  998. * Returned value will be zero if the bit was cleared before the
  999. * @timeout elapsed, or non-zero if the @timeout elapsed or process
  1000. * received a signal and the mode permitted wakeup on that signal.
  1001. */
  1002. static inline int
  1003. wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
  1004. unsigned long timeout)
  1005. {
  1006. might_sleep();
  1007. if (!test_bit(bit, word))
  1008. return 0;
  1009. return out_of_line_wait_on_bit_timeout(word, bit,
  1010. bit_wait_timeout,
  1011. mode, timeout);
  1012. }
  1013. /**
  1014. * wait_on_bit_action - wait for a bit to be cleared
  1015. * @word: the word being waited on, a kernel virtual address
  1016. * @bit: the bit of the word being waited on
  1017. * @action: the function used to sleep, which may take special actions
  1018. * @mode: the task state to sleep in
  1019. *
  1020. * Use the standard hashed waitqueue table to wait for a bit
  1021. * to be cleared, and allow the waiting action to be specified.
  1022. * This is like wait_on_bit() but allows fine control of how the waiting
  1023. * is done.
  1024. *
  1025. * Returned value will be zero if the bit was cleared, or non-zero
  1026. * if the process received a signal and the mode permitted wakeup
  1027. * on that signal.
  1028. */
  1029. static inline int
  1030. wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
  1031. unsigned mode)
  1032. {
  1033. might_sleep();
  1034. if (!test_bit(bit, word))
  1035. return 0;
  1036. return out_of_line_wait_on_bit(word, bit, action, mode);
  1037. }
  1038. /**
  1039. * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
  1040. * @word: the word being waited on, a kernel virtual address
  1041. * @bit: the bit of the word being waited on
  1042. * @mode: the task state to sleep in
  1043. *
  1044. * There is a standard hashed waitqueue table for generic use. This
  1045. * is the part of the hashtable's accessor API that waits on a bit
  1046. * when one intends to set it, for instance, trying to lock bitflags.
  1047. * For instance, if one were to have waiters trying to set bitflag
  1048. * and waiting for it to clear before setting it, one would call
  1049. * wait_on_bit() in threads waiting to be able to set the bit.
  1050. * One uses wait_on_bit_lock() where one is waiting for the bit to
  1051. * clear with the intention of setting it, and when done, clearing it.
  1052. *
  1053. * Returns zero if the bit was (eventually) found to be clear and was
  1054. * set. Returns non-zero if a signal was delivered to the process and
  1055. * the @mode allows that signal to wake the process.
  1056. */
  1057. static inline int
  1058. wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
  1059. {
  1060. might_sleep();
  1061. if (!test_and_set_bit(bit, word))
  1062. return 0;
  1063. return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
  1064. }
  1065. /**
  1066. * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
  1067. * @word: the word being waited on, a kernel virtual address
  1068. * @bit: the bit of the word being waited on
  1069. * @mode: the task state to sleep in
  1070. *
  1071. * Use the standard hashed waitqueue table to wait for a bit
  1072. * to be cleared and then to atomically set it. This is similar
  1073. * to wait_on_bit(), but calls io_schedule() instead of schedule()
  1074. * for the actual waiting.
  1075. *
  1076. * Returns zero if the bit was (eventually) found to be clear and was
  1077. * set. Returns non-zero if a signal was delivered to the process and
  1078. * the @mode allows that signal to wake the process.
  1079. */
  1080. static inline int
  1081. wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
  1082. {
  1083. might_sleep();
  1084. if (!test_and_set_bit(bit, word))
  1085. return 0;
  1086. return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
  1087. }
  1088. /**
  1089. * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
  1090. * @word: the word being waited on, a kernel virtual address
  1091. * @bit: the bit of the word being waited on
  1092. * @action: the function used to sleep, which may take special actions
  1093. * @mode: the task state to sleep in
  1094. *
  1095. * Use the standard hashed waitqueue table to wait for a bit
  1096. * to be cleared and then to set it, and allow the waiting action
  1097. * to be specified.
  1098. * This is like wait_on_bit() but allows fine control of how the waiting
  1099. * is done.
  1100. *
  1101. * Returns zero if the bit was (eventually) found to be clear and was
  1102. * set. Returns non-zero if a signal was delivered to the process and
  1103. * the @mode allows that signal to wake the process.
  1104. */
  1105. static inline int
  1106. wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
  1107. unsigned mode)
  1108. {
  1109. might_sleep();
  1110. if (!test_and_set_bit(bit, word))
  1111. return 0;
  1112. return out_of_line_wait_on_bit_lock(word, bit, action, mode);
  1113. }
  1114. /**
  1115. * wait_on_atomic_t - Wait for an atomic_t to become 0
  1116. * @val: The atomic value being waited on, a kernel virtual address
  1117. * @action: the function used to sleep, which may take special actions
  1118. * @mode: the task state to sleep in
  1119. *
  1120. * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
  1121. * the purpose of getting a waitqueue, but we set the key to a bit number
  1122. * outside of the target 'word'.
  1123. */
  1124. static inline
  1125. int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
  1126. {
  1127. might_sleep();
  1128. if (atomic_read(val) == 0)
  1129. return 0;
  1130. return out_of_line_wait_on_atomic_t(val, action, mode);
  1131. }
  1132. #endif /* _LINUX_WAIT_H */