swait.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #ifndef _LINUX_SWAIT_H
  2. #define _LINUX_SWAIT_H
  3. #include <linux/list.h>
  4. #include <linux/stddef.h>
  5. #include <linux/spinlock.h>
  6. #include <asm/current.h>
  7. /*
  8. * Simple wait queues
  9. *
  10. * While these are very similar to the other/complex wait queues (wait.h) the
  11. * most important difference is that the simple waitqueue allows for
  12. * deterministic behaviour -- IOW it has strictly bounded IRQ and lock hold
  13. * times.
  14. *
  15. * In order to make this so, we had to drop a fair number of features of the
  16. * other waitqueue code; notably:
  17. *
  18. * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue;
  19. * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right
  20. * sleeper state.
  21. *
  22. * - the exclusive mode; because this requires preserving the list order
  23. * and this is hard.
  24. *
  25. * - custom wake functions; because you cannot give any guarantees about
  26. * random code.
  27. *
  28. * As a side effect of this; the data structures are slimmer.
  29. *
  30. * One would recommend using this wait queue where possible.
  31. */
  32. struct task_struct;
  33. struct swait_queue_head {
  34. raw_spinlock_t lock;
  35. struct list_head task_list;
  36. };
  37. struct swait_queue {
  38. struct task_struct *task;
  39. struct list_head task_list;
  40. };
  41. #define __SWAITQUEUE_INITIALIZER(name) { \
  42. .task = current, \
  43. .task_list = LIST_HEAD_INIT((name).task_list), \
  44. }
  45. #define DECLARE_SWAITQUEUE(name) \
  46. struct swait_queue name = __SWAITQUEUE_INITIALIZER(name)
  47. #define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \
  48. .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  49. .task_list = LIST_HEAD_INIT((name).task_list), \
  50. }
  51. #define DECLARE_SWAIT_QUEUE_HEAD(name) \
  52. struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name)
  53. extern void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  54. struct lock_class_key *key);
  55. #define init_swait_queue_head(q) \
  56. do { \
  57. static struct lock_class_key __key; \
  58. __init_swait_queue_head((q), #q, &__key); \
  59. } while (0)
  60. #ifdef CONFIG_LOCKDEP
  61. # define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
  62. ({ init_swait_queue_head(&name); name; })
  63. # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  64. struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name)
  65. #else
  66. # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  67. DECLARE_SWAIT_QUEUE_HEAD(name)
  68. #endif
  69. static inline int swait_active(struct swait_queue_head *q)
  70. {
  71. return !list_empty(&q->task_list);
  72. }
  73. extern void swake_up(struct swait_queue_head *q);
  74. extern void swake_up_all(struct swait_queue_head *q);
  75. extern void swake_up_locked(struct swait_queue_head *q);
  76. extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
  77. extern void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state);
  78. extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state);
  79. extern void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  80. extern void finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  81. /* as per ___wait_event() but for swait, therefore "exclusive == 0" */
  82. #define ___swait_event(wq, condition, state, ret, cmd) \
  83. ({ \
  84. struct swait_queue __wait; \
  85. long __ret = ret; \
  86. \
  87. INIT_LIST_HEAD(&__wait.task_list); \
  88. for (;;) { \
  89. long __int = prepare_to_swait_event(&wq, &__wait, state);\
  90. \
  91. if (condition) \
  92. break; \
  93. \
  94. if (___wait_is_interruptible(state) && __int) { \
  95. __ret = __int; \
  96. break; \
  97. } \
  98. \
  99. cmd; \
  100. } \
  101. finish_swait(&wq, &__wait); \
  102. __ret; \
  103. })
  104. #define __swait_event(wq, condition) \
  105. (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
  106. schedule())
  107. #define swait_event(wq, condition) \
  108. do { \
  109. if (condition) \
  110. break; \
  111. __swait_event(wq, condition); \
  112. } while (0)
  113. #define __swait_event_timeout(wq, condition, timeout) \
  114. ___swait_event(wq, ___wait_cond_timeout(condition), \
  115. TASK_UNINTERRUPTIBLE, timeout, \
  116. __ret = schedule_timeout(__ret))
  117. #define swait_event_timeout(wq, condition, timeout) \
  118. ({ \
  119. long __ret = timeout; \
  120. if (!___wait_cond_timeout(condition)) \
  121. __ret = __swait_event_timeout(wq, condition, timeout); \
  122. __ret; \
  123. })
  124. #define __swait_event_interruptible(wq, condition) \
  125. ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \
  126. schedule())
  127. #define swait_event_interruptible(wq, condition) \
  128. ({ \
  129. int __ret = 0; \
  130. if (!(condition)) \
  131. __ret = __swait_event_interruptible(wq, condition); \
  132. __ret; \
  133. })
  134. #define __swait_event_interruptible_timeout(wq, condition, timeout) \
  135. ___swait_event(wq, ___wait_cond_timeout(condition), \
  136. TASK_INTERRUPTIBLE, timeout, \
  137. __ret = schedule_timeout(__ret))
  138. #define swait_event_interruptible_timeout(wq, condition, timeout) \
  139. ({ \
  140. long __ret = timeout; \
  141. if (!___wait_cond_timeout(condition)) \
  142. __ret = __swait_event_interruptible_timeout(wq, \
  143. condition, timeout); \
  144. __ret; \
  145. })
  146. #endif /* _LINUX_SWAIT_H */