Skip to content

Commit 82101ef

Browse files
committed
Change task management to use an embedded list node design
The previous non-intrusive list node approach for tcb_t introduced unnecessary memory operations and ambiguity around list node lifecycle management. This change switches task list management to an embedded list node design, aligning task handling with the updated list operation model.
1 parent d247d54 commit 82101ef

File tree

5 files changed

+69
-72
lines changed

5 files changed

+69
-72
lines changed

arch/riscv/hal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ void hal_switch_stack(void **old_sp, void *new_sp)
653653
*/
654654
void hal_interrupt_tick(void)
655655
{
656-
tcb_t *task = kcb->task_current->data;
656+
tcb_t *task = tcb_from_global_node(kcb->task_current);
657657
if (unlikely(!task))
658658
hal_panic();
659659

include/sys/task.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ typedef struct tcb {
8686

8787
/* Stack Protection */
8888
uint32_t canary; /* Random stack canary for overflow detection */
89+
90+
/* Embedded node for global task list */
91+
list_node_t global_node;
92+
93+
/* Mutex node */
94+
list_node_t mutex_node;
8995
} tcb_t;
9096

9197
/* Kernel Control Block (KCB)

kernel/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ int32_t main(void)
6363
* 'kcb->task_current' was set by the first call to mo_task_spawn.
6464
* This function transfers control and does not return.
6565
*/
66-
tcb_t *first_task = kcb->task_current->data;
66+
tcb_t *first_task = tcb_from_global_node(kcb->task_current);
6767
if (!first_task)
6868
panic(ERR_NO_TASKS);
6969

kernel/mutex.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@ static inline void cond_invalidate(cond_t *c)
4545
*/
4646
static bool remove_self_from_waiters(list_t *waiters)
4747
{
48-
if (unlikely(!waiters || !kcb || !kcb->task_current ||
49-
!kcb->task_current->data))
48+
if (unlikely(!waiters || !kcb || !kcb->task_current))
5049
return false;
5150

52-
tcb_t *self = kcb->task_current->data;
51+
tcb_t *self = tcb_from_global_node(kcb->task_current);
5352

5453
/* Search for and remove self from waiters list */
5554
list_node_t *curr = waiters->head->next;
5655
while (curr && curr != waiters->tail) {
57-
if (curr->data == self) {
56+
if (tcb_from_global_node(curr) == self) {
5857
list_remove(waiters, curr);
5958
return true;
6059
}
@@ -66,14 +65,13 @@ static bool remove_self_from_waiters(list_t *waiters)
6665
/* Atomic block operation with enhanced error checking */
6766
static void mutex_block_atomic(list_t *waiters)
6867
{
69-
if (unlikely(!waiters || !kcb || !kcb->task_current ||
70-
!kcb->task_current->data))
68+
if (unlikely(!waiters || !kcb || !kcb->task_current))
7169
panic(ERR_SEM_OPERATION);
7270

73-
tcb_t *self = kcb->task_current->data;
71+
tcb_t *self = tcb_from_global_node(kcb->task_current);
7472

7573
/* Add to waiters list */
76-
if (unlikely(!list_pushback(waiters, self)))
74+
if (unlikely(!list_pushback(waiters, &self->mutex_node)))
7775
panic(ERR_SEM_OPERATION);
7876

7977
/* Block and yield atomically */
@@ -218,8 +216,8 @@ int32_t mo_mutex_timedlock(mutex_t *m, uint32_t ticks)
218216
}
219217

220218
/* Slow path: must block with timeout using delay mechanism */
221-
tcb_t *self = kcb->task_current->data;
222-
if (unlikely(!list_pushback(m->waiters, self))) {
219+
tcb_t *self = tcb_from_global_node(kcb->task_current);
220+
if (unlikely(!list_pushback(m->waiters, &self->mutex_node))) {
223221
NOSCHED_LEAVE();
224222
panic(ERR_SEM_OPERATION);
225223
}
@@ -378,11 +376,11 @@ int32_t mo_cond_wait(cond_t *c, mutex_t *m)
378376
if (unlikely(!mo_mutex_owned_by_current(m)))
379377
return ERR_NOT_OWNER;
380378

381-
tcb_t *self = kcb->task_current->data;
379+
tcb_t *self = tcb_from_global_node(kcb->task_current);
382380

383381
/* Atomically add to wait list */
384382
NOSCHED_ENTER();
385-
if (unlikely(!list_pushback(c->waiters, self))) {
383+
if (unlikely(!list_pushback(c->waiters, &self->mutex_node))) {
386384
NOSCHED_LEAVE();
387385
panic(ERR_SEM_OPERATION);
388386
}
@@ -420,11 +418,11 @@ int32_t mo_cond_timedwait(cond_t *c, mutex_t *m, uint32_t ticks)
420418
return ERR_TIMEOUT;
421419
}
422420

423-
tcb_t *self = kcb->task_current->data;
421+
tcb_t *self = tcb_from_global_node(kcb->task_current);
424422

425423
/* Atomically add to wait list with timeout */
426424
NOSCHED_ENTER();
427-
if (unlikely(!list_pushback(c->waiters, self))) {
425+
if (unlikely(!list_pushback(c->waiters, &self->mutex_node))) {
428426
NOSCHED_LEAVE();
429427
panic(ERR_SEM_OPERATION);
430428
}

0 commit comments

Comments
 (0)