Skip to content

Commit 87aa621

Browse files
VudentzAnas Nashif
authored andcommitted
kernel: Use SYS_DLIST_FOR_EACH_CONTAINER whenever possible
SYS_DLIST_FOR_EACH_CONTAINER is preferable over using SYS_DLIST_FOR_EACH_NODE as that avoid casting directly which assumes the node field is always at the beginning. Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent e27a162 commit 87aa621

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

kernel/include/timeout_q.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ static inline void _handle_one_expired_timeout(struct _timeout *timeout)
108108

109109
static inline void _handle_expired_timeouts(sys_dlist_t *expired)
110110
{
111-
sys_dnode_t *timeout, *next;
111+
struct _timeout *timeout, *next;
112112

113-
SYS_DLIST_FOR_EACH_NODE_SAFE(expired, timeout, next) {
114-
sys_dlist_remove(timeout);
115-
_handle_one_expired_timeout((struct _timeout *)timeout);
113+
SYS_DLIST_FOR_EACH_CONTAINER_SAFE(expired, timeout, next, node) {
114+
sys_dlist_remove(&timeout->node);
115+
_handle_one_expired_timeout(timeout);
116116
}
117117
}
118118

@@ -161,13 +161,13 @@ static inline void _dump_timeout(struct _timeout *timeout, int extra_tab)
161161
static inline void _dump_timeout_q(void)
162162
{
163163
#ifdef CONFIG_KERNEL_DEBUG
164-
sys_dnode_t *node;
164+
struct _timeout *timeout;
165165

166166
K_DEBUG("_timeout_q: %p, head: %p, tail: %p\n",
167167
&_timeout_q, _timeout_q.head, _timeout_q.tail);
168168

169-
SYS_DLIST_FOR_EACH_NODE(&_timeout_q, node) {
170-
_dump_timeout((struct _timeout *)node, 1);
169+
SYS_DLIST_FOR_EACH_CONTAINER(&_timeout_q, timeout, node) {
170+
_dump_timeout(timeout, 1);
171171
}
172172
#endif
173173
}

kernel/mailbox.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,8 @@ static int _mbox_message_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
240240
s32_t timeout)
241241
{
242242
struct k_thread *sending_thread;
243-
struct k_thread *receiving_thread;
243+
struct k_thread *receiving_thread, *next;
244244
struct k_mbox_msg *rx_msg;
245-
sys_dnode_t *wait_q_item, *next_wait_q_item;
246245
unsigned int key;
247246

248247
/* save sender id so it can be used during message matching */
@@ -255,10 +254,8 @@ static int _mbox_message_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
255254
/* search mailbox's rx queue for a compatible receiver */
256255
key = irq_lock();
257256

258-
SYS_DLIST_FOR_EACH_NODE_SAFE(&mbox->rx_msg_queue, wait_q_item,
259-
next_wait_q_item) {
260-
261-
receiving_thread = (struct k_thread *)wait_q_item;
257+
SYS_DLIST_FOR_EACH_CONTAINER_SAFE(&mbox->rx_msg_queue, receiving_thread,
258+
next, base.k_q_node) {
262259
rx_msg = (struct k_mbox_msg *)receiving_thread->base.swap_data;
263260

264261
if (_mbox_message_match(tx_msg, rx_msg) == 0) {
@@ -428,9 +425,8 @@ static int _mbox_message_data_check(struct k_mbox_msg *rx_msg, void *buffer)
428425
int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg, void *buffer,
429426
s32_t timeout)
430427
{
431-
struct k_thread *sending_thread;
428+
struct k_thread *sending_thread, *next;
432429
struct k_mbox_msg *tx_msg;
433-
sys_dnode_t *wait_q_item, *next_wait_q_item;
434430
unsigned int key;
435431
int result;
436432

@@ -440,10 +436,9 @@ int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg, void *buffer,
440436
/* search mailbox's tx queue for a compatible sender */
441437
key = irq_lock();
442438

443-
SYS_DLIST_FOR_EACH_NODE_SAFE(&mbox->tx_msg_queue, wait_q_item,
444-
next_wait_q_item) {
439+
SYS_DLIST_FOR_EACH_CONTAINER_SAFE(&mbox->tx_msg_queue, sending_thread,
440+
next, base.k_q_node) {
445441

446-
sending_thread = (struct k_thread *)wait_q_item;
447442
tx_msg = (struct k_mbox_msg *)sending_thread->base.swap_data;
448443

449444
if (_mbox_message_match(tx_msg, rx_msg) == 0) {

kernel/sched.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,12 @@ void _pend_thread(struct k_thread *thread, _wait_q_t *wait_q, s32_t timeout)
188188
{
189189
#ifdef CONFIG_MULTITHREADING
190190
sys_dlist_t *wait_q_list = (sys_dlist_t *)wait_q;
191-
sys_dnode_t *node;
192-
193-
SYS_DLIST_FOR_EACH_NODE(wait_q_list, node) {
194-
struct k_thread *pending = (struct k_thread *)node;
191+
struct k_thread *pending;
195192

193+
SYS_DLIST_FOR_EACH_CONTAINER(wait_q_list, pending, base.k_q_node) {
196194
if (_is_t1_higher_prio_than_t2(thread, pending)) {
197-
sys_dlist_insert_before(wait_q_list, node,
195+
sys_dlist_insert_before(wait_q_list,
196+
&pending->base.k_q_node,
198197
&thread->base.k_q_node);
199198
goto inserted;
200199
}

0 commit comments

Comments
 (0)