Skip to content

Commit 3a1f682

Browse files
committed
Add list helpers for scheduler ready queues
Previously, list_pushback() and list_remove() were the only list APIs available for manipulating task lists. However, both functions perform malloc/free internally, which is unnecessary and inefficient during task state transitions. The previous commit introduced an intrusive ready-queue membership structure in tcb_t. To support this design and improve efficiency, this commit adds two helper functions for intrusive list manipulation, eliminating overhead malloc/free operation during task lifecycle. - list_pushback_node(): append an existing node to the end of the list in O(n) time without allocating memory. - list_remove_node(): remove a node from the list without freeing it. Both helper functions are operated in O(n) by linearly searching method.
1 parent 4f29f87 commit 3a1f682

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

include/lib/list.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ static inline list_node_t *list_pushback(list_t *list, void *data)
100100
return node;
101101
}
102102

103+
/* Pushback list node into list */
104+
static inline void list_pushback_node(list_t *list, list_node_t *target)
105+
{
106+
if (unlikely(!list || !target || target->next))
107+
return;
108+
109+
target->next = list->tail;
110+
111+
/* Insert before tail sentinel */
112+
list_node_t *prev = list->head;
113+
while (prev->next != list->tail)
114+
prev = prev->next;
115+
116+
prev->next = target;
117+
list->length++;
118+
return;
119+
}
120+
103121
static inline void *list_pop(list_t *list)
104122
{
105123
if (unlikely(list_is_empty(list)))
@@ -134,6 +152,25 @@ static inline void *list_remove(list_t *list, list_node_t *target)
134152
return data;
135153
}
136154

155+
/* Remove a node from list without freeing */
156+
static inline void list_remove_node(list_t *list, list_node_t *target)
157+
{
158+
if (unlikely(!list || !target || list_is_empty(list)))
159+
return;
160+
161+
list_node_t *prev = list->head;
162+
while (prev->next != list->tail && prev->next != target)
163+
prev = prev->next;
164+
165+
if (unlikely(prev->next != target))
166+
return; /* node not found */
167+
168+
prev->next = target->next;
169+
target->next = NULL;
170+
list->length--;
171+
return;
172+
}
173+
137174
/* Iteration */
138175

139176
/* Callback should return non-NULL to stop early, NULL to continue */

0 commit comments

Comments
 (0)