Skip to content

Commit f556a2c

Browse files
committed
Add ready queue helpers for mutex/semaphore
Previously, task operations lacked public enqueue/dequeue helpers usable from outside the task core, which made it impossible to keep ready queue synchronized when tasks crossed the runnable boundary. This change introduces two helpers to be used by mutex and semaphore resource-wait and wakeup paths, ensuring their state transitions update ready queue explicitly and remain aligned with ready-queue semantics. Both helpers are prefixed with _sched_block to emphasize their role in TASK_BLOCKED-related transitions. This keeps mutex and semaphore APIs consistent with ready-queue semantics.
1 parent 202b115 commit f556a2c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

include/sys/task.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,14 @@ uint64_t mo_uptime(void);
298298
*/
299299
void _sched_block(queue_t *wait_q);
300300

301+
/* Dequeue path for task with TASK_BLOCKED state. It must be called before task
302+
* state set as TASK_BLOCKED. Currently, this API is used in mutex lock case.*/
303+
void _sched_block_dequeue(tcb_t *blocked_task);
304+
305+
/* Enqueue path for the task with TASK_BLOCKED state. This API is the mainly
306+
* enqueuing path for semaphore and mutex operations. */
307+
void _sched_block_enqueue(tcb_t *blocked_task);
308+
301309
/* Application Entry Point */
302310

303311
/* The main entry point for the user application.

kernel/task.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,23 @@ void sched_wakeup_task(tcb_t *task)
450450
sched_enqueue_task(task);
451451
}
452452

453+
/* Dequeue blocked task when mutex lock is triggered */
454+
void _sched_block_dequeue(tcb_t *blocked_task)
455+
{
456+
if (unlikely(!blocked_task || blocked_task->state != TASK_BLOCKED))
457+
return;
458+
459+
sched_dequeue_task(blocked_task);
460+
}
461+
462+
void _sched_block_enqueue(tcb_t *blocked_task)
463+
{
464+
if (unlikely(!blocked_task || blocked_task->state != TASK_BLOCKED))
465+
return;
466+
467+
sched_enqueue_task(blocked_task);
468+
}
469+
453470
/* Efficient Round-Robin Task Selection with O(n) Complexity
454471
*
455472
* Selects the next ready task using circular traversal of the master task list.

0 commit comments

Comments
 (0)