Branch data Line data Source code
1 : : /* wait queue for multiple threads on kernel objects */
2 : :
3 : : /*
4 : : * Copyright (c) 2015 Wind River Systems, Inc.
5 : : *
6 : : * SPDX-License-Identifier: Apache-2.0
7 : : */
8 : :
9 : : #ifndef ZEPHYR_KERNEL_INCLUDE_WAIT_Q_H_
10 : : #define ZEPHYR_KERNEL_INCLUDE_WAIT_Q_H_
11 : :
12 : : #include <zephyr/kernel_structs.h>
13 : : #include <zephyr/sys/dlist.h>
14 : : #include <zephyr/sys/rb.h>
15 : : #include <timeout_q.h>
16 : : #include <priority_q.h>
17 : : #include <kspinlock.h>
18 : :
19 : : #ifdef __cplusplus
20 : : extern "C" {
21 : : #endif
22 : :
23 : : #ifdef CONFIG_WAITQ_SCALABLE
24 : :
25 : : /**
26 : : * Walk all threads pending on a wait queue.
27 : : *
28 : : * @param wq Wait queue
29 : : * @param thread_ptr Thread pointer iterator variable
30 : : *
31 : : * @warning Do not remove thread from wait queue when using this macro.
32 : : */
33 : : #define _WAIT_Q_FOR_EACH(wq, thread_ptr) \
34 : : RB_FOR_EACH_CONTAINER(&(wq)->waitq.tree, thread_ptr, base.qnode_rb)
35 : :
36 : : /*
37 : : * RB_FOR_EACH_SAFE() does not (and cannot) exist so we are not
38 : : * able to provide _WAIT_Q_FOR_EACH_SAFE when CONFIG_WAITQ_SCALABLE=y.
39 : : */
40 : :
41 : : static inline void z_waitq_init(_wait_q_t *w)
42 : : {
43 : : w->waitq = (struct _priq_rb) {
44 : : .tree = {
45 : : .lessthan_fn = z_priq_rb_lessthan
46 : : }
47 : : };
48 : : }
49 : :
50 : : static inline struct k_thread *z_waitq_head_locked(_wait_q_t *w)
51 : : {
52 : : return (struct k_thread *)rb_get_min(&w->waitq.tree);
53 : : }
54 : :
55 : : #else /* !CONFIG_WAITQ_SCALABLE: */
56 : :
57 : : /**
58 : : * Walk all threads pending on a wait queue.
59 : : *
60 : : * @param wq Wait queue
61 : : * @param thread_ptr Thread pointer iterator variable
62 : : *
63 : : * @warning Do not remove thread from wait queue when using this macro.
64 : : * Use @ref _WAIT_Q_FOR_EACH_SAFE instead if this is required.
65 : : */
66 : : #define _WAIT_Q_FOR_EACH(wq, thread_ptr) \
67 : : SYS_DLIST_FOR_EACH_CONTAINER(&((wq)->waitq), thread_ptr, \
68 : : base.qnode_dlist)
69 : :
70 : : /**
71 : : * Walk all threads pending on a wait queue.
72 : : *
73 : : * @note The iterator thread may be safely detached from the wait
74 : : * queue as part of the loop when using this macro.
75 : : *
76 : : * @warning `CONFIG_WAITQ_SCALABLE=n` is required to use this macro.
77 : : * Use it sparingly and make sure to have a fallback implementation
78 : : * which works when `CONFIG_WAITQ_SCALABLE=y`.
79 : : *
80 : : * @param wq Wait queue
81 : : * @param thread_ptr Thread pointer iterator variable
82 : : * @param loop_ptr Thread pointer variable used by loop
83 : : */
84 : : #define _WAIT_Q_FOR_EACH_SAFE(wq, thread_ptr, loop_ptr) \
85 : : SYS_DLIST_FOR_EACH_CONTAINER_SAFE(&((wq)->waitq), thread_ptr, \
86 : : loop_ptr, base.qnode_dlist)
87 : :
88 : 56 : static inline void z_waitq_init(_wait_q_t *w)
89 : : {
90 : 56 : sys_dlist_init(&w->waitq);
91 : 56 : }
92 : :
93 : 60 : static inline struct k_thread *z_waitq_head_locked(_wait_q_t *w)
94 : : {
95 : 60 : return (struct k_thread *)sys_dlist_peek_head(&w->waitq);
96 : : }
97 : :
98 : : #endif /* !CONFIG_WAITQ_SCALABLE */
99 : :
100 : : static inline struct k_thread *z_waitq_head(_wait_q_t *w)
101 : : {
102 : : struct k_thread *thread = NULL;
103 : :
104 : : /*
105 : : * On a single core system, all callers of this function are already
106 : : * protected by a spinlock held by the caller. On those systems there
107 : : * is no need to lock the scheduler's spinlock. However, on SMP systems
108 : : * the caller controlled spinlock is insufficient as the thread timeout
109 : : * handler may be running on another CPU. Use LOCK_SCHED_SPINLOCK
110 : : * to protect the scope as necessary.
111 : : */
112 : :
113 : : LOCK_SCHED_SPINLOCK {
114 : : thread = z_waitq_head_locked(w);
115 : : }
116 : :
117 : : return thread;
118 : : }
119 : :
120 : :
121 : : #ifdef __cplusplus
122 : : }
123 : : #endif
124 : :
125 : : #endif /* ZEPHYR_KERNEL_INCLUDE_WAIT_Q_H_ */
|