xref: /linux-6.15/include/linux/sched/wake_q.h (revision 564f7dfd)
1 #ifndef _LINUX_SCHED_WAKE_Q_H
2 #define _LINUX_SCHED_WAKE_Q_H
3 
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 
31 #include <linux/sched.h>
32 
33 struct wake_q_head {
34 	struct wake_q_node *first;
35 	struct wake_q_node **lastp;
36 };
37 
38 #define WAKE_Q_TAIL ((struct wake_q_node *) 0x01)
39 
40 #define DEFINE_WAKE_Q(name)				\
41 	struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
42 
43 static inline void wake_q_init(struct wake_q_head *head)
44 {
45 	head->first = WAKE_Q_TAIL;
46 	head->lastp = &head->first;
47 }
48 
49 extern void wake_q_add(struct wake_q_head *head,
50 		       struct task_struct *task);
51 extern void wake_up_q(struct wake_q_head *head);
52 
53 #endif /* _LINUX_SCHED_WAKE_Q_H */
54