wake_q.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_SCHED_WAKE_Q_H
  3. #define _LINUX_SCHED_WAKE_Q_H
  4. /*
  5. * Wake-queues are lists of tasks with a pending wakeup, whose
  6. * callers have already marked the task as woken internally,
  7. * and can thus carry on. A common use case is being able to
  8. * do the wakeups once the corresponding user lock as been
  9. * released.
  10. *
  11. * We hold reference to each task in the list across the wakeup,
  12. * thus guaranteeing that the memory is still valid by the time
  13. * the actual wakeups are performed in wake_up_q().
  14. *
  15. * One per task suffices, because there's never a need for a task to be
  16. * in two wake queues simultaneously; it is forbidden to abandon a task
  17. * in a wake queue (a call to wake_up_q() _must_ follow), so if a task is
  18. * already in a wake queue, the wakeup will happen soon and the second
  19. * waker can just skip it.
  20. *
  21. * The DEFINE_WAKE_Q macro declares and initializes the list head.
  22. * wake_up_q() does NOT reinitialize the list; it's expected to be
  23. * called near the end of a function. Otherwise, the list can be
  24. * re-initialized for later re-use by wake_q_init().
  25. *
  26. * NOTE that this can cause spurious wakeups. schedule() callers
  27. * must ensure the call is done inside a loop, confirming that the
  28. * wakeup condition has in fact occurred.
  29. *
  30. * NOTE that there is no guarantee the wakeup will happen any later than the
  31. * wake_q_add() location. Therefore task must be ready to be woken at the
  32. * location of the wake_q_add().
  33. */
  34. #include <linux/sched.h>
  35. struct wake_q_head {
  36. struct wake_q_node *first;
  37. struct wake_q_node **lastp;
  38. int count;
  39. };
  40. #define WAKE_Q_TAIL ((struct wake_q_node *) 0x01)
  41. #define WAKE_Q_HEAD_INITIALIZER(name) \
  42. { WAKE_Q_TAIL, &name.first }
  43. #define DEFINE_WAKE_Q(name) \
  44. struct wake_q_head name = WAKE_Q_HEAD_INITIALIZER(name)
  45. static inline void wake_q_init(struct wake_q_head *head)
  46. {
  47. head->first = WAKE_Q_TAIL;
  48. head->lastp = &head->first;
  49. head->count = 0;
  50. }
  51. static inline bool wake_q_empty(struct wake_q_head *head)
  52. {
  53. return head->first == WAKE_Q_TAIL;
  54. }
  55. extern void wake_q_add(struct wake_q_head *head, struct task_struct *task);
  56. extern void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task);
  57. extern void wake_up_q(struct wake_q_head *head);
  58. #endif /* _LINUX_SCHED_WAKE_Q_H */